Its' GC now.C the following q.
Which is the earliest line in the following code after which the object created on the // 1 can be garbage collected, assuming no compiler optimizations are done?
public class NewClass
{
static
String enterTheDragon()
{
String a = new String("hello");
String b = new String("world !"); // 1
String c = new String(a + b + ""); // 2
String d = b; // 3
b = a; // 4
d = a; // 5
return c; // 6
}
public static void main(String args[])
{
String s = enterTheDragon(); // 7
System.out.println(s); // 8
}
}
// Answer: line 5
But I think it is Line no: 4 .Ok..ok..I am telling wht. I think.
The string created in //1 i.e. "world !" which is ref. by var. b upto line no. 3 BUT after line no. 3 that means in line no. 4 , b gets a's value.So, now the variable (ref.) b is pointing to the string "hello" ...........that means the string created in line 1(i.e. "world !") is now hanging and eligible for GC.tay is why I am telling the ans. is libe 4.Thats all.
wht. u say ? am I going wrong somewhere?
<marquee>
Ratul Banerjee </marquee>