Which is the first line in the following code after which the object created in the line marked (0) will be a candidate for garbage collection, assuming no compiler optimizations are done?.
public class Main { static String f() { String a = "hello"; String b = "bye"; // (0) String c = b + "!"; // (1) String d = b; // (2) b = a; // (3) d = a; // (4) return c; // (5) }/* w w w.j a v a 2 s.c o m*/ public static void main(String[] args) { String msg = f(); System.out.println(msg); // (6) } }
Select the one correct answer.
(d)
At (1), a new String object is constructed by concatenating the string "bye" in the String object denoted by b and the string "!".
After line (2), d and b are aliases.
After line (3), b and a are aliases, but d still denotes the String object with "bye" from line (0).
After line (4), d and a are aliases.
Reference d no longer denotes the String object created in line (0).
This String object has no references to it and is, therefore, a candidate for garbage collection.