What is the String Constant Pool in Java?
The String Constant Pool (also called String Intern Pool) is a special memory region inside the JVM's heap where string literals are stored and reused. When you write a string literal like `String s = "hello"`, the JVM checks the pool first. If "hello" already exists, it returns the same reference rather than allocating a new object. This is possible precisely because Strings are immutable — sharing references is safe since no one can modify the underlying character data.
Prior to Java 7, the pool lived in PermGen (Permanent Generation), which had a fixed size and could cause `OutOfMemoryError` under heavy string usage. From Java 7 onwards, the pool was moved to the main heap, allowing it to participate in normal garbage collection and be resized dynamically.
You can manually add a string to the pool using `String.intern()`. This is useful when you receive strings from external sources (e.g., parsing files) and want to deduplicate them for memory efficiency. For example, `String s = new String("hello").intern()` places the value in the pool and returns the canonical reference.
The key distinction is between `String s1 = "hello"` (uses the pool) and `String s2 = new String("hello")` (creates a new heap object, bypassing the pool). This is why `s1 == s2` returns false even though the content is identical — `==` compares references, and `s2` is a distinct heap object.
Understanding the pool matters for performance-sensitive applications: overuse of `new String()` inflates heap usage unnecessarily, while aggressive interning of dynamic strings can bloat the pool. The practical guidance is to use literals and compile-time constants wherever possible and reserve `intern()` for well-understood deduplication scenarios.
Explains that the pool caches string literals and that the JVM reuses references for identical strings.
Also covers the PermGen-to-heap migration in Java 7, the role of immutability in making sharing safe, and the behavioral difference between literals and `new String()`.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment