Which is the earliest line in the following code after which the object created on line 0 will be a candidate for being garbage collected?
public class Question {
static String f() {
String a = "hello";
String b = "bye"; // line 0
String c = b + "!"; // line 1
String d = b; // line 2
b = a; // line 3
d = a; // line 4
return c; // line 5
}
public static void main( String args[] ) {
String msg = f();
System.out.println( msg ); // line 6
}
}
This code fragment is from Khalid's mock exam.
Case A:
If all the strings created in the String pool are not going to be garbage collected, then the answer to the above question should be none, however, the answer given by Khalid is line 4. I really need an expert to tell me the right answer.
Case B:
Suppose Khalid's answer is valid. (It is an assumption!) And we delete line 4. Therefore, d still points to "bye", and "bye" is not hanging. My question is that those strings are eligible for being garbage collected after the method completes? or after the main method exits?
Please help because I really don't know.
public class Question {
static String f() {
String a = "hello";
String b = "bye"; // line 0
String c = b + "!"; // line 1
String d = b; // line 2
b = a; // line 3
d = a; // line 4
return c; // line 5
}
public static void main( String args[] ) {
String msg = f();
System.out.println( msg ); // line 6
}
}
This code fragment is from Khalid's mock exam.
Case A:
If all the strings created in the String pool are not going to be garbage collected, then the answer to the above question should be none, however, the answer given by Khalid is line 4. I really need an expert to tell me the right answer.
Case B:
Suppose Khalid's answer is valid. (It is an assumption!) And we delete line 4. Therefore, d still points to "bye", and "bye" is not hanging. My question is that those strings are eligible for being garbage collected after the method completes? or after the main method exits?
Please help because I really don't know.