StringBuilder vs StringBuffer — does the difference still matter in modern code?
Reviewed by Gurusankar M.
Short Answer
Both are mutable char buffers; StringBuffer is synchronized, StringBuilder isn't. Use StringBuilder by default (local, single-threaded). StringBuffer's per-call locking is rarely useful since builders are almost always method-local.
On this page
Concept Made Simple
Coffee Chat Question
“StringBuilder vs StringBuffer — does the difference still matter in modern code?”
Remember It Faster
Mind Map Answer
- StringBuilder
- not synchronized — faster, default
- StringBuffer
- synchronized — legacy
- Reality
- buffers are local → no sharing → no lock needed
Learn by Doing
Hands-on Keyboard
StringBuilder sb = new StringBuilder(64); // pre-size to avoid resizes
for (Order o : orders) sb.append(o.id()).append(',');
String csv = sb.toString();- Time:
- O(n) total — vs O(n²) for String + in a loop
Think Beyond the Expected
What If?
Is String concatenation with + in a loop the same as StringBuilder?
No. `+` in a loop creates a new String each iteration → O(n²) copying. The compiler optimizes a single-expression concat, but loops need an explicit StringBuilder for O(n).
Real World
Building a large CSV/JSON payload with `result += line` in a loop is a frequent cause of CPU spikes; switching to a pre-sized StringBuilder fixes it.
Interviewer's Expectation
Keywords they're listening for:
- synchronized vs not
- default to StringBuilder
- + in loop is O(n²)
- pre-sizing capacity
Common Mistakes
- Using StringBuffer 'just in case' for thread-safety it doesn't need
- String += inside a loop
- Not pre-sizing when the length is known
Best Practices
- Default to StringBuilder; pre-size with an initial capacity
- Reserve StringBuffer for genuinely shared mutable buffers (rare)
- Use String.join / Collectors.joining for simple cases
Follow-up Questions
- How does StringBuilder grow its internal array?
- What does the compiler do with a + b + c in one expression?
- When is StringBuffer ever justified?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Strings (Core Java) Interview question: "StringBuilder vs StringBuffer — does the difference still matter in modern code?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair