Identify the object that will be eligible for garbage collection after execution of line15
select any 1 option
1)object created at line 6 2)object created at line 7 3)object created at line 8 4)All of them 5)any of them.
I believe both objects at line 7 and line 8 is garbage collected.but the Question is to choose only one option.please let me know the closest one to choose.
Anand Loni
Ranch Hand
Joined: Jan 20, 2006
Posts: 150
posted
0
Hi,
I think correct answer is 4. As line 15 is str=null, after execution of this line object refered by str will also eligible for garbage collection. So objects created on lines 6, 7 and 8 will be eligible for garbage collection.
Regards, Anand.
~ Anand,
SCJP 1.5
SCWCD 1.5
marko salonen
Greenhorn
Joined: Nov 19, 2006
Posts: 8
posted
0
I would say that only object created at line 7 is ready for garbage. Why? Because variable one still holds its reference to Integer instance. Variable c is still holding its reference to str. The only instance that is not referenced is Float because non of a, b or c are referenced to it and variable titanic is set to null. Or, b is set but later it is changed to reference on str. [ November 24, 2006: Message edited by: marko salonen ]
At line 6, one = 1 At line 7, titanic = 1.23 At line 8, str = sailing... At line 9, a, one = 1 At line 10, b, titanic = 1.23 At line 11, c, str = sailing... At line 12, c's value is given to b which in turn is given to a, so in effect a, b, c, str = sailing... and titanic = 1.23 At line 13, b, c, str = sailing... At line 14, No one is pointing to 1.23 after making titanic = null
So in conclusion, the object created at line 7 is the only one which is eligible for gc.