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 Will really String Pool will not have duplicate entries. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Will really String Pool will not have duplicate entries." Watch "Will really String Pool will not have duplicate entries." New topic

Will really String Pool will not have duplicate entries.

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
I have a doubt that, Will string pool can not have any duplicate entries?
Please go through the below part of the programme,
1. String s1= "ABC";
2. String s2=s1.toLowerCase();
3. String s3="abc";
4. boolean b = s3== s2;
Now will the value of b is true or false. when i compiled i am getting it as false, i.e. s3 are s2 are two different string each one is holding the value, "abc". My doubt is where will s2 will store, will it be in string pool or some where. If it is in string, so it will have two entries of "abc".
Pleae some one explain me.


Bojan Knezovic
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
If I understood it correctly, string pool will hold only string literal values, i.e. when you say String s="abc", that WILL go to the string pool.
But anything else will go to the heap, i.e. String s1=s+"d" and most likely result of the .toLowerCase() method.

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Using a String literal, such as "abc", will cause a new entry to be created in the String literal pool. (Makes sense, right? String literals go into the String literal pool. )
However, when you invoke a method such as toLowerCase(), which returns a new Sting, we create a brand new String object on the heap - not in the String literal pool.
If you wanted to optimize your code, you could do this:

Theintern method will check the String literal pool and determine if an identical String already exists. If it does, a reference to that String object is returned so that this one (which is on the heap) can be garbage collected.
However, none of this is on the SCJP exam.

SCJP Tipline, etc.
It is sorta covered in the JavaRanch Style Guide.
subject: Will really String Pool will not have duplicate entries.
It's not a secret anymore!