• 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

Will really String Pool will not have duplicate entries.

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.

Thanks.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

HTH,
B.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic