String constant pool

It is also called as String literal pool or String pool.

As the name suggests, String constant pool is a pool of strings stored in a separate place within the Heap area.

We all know that we can create strings using “new” or by directly defining value in double quotes.

Whenever we create a string using double quotes, it gets stored in the string constant pool

Whenever we create a string using “new” it gets stored in Heap area and also same string will be added to String constant pool.


Why Java created String constant pool?


We know that Memory allocation operation for every new object is costly in terms of both time and memory.

So in order to reduce the memory overhead and also to increase the performance of object creation Java has introduced this String constant pool concept.

In this mechanism, it will drastically reduce the number of objects gets created in the Heap memory.

Each time we create a new String literal , JVM first looks at the String constant pool, if the same String literal already exist in pool, reference to the same pooled instance will be returned rather than creating a new string object.

If string literal does not exist in the pool then new string object will be created and kept in the Pool

This concept can be safely achieved without any data corruption as String is immutable in Java.

Example

  1. class StringConstantPool1{
  2.  public static void main(String args[]){
  3.    String s1="java";
  4.    String s2="java";
  5.    String s3="java";
  6.    String s4="java";
  7.  
  8.    System.out.println(s1==s2);
  9.    System.out.println(s3==s4);
  10.    System.out.println(s1==s4);
  11.    System.out.println(s2==s4);
  12.  }
  13. }
class StringConstantPool1{
 public static void main(String args[]){
   String s1="java";
   String s2="java";
   String s3="java";
   String s4="java";

   System.out.println(s1==s2);
   System.out.println(s3==s4);
   System.out.println(s1==s4);
   System.out.println(s2==s4);
 }
}



In the above example, we have created string using string literal and hence it gets stored in String constant pool.

How many object gets created in the above program ?


Your answer should be one and only one.

Yes, though we have created 4 strings using String literal, It has created only one string and same reference will be returned back for other string objects.

This way String constant pool helps in terms of saving heap memory.

String_Constant_Pool

As we could see in the above diagram, there is only one object gets created and all other further creation of same string using literals will get the reference to same string literal.

What happens when we create string using “new”.


Unfortunately In this case, Java creates a new additional string inside Heap but outside String pool no matter whether the same string already exist or not in the Pool.

Example :

  1. class StringConstantPool2{
  2.  public static void main(String args[]){
  3.    String s1="java";
  4.    String s2=new String("java");
  5.    System.out.println(s1==s2);
  6. }
  7. }
class StringConstantPool2{
 public static void main(String args[]){
   String s1="java";
   String s2=new String("java");
   System.out.println(s1==s2);
}
}



Let us understand the same by diagram

String_Constant_Pool_2

We can see that both s1 and s2 are pointing to different memory locations

S1 is pointing to the string literal created in string pool

S2 is pointing to the string object created in the heap

Because of this, output for s1==s2 is returning false

As we know that String comparison using == always compares the references not the actual content.

Read String comparison article for more details on String equality check.

Note :

Creating string using “new” never respect string constant pool mechanism and it always creates one new string in the Heap (outside string pool).
So, avoid creating String using “new” unless and until you know that you need it that way.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on