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 String literals immune from GC, correct? 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 "String literals immune from GC, correct?" Watch "String literals immune from GC, correct?" New topic
Author

String literals immune from GC, correct?

Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
String literals are always immune from the GC, right?
So if asked a question about when the following object is available for GC:
String str = "Java Ranch";
str = null;
The answer is never??
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an example to show (but not prove) that string literals and interned string objects are not garbage collected.

Output:
string-1,string-2,string-3
string-1,string-2,string-3
string-1,string-2,null
Jose, did I do this correctly? Thank you for the idea.
[ June 25, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

The same constant is loaded. Does that suggest that the same String object is being referenced?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Marlene Miller:

The same constant is loaded. Does that suggest that the same String object is being referenced?

Hi Marlene,
Yes, I think its the same 'Object'.
From the book "Inside the Java Virtual Machine by Bill Venners"

Three opcodes (ldc,ldc_w, ldc2_w) push constants from the constant pool. These opcodes take operands that specify a constant pool index. The Java virtual machine looks up the constant pool entry given the index, determines the constant's type and value, and pushes it onto the stack.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Brian Joseph:
String literals are always immune from the GC, right?

Right

So if asked a question about when the following object is available for GC:
String str = "Java Ranch";
str = null;
The answer is never??

You should never be asked a question like this (at least for the SCJP exam) because garbage collection of String literals IS NOT covered on the exam.
Corey


SCJP Tipline, etc.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Thanks for all the replies. And thanks for the clarification, Corey. I just assumed garabage collection meant that. I downloaded a 1.4 mock exam program, but after taking it and being asked such a question I had no way to verify it because the darn thing didn't show me what I got wrong! It really was like the real thing :-)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
WARNINGthe following is not exam stuffWARNING
Hey Marlene I am sure you would appreciate to know that string literals can be g.c.ed as this post shows.


SCJP2. Please Indent your code using UBB Code
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you very much Jose - in advance of reading your reference. I scanned the forum (104 search results), but I missed that one. Yes, I am interested.
[ June 26, 2003: Message edited by: Marlene Miller ]
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
I think it's great to have the experienced ranchers point out when a topic is not covered on the exam.
Since there "seems" to be an infinite number of details you can learn if you want, there comes a point where you really only want spend your time learning topics which Sun considers most relevant (thus are on the exam).
Thanks!
[ June 26, 2003: Message edited by: Brian Joseph ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literals immune from GC, correct?
 
Similar Threads
Quick Q on String and GC
GC Question
String literal GCed?
Garbage collector
Self Written Code on Garbage Collection