You can solve these type of questions pictorially. Thats the best way represent each object with a circle , assignment statements wit a link to the object.
So in line 2 a new String object is created and it points to this object In line 4 another string reference variable points to the object created in line 2 Now we have two links or references to the string object.
At line five only one string reference is set to null the other reference to the string object still exists and so this string object is not eligible for GC
COuld anyone tell me with appropriate reasons where the Garbage Collector can collect any object....... in this code......
Even if the two string references are set to null we can't say whether the Garbage collector will collect it, we can only say the object is eligible for GC. [ April 28, 2006: Message edited by: Srinivasa Raghavan ]