thelordoftherings
Programmer
Hello,
I've encountered with this question in a certain exam:
Given:
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).
The correct answer is: B.
And my question is why? oa[0] still holds reference to this Object and it returns it to the method caller, so if there is still live regerence to this Object how can it be eligable for garbage collection?
I've encountered with this question in a certain exam:
Given:
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).
The correct answer is: B.
And my question is why? oa[0] still holds reference to this Object and it returns it to the method caller, so if there is still live regerence to this Object how can it be eligable for garbage collection?