• 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 Pool

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one explain the concept of String pool, Why Java prefer to use String pool instead of making new String object in heap or stack. Isn't additional task for Java to look for the same strings with the pool?
Thanks in advance.
Salman
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simplest answer would be to "Save On Memory"
Regards
Vikram
 
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 believe this is one type of questions SCJP favoriates. I remember I read something about it before but cannot remember the real words.
In addition to the memory-saving, Java does this for another very good reason. Please remember String object are "immutable", i.e, once you do aString="another string", the aString object is assigned to another String object (creating one if the String pool does not contain the string, or the aString object refers to the exiting String object). Therefore, to maintain a pool of String constants before and after your application is started, will boost performance by reducing number of objects to be created.
I did a little statistic study on one of the project I was involved before, there were 22,000 String objects created out of about 44,000 objects total.
Of course, it is under the assumption that you init your String objects by constant assignment instead of "new String" statement...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic