I found this question in a mock test - From the following code how many objects are eligible for garbage collection? String string1 = "Test"; String string2 = "Today"; string1 = null; string1 = string2; A) 1 B) 2 C) 3 D) 0 The answer given in the test is 'A'. I would expect the answer to be 'D' because the object "Test" is not garbage collected but rather put back into the pool maintained by the JVM. Please explain.
I agree with Vivek and Yan. now take the follwing situation.. String s1="test"//line 1 String s2=s1.toUpperCase()//line 2 s1=null//line 3 s2=null//line 4 value hold by s1 will not be eligible for garbage collection after line 3 coz "test" is string literal. But wat about s2.Does the value it refer at line 3 not be eligible for garbage collection after line 4.???what i mean to ask is "s1.toUpperCase()" gives string literal or string object. thanx in advance. regds Arpana
s1.toUpperCase() returns a new String object since it has to change the string. It will be eligible for garbage collection after line 4, when the reference "s2" no longer points to it. However, note that "FOO".toUpperCase() does not return a new string -- it returns its argument, since it's already all-upper-case.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Joined: Nov 12, 2002
Do u mean to say that in the above code, the value held by s2 in line 1 will not be eligible for garbage collection after line 2 coz here s1.toUpperCase() doesn't return any string object coz that string already present in string pool and both s1 and s2 refer to the same literal. Thanx in advance. regds Arpana
Joined: Jun 06, 2002
Yep! In fact, after line 1, you will find that s1==s2. [ November 15, 2002: Message edited by: Ron Newman ]
String objects that are interned are placed in the String pool, if they were not already there. Strings computed from compile constant expressions (JLS 15.28) are also placed in it. That is, any String expression made up from string literals and others compile constants:
Parsing the class file with "javap" there is a line... "3 ldc #3 String "The integer 9223372036854775807 is mighty big." ...that shows that the compiler has identified the first string expression to print as a compile constant. At runtime the JVM will create a String object to represent this information, and it will place it in the string pool. That object will be used in the future for any reference to a string "interned" with the same content. However the second string expression to print spanws the typical sequence of "StringBuffer" and sucessive "appends" to concatenate the strings. The resulting string is not placed in the string pool.
Originally posted by Vivek Talyan: I found this question in a mock test - From the following code how many objects are eligible for garbage collection? String string1 = "Test"; String string2 = "Today"; string1 = null; string1 = string2; A) 1 B) 2 C) 3 D) 0 The answer given in the test is 'A'. I would expect the answer to be 'D' because the object "Test" is not garbage collected but rather put back into the pool maintained by the JVM. Please explain.
I really want to know the answer of this question. I meet at a mock exam too.
Joined: Jul 03, 2001
The real answer is 0, because string literal are not garbage collected unless the class that declared them is unloaded from the JVM. The exam is nottesting the eligibility for garbage collection of objects pointed to by string literals. Many mock exams, however, do not level up to this knowledge. Thus you can pretend the string literals were created with the new operator and answer the mock. Do not worry about the exam. You will not be asked about garbage collection of string literals. [ November 16, 2002: Message edited by: Jose Botella ]