Quick revision
The short answer
String is immutable, while StringBuilder and StringBuffer are mutable character sequences with practically the same API. The difference between the two builders is that StringBuffer's methods are synchronized and StringBuilder's are not, so StringBuilder is faster and is the normal choice, because a builder is almost never shared between threads. We still use StringBuffer only where an older API requires it, such as the Matcher.appendReplacement overloads that predate the StringBuilder versions added in Java 9.
What to remember
In this answer, we saw that String is immutable while StringBuilder and StringBuffer are mutable sequences with the same API, built on the same internal class. StringBuffer synchronizes every method, which costs time and still does not make sequences of appends from several threads meaningful, so StringBuilder is the normal choice and a shared builder is usually a design problem. StringBuffer survives mainly in older APIs such as the pre-Java 9 Matcher.appendReplacement overloads and StringWriter.getBuffer().
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
String is immutable, while StringBuilder and StringBuffer are mutable character sequences with practically the same API. The difference between the two builders is that StringBuffer's methods are synchronized and StringBuilder's are not, so StringBuilder is faster and is the normal choice, because a builder is almost never shared between threads. We still use StringBuffer only where an older API requires it, such as the Matcher.appendReplacement overloads that predate the StringBuilder versions added in Java 9.
Why this question is asked
This is a basic question, but a good answer shows we understand immutability, the cost of synchronization and when thread safety is actually needed. It also shows practical judgement: many developers still pick StringBuffer because it sounds safer, and an interviewer wants to hear why that is usually the wrong instinct. In this answer, we will recap what each type is, see why modifying a String in a loop is costly, compare the two builders, and look at the few places StringBuffer still appears.
1A Quick Recap of the Three Types
A String never changes after it is created. Methods such as concat or replace return a new String and leave the original alone. That makes Strings safe to share and use as map keys, but it means building text piece by piece creates a new object at every step.
StringBuilder and StringBuffer are the mutable companions. Each holds an internal array with spare capacity, and append, insert, delete and reverse change that array in place. When we are done, toString() creates the final String. Both implement CharSequence and have the same methods, because both extend the same internal class, AbstractStringBuilder.
Notice that append returns the same builder, which is why calls can be chained.
2StringBuilder Versus StringBuffer
StringBuffer came first, in Java 1.0, when the designers made many core classes thread-safe by default. Every public method is synchronized, so each call acquires the object's monitor lock. StringBuilder arrived in Java 5 as the same class without the locks.
In single-threaded code, which is nearly all builder code, those locks protect nothing and still cost something. The JIT can sometimes remove a lock when it proves the object never escapes the method, but we should not rely on that, and since biased locking was disabled in JDK 15 and later removed, uncontended locking is a little more expensive than it used to be. So StringBuilder is the default.
| String | StringBuilder | StringBuffer | |
|---|---|---|---|
| Mutable | no | yes | yes |
| Thread-safe | yes, by immutability | no | yes, synchronized methods |
| Speed for building text | slow in loops | fastest | slower: a lock per call |
| Since | 1.0 | 5 | 1.0 |
| Use it for | values, keys, results | building text locally | legacy APIs only |
3Why Sharing a Builder Between Threads Is a Design Smell
The argument for StringBuffer is thread safety, but look at what that safety actually gives. Each method call is atomic, yet a sequence of calls is not. If two threads append to the same StringBuffer, each append is intact, but the order of the pieces is whatever the scheduler decides.
So StringBuffer stops the internal array from being corrupted, but it cannot make the result meaningful. That is why shared builders usually signal a design problem. The better design is for each thread to build its own text with a StringBuilder and combine the results at the end, or to send the pieces to something designed for concurrency, such as a queue or a logger.
4Where StringBuffer Still Appears
The main reason we still meet StringBuffer is older APIs that take or return it. Matcher.appendReplacement and appendTail took only a StringBuffer until Java 9 added StringBuilder overloads. StringWriter.getBuffer() returns a StringBuffer because StringWriter stores its text in one. Some older libraries have StringBuffer in their method signatures too. In those cases we use it because the API demands it, not by choice.
On current JDKs even this case rarely needs StringBuffer, and replaceAll with a function is often the simplest option.
5Follow-up questions
Is s1 + s2 + s3 in one expression slow?
No. A single concatenation expression is compiled efficiently, since JDK 9 through invokedynamic, which computes the final size and copies each piece once. The cost appears only when we concatenate repeatedly in a loop, because each iteration copies everything built so far. That is when a StringBuilder helps.
Should I pre-size a StringBuilder?
When we roughly know the final length, yes. The default capacity is 16 characters, and every time it runs out the builder allocates a bigger array, about twice the size, and copies. new StringBuilder(expectedLength) avoids those rounds for large output.
Does StringBuilder have equals and hashCode based on content?
No, it uses identity from Object. To compare content we call compareTo, which StringBuilder implements since Java 11, or convert with toString(). This is one reason builders make poor map keys.
6Conclusion
In this answer, we saw that String is immutable while StringBuilder and StringBuffer are mutable sequences with the same API, built on the same internal class. StringBuffer synchronizes every method, which costs time and still does not make sequences of appends from several threads meaningful, so StringBuilder is the normal choice and a shared builder is usually a design problem. StringBuffer survives mainly in older APIs such as the pre-Java 9 Matcher.appendReplacement overloads and StringWriter.getBuffer().
Open with
String is immutable: every operation that seems to change it returns a new String. StringBuilder and StringBuffer are the mutable versions. They keep an internal array with spare capacity and append, insert or delete in place, and toString gives the final String.
Then, if they want more
They have the same API because they share a base class. The difference is that every StringBuffer method is synchronized and StringBuilder's are not. In practice a builder is a local variable used by one thread, so the locks protect nothing and just cost time, which is why StringBuilder is the default.
Even when threads do share a StringBuffer, the safety is limited: each append is atomic, but the order of pieces from different threads is random, so the result is rarely useful. Sharing a builder usually means the design is off, and I would rather have each thread build its own string and combine them at the end.
I would still use StringBuffer only when an API requires it, for example older code calling Matcher.appendReplacement, which only had StringBuffer overloads before Java 9, or StringWriter.getBuffer. And for a single expression like a plus b plus c, I just use plus, since the compiler handles that efficiently; builders matter in loops.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 11, Java 15, Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare String, StringBuilder and StringBuffer. When would you still use StringBuffer?": https://scrivio-interview-library.netlify.app/java/strings/85f528fa18/