Eligibility for garbage collection
In the SCJP book I am reading, there is a question thus:
2. Given:
class CardBoard
{
Short story = 5;
CardBoard go(CardBoard cb)
{
cb =null;
return cb;
}
publicstaticvoid main(String[] args)
{
CardBoard c1 =new CardBoard();
CardBoard c2 =new CardBoard();
CardBoard c3 = c1.go(c2);
c1 =null;
// do Stuff
}
}
When// doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.
The answer is (rather unsurprisingly) not the one I was expecting:
Answer:
3 C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
A, B, D, E, and F are incorrect based on the above. (Objective 7.4)
My question is, why is c2 not eligible also?
I would have thought that this line:
CardBoard c3 = c1.go(c2);
would first, reassign c2 to a null reference, and second, assign that null reference to c3. So, why does c1.go(c2) not nullify the object reference c2, and thereby render the object that c2 did point to as eligible for gc?
Thanks in advance to anyone who can help.
So what's your question? What part of the explanation didn't you understand? What did you think the answer was and why? It's rather difficult to dispel your confusion if you don't make clear what that confusion is.
> So what's your question? What part of the explanation
> didn't you understand? What did you think the answer
> was and why? It's rather difficult to dispel your
> confusion if you don't make clear what that confusion
> is.
(Scratches head since all of this was clearly stated in the post..perhaps though in invisible ink?)
Please see this text in the original post:
My question is, why is c2 not eligible also?
I would have thought that this line:
CardBoard c3 = c1.go(c2);
would first, reassign c2 to a null reference, and second, assign that null reference to c3. So, why does c1.go(c2) not nullify the object reference c2, and thereby render the object that c2 did point to as eligible for gc?
In Java parameters are passed by value.
This means that when you call "go(c2)", the go method gets a
copy of the value of the variable c2 -- not the variable c2 itself.
Nothing that happens inside of the method can change the
value of the variable c2: you can check this with a simple print
statement.CardBoard c2 = new CardBoard();
System.out.println("Before: " + c2);
CardBoard c3 = c1.go(c2);
System.out.println("After: " + c2);
> In Java parameters are passed by value.
>
> This means that when you call "go(c2)", the go method
> gets a
> copy of the value of the variable c2 -- not the
> variable c2 itself.
> Nothing that happens inside of the method can change
> the
> value of the variable c2: you can check this with a
> simple print
> statement.CardBoard c2 = new CardBoard();
>System.out.println("Before: " + c2);
> CardBoard c3 = c1.go(c2);
>System.out.println("After: " + c2);
Oh, okay. So even though you can make changes to the object that the reference points to that persist beyond the closing brace of the method, the reference itself that is passed in is just a copy of the original reference? I guess so. Thanks.
> In Java parameters are passed by value.But thats only for primitives.For objects references are passed!
> > In Java parameters are passed by value.> > But thats only for primitives.For objects references> are passed!No!!!
> > In Java parameters are passed by value.
>
> But thats only for primitives.
Nope.
> For objects references
> are passed!
References are passed by value.
http://javadude.com/articles/passbyvalue.htm
http://java.sun.com/developer/JDCTechTips/2001/tt1009.html#tip1
http://www.javaranch.com/campfire/StoryPassBy.jsp
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html
http://www.cs.toronto.edu/~dianeh/tutorials/params/
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38698
http://radio.javaranch.com/channel/val/2004/05/21/1085125887000.html
There is exactly one parameter passing mode in Java -- pass by value -- and that helps keep things simple.
-- James Gosling, "The Java Programming Language, Second Edition"
> But thats only for primitives.For objects references> are passed!Read this. It makes it really simple. http://www.javaranch.com/campfire/StoryCups.jsp http://www.javaranch.com/campfire/StoryPassBy.jsp
> > But thats only for primitives.For objects
> references
> > are passed!
>
> Read this. It makes it really simple.
>
> http://www.javaranch.com/campfire/StoryCups.jsp
> http://www.javaranch.com/campfire/StoryPassBy.jsp
Got it. Thanks.