yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.
I disagree. The correct answer is "Before statement labeled 4". The reason is that "the object initially referenced by s" is a String object with the value of "abc". In statement 3, a String object is created with the value "abcnull" and that String is then assigned to s. As a result, the "abc" String that used to be referenced by s is no longer referenced by anything, so the object initially referenced by s is eligible for garbage collection as soon as statement 3 completes, which is immediately before statement 4.
Originally posted by Sisir Chanda: yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.
where u have read this about... give me the linke....
i think joe sanawitz is perfectly right and i have seen that explanation every where but your statement is to be honest strange..
anyway give me the link from where you have read this..
Thanks and Regards, Amit Taneja
Joined: Jan 03, 2005
yep...It was my mistake...I read that the equestion is '...when the object refernced by s is eligible for GC...'. Since before line 4 , '....s = s + r; //3
...' was used, so my answer was s will never be eligible for GC. But the actual question was '....when the INITIAL object referenced by s will be eligible for GC.....'
The the answer is definitely....Before statement labeled 4. since at line 3 s has been assigned to a different object and s is referring to a different object. Having said that, the INITIAL object reference by s will become eligible for GC.