Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
String Pool

String Pool

Nov 25 2025

String pool is a memory structure at runtime that holds string objects and enables avoiding extra memory allocations when creating a new string object. The string pool exists because strings are immutable and have an interning mechanism.

In C# and Java, string interning helps store a single String instance in memory for all identical strings. If an application handles many duplicated strings, there's no need to create a new String object when a value already exists. Instead, an existed instance can be reused from the intern table.

String literals are interned automatically; however, developers can manually intern strings using the intern method.

The process of creating a string using the "hello" literal looks like this:

  • The string pool searches for an object with the same value. If it's found, its reference is returned.
  • If the string isn't found, a new string object is created, placed in the pool, and then its reference is returned.

String comparison specifics in Java

Since the pool ensures that identical strings have identical references, they can be compared using the == operator:

String a = "hello";
String b = "hello";
System.out.println(a == b); // true

Creating a string with the class constructor produces a new object outside the string pool unless the intern() method is explicitly invoked. In this case, the string comparison results will always return false.

String a = new String("hello");
String b = "hello";
System.out.println(a == b); // false

Therefore, it pays to be extra cautious when comparing strings using the == operator in Java. The string from an external library or other program location may have been created via a constructor and not be interned.

If there's no guarantee whether the string is in the pool, it'd be better to use string comparison with the String.equals() method. It compares the internal string contents and doesn't depend on the string pool or the interning mechanism.

String a = new String("hello");
String b = "hello";
System.out.println(a.equals(b)); // true
Popular related articles