Sunday, June 2, 2013

String Literal Pool or String Constant Pool or String Pool.

Recently one of the colleague asked me - What is String Pool?
I was surprised that this being one of the important concept in java lot of people are not aware of this. So I thought of just jotting down couple of points to explain it.
Hope it helps someone.

To Understand the concept of String Pool - the first thing you need to know - What are String 'Literals?
Literal in general means any number, text or information which represent a value. To make things simple, we can say - A String literal is a sequence of characters in a single line and has to be in double-quotes ("") e.g. "literal".

Now coming back to the concept of String Pool - most of the times we create String in java in either of two ways

String object = new String("object");
String literal = "literal" - String Literal

Whenever you are using a new Operator you are telling the java compiler to create a new Object on the HEAP. Consider a scenario where you have to  use the same String 'object' at multiple places in your program and you use the new operator every time to create it. This will basically create multiple instances of same String on Heap. Calling new String("object") never makes sense in Java and is unnecessarily inefficient and puts additional burden on memory.

Now java designers came out with a solution for this problem. They created a separate cache and called it String Constant Pool or String Literal Pool or String Pool - which maintains a reference of all the String literals created in the program. To put it in simple words, 'string pool' comes from the idea that all already defined string are stored in some 'pool' and before creating new String object compiler checks if such string is already defined.

So how this works?

When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals.

JVM encounters statement like String s1 = "literal";.
JVM checks the String pool to see if an equivalent String is already referenced from the heap.
If the String does not exist, it creates a new String object "literal" on the heap and its reference is maintained in the pool (constant table).
This statement creates one String object "literal" on heap.
Variable ‘s1′ refers to the same object.

JVM encounters another statement like String s2 = "literal";
JVM checks the String pool again.
Since the String with same value already exists in heap, a reference to the pooled instance is returned to s2.
This statement does not create any String object in the memory
Variable ‘s2′ refers the same object as ‘s1′.

You can check the above by comparing == operator on s1 and s2. It will always return true.

Some Facts about String Pool :-
  • String pooling is possible only because Strings in java are 'Immutable'.
  • All the string literals are created and their references are placed in the pool while JVM loads the class.
  • Designed on Fly Weight Design Pattern.
  • In early JVM's String literals in the String Pool never got GC'ed. In the newer JVM's, WeakReferences are used to reference the Strings in the pool, so interned Strings can actually get GC'ed, but only during Full Garbage collections.
  • Java automatically interns String literals.
  • You can use .intern() method of new String() not to create a new object on heap instead get the reference from String pool if already present and if not present then it will create a new object on the heap and add the reference to the string pool.

No comments: