File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection and String Literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection and String Literals" Watch "Garbage Collection and String Literals" New topic

Garbage Collection and String Literals

Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
Please see the code

How many objects are elgible for garbage cllection at the end of line 4. I say None.
Can somebody please explain the answer.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Sunita,
Think the following 3 items are available for garbage collection:
str1 - set to 'null' in Line 2. Any object set to 'null' is eligible for garbage collection.
The literal strings "second string" and "third string" as they are no longer being referenced. (Not 100% sure of these, as they would be in the string pool and I'm not clear on gc and the string pool).


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I think all are String literals...and they should be saved in String pools. There is no new String() created so no String object gc ed?
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
I agree with Ricky

Kalpesh Pandya

Joined: Oct 07, 2000
Posts: 14
I am bit confused. Why GC is not considering String pool ?
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Hi Friends,
Please see following URL ---
Since String pool is not in the objectives of SCJP ( as mentioned in the above link) so....
I think there will only be two objects available for GC. i.e. "second string" and "Third string".
Maha Anna please say something about this code.
[This message has been edited by Manish Singhal (edited October 08, 2000).]
Santosh Jaiswal

Joined: Oct 04, 2000
Posts: 26
I am thinking all string objects str0, str1, str2, str3 will be available for GC, after line 4.
any comments?
Santosh Jaiswal
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
I would agree with Manish.
also Jane, even if we are setting str1 to null in line 2, the String object that str1 was refering to ( ie. "some string") is still being refered to by str0 now. So that object will not be Garbage Collected.
Shameen MK

Joined: Sep 28, 2000
Posts: 22
String str0 = null;
String str1 = "some string";
String str2 = "second string";
String str3 = "Third string";
str0 = str1; // 1
str1 = null; //2
str2 = str0; //3
str3 = str2; // 4
According to me, at line 4 , this will be the case:
str1 str0 str2 str3
| \ | / null Some string
therefore, second string and third string will be ready for garbage collection.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
For your information, string literals( constructed using the = operator instead of new() ) are stored in the String pool. These strings are re-used if the program tries to create identical strings.
Strings in the string pool live longer than the objects created using the new() operator. The JVM garbage collects the strings in the pool when the class is unloaded.
Please note that you are not required to know about GC of string literals for the exam. The GC-related questions in the exam almost always deal with regular new()'d objects. However, if in case questions do appear about GC of string literals, treat them as any regular object.
Hope that helps,

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: Garbage Collection and String Literals
jQuery in Action, 3rd edition