← Back to Java

StringBuffer vs StringBuilder

JavaEntryjava

The Question

What is the difference between StringBuffer and StringBuilder?

What a Strong Answer Covers

  • StringBuffer synchronized/thread-safe
  • StringBuilder not synchronized
  • StringBuilder faster
  • StringBuffer Java 1.0 vs StringBuilder Java 1.5

Senior-Level Answer

Both `StringBuffer` and `StringBuilder` are mutable character sequences used when you need to build or modify strings without creating multiple intermediate `String` objects. They share the same API — `append()`, `insert()`, `delete()`, `reverse()`, `toString()` — and both inherit from `AbstractStringBuilder`. The critical difference is thread safety.

`StringBuffer` was introduced in Java 1.0 and every public method is `synchronized`. This means only one thread can execute any mutating method at a time, making it safe for concurrent use. The synchronization overhead, however, makes it slower in single-threaded contexts.

`StringBuilder` was introduced in Java 5 specifically to address that overhead. Its methods are not synchronized. In a single-threaded environment — which covers the vast majority of string-building work — it is the correct choice and is measurably faster.

In practice, `StringBuilder` is almost always the right default. `StringBuffer` is appropriate only when you genuinely need a shared mutable string across threads, which is rare in modern code because most such patterns are better served by other concurrency primitives or by passing immutable strings across thread boundaries.

The Java compiler itself uses `StringBuilder` internally when it optimizes the `+` operator for string concatenation (up to Java 8; Java 9+ uses `invokedynamic` with `StringConcatFactory` for even more efficient compilation).

Capacity management is identical in both: they start with a default internal buffer of 16 characters and grow by doubling when capacity is exceeded. Pre-sizing with `new StringBuilder(expectedSize)` avoids reallocations in performance-sensitive code.

For interviews, the clean summary is: same API, `StringBuffer` is synchronized (thread-safe, slower), `StringBuilder` is not (not thread-safe, faster). Use `StringBuilder` by default.

Key Differences

AspectStringBufferStringBuilder
Thread safetyYes — all methods synchronizedNo — not synchronized
PerformanceSlower due to lock overheadFaster in single-threaded use
Introduced inJava 1.0Java 5
Use caseShared mutable string across threadsSingle-threaded string building
API surfaceIdentical to StringBuilderIdentical to StringBuffer
Preferred defaultNoYes

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly identifies that StringBuffer is synchronized and StringBuilder is not, and states the performance implication.

3/3 — Strong Answer

Also explains when to use each, mentions they share AbstractStringBuilder, and notes the compiler's preference for StringBuilder internally.

Common Mistakes

  • Saying StringBuilder is thread-safe — it is explicitly not; concurrent use without external synchronization produces undefined behavior.
  • Recommending StringBuffer as a default 'just to be safe' — the synchronization cost is unnecessary overhead in single-threaded contexts.
  • Forgetting that String concatenation with + in a loop bypasses both and creates excessive garbage; StringBuilder should be used explicitly.

Follow-Up Questions

  • How does the Java compiler handle `String s = a + b + c` under the hood? — Pre-Java 9: compiles to a StringBuilder chain. Java 9+: uses invokedynamic with StringConcatFactory.
  • If StringBuffer is thread-safe, can you safely use it as a shared log buffer across threads without any other synchronization? — Individual method calls are atomic, but compound operations (check-then-act) still require external locking.
  • What happens when a StringBuilder's internal buffer is exhausted? — It allocates a new array of (current capacity * 2 + 2) and copies existing content — similar to ArrayList growth.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout