Learn Java
import com.learnjava.datastructurescollections.StringMethodsBuilder;

String Methods & StringBuilder in Java

@Concept

Beyond the basics, String has dozens of useful methods — trim() removes leading and trailing whitespace, split(regex) breaks a string into an array of pieces, replace(old, new) swaps every occurrence of one substring for another — all returning a new String, since String itself never changes in place. That immutability becomes a real performance problem when building up text through many small concatenations in a loop, since each + creates an entirely new String object, discarding the previous one immediately.

StringBuilder solves that by being genuinely mutable — its append() method adds text to the same underlying buffer in place, with no new object created per call. That makes it the standard tool for assembling a string piece by piece before converting the final result back to a String with toString() only once, at the very end.

@HowItWorks
1

Common String methods

trim(), split(regex), replace(old, new), and similar all return a new String, since the original is never modified.

2

Why repeated concatenation is slow

Every + in a loop creates an entirely new String object and discards the previous one, which adds up across many iterations.

3

StringBuilder is mutable

Its internal buffer grows and changes in place, unlike String.

4

append()

Adds text, of nearly any type, to the end of the buffer directly, with no new object created per call.

5

toString() at the end

StringBuilder converts to a regular String only once, when the building process is actually finished.

@Example
public class BuilderDemo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 3; i++) {
sb.append("Item ").append(i).append(" ");
}
String result = sb.toString().trim();
System.out.println(result);
}
}
Output
Item 1 Item 2 Item 3
1Each loop iteration calls append three times on the same StringBuilder object, adding text directly to its buffer rather than creating new objects.
2After three iterations, the buffer holds "Item 1 Item 2 Item 3 " with one trailing space.
3toString() converts that buffer into a regular String exactly once, and trim() removes the trailing space from the result.
4The single println call prints that final, cleaned-up string as one line.
@Complexity
time StringBuilder.append: O(1) amortized per call; n concatenations: O(n) with StringBuilder versus O(n²) with repeated String +=
space O(n)

The quadratic cost of repeated String concatenation comes from re-copying all previously accumulated characters into a new String on every single +=. StringBuilder avoids that by growing its own buffer in place instead.

@UseCases
Cleaning up user input with trim() before storing or comparing it
Breaking a comma-separated line of text into its individual fields with split
Building a long string piece by piece inside a loop, efficiently, with StringBuilder
Choosing StringBuilder over repeated String concatenation in performance-sensitive code

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.