Hi guys,
I have a problem with this question:
When line 8 is reached, which are true? (Choose all that apply.)
A. a1.acctNum == a3.password
B. a1.password == a2.password
C. Three objects are eligible for garbage collection.
D. Four objects are eligible for garbage collection.
E. Six objects are eligible for garbage collection.
F. Less than three objects are eligible for garbage collection.
G. More than six objects are eligible for garbage collection.
And this is the answer in the book:
Answer (for Objective 7.4):
- B and D are correct. B is correct, although when line 8 is reached, the references are lost.
D is correct because only one Account object is created, and a1, a2, and a3 all refer to it.
The Account object has two Long objects associated with it, and finally the anonymous
Banker object is also eligible.
- A, C, E, F, and G are incorrect based on the above.
Option B is difficult to understand. When line 8 is reached all references are lost. Well, I will believe this because I think the author wanted to ask if both 'password' references point to the same object in the heap.
My worry is with option D. I think there are 5 objects eligible for garbage collection and not 4. The Account object, one Long created in line 12 using constructor Long(
String) (the String is in the pool, so is reachable in line 8), one more Long created by boxing in line 15 , another Long created by boxing in line 16 and the Banker object.
What do you think?
Thanks in advance,
Alvaro