About the Short:
c1 is no longer accessible and it contains a Short object, so that object is also eligible for GC.
Remember that c3 is only a reference. c1 and c2 were referencing an Object but there was no object for c3. Therefor there is no object to be GC'ed. If c3 was referencing to an object then it would also be eligible.
Next time post the question so that others that don't have the book could help you.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
wei not everyone has that book and even if they have the book, they might not have the book with them all the time. In the future you can get better response if you provide the question with which you are confused. This provides an opportunity for everyone to help you and also helps them to understand the topic and increase their own knowledge...
Along the same line, my question for Q1 in chapter 3 is that if the local variable cb is also eligible for GC? why or why not? Thanks.
Below is the question again:
When // doStuff is reached, how many objects are eligible for GC?
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime
✓ 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)
Shifong Chang wrote: my question for Q1 in chapter 3 is that if the local variable cb is also eligible for GC? why or why not?
Not variables are eligible for the GC. Only the objects, which are *not* referred by any variable, are eligible for the GC. The object passed to the go(~) method (line-11) is already referenced by'c2' - hence, it is not eligible for the GC.