What Is the String Pool in Java?
Fast recall
Quick revision
Core answer
String.intern() returns the canonical object for equal content. Pooled strings are ordinary String objects managed in the heap in modern HotSpot; do not use == as a content test even though equal interned references are identical.Whether you understand where string literals live in memory and why the JVM can safely share them.
Thinking all Strings go into the pool. Only literals and interned strings go there. `new String('hello')` is not itself automatically interned and always creates a new heap object.
Mention that the pool is only safe because Strings are immutable — if one variable could modify 'hello', every other variable pointing to the same pooled object would be corrupted. Immutability and the pool are inseparable.
Interview-ready guide
Interview answer
The string pool stores a canonical object for each interned string value. Literals and compile-time constant string expressions are interned automatically, and intern() returns the canonical object for a runtime value. That is why two identical literals compare true with ==.
I still use equals() for content, because input and most runtime-created strings are not automatically interned. The pool is an optimization and canonicalization mechanism; it should not decide whether an equality check works.
What Is the String Pool in Java
Direct Answer
The string pool is the JVM's canonical set of interned strings. String literals and compile-time constant string expressions are interned, and String.intern() returns the canonical object for equal content. Pooled strings are ordinary String objects managed in the heap in modern HotSpot; do not use == as a content test even though equal interned references are identical.
Code Example
public class StringPoolDemo {
public static void main(String[] args) {
// Literals — both go through the pool
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2); // true — same pooled object
System.out.println(s1.equals(s2)); // true — same content
// new String() is not itself automatically interned — always creates a new heap object
String s3 = new String("hello");
System.out.println(s1 == s3); // false — different objects
System.out.println(s1.equals(s3)); // true — same content
// Always use .equals() to compare content — never == for Strings
// == checks address (are they the same object?)
// .equals() checks content (do they have the same characters?)
}
}
Common Mistakes & Pitfalls
- Thinking every
Stringlives in the pool — literals, compile-time constant string expressions, and strings represented canonically throughintern()are pooled;new String("hello"), concatenation results, and strings from input or files land on the regular heap. - Comparing content with
==—==checks whether two references point to the same object, not whether the characters match; it returnstruefor two literals by luck of pooling andfalsefor anew String. Use.equals(). - Assuming
new String("hello")holds different text — the content is identical to the literal; only the object identity differs, which is exactly why==and.equals()disagree. - Believing the pool still lives in PermGen — since Java 7 it sits in the normal heap, so pooled strings are subject to garbage collection like any other object.
Check your understanding
Answer these without looking back at the article.
- What is the difference between String s = 'hello' and String s = new String('hello')?
- Why should you always use .equals() to compare Strings in Java?
- What does String.intern() do?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.