Interview replay
Full round replay — String immutability and the pool
Timed verbal replay with pass/fail criteria per follow-up.
How to run this
The opener
“Why is String immutable, and what is the string pool?”
Budget: 45 seconds. Going long here is itself a fail signal.
Follow-ups
1. “Is it immutable because the value field is final?”
Testing: The single most common wrong reason, stated confidently everywhere.
Scoring
Pass: No. final stops the reference being reassigned; the array contents stay writable. Immutability comes from the array being private and never leaked — toCharArray() and getBytes() both copy.
Fail: Yes, or 'final plus the class is final' with no mention of encapsulation.
2. “Then why does == sometimes work on strings?”
Testing: Whether they can explain interning rather than just name it.
Scoring
Pass: Literals and compile-time constants are interned into a JVM-wide pool, so identical literals are one object. Runtime-built strings are fresh objects.
Fail: 'Java optimises it' with no mechanism.
3. “Does adding final to a local variable change the result of a == comparison?”
Testing: Constant variables in the JLS sense — almost nobody knows this.
Scoring
Pass: Yes. A final local initialised with a literal is a constant variable, so expressions using it are folded at compile time and interned. Same expression without final is computed at runtime and is not.
Fail: Says final cannot possibly affect it.
4. “Where does the pool live?”
Testing: Version awareness.
Scoring
Pass: The heap, since Java 7. It was in PermGen before that, which is why interning user input used to cause OutOfMemoryError: PermGen space. PermGen itself is gone since Java 8.
Fail: 'PermGen' with no version qualifier.
5. “What does substring() cost, and has that changed?”
Testing: The 7u6 change and why it was made.
Scoring
Pass: O(n), it copies. Before 7u6 it was O(1) and shared the parent array via offset/count, so a small substring pinned the whole original array alive.
Fail: Says it still shares the array.
6. “What is a String made of on Java 21?”
Testing: Compact strings.
Scoring
Pass: A private final byte[] plus a one-byte coder. Latin-1 is one byte per char; anything outside it is UTF-16 at two. Decided per string, so one CJK character doubles the whole string.
Fail: char[].
7. “Does + still compile to StringBuilder?”
Testing: JEP 280 — most candidates are eight years out of date here.
Scoring
Pass: Not since Java 9. It compiles to invokedynamic makeConcatWithConstants. StringBuilder is still right in a loop, because each + still allocates a whole new string.
Fail: Yes, javac rewrites it to StringBuilder.
8. “Give me a reason immutability matters that isn't thread safety.”
Testing: Depth beyond the memorised list.
Scoring
Pass: The hash code is cached after first use, which is only sound because the value cannot change — that is what makes String cheap as a map key. Interning depends on it too.
Fail: Repeats thread safety in different words.
Score yourself
← Back to Why is String immutable, and what is the string pool?