• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string constant pool from K&B book

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
two objects.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose's got it!
On a related topic:
https://coderanch.com/t/243261/java-programmer-SCJP/certification/String-Garbage-Collection
 
reply
    Bookmark Topic Watch Topic
  • New Topic