Detailed explanation
The String pool, also called the intern pool, is a JVM-managed mechanism that maintains one canonical String reference for each distinct interned character sequence. In modern Java, pooled String objects reside in the heap. String literals such as "hello" and strings produced by compile-time constant expressions are interned automatically. Therefore, String a = "hello"; and String b = "hello"; assign the same reference, making a == b true. Pooling allows repeated literals to share an immutable object rather than requiring a separate String object for every occurrence.
The expression new String("hello") creates a distinct String object instead of returning the pooled reference. However, the literal "hello" used as its argument is itself interned. If String d = new String("hello");, then a == d is false, while a.equals(d) is true. Calling d.intern() asks the JVM for the canonical reference representing d's contents. If an equal string already exists in the pool, intern() returns that existing object; otherwise, it adds d to the pool and returns d. It does not modify d or automatically reassign references already pointing to it.
In the example String c = new String("hello").intern();, c receives the already-pooled "hello" reference, so a == c is true. Saying that intern() 'forces c into the pool' is misleading: c is a variable, and the newly constructed object is not added when an equal pooled string exists. Under the hood, the JVM looks up interned strings in an internal table using their contents. String immutability makes sharing safe because one reference cannot change the characters observed through another reference.
Interviewers usually check whether you distinguish reference identity from content equality and understand which expressions are automatically interned. For example, "hel" + "lo" is a compile-time constant and shares the "hello" reference, whereas concatenation involving a non-constant variable is generally evaluated at runtime and is not automatically interned. Explicit interning can reduce duplicate retained String objects in suitable workloads, but lookups and pool management have costs. Use equals() for ordinary content comparisons; treat interning as a deliberate optimisation, not a requirement for correct String handling.
Key points to remember
- The pool provides one canonical reference per distinct interned String value.
- String literals and String-valued compile-time constant expressions are automatically interned.
- new String(...) creates a distinct object, even when its argument is a pooled literal.
- intern() returns the canonical reference; callers must use its return value.
- == compares references, while equals() compares String contents.
Common mistakes candidates make
- Claiming that every String created at runtime is automatically pooled.
- Saying intern() always inserts the newly created object or changes the original variable's reference.
- Using == for content comparisons because it appears to work with literals.
Likely follow-up questions
How many objects does new String("hello") create?
The new expression creates one distinct String object. The literal also requires a pooled String if one does not already exist, so avoid claiming that this statement always creates exactly two String objects.
Is a concatenated String automatically interned?
A compile-time constant expression such as "hel" + "lo" is automatically interned. Concatenation involving a non-constant variable is evaluated at runtime and is not automatically interned; a final String variable initialised with a constant expression can still participate in compile-time concatenation.
Can pooled Strings be garbage-collected?
In modern HotSpot JVMs, an interned String can be collected when it is no longer strongly reachable; interning alone does not keep it alive forever. Literals may remain reachable through loaded classes, so their lifetime depends on those references as well.
Interview tip: Explain the pool through the supplied example, contrast == with equals(), and clarify that intern() returns a reference rather than modifying an object.
