Strings in depth: interview questions & scenarios
These are the string questions I ask most, and almost none of them are about method names. A two-line == puzzle or "how many objects?" tells me in a minute whether you can picture the heap, the pool and the bytecode behind a line of Java.
Here's how to use this page: read the question, say your answer out loud first, then open the model answer. The "What if..." follow-ups are the ones I actually ask next. If one stumps you, go back to the lessons: Why String is immutable, How Strings live in memory: the string pool, StringBuilder vs StringBuffer and Interning and compact strings.
What's the difference between String, StringBuilder and StringBuffer?
Tests immutability, mutability, synchronization
Show answerHide answer
Model answer
String is immutable: every "change" returns a new object, which makes it safe to share, cache and use as a map key. StringBuilder and StringBuffer are mutable buffers (a byte[] with spare capacity) for building text step by step and calling toString() once at the end.
The only real difference between the two builders is that StringBuffer (Java 1.0) makes every method synchronized, while StringBuilder (Java 5) has no locks. Use StringBuilder by default; strings are almost always built in a local variable on one thread.
How I’d say it in the interview
"String is immutable, so it's thread-safe and great as a key, but modifying it in a loop creates garbage. StringBuilder and StringBuffer are mutable buffers; Buffer synchronizes every method, Builder doesn't. I default to StringBuilder in local variables."
Signals depth
Mentioning that per-method locking doesn't make a sequence of appends atomic, and that on JDK 21 the JIT often elides StringBuffer's locks on a single thread, so the speed gap is small.
Red flags
"StringBuffer is faster" or "StringBuilder is immutable". Also: "always use StringBuffer to be safe".
Follow-ups interviewers ask next
What if I need to compare two
StringBuilders by content?equalsisn't overridden, sosb1.equals(sb2)compares identity. Usesb1.compareTo(sb2) == 0(Java 11+),"...".contentEquals(sb), or compare theirtoString()results.
What does this print?
Tests string pool, == vs equals
Show answerHide answer
Model answer
String a = "PAID";
String b = "PAID";
String c = new String("PAID");
String d = c.intern();
System.out.println(a == b); // true
System.out.println(a == c); // false
System.out.println(a.equals(c)); // true
System.out.println(a == d); // true
Both literals resolve to the same pooled object, so a == b. new always creates a fresh object, so a == c is false even though the content matches. intern() returns the pooled instance for "PAID", which is a.
How I’d say it in the interview
"True, false, true, true. Literals share one pooled object, new makes a separate one, equals compares content, and intern hands back the pooled instance. In real code I'd only ever use equals."
Signals depth
Saying the pooled and new objects live in the same heap, and that on Java 9+ c shares a's byte[].
Red flags
"a == c is true because the text is the same." That's confusing reference equality with value equality.
Follow-ups interviewers ask next
What if
ccame fromrequest.getParameter("status")?Then
a == cisfalsetoo, for the same reason: strings created at runtime are never pooled automatically. That's the production version of this bug.
How many objects does new String("a") create?
Tests ldc, constant pool, heap
Model answer, how to say it, 2 follow-ups: unlock with the Java Pass
Why shouldn't you use + inside a loop? Doesn't the compiler optimise it?
Tests O(n²) concatenation, JEP 280
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does this print, and why?
Tests constant expressions, final, JLS 15.29
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Where is the string pool stored, and can pooled strings be garbage collected?
Tests PermGen, heap, weak references
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Is StringBuffer thread-safe? Would you use one as a shared audit log?
Tests synchronized, compound actions, confinement
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A tenant check passes every unit test, but in production every request is treated as "unknown tenant". Walk me through it.
Tests == on runtime strings, pool
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
An export endpoint was fine at launch and now takes seconds as data grew. The profiler shows time in Arrays.copyOf and lots of young GCs. What's happening?
Tests quadratic concatenation, builders, streaming
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does intern() do, and when would you actually use it?
Tests canonicalisation, cost, alternatives
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What are compact strings, and why can one emoji double a string's memory?
Tests JEP 254, coder, Latin-1 vs UTF-16
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
After loading a product catalogue, your service's heap is 70% strings and pods are getting OOM-killed. What do you do?
Tests measurement, dedup, intern, enums
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does this print on Java 21?
Tests intern semantics, Java 7 change
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
You changed a constant in a shared library, redeployed the service, and it still uses the old value. How?
Tests constant inlining, binary compatibility
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
We added .intern() to every incoming header value to save memory. P99 latency went up and memory didn't drop. Why?
Tests intern cost, cardinality
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How does javac compile "order-" + id + ":" + qty since Java 9?
Tests invokedynamic, StringConcatFactory, JEP 280
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal