• 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 and heap memory

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

here i have a confusion about String instantiation .

Let suppose I have a code fragment like bellow


String str="abc";

String str1="abc";

String st=new String("abc");


when I use a == operator for str and str1(str==str1) String object I will get true. Which means both the reference pointing to a same String object.

But when I use a == operator between str and st (str==st) then it will come as false (in spite there hash code being same . ) which means they are pointing to different object
which should not be as far as String pool is concerned. So is the third object is going to the heap rather than a String pool.

please help me regarding this String pool and heap concept.

Thanks
Biswajit

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every time you use the new keyword an object is created inside the heap, even for Strings. The String pool is only used for String literals ("xxx") and when you call intern() on a String.
 
bjit babu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I got the point .but what is the point of having pool. basically why we need a sting pool when a String can a take place in heap.


Thanks
Biswajit.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's so you can use a String literal as often as you like in your code, but only one instance of it is stored in memory.
 
bjit babu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So its basically we use a String pool for the performance of java engine.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you write code like this:

then it makes no sense for Java to make four different String objects. Strings are immutable (the content of a String object cannot be changed after it has been created). So, to save memory, Java will create just one String object, in the string pool, and it will reuse it whenever you use the same string literal. In the example above, all four variables will refer to the same String object.

The purpose of the string pool is to make it possible to do this sharing. It's a memory usage optimization.
 
reply
    Bookmark Topic Watch Topic
  • New Topic