Thursday, April 9, 2015

String and String Constant Pool

String and String Constant Pool

String is the most known Immutable Class in Java. Immutable means, the contents can't be changed once initialized. That's the reason, we keep hearing that to use StringBuffer for string modifications instead of String. When we modify the contents of String (see samples below) will create a new copy of String and assigns the reference. String Constant Pool is a pool of Strings maintained by JVM for storing the strings created and its references. See code below to know what happens when a string constant is created.
      String string1 = "veeresh"; 
      String string2 = "veeresh"; 
      string1.concat(string2);

Points to be noted

  • A constant veeresh will be created and reference will be assigned to string1
  • When trying to create one more constant with the same value, JVM will pick it up from String constant pool and get the reference and assigns to string2. So, string1 and string2 are referring same.
  • If we perform an operation on a string - like concat in the above code, a new string will be created with value veereshveeresh. But there is no assignment, hence we can't acess it in our program but it will present in String constant pool

More about String Constant Pool

No new string will be created only if it's a constant. If we create a string using new keyword, then a new string reference will be created. See below
        String string1 = "veeresh";
        String string2 = new String("veeresh");
        String string3 = "hi";
        String string4 = string3.concat(" "+string2);
In above code
  • On line 1, a new constant veeresh will be created in String Constant Pool and reference is assigned to string1
  • On line 2, a new Object whose value is veeresh will be created - not taken from string constant pool as we used new keyword.
  • On line 3, a new constant hi will be created in String Constant Pool and reference is assigned to string3
  • On line 4, a new constant Space (" ") will be created and then " veeresh" (Space suffixed with veeresh) created and finally "hi veeresh" will be created in String Constant Pool and assigned to string4 as reference. So, total 3 strings will be created here (Space, " veeresh" and "hi veeresh").

Why Only for String

Only String class  has Constant Pool, Other types like Integer, Char, Number etc doesn't have Constant pool even though they are Immutable. The important reason so far i got are Security and Frequency of Usage.
  • String is the most frequent used like System.out.println and arguments that we pass most of the methods and even main method accepts an array of Strings. So, JVM referes to String Constant Pool every time when a new string created in order to reduce the number of references.
  • I am not really sure what security we get but i found on web as this is one of the reasons.
I am in search of other reasons for why only String. 

To Summarize

  • Every time, when a string constant is created, JVM checks String Constant Pool whether exists. If exists, the same reference will be returned otherwise creates it
  • In order to create new string without taking from Constant Pool, use new keyword
  • Any operation on string will result in new object (or constant)
  • If we want to create strings for doing some modifications, use StringBuffer instead of String
Help me with more details if you know anything else. Happy Learning!!!

No comments:

Post a Comment