Wednesday, June 5, 2013

String literal pool in Java

Hi welcome,
In this post we will discuss about   String  literal pool.
String literal pool:- Is a collection of  String object references ,its  not collection of String objects.

This means that String literal pool  contains references to String literals .If an String literal is already present and we are declaring it again ,JVM will not directly create a new one,first it will look in literal pool and if it finds a reference then  it will reuse it

String is nothing but a array of characters.There are different ways of creating a String object.

1.String userName="Google";
The  text between  " " is a String literal,internally a String object is  created and stored in Heap memory area.
A reference from  userName variable will point to Google and there will be another reference  from String literal pool to Google object as shown in the below diagram





In general there will be a misunderstanding that  when we declare a String literal two objects will be created one in heap and one in String constant pool. The actual concept is  there will be  a reference from String literal pool.

Lets examine the below example.

How many String objects are created -one -Google.There will be two references to it  one userName and other from String literal pool. .At line 5  we have de-referenced the Google String object.Now is it eligible Garbage Collectable? Most of them think it will be garbage collected, but  no it will not be eligible for garbage collected  because there will be a reference from String constant pool.

We will add the below line of code.@line 5
String userName2="Google";

When  the above code is executed JVM will look into pool whether there is a reference to Google object.
if it finds a reference it will not create a new object.


we have another way of creating a String object using new (),operator.
Lets see the difference of creating two ways of Strings.
String str1="Java-recent";
String str2="Java-recent";
String str3=new String("Java-recent");

Examine the below code:-

What will be output of above program?
false false..? wrong the result will be true false

The main reason for this output is when we create a String object using new() operator we are forcing JVM to create a new object and this type of String will not have a reference from string literal pool .

we can observe that str3 is referring to new String object.


Conclusion:-

1. String literal pool is just collection of String object references not String objects itself.
2.If we are creating a String object using new operator then we are forcing JVM  to create a new object.
3.String literals are stored in premgen(permanent generation), a part of heap area where long lived objects will be stored,the objects are garbage collected only when GC decided to  remove class loaders


References:-  http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html 
http://docs.oracle.com/javase/6/docs/api/

No comments:

Post a Comment

Like and Share