Dung (Donny) Nguyen

Senior Software Engineer

String Pool

The String Pool (also known as the String Intern Pool) is a special memory region where String literals are stored. It is part of the heap memory and is used to optimize memory usage by storing only one copy of each distinct string value.

Here’s how the String Pool works:

String Literal:

When we create a string using double quotes (e.g., String s1 = "Hello";), the JVM checks the String Pool to see if a string with the same value already exists.

Example:

String s1 = "Hello";
String s2 = "Hello";

System.out.println(s1 == s2); // true, because both point to the same object in the String Pool

String Object:

If we create a string using the new keyword (e.g., String s3 = new String("Hello");), a new object is created in the heap, and it is not placed in the String Pool unless explicitly requested.

Example:

String s3 = new String("Hello");
System.out.println(s1 == s3); // false, because s3 refers to a different object

String Interning:

We can manually add a string to the String Pool using the intern() method. The intern() method checks the String Pool, and if the string is already there, it returns the reference to the pooled string. If not, it adds the string to the pool.

Example:

String s4 = new String("Hello");
String s5 = s4.intern();

System.out.println(s1 == s5); // true, because s5 now refers to the interned string in the pool

Advantages of the String Pool:

However, excessive string creation or manipulation can lead to performance issues, so understanding when and how to use the String Pool effectively is important in Java programs.