All topics
library
beginner

StringBuilder vs StringBuffer

Know the difference, when to use each, and why StringBuilder replaced StringBuffer in modern code.

Both are mutable text buffers with the same API. The difference is one word.

StringBuilder = a private notebook (fast, only you use it). StringBuffer = a shared whiteboard with a lock (slower because you must wait for the key).

Key Concepts

1
    StringBuffer     every method is synchronized   JDK 1.0
    StringBuilder    no synchronization             Java 5
2
    StringBuilder sb = new StringBuilder();
    for (String w : words) sb.append(w).append(' ');
    String out = sb.toString();
3
Use StringBuilder. String building almost always happens inside one method, on a local variable no other thread can see, so the locking in StringBuffer protects nothing and costs maybe 15 to 30 per cent.
StringBuilderStringBuffer
4
StringBuffer exists for backward compatibility. Reach for it only if a buffer genuinely is shared between threads — and if it is, a lock around the whole operation is usually what you actually need, because append being atomic does not make a sequence of appends atomic.
StringBufferappend
5
Why either beats + in a loop.
    String s = "";
    for (int i = 0; i < n; i++) s += i;     // O(n^2)
+
6
Each += copies the whole string so far into a new object. The builder writes into one growing array instead.
+=
7
Pre-size it when you know roughly how much you need.
    new StringBuilder(1024);
8
The default capacity is 16 characters, and the array doubles when it fills, copying each time.
9
The compiler already optimises simple concatenation.
    String s = a + b + c;    // compiled into one efficient operation
10
Since Java 9 that goes through StringConcatFactory, which can beat a hand-written builder for a fixed number of pieces. So a + b + c is fine and clear; only the loop needs a builder.
StringConcatFactorya + b + c
11
The useful methods beyond append.
    insert(index, x)    delete(from, to)    deleteCharAt(i)
    replace(from, to, s)    reverse()    setLength(n)
append
12
What the interviewer is probing.1. "Which should you use, and why does the other exist?" Probing: whether you know the difference is synchronisation. Stalls: "StringBuilder, it is newer." Moves up: StringBuilder — StringBuffer synchronises every method, which protects nothing when the buffer is a local variable. It exists for backward compatibility.
13
2. "Does StringBuffer make your code thread-safe?" Probing: the limit of method-level synchronisation. Stalls: "Yes, that is the point." Moves up: only each call is atomic; a sequence of appends is not, so shared use still needs your own lock.
14
3. "Is a + b + c slower than a StringBuilder?" Probing: what the compiler already does. Stalls: "Yes, always use a builder." Moves up: no — the compiler optimises fixed concatenation, and since Java 9 through StringConcatFactory. Only a loop needs an explicit builder.
15
4. "Why pre-size a StringBuilder?" Probing: the growth cost. Stalls: "It does not matter." Moves up: the default capacity is 16 and the array doubles when it fills, copying each time; pre-sizing avoids repeated copies.