Identify the location in the following program where the object,
initially referenced with arg1, is eligible for garbage collection.
public class Main { public static void main(String[] args) { String msg;/*from w ww. j a v a2 s. co m*/ String pre = "test "; String post = " test."; String arg1 = new String((args.length > 0) ? "'" + args[0] + "'" : "<no argument>"); msg = arg1; arg1 = null; // (1) msg = pre + msg + post; // (2) pre = null; // (3) System.out.println(msg); msg = null; // (4) post = null; // (5) args = null; // (6) } }
Select the one correct answer.
(b)
Before (1), the String object initially referenced by arg1 is denoted by both msg and arg1.
After (1), the String object is only denoted by msg.
At (2), reference msg is assigned a new reference value.
This reference value denotes a new String object created by concatenating contents of several other String objects.
After (2), there are no references to the String object initially referenced by arg1.
The String object is now eligible for garbage collection.