Java StringBuilder
A String in Java never changes after you create it. Every change builds a brand-new String and leaves the old one behind. StringBuilder solves this problem. It holds text that you can edit in place, which saves time and memory.
A Notebook and a Whiteboard
A String works like a printed notebook page. A mistake means you print a fresh page. A StringBuilder works like a whiteboard. You erase a word, write a new one, and keep using the same board.
Diagram: String and StringBuilder
String s = "Hi";
s = s + " there";
s = s + "!";
Memory: "Hi" "Hi there" "Hi there!" (3 objects)
StringBuilder sb = new StringBuilder("Hi");
sb.append(" there");
sb.append("!");
Memory: [ Hi there! ] (1 object)
Creating a StringBuilder
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder("Java");
StringBuilder c = new StringBuilder(100); // starting capacity
The capacity tells Java how much room to reserve. The builder grows by itself when the text gets longer.
Important Methods
append
Adds text to the end.
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
sb.append(2026);
System.out.println(sb); // Hello World2026
insert
Adds text at a chosen position.
StringBuilder sb = new StringBuilder("Hello World");
sb.insert(5, ",");
System.out.println(sb); // Hello, World
replace and delete
Replace swaps a range of characters. Delete removes a range. Both methods take a start index and an end index. The end index stays outside the range.
StringBuilder sb = new StringBuilder("I like tea");
sb.replace(7, 10, "milk");
System.out.println(sb); // I like milk
sb.delete(0, 2);
System.out.println(sb); // like milk
reverse
Flips the whole text.
StringBuilder sb = new StringBuilder("stressed");
System.out.println(sb.reverse()); // desserts
Other Useful Methods
| Method | Job |
|---|---|
| length() | Returns the number of characters |
| charAt(i) | Returns the character at index i |
| setCharAt(i, ch) | Changes one character |
| deleteCharAt(i) | Removes one character |
| toString() | Converts the builder to a String |
Speed Test in a Loop
Joining text inside a loop shows the biggest difference. A String creates thousands of temporary objects. A StringBuilder reuses one object.
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++) {
sb.append(i).append(" ");
}
System.out.println(sb.toString().trim()); // 1 2 3 4 5
Chaining works because each append call returns the same builder.
StringBuilder and StringBuffer
StringBuffer offers the same methods as StringBuilder. StringBuffer protects its data when many threads use it together, and this protection makes it slower. Choose StringBuilder for single-thread programs, which covers most beginner work.
When to Use Each Type
| Situation | Best Choice |
|---|---|
| Text never changes | String |
| Many edits or loops | StringBuilder |
| Many threads edit the same text | StringBuffer |
Common Mistakes
- Comparing two builders with equals, which checks addresses and not text
- Printing a builder when you need a String and forgetting toString
- Using an end index that falls outside the text
Real Example: Building a Report Line
A billing program creates one text line for each item. The program joins the item name, the quantity, and the price. A StringBuilder collects all parts and returns one clean line.
StringBuilder line = new StringBuilder();
line.append("Pen").append(" x ").append(3).append(" = ").append(45.0);
System.out.println(line); // Pen x 3 = 45.0
The same approach builds long messages, file contents, and web page text. Each added piece costs almost nothing.
Capacity and Growth
A builder starts with room for 16 characters when you give no size. Java doubles the space and adds a little when the text outgrows the room. Setting a good starting capacity avoids repeated growth in large jobs.
Quick Summary
- StringBuilder edits text in place
- append, insert, replace, delete, and reverse handle most jobs
- Loops with many joins run faster with StringBuilder
