Lesson · Strings & Text
The String type: immutable, pooled, special-cased
11 min readThe String Type
Strings are objects, but Java treats them like values: literals share storage in the pool, every operation returns a new string, and equality needs equals rather than ==
Immutable by decree
A String cannot change after construction. s.toUpperCase() does not modify s; it returns a new string and leaves the original alone. That is why string code reads like a pipeline of transformations, chained calls each producing the next value.
Immutability is the foundation for everything else: strings are safe to share, safe to use as HashMap keys (their hash can be cached and never goes stale), safe to pass across threads, and safe as the element of the pool. The API is full of methods that would be void in a mutable design but return String here by necessity.
The string pool
String literals are interned: the compiler emits each distinct literal once into the class file's constant pool, and at load time the JVM places one copy into the string pool. Two literals with the same characters are therefore the same object, and "hi" == "hi" is true. A new String("hi") deliberately bypasses this and creates a fresh heap object with the same characters.
The distinction shows in equality: == compares references, so it is true for pooled literals and false when either side came from new (or from concatenation at runtime). equals compares characters always. The rule of practice is absolute: compare strings with equals, and treat == as a trap. intern() deliberately returns the pooled instance, which is occasionally useful for memory and almost never in ordinary code.
String pool · identity vs equality
String a = "hi";
String b = "hi";
String c = new String("hi");
a == b
a == c
a.equals(c)
c.intern() == a
// no output yet
String pool
Variables
A literal is created. The JVM first consults the string pool: with hi absent, one instance is allocated and registered with the label #1.
Concatenation and the compiler's help
The + operator works on strings because the compiler rewrites it: a chain of + in one expression becomes one StringBuilder append sequence, so "a" + b + "c" in a loop body is a single builder, not a cascade of temporaries. Two literals added with + at compile time ("he" + "llo") are folded into one constant, which is why that form IS pooled.
The trap lives in loops: when each iteration's result feeds the next, the compiler cannot fold anything, and every round builds a new string. That is the ancient case for StringBuilder, and the next topics show it in full. For occasional concatenation, + remains the readable choice; the compiler's constant folding handles the rest.
Why the special treatment exists
The pool saves memory across a program's lifetime, since real programs repeat the same class names, keys and constants endlessly. Immutability makes the sharing invisible and safe: no one can corrupt a shared literal because no one can write to a string at all. And equals/hashCode over the characters make strings usable as map keys, which is the property that the internals topic expands.
The design is a compact answer to a common problem: make the most-used reference type behave like a value. Generics and modern records would later formalise parts of the idea, but String got there first, at a time the language could afford the special cases.
== compares references; equals compares characters
For interned literals == happens to be true, which is exactly why it keeps being written and later fails on data from user input, substrings or new String. There is no context in ordinary code where == on strings is correct. String equality is always equals (or equalsIgnoreCase, or Objects.equals for null safety).
Interview angles
Why is String immutable?+
To make sharing safe: interning, HashMap keys, thread sharing and cached hash all rest on it. If strings could change, every one of those would corrupt silently. Immutability turned a resource optimisation into a language guarantee.
Difference between the pool and new String?+
A literal consults the pool and reuses one instance; new String allocates a fresh object on the heap with the same characters. == sees the difference; equals and hashCode do not. new String is almost always a mistake or an interview probe.
When is string concatenation with + a performance problem?+
When it accumulates across iterations: each round builds a new string while the previous is discarded. The compiler folds constants and chains within one expression; it cannot help when the value is rebuilt per loop pass. That is StringBuilder's job.