This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion in String Constant Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion in String Constant Pool" Watch "Confusion in String Constant Pool" New topic
Author

Confusion in String Constant Pool

Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

hi

from the K and B book

When the
compiler encounters a String literal, it checks the pool to see if an identical String
already exists. If a match is found, the reference to the new literal is directed to the
existing String, and no new String literal object is created. (The existing String simply
has an additional reference.
)
does this mean that only those strings are checked which hava reference ? what about the lost objects which they dont have nay
reference pointing to them?


String s = new String("abc"); // creates two objects,
// and one reference variable
In this case, because we used the new keyword, Java will create a new String object
in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will
be placed in the pool.
>> this is alittle bit confusing could somebody please clear it up for me? does this mean s refers to two objects? why do we need to create two objects ? so what happens to the object which is in normal (nonpool) memory when we want to alter the objec refered to by s ? will it be aaboned as well? at last isn't it redundancy?


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
Cole Tarbet
Ranch Hand

Joined: Aug 02, 2011
Posts: 30
Great article at our very own JavaRanch:

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

...Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.


Since Strings are immutable, multiple references can share the same actual value. This is done to save resources. However, using the new keyword manually creates unique objects and is redundant like you say.

Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

I think creating String s = new String("abc") should not put put String "abc" into String pool , otherwise why do we need intern() ? Please correct me if I am wrong.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Cole Tarbet
Ranch Hand

Joined: Aug 02, 2011
Posts: 30
I've been reading about intern(). Mostly confusing.

http://weblogs.java.net/blog/enicholas/archive/2006/06/all_about_inter.html

Sounds like intern() is used for specialized comparison of Strings. I must not be using intern() properly because it looks to me like it slows things down.

Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

If you are perform intern() on String object jvm checks that String object content is available in any literal object. if it present means return that literal object reference otherwise it create one literal object and that reference will return.


OCPJP
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144



in the above example is the object eligible for garbage collection before the main completes? i think it shouldn't be because the literal "abc" will be placed in the pool and hence it will alwyas has a reference, am i right?
nitin sethi
Ranch Hand

Joined: Jul 14, 2011
Posts: 59

The below line 1 creates 2 objects: One in the heap(the non-pool memory) and one in the String Constant pool.

The line 2 makes only the object created in the heap eligible for gc.
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

objects are always created in heap memory only. String constant pool doesn't contain objects it contain literal object references only so one object is eligible for GC. literal object present in heap and it's reference is in string constant pool memory.
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

thank you guys.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
fwiw, intern() is not on the real exam, and on the real exam GC questions never use objects of type String.

hth,

Bert


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: Confusion in String Constant Pool
 
Similar Threads
String confusion
Regarding the functioning of string literals...
question with GC
String literal Pool doubt
Question to Kathy - from your book