Java quiz

R

Razvan

Hi !



I saw the following question on a Java quiz:



10. public Object m () {
11. Object o = new Float (3.14F);
12. Object [] oa = new Object [1];
13. oa [0] = o;
14. o = null;
15. return oa [0];
16. }

When is the Float object, created in line 11, eligible for garbage
collection ?
A. Just after line 13.
B. Just after line 14.
C. Never in this method.
D. Just after line 15 (that is, as the method returns).

Answer: B


I have answered "C". Since a reference to the Float object is kept in
the array the object cannot be garbage-collected. If the function that
calls m() chooses to scrap the received reference (in the form of
oa[0]) then the object can be garbage collected, but not before. Where
is the truth ?



Regards,
Razvan
 
J

Joona I Palaste

Razvan said:
I saw the following question on a Java quiz:
10. public Object m () {
11. Object o = new Float (3.14F);
12. Object [] oa = new Object [1];
13. oa [0] = o;
14. o = null;
15. return oa [0];
16. }
When is the Float object, created in line 11, eligible for garbage
collection ?
A. Just after line 13.
B. Just after line 14.
C. Never in this method.
D. Just after line 15 (that is, as the method returns).
Answer: B
I have answered "C". Since a reference to the Float object is kept in
the array the object cannot be garbage-collected. If the function that
calls m() chooses to scrap the received reference (in the form of
oa[0]) then the object can be garbage collected, but not before. Where
is the truth ?

The truth is, after having only seen this one sample method, "never".
Or to be more exact, "not necessarily during the entire program".
The first element of oa (element 0) refers to the same Float object as
o did when it was first initialised. What happens to the variable o is
none of the variable oa's business, so the line oa=null; does not cause
anything to happen in the array refererred to by oa.
The calling code might do anything it wants with the return value. If it
throws it away, then the answer is D. If it stores it away for
safekeeping, then the answer is C, or more exactly "not necessarily
during the entire program", as I have said.
 
D

Dave Neary

Hi,

10. public Object m () {
11. Object o = new Float (3.14F);
12. Object [] oa = new Object [1];
13. oa [0] = o;
14. o = null;
15. return oa [0];
16. }

I have answered "C". Since a reference to the Float object is kept in
the array the object cannot be garbage-collected.

No - the object created in line 11 is first given 0 label o, then oa[0]
is made to refer to the same object as o. But then o is made to refer to
null, so oa[0] also refers to null, and nothing is referring to the
Float object.

If you want to think about it in refcounts, the object has a refcount of
1 after line 11, 2 after line 13 an d 0 after line 14. oa[0] = o mleans
that oa[0] will always refer to the same object as o, until it's
reassigned.

Cheers,
Dave.
 
M

Michael Borgwardt

Dave said:
No - the object created in line 11 is first given 0 label o, then oa[0]
is made to refer to the same object as o. But then o is made to refer to
null, so oa[0] also refers to null, and nothing is referring to the
Float object.

Nonsense. o and oa[0] are two completely separate references. Changing one
of them has no effect whatsoever on the other.

1 after line 11, 2 after line 13 an d 0 after line 14. oa[0] = o mleans
that oa[0] will always refer to the same object as o

No, it doesn't.
 
S

Sudsy

Michael said:
Dave said:
No - the object created in line 11 is first given 0 label o, then oa[0]
is made to refer to the same object as o. But then o is made to refer to
null, so oa[0] also refers to null, and nothing is referring to the
Float object.


Nonsense. o and oa[0] are two completely separate references. Changing one
of them has no effect whatsoever on the other.

1 after line 11, 2 after line 13 an d 0 after line 14. oa[0] = o mleans
that oa[0] will always refer to the same object as o


No, it doesn't.

THANK YOU, MICHAEL!
For the sake of those who find this in the archives, Michael is correct
and Dave is way out in left field.
oa[0] = o;
is an assignment of an object reference. You can do whatever you wish
to o but it won't change the fact that we're dealing with object
references in Java.
More importantly, what o references is NOT reflected in what oa[0]
references subsequent to the initial assignment.
We're not talking pointers-to-pointers (** in C jargon). So this
statement is patently false (thank goodness!):
"oa[0] = o mleans that oa[0] will always refer to the same object as o"

Dave: while input is always welcome, erroneous statements do more to
confuse than elucidate. Please restrict your contributions to those
areas with which you're intimately familiar. No offense intended.
 
T

Thomas G. Marshall

Dave Neary coughed up:

....[rip]...

No - the object created in line 11 is first given 0 label o, then
oa[0] is made to refer to the same object as o. But then o is made to
refer to null, so oa[0] also refers to null, and nothing is referring
to the Float object.

You've already been (rightly so) smacked around for this, so I'll let it
go...

If you want to think about it in refcounts, the object has a refcount
of 1 after line 11, 2 after line 13 an d 0 after line 14. oa[0] = o
mleans that oa[0] will always refer to the same object as o, until
it's reassigned.

Just a note: Java GC does not employ reference counting. If it did, then
circular references would cause a memory leak. It uses variations on the
mark-and-sweep algorithm.

Cheers,
Dave.

--
Iamamanofconstantsorrow,I'veseentroubleallmydays.Ibidfarewelltoold
Kentucky,TheplacewhereIwasbornandraised.ForsixlongyearsI'vebeenin
trouble,NopleasureshereonearthIfound.ForinthisworldI'mboundtoramble,
Ihavenofriendstohelpmenow....MaybeyourfriendsthinkI'mjustastrangerMyface,
you'llneverseenomore.ButthereisonepromisethatisgivenI'llmeetyouonGod's
goldenshore.
 
B

Bryan Castillo

Dave Neary said:
Hi,

10. public Object m () {
11. Object o = new Float (3.14F);
12. Object [] oa = new Object [1];
13. oa [0] = o;
14. o = null;
15. return oa [0];
16. }

I have answered "C". Since a reference to the Float object is kept in
the array the object cannot be garbage-collected.

No - the object created in line 11 is first given 0 label o, then oa[0]
is made to refer to the same object as o. But then o is made to refer to
null, so oa[0] also refers to null, and nothing is referring to the
Float object.

Did you check that? Because I think the original answer is right.
oa[0] still contains the Float. There is a reference to the Float
object through the entire method.

public class Test {
public static Object test() {
Object o = new Float (3.14F);
Object [] oa = new Object [1];
oa [0] = o;
o = null;
System.out.println(oa[0]);
return oa [0];
}
public static void main(String [] args) {
test();
}
}

C:\dev>java Test
3.14


If you want to think about it in refcounts, the object has a refcount of
1 after line 11, 2 after line 13 an d 0 after line 14. oa[0] = o mleans
that oa[0] will always refer to the same object as o, until it's
reassigned.

Are you thinking about some other language? Because that is
completely inaccurate. oa[0] and o are just reference which may refer
to the same object, but when one of the references is changed to refer
to another object it does not affect the other reference.
 
T

Tony Morris

THANK YOU, MICHAEL!
For the sake of those who find this in the archives, Michael is correct
and Dave is way out in left field.
oa[0] = o;
is an assignment of an object reference.

Agreed.
 
D

Dave Neary

Hi,

Dave said:
No - the object created in line 11 is first given 0 label o, then oa[0]
is made to refer to the same object as o. But then o is made to refer to
null, so oa[0] also refers to null, and nothing is referring to the
Float object.

Nonsense. o and oa[0] are two completely separate references. Changing one
of them has no effect whatsoever on the other.

Ooops. What was I thinking. I guess I was trying to explain the wrong
answer to the quiz question...

Yes, I was wrong, wrong, wrongedywrong.

Dave.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top