StringBuilder vs StringBuffer in Java
Fast recall
Quick revision
Core answer
StringBuilder and StringBuffer are mutable character-sequence builders with similar APIs. StringBuffer synchronizes its individual operations and can be shared for those operations; StringBuilder provides no synchronization and is the normal choice when the builder is confined to one thread. A multi-call check-then-act sequence is not atomic merely because it uses StringBuffer.Whether you know the thread-safety distinction and can make the right choice for a given context.
Always using StringBuffer 'to be safe' — its synchronisation overhead makes it slower in single-threaded code, which is 99% of the time you're building strings.
Mention that StringBuffer exists since Java 1.0 and StringBuilder was introduced in Java 5 specifically to give a faster non-synchronized alternative. In practice, StringBuilder is almost always the right choice — if you genuinely need thread-safe string building, you usually coordinate at a higher level anyway.
Interview-ready guide
Interview answer
StringBuilder and StringBuffer both provide a mutable sequence for assembling text. StringBuffer synchronizes individual operations; StringBuilder does not. I normally use StringBuilder because builders are usually local to one thread.
If a mutable builder really is shared, StringBuffer can protect single method calls, but it does not make a sequence such as check-length-then-append atomic. I would either synchronize the complete operation or avoid sharing the builder and publish the completed String instead.
StringBuilder vs StringBuffer in Java
Direct Answer
StringBuilder and StringBuffer are mutable character-sequence builders with similar APIs. StringBuffer synchronizes its individual operations and can be shared for those operations; StringBuilder provides no synchronization and is the normal choice when the builder is confined to one thread. A multi-call check-then-act sequence is not atomic merely because it uses StringBuffer.
Code Example
public class BuilderDemo {
public static void main(String[] args) {
// StringBuilder — preferred for single-threaded code
StringBuilder sb = new StringBuilder();
sb.append("Java");
sb.append(" is");
sb.append(" fast");
System.out.println(sb.toString()); // Java is fast
// Chaining — append() returns 'this' so calls can be chained
String result = new StringBuilder()
.append("Hello")
.append(", ")
.append("World")
.append("!")
.toString();
System.out.println(result); // Hello, World!
// Other useful methods
StringBuilder s = new StringBuilder("Hello World");
s.insert(5, ","); // Hello, World
s.delete(0, 6); // World
s.reverse(); // dlroW
System.out.println(s); // dlroW
System.out.println(s.length()); // 5
// StringBuffer has Similar core API — just replace the class name
// StringBuffer sbf = new StringBuffer(); -- thread-safe but slower
}
}
Common Mistakes & Pitfalls
- Picking
StringBuffer"to be safe" — its per-call lock is pure waste in single-threaded code, which is almost all string building.StringBuilderis the correct default, not the risky one. - Believing
synchronizedmethods make multi-step logic thread-safe — each call is atomic, but a check-then-append spanning two calls (if (sb.length() == 0) sb.append(x)) still races across threads. Thread safety of one method is not thread safety of your sequence. - Sharing a local
StringBuilderacross threads when you don't need to — a builder created inside a method is already thread-confined; reaching forStringBufferthere adds lock overhead for nothing. - Assuming they have different APIs — they don't. Every method matches, so switching between them is just changing the class name; there is nothing new to learn.
Check your understanding
Answer these without looking back at the article.
- When should you use String vs StringBuilder in Java?
- How does StringBuilder manage its internal capacity?
- What is the difference between String, StringBuilder, and StringBuffer?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.