StringBuffer vs StringBuilder
StringBuffer and StringBuilder look identical: same methods, same results. They differ in one thing only: StringBuffer locks itself on every call so several threads can use it safely, and StringBuilder doesn't. That single difference decides which you should use, and the answer for almost all new code is StringBuilder.
One Difference
You can confirm it by asking Java whether the append methods are synchronized:
import java.lang.reflect.Modifier;
public class Locks {
public static void main(String[] args) throws Exception {
int buffer = StringBuffer.class.getMethod("append", String.class)
.getModifiers();
int builder = StringBuilder.class.getMethod("append", String.class)
.getModifiers();
System.out.println("StringBuffer.append synchronized: "
+ Modifier.isSynchronized(buffer));
System.out.println("StringBuilder.append synchronized: "
+ Modifier.isSynchronized(builder));
}
}StringBuffer.append synchronized: true
StringBuilder.append synchronized: falseThis uses reflection (covered later in the course) to read the method's modifiers. The point is simple: the lock is the only real difference.
Why StringBuilder Is Faster
Taking and releasing a lock costs a little time on every call, even when no other thread is around. In one timing run of 5 million single-character appends, StringBuffer took about 21 ms and StringBuilder about 14 ms. Modern JVMs remove much of the locking cost when they can prove only one thread uses the object, so the gap is usually small; but StringBuilder is never slower.
String, StringBuffer and StringBuilder
String | StringBuffer | StringBuilder | |
|---|---|---|---|
| Mutable? | No | Yes | Yes |
| Thread-safe? | Yes (immutable) | Yes (synchronized) | No |
| Speed for many changes | Slow | Fast | Fastest |
equals compares text? | Yes | No | No |
| Since | Java 1.0 | Java 1.0 | Java 5 |
| Use for | Values you store, compare and share | Text built by several threads together | Text built step by step, in one thread |
Common Mistakes
Picking StringBuffer by habit from old tutorials. Default to StringBuilder.
Sharing a StringBuilder field between threads. That's the one case it breaks; use a local builder per call, or StringBuffer.
Using either one when + on one line would do. Builders are for many steps.
Interview Corner
What is the difference between StringBuffer and StringBuilder?
Both are mutable sequences of characters with the same API. StringBuffer, from Java 1.0, synchronizes its methods, so it's thread-safe but slightly slower. StringBuilder, added in Java 5, isn't synchronized, so it's faster but must not be shared between threads without external locking. StringBuilder is the default choice for building strings in single-threaded code.
Compare String, StringBuffer and StringBuilder.
String is immutable and thread-safe by nature, best for values. StringBuffer is mutable and synchronized, for text modified by multiple threads. StringBuilder is mutable and unsynchronized, the fastest choice for building text in one thread. Only String overrides equals to compare content and uses the string pool.
Practice
- Easy: Replace every
StringBufferin the "StringBuffer in Java" examples withStringBuilderand check that the single-threaded output doesn't change. - Harder: Write a method that builds a comma-separated line from an
int[], using a localStringBuilder. Explain why it's thread-safe even if many threads call the method at once.
Key Takeaways
Was this tutorial helpful?