StringBuilder in Java
Mutable builder: append, insert, reverse, deleteCharAt; why it beats + in loops.
report += line;. With 1,000 lines it's instant. With 100,000 lines, you have time to make coffee. Same code — why does it collapse?The hidden copying
Strings are immutable, so report += line can't append in place. It builds a new String containing all the old text plus the line. Line 50,000 copies 49,999 lines first. The total work grows quadratically — double the lines, four times the time.
A whiteboard instead of stone
StringBuilder is a mutable sequence of characters: one buffer that grows as you append. No copying of everything each time. When you're done, toString() gives you the final String.
var sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
sb.append(i).append(',');
}
String result = sb.toString(); // "0,1,2,"Chaining and editing
Most methods change the builder itself and return it (this), so calls can be chained. Besides append, you can insert(index, x), deleteCharAt(index) and reverse() — all on the same object.
var sb = new StringBuilder("hello");
sb.deleteCharAt(0); // "ello"
sb.insert(0, "J"); // "Jello"Order of operations
What does this print?
var sb = new StringBuilder("dog");
sb.insert(0, "hot ").append("s");
System.out.println(sb);hot doghot dogsdogs hotdogshot
Show the answer
hot dogs. insert(0, ...) puts "hot " at the front, then append adds "s" at the end — both on the same builder, which println prints via toString().
A sweet reversal
What does this print?
var sb = new StringBuilder("stressed");
sb.reverse();
System.out.println(sb);stresseddessertsdessert
Show the answer
desserts 🍰 — reverse() flips the builder in place. No need to assign the result: the builder itself changed.
equals doesn't compare text
StringBuilder does not override equals: it inherits Object's version, which compares references. Two builders holding "x" are not "equal". Compare their contents with compareTo (StringBuilder is Comparable since Java 11) or a.toString().equals(b.toString()).
var a = new StringBuilder("x");
var b = new StringBuilder("x");
a.equals(b); // false!
int c = a.compareTo(b); // 0In real projects
Building CSV exports, log lines, SQL or HTML in loops? Use a StringBuilder. For a single expression like a + ":" + b, don't bother — since Java 9 the compiler already optimizes that (JEP 280). The danger zone is += inside a loop.
Key takeaways
- StringBuilder methods modify the same object
- Most methods return this, so you can chain them
- Call toString() to get the final String
- StringBuilder does not override equals: compare with toString() or compareTo
new StringBuilder() starts with room for 16 characters. When it fills up, it grows to about double (old × 2 + 2), so even millions of appends stay cheap on average.
Practice questions
What does this print?
var sb = new StringBuilder("cat");
sb.append("s").insert(0, "my ");
System.out.println(sb);- cats
- my cats
- my cat
- catsmy
Check your answer
my cats. append adds "s" at the end, then insert(0, ...) puts "my " at index 0. Both modify the same builder, which println prints via toString.
What does this print?
var sb = new StringBuilder("hello");
sb.deleteCharAt(0).reverse();
System.out.println(sb);- olleh
- olle
- ello
- lleh
Check your answer
olle. deleteCharAt(0) removes 'h' giving "ello", and reverse turns it into "olle". The chained calls act on the same object.