String is immutable, StringBuilder is a mutable, unsynchronised builder for single-threaded code, and StringBuffer is a mutable builder whose methods are synchronised. Use String for fixed text, StringBuilder when you build text in a loop, and StringBuffer only for legacy code that shares one builder across threads.
The practical difference is performance. Every change to a String creates a new object, while the builders modify an internal character buffer in place.
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread safety | Safe (immutable) | Not thread-safe | Thread-safe (synchronised methods) |
| Speed for repeated edits | Slow (new object each time) | Fastest | Slower than StringBuilder |
| Since | Java 1.0 | Java 5 | Java 1.0 |
| Part of string pool | Yes (literals) | No | No |
| Use when | Text does not change | Building text in one thread | Legacy shared builder |
Once created, a String never changes. Methods such as concat, replace and toUpperCase return a new object. Immutability gives you:
hashCode, which makes strings fast hash map keys.Building a long text with += in a loop creates a new string and copies all characters on every iteration, which is O(n squared) overall.
StringBuilder keeps a resizable internal array and appends in amortised constant time per character.
Useful methods are append, insert, delete, deleteCharAt, replace, reverse, setLength and toString. If you know the approximate size, pass it to the constructor, as in new StringBuilder(1024), to avoid reallocations.
StringBuffer has the same API, but each method is synchronised. This protects single calls, but not sequences of calls, so compound operations still need external locking. Because of the locking overhead and because builders are rarely shared across threads, StringBuffer is seldom needed in new code. If you need to assemble text from several threads, collect results per thread and join them afterwards, or use a thread-safe structure such as a ConcurrentLinkedQueue of parts.
A single expression such as "a" + b + "c" is compiled efficiently. Since Java 9, the compiler uses invokedynamic string concatenation instead of generating StringBuilder code, so simple concatenation is fine. The problem is only concatenation inside loops, where each iteration creates a new intermediate string.
Other good options are String.join(",", list), Collectors.joining(","), String.format, text blocks (Java 15 and later) for multi-line literals, and String.formatted for templates.
String.StringBuilder.StringBuffer only fixes single-method races.String.join or Collectors.joining.setLength(0) between runs.The capacity of a builder starts at 16 characters and grows as needed, roughly doubling. Setting it up front is a cheap optimisation when the output size is predictable. Measure with a benchmark tool such as JMH before tuning further, because modern JIT compilers remove much of the cost of small builders.
For security, thread safety, caching of the hash code and string pool reuse. The cost is extra objects when you modify text a lot.
Rarely. It exists mainly for backward compatibility. Prefer StringBuilder, and handle concurrency at a higher level.
No. A single expression is optimised by the compiler. It is slow only when repeated in a loop.
Use equals, or equalsIgnoreCase, not ==, which compares references.
String is immutable and safe to share, StringBuilder is the fast choice for building text in one thread, and StringBuffer adds synchronisation at a speed cost and is mostly legacy. Avoid += in loops, and choose StringBuilder or String.join instead.