This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm confused with the answer to this question. This question is question one from chapter 3's self test section. It asks that when '// do stuff' is reached, how many objects are eligible for GC?
I know c2 is not null because it is referencing to a CardBoard object. c3 is referencing to the cb reference variable (Is this correct?). c1 is null. But how does the Short wrapper object becomes eligible for GC? The answer says "it has an associated Short wrapper object that is also eligible." what is 'it'? c1? Please help.. My mind is doing an infinite loop and about to blow up the stack.. :P Thanks.
The answer is 2, but I don't know where one of them comes from.
From what I understand, there are 2 cardboard objects and 2 Short wrapper objects (created when the two cardboard objects are initialized. Let's call cardboard initialized to c1 is A and the other B.
by the time // dostuff is reached, c1 and c3 is null. since c1 is null, cardboard A has no more references referring it. c2 is still referring to cardboard B, because go() method doesn't use the c2, instead it creates a copy of the c2 variable. However the cb variable in go() method is referring to cardboard B. Regardless, cb is null when // dostuff is reached.
So it goes like this:
c1 -> cardboard A.
c2, cb -> cardboard B.
c3 -> null
When // dostuff is reached, c1 and cb becomes null. But c2 still exist. So cardboard A and its Short wrapper is eligible for GC. Is this the right explanation? Sorry that I kinda go around in circles. I am still trying to sort my thoughts. lol..