All topics
library
beginner

String Immutability & String Pool

Understand why Strings are immutable, how the String Pool works, and the performance implications.

A String never changes after it is created. Every method that looks like it edits one returns a new object.

String Pool = a library's reference shelf. Instead of buying the same book twice, the library keeps one copy and gives everyone the same reference.

Key Concepts

1
    String s = "hello";
    s.toUpperCase();         // returns "HELLO", s is still "hello"
    s = s.toUpperCase();     // now s points at the new object
2
Why immutability was chosen.
    thread safety    shareable with no synchronisation
    security         class names, file paths and URLs cannot be altered
                     after a permission check
    hashCode cache   computed once, which is why String keys are fast
    the pool         identical literals can share one object safely
3
The pool is where literals live.
    String a = "hello";
    String b = "hello";
    a == b;                  // true -- the same pooled object
4
    String c = new String("hello");
    a == c;                  // false -- a fresh heap object
    a.equals(c);             // true
5
new String(...) deliberately bypasses the pool, which is why it is almost never what you want.
new String(...)
6
intern() puts a string into the pool, or returns the copy already there:
intern()
7
    c.intern() == a;         // true
8
Useful when you hold millions of strings drawn from a small set of values. Costly otherwise.
9
So compare with equals, never ==. The pool makes == appear to work for literals, and it fails the moment a string arrives from a file, a request or a database.
equals==
10
Concatenation in a loop is the performance trap.
    String s = "";
    for (String w : words) s += w;       // a new String every iteration
11
That is O(n^2) copying. A StringBuilder is O(n).
StringBuilder
12
Since Java 9 a String stores bytes, not chars. Latin-1 text uses one byte per character instead of two, which cut memory for ASCII-heavy applications by roughly half. The pool moved from permgen to the heap back in Java 7, so it is garbage collected like anything else.
13
What the interviewer is probing.1. "Why are Strings immutable?" Probing: whether you can give more than one reason. Stalls: "For safety." Moves up: thread safety without synchronisation, a cached hashCode that makes them fast map keys, safe sharing through the pool, and security for values checked once then used.
14
2. "Is new String('x') == 'x' true or false?" Probing: the pool. Stalls: "True, same characters." Moves up: false — the literal is pooled and new String deliberately allocates a fresh object. equals is true.
15
3. "What does intern() do and when is it worth it?" Probing: whether you know the cost as well as the call. Stalls: "It makes strings faster." Moves up: it returns the pooled instance, worth it when you hold millions of strings drawn from a small set of values, and wasteful otherwise.
16
4. "Why is += in a loop a problem?" Probing: the quadratic copy. Stalls: "It is a bit slower." Moves up: each += copies the whole string so far into a new object, so the loop is O(n^2); StringBuilder writes into one growing array.