# String Immutability and the String Pool

> Why is String immutable in Java? What is the String constant pool, and how many objects are created by new String("hello")?

Canonical: https://www.internhack.in/learn/interview/java-interview/java-string-immutability-pool
Last updated: 2026-11-28

Difficulty: Beginner

## Question

Why is String immutable in Java? What is the String constant pool, and how many objects are created by new String("hello")?

## Answer

If there is one class every Java interviewer will poke at, it is String. Let me explain it the way I would to a new joiner on my team: first why the designers made it immutable, then how the pool works, then the trick questions.

Immutable means once a String object is created, its content can never change. Methods like concat, replace or toUpperCase do not modify the original; they return a new String. The original stays untouched. Inside the class, the value is stored in a private final array (byte\[\] since Java 9, with compact strings), and there is no method that exposes or modifies it.

Why did the designers do this? There are four solid reasons. First, security: Strings carry database URLs, file paths, usernames and class names. If someone could mutate a path after the security check passed, they could bypass it. Second, thread safety: since the content never changes, a String can be shared between threads with no synchronization. Third, hashCode caching: String caches its hash code after the first computation, which makes it an excellent HashMap key, but that only works if the content can never change. Fourth, the String pool itself: sharing one object between many references is only safe when nobody can modify it.

Now the pool. The String constant pool (also called the intern pool) is a special area inside the heap, moved there from PermGen in Java 7. When you write a literal like "hello", the JVM first checks the pool. If an equal String already exists, your variable simply points to it. Otherwise a new one is created in the pool. So two literals with the same text are the same object, and == returns true.

When you write new String("hello"), you force a brand new object on the heap, outside the pool reference you would normally get. So how many objects? If the literal "hello" was not yet in the pool, two objects are created: one in the pool for the literal and one on the heap for the new call. If it was already in the pool, just one new heap object is created. This is the answer interviewers want, with the conditional.

The intern method takes a String and returns the canonical pool version, adding it if absent. So new String("hello").intern() == "hello" is true.

Watch out for compile-time constants. The expression "hel" + "lo" is folded by the compiler to "hello", so it lands in the pool and == works. But if one operand is a non-final variable, the concatenation happens at runtime and creates a new heap object, so == fails. If the variable is declared final and initialized with a constant, the compiler again folds it.

One more subtle point: immutability does not mean the reference variable cannot change. You can reassign s = s + "x", which points s to a new object, leaving the old one for garbage collection. That is also why repeated concatenation in a loop is wasteful, and why StringBuilder exists.

Finally, a security note: prefer char\[\] over String for passwords. A String stays in the pool and memory until garbage collected and can show up in heap dumps, while a char\[\] can be overwritten after use.

## Code examples

### Pool behaviour and equality

```
public class PoolDemo {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = new String("hello");
        String d = c.intern();
        String x = "hel";
        String y = x + "lo";
        final String fx = "hel";
        String z = fx + "lo";

        System.out.println(a == b);
        System.out.println(a == c);
        System.out.println(a == d);
        System.out.println(a == y);
        System.out.println(a == z);
        System.out.println(a.equals(c));

        String s = "java";
        s.concat(" rocks");
        System.out.println(s);
    }
}
```

y is built at runtime from a non-final variable so it is a new heap object. z uses a final constant, so the compiler folds it to the pooled literal. concat returns a new String that we threw away, so s is still java.

## Key points

- String is immutable for security, thread safety, hashCode caching and pooling.
- Literals share pooled objects; new String creates a fresh heap object.
- The pool lives in the heap since Java 7, not in PermGen.
- intern returns the canonical pooled instance.
- Always compare Strings with equals, not ==

## Common follow-up questions

- How can you make your own immutable class?
- Where is the String pool located in memory?
- Is it possible to change a String using reflection?
- Why is char\[\] preferred for passwords?

## Interview tips

- When asked how many objects new String("x") creates, always answer with the conditional: one or two depending on whether the literal is already in the pool.
- Do not confuse an immutable object with a final reference. They are different things.

## Concepts covered

String, Immutability, String Pool, intern, Memory

Previous: [JDK vs JRE vs JVM and Platform Independence](https://www.internhack.in/learn/interview/java-interview/java-jdk-jre-jvm) · Next: [String vs StringBuilder vs StringBuffer](https://www.internhack.in/learn/interview/java-interview/java-string-stringbuilder-stringbuffer) · [All of Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)

## More Core Java & Collections questions

- [== vs equals() and the hashCode Contract](https://www.internhack.in/learn/interview/java-interview/java-equals-hashcode-contract)
- [final vs finally vs finalize](https://www.internhack.in/learn/interview/java-interview/java-final-finally-finalize)
- [The static Keyword: Variables, Methods, Blocks and Nested Classes](https://www.internhack.in/learn/interview/java-interview/java-static-keyword)
- [Wrapper Classes, Autoboxing and the Integer Cache](https://www.internhack.in/learn/interview/java-interview/java-wrapper-autoboxing-integer-cache)
- [Checked vs Unchecked Exceptions](https://www.internhack.in/learn/interview/java-interview/java-checked-vs-unchecked-exceptions)
- [try-with-resources and AutoCloseable](https://www.internhack.in/learn/interview/java-interview/java-try-with-resources)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)
