• Post Reply Bookmark Topic Watch Topic
  • New Topic

stringpooling  RSS feed

 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tellme about the stringpooling in java
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String allocation, like all object allocation, proves costly in both time and memory. To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time you create a string literal, the pool is checked. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool. Java can make this optimization since strings are immutable and can be shared without fear of data corruption.

Unfortunately, creating a string through new defeats this pooling mechanism by creating multiple String objects, even if an equal string already exists in the pool. Considering all that, avoid new String unless you specifically know that you need it!


From http://www.javaworld.com/javaqa/2002-09/01-qa-0906-strings.html
which I found through google.
[ October 10, 2005: Message edited by: Tim McGuire ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!