Skip to main content
๐Ÿ“œ WAYPOINT LESSON

StringBuilder & String Building

โญ beginnerโณ 15 min read๐Ÿ“ Lesson 28 of 180

Why + in loops squares the cost, the builder pattern, join and formatted.

Because strings are immutable, building one piece at a time with + inside a loop creates and throws away an object every iteration:

String html = "";
for (String row : rows) {
    html = html + row + "\n";   // copies EVERYTHING so far, every pass
}

For a few iterations that is fine โ€” readability wins. For thousands, the copies square the cost. StringBuilder is the fix: a mutable buffer you append to, converting to a String once at the end:

StringBuilder html = new StringBuilder();
for (String row : rows) {
    html.append(row).append("\n");    // appends return the builder: chainable
}
String result = html.toString();      // one final conversion

StringBuilder also has insert, replace, delete, reverse โ€” the reverse() call is the palindrome check from Module 5 in one line.

The decision rule, worth internalizing now: + for a fixed, small number of pieces; StringBuilder inside loops. A modern compiler optimizes simple + chains into builder code anyway โ€” the loop case is the one it cannot rescue, because the builder must survive across iterations.

Two more string idioms you will use constantly:

String.join(", ", "a", "b", "c");          // "a, b, c" โ€” the inverse of split
"key=%s value=%d".formatted(key, value);   // template-style formatting

Next: practice โ€” text analysis on real strings.