String Pool and Interning
Fast recall
Quick revision
Core answer
Whether you understand heap vs pool, PermGen vs heap evolution, and the performance trade-offs of intern().
Thinking the pool is in PermGen in all Java versions. Since Java 7, it moved to the main heap and is GC-eligible.
Discuss G1 GC's String Deduplication feature (-XX:+UseStringDeduplication) as an automatic alternative to manual interning.
Interview-ready guide
Interview answer
The string pool is how the JVM avoids allocating duplicate objects for identical string literals — when you write "hello" twice in source, both references point to the same pool entry.
How it works:
- String literals are automatically interned at class loading time
"hello" == "hello"is true because both reference the same pool objectnew String("hello")bypasses the pool —==against a literal returns false;.equals()returns true
History (PermGen → heap):
- Before Java 7: pool lived in PermGen — a fixed-size non-GC region; too many interned strings caused
OutOfMemoryError: PermGen space - Java 7+: pool moved to the main heap, making interned strings GC-eligible when unreachable
intern() use case:
- Useful when parsing millions of rows with repetitive values (status codes, country codes)
- Acquires a global lock on the string table — not worth calling on per-request strings
- Modern alternative: G1's
-XX:+UseStringDeduplicationdeduplicates the backingbyte[]during GC without any lock contention
I recommend -XX:+UseStringDeduplication over manual intern() for most services — it works without any code changes.
String Pool and Interning
Direct Answer
The String pool (intern table) deduplicates literals on the heap (Java 7+; PermGen before). Literals hit the pool first; String.intern() adds a runtime string and returns the canonical reference. Interned strings may use == for identity — but intern() is costly; prefer literals or bounded caches.
Common Mistakes & Pitfalls
Thinking the pool is in PermGen in all Java versions. Since Java 7, it moved to the main heap and is GC-eligible.
Check your understanding
Answer these without looking back at the article.
- What is the difference between String, StringBuilder, and StringBuffer?
- How does Java 9's compact strings optimization work?
- What happens when you concatenate strings with + in a loop?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.