File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC for String object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC for String object" Watch "GC for String object" New topic
Author

GC for String object

Vivek Talyan
Greenhorn

Joined: Nov 13, 2002
Posts: 2
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.
ramki srini
Greenhorn

Joined: Aug 27, 2002
Posts: 26
But there is no reference to String Test. So its eligible for Garbage collection.
Note : Question is asking whether eligible for garbage collection.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Originally posted by ramki srini:
But there is no reference to String Test. So its eligible for Garbage collection.
Note : Question is asking whether eligible for garbage collection.

But String literals in the String pool are never garbaged collected. "Test" should not be eligible for GC.


SCJP 1.4
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
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


SCJP1.4(91%)
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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)
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93

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
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Yep!
In fact, after line 1, you will find that s1==s2.
[ November 15, 2002: Message edited by: Ron Newman ]
Vijay Albuquerque
Ranch Hand

Joined: Feb 17, 2002
Posts: 42
Probably a very basic question:
Are ALL and ONLY string literals put in the string pool?


Vijay Albuquerque
SCJP 1.4, SCWCD 1.3
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
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.


SCJP1.4
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 ]
 
 
subject: GC for String object
 
Similar Threads
question about gc
garbage collector
GC
Garbage Collection of Strings
garbage collection ?