Understanding and Using the Java String Pool in Java

Murat
4 min readDec 25, 2022

--

Java String Pool is a special storage area in the Java heap where string literals are stored. It is implemented to improve the performance of string operations and to conserve memory. It is also known as String Intern Pool or String Constant Pool.

When you create a new string literal from the String class, the JVM will first check if the string already exists in the string pool. If it does, the JVM will return a reference to the existing string object, rather than creating a new object. This is known as string interning.

For example, consider the following code:

String s1 = "Harry Potter";
String s2 = "The Lord of the Rings";
String s3 = "Harry Potter";

In this code, the JVM will create a single string object with the value “Harry Potter” and store it in the string pool. Both s1 and s3 will be references to this single string object.

If the literal is not present in the pool (“The Lord of the Rings) , a new String object takes place in the String pool.

String Pool representation with String literal

Using the String Pool

There are two ways the create String in Java programming language. The first way is using String Literal and the other way is using new keyword.

String literal object creation example:

String s1 = "Harry Potter";
String s2 = "The Lord of the Rings";
String s3 = "Harry Potter";

String creation example with new keyword:

String s4 = new String("Harry Potter");
String s5 = new String("The Lord of the Rings");

Let’s understand what is the difference between them. Let’s compare the string literals’ references.

s1==s3 //true
s1==s4 //false

Short Reminder! When you compare two objects using == operator, It compares addresses in the memory.

String Pool representation with String literal and New Keyword

As you can see the picture and example in the above, whenever we use a new operator to create a string it creates a new string object in the Java heap and don’t check the object whether its in the String pool or not.

String.intern() Method

String literals are automatically interned, which means that they are stored in the string pool. However, you can also intern strings manually using the intern() method of the String class. This is useful if you want to ensure that a particular string is stored in the string pool and can be shared by multiple variables or objects.

String s6 = new String("The Lord of the Rings").intern();
Intern Method Usage

Now we can see that;

s2==s6 //true
s2==s5 //false

Benefits of the String Pool

  • Improved performance: Because the JVM can return a reference to an existing string object rather than creating a new one, string operations are faster when using the string pool.
  • Memory conservation: The string pool helps to conserve memory by avoiding the creation of unnecessary string objects.
  • Shared strings: The string pool allows you to share strings between different variables and objects, which can be useful for reducing the memory footprint of your program.

Downside of String Pool

it can lead to decreased performance. Retrieving a string from the pool requires a search through all of the strings in the pool, which can be slower than simply creating a new string object. This is especially true if the program creates and discards a large number of strings, as the string pool will need to be searched each time a string is used.

Some Notes to Remember

  • String pool is an example of the Flyweight Design Pattern.
  • All literal strings and string-valued constant expressions are interned.

Before Java 7, the JVM placed the Java String Pool in the PermGen space, which has a fixed size — it can’t be expanded at runtime and is not eligible for garbage collection.

The risk of interning Strings in the PermGen (instead of the Heap) is that we can get an OutOfMemory error from the JVM if we intern too many Strings.

From Java 7 onwards, the Java String Pool is stored in the Heap space, which is garbage collected by the JVM. The advantage of this approach is the reduced risk of OutOfMemory error because unreferenced Strings will be removed from the pool, thereby releasing memory.

--

--