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 constant pool from K&B book 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 "string constant pool from K&B book" Watch "string constant pool from K&B book" New topic
Author

string constant pool from K&B book

Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
Hi java gurus,
As explained in the 'awesome' K&B book (page 359):
".... JVM sets aside a special area of memory called the 'string constant pool'. When
the compiler encounters a string literal, it checks the pool if an identical string
exists....."
I have two doubts:
a) So in the following piece of code, is the "java" object is created in the pool twice??
As I understand, jvm creates this string in the pool then returns it's reference to 's'. but after
line#2 executes, reference to "java" is lost ...so after line#3 whether compiler will
create a new "java" in the pool and returns its reference or it will use the first one only?
If it uses the first one then how it gets to it as after line#2, the reference is kinda lost
in open space?
<code>
1. String s = "java";
2. String s = "language";
3. String s1 = "java";
</code>
b) Does the garbage collection also runs on this 'string constant pool' or what?
Thanks,
Deep
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Each time the Java "interpreter" finds a string literal (like "java") it checks if a String object was already created in the string pool representing the same content. If it finds it, it does not create it again, but it return its reference. (*)
Yes String objects created for string literals can be g.c.ed , but luckily, under certain conditions that are far beyond what is required for the SCJP exam.

(*) Not the whole truth: the compiler makes this process more much more efficent but it is an easy way to understand the string pool.


SCJP2. Please Indent your code using UBB Code
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
Thanks. However, I still have doubt reg Q#1:
In the following code, how many String objects are created? As per your explanation, it should be two only. Please correct me if I'm wrong.
code:
1. String s = "java";
2. String s = "language";
3. String s1 = "java";

Thanks,
Deep
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
two objects.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
I also got confused with this one. If indeed this program creates 2 objects, did the compiler find "java" so as not to create a new object referred to by 's2'?


Thinking is the talking of the soul with itself...-Plato
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Yes.
Originally posted by K Ville:
I also got confused with this one. If indeed this program creates 2 objects, did the compiler find "java" so as not to create a new object referred to by 's2'?


Groovy
Isuru_Suriyapperuma
Greenhorn

Joined: Aug 29, 2003
Posts: 8

Tell me.. if the code after line 3 never refers s nor s1, does that mean that they are eligible for GC even though they are strings created in the string pool or are they ?
Thanks!
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Well, to make it less confusing, this code doesn't compile because s reference is defined twice
Then, if we are to follow K&B, I don't see either nulling the references, or rereferencing, or islands of isolation here. As Jose said, there might be certain circumstances where the String literals would be gc, I just don't see it from the code.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Jose's got it!
On a related topic:
http://www.coderanch.com/t/243261/java-programmer-SCJP/certification/String-Garbage-Collection


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string constant pool from K&B book