In Java, java.lang.StringBuilder (added in Java 1.5) is a final class that stores a mutable sequence of characters. Because a String is immutable, every edit on a String creates a brand-new object. A StringBuilder lets you append(), insert(), delete(), replace(), and reverse() characters right inside one resizable buffer.
It lives in java.lang, so no import is needed, and it implements Serializable, CharSequence, and (since Java 11) Comparable<StringBuilder>. When you finish building text, call toString() to get an immutable String. Browse every guide on the StringBuilder Methods index.
Why loops prefer StringBuilder.
Writing s += item inside a loop can copy the entire string on every iteration. Calling sb.append(item) inside a loop writes into one growing buffer, and mutating methods return this so calls like sb.append("a").append(1) chain cleanly on one line.
Syntax
Constructors and Basic Syntax
Create a builder empty, with a starting capacity, or from existing text:
Java
StringBuilder a = new StringBuilder(); // empty, initial capacity 16
StringBuilder b = new StringBuilder(128); // empty, initial capacity 128
StringBuilder c = new StringBuilder("Hello"); // text "Hello", capacity 5 + 16 = 21
c.append(", Java").insert(5, "!").reverse(); // chaining works (returns this)
String text = c.toString(); // convert to immutable String
Reference
StringBuilder vs StringBuffer vs String
Choose the class that matches whether the text changes and whether multiple threads share it:
Fixed text, constants, map keys, and final return values
StringBuilder
Yes
No
Default choice for building or editing text in loops or one thread
StringBuffer
Yes
Yes (synchronized)
Legacy or shared multi-thread buffer; slower in single-threaded code due to locking
Concept
Capacity vs Length
length() counts how many UTF-16 char units are currently in the builder. capacity() reports how many slots its internal array holds before it must allocate a bigger array. An empty new StringBuilder() starts with capacity 16, and new StringBuilder(str) starts with str.length() + 16. When text outgrows capacity, the buffer grows automatically (typically to 2 * oldCapacity + 2 in OpenJDK, though the exact formula is an implementation detail). Use ensureCapacity() before huge appends or trimToSize() afterward to manage memory.
Reference
Common Pitfalls
Mistake
What goes wrong
What to do instead
sb1.equals(sb2)
Checks object identity (==), not text content
Compare sb1.toString().equals(sb2.toString()) (or compareTo == 0 in Java 11+)
Forgetting toString()
Passing StringBuilder where a String parameter is required fails to compile
Call sb.toString() when done building
Leaving a trailing comma
Looping with sb.append(x).append(",") leaves an extra comma at the end
Trim with sb.setLength(sb.length() - 1) or deleteCharAt() when sb.length() > 0
java.lang.StringBuilder is a final, mutable sequence of characters introduced in Java 1.5. Unlike String, you can append, insert, delete, replace, and reverse characters inside the same object without creating a new String on every edit.
Strings are immutable, so repeating result += piece inside a loop can copy all accumulated characters into a brand-new String each step. Appending to a StringBuilder writes into one resizable char buffer and avoids that repeated copying.
String is immutable. StringBuilder and StringBuffer are both mutable with the same methods, except StringBuffer methods are synchronized for thread safety and therefore slightly slower in single-threaded code.
StringBuilder inherits Object.equals, which checks whether two references point to the exact same object. Compare content with sb1.toString().equals(sb2.toString()) (or sb1.compareTo(sb2) == 0 on Java 11+).
length() is how many char units are currently stored in the builder. capacity() is how many char units its internal array can hold before allocating a larger buffer.