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
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