StringBuilder
in Java
Learn how to use StringBuilder for efficient string concatenation, understand mutable vs. immutable objects, and write faster Java code.
The Problem with String Concatenation
In Java, String objects are immutable: once created, they cannot be changed. Every time you concatenate strings with +, Java creates a brand-new String object in memory and copies the old data into it.
String result = ""; for (int i = 0; i < 1000; i++) { result = result + "x"; // creates a new String every iteration! }
Immutable vs. Mutable
How Memory Differs
What Is StringBuilder?
StringBuilder is a class in java.lang that represents a mutable sequence of characters. It provides methods to append, insert, delete, and reverse characters without creating new objects.
Core API Methods
| Method | Returns | Description |
|---|---|---|
| append(x) | StringBuilder | Adds x to the end |
| insert(i, x) | StringBuilder | Inserts x at index i |
| delete(s, e) | StringBuilder | Removes chars from s to e (exclusive) |
| reverse() | StringBuilder | Reverses the sequence |
| toString() | String | Converts to immutable String |
| length() | int | Current number of characters |
String vs. StringBuilder vs. StringBuffer
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread Safe? | Yes (immutable) | No | Yes (synchronized) |
| Performance | Slow (concat) | Fastest | Moderate |
| Use When | Few changes | Many changes, single thread | Many changes, multi-thread |
How to Apply StringBuilder
Example 1: Basic Usage
StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(" "); sb.append("World"); String result = sb.toString(); // result: "Hello World"
Example 2: Method Chaining
String csv = new StringBuilder() .append("Name") .append(",") .append("Age") .append(",") .append("Grade") .toString(); // csv: "Name,Age,Grade"
Example 3: Loop with StringBuilder
String[] words = {"Java", "is", "fast"}; StringBuilder sb = new StringBuilder(); for (int i = 0; i < words.length; i++) { sb.append(words[i]); if (i < words.length - 1) { sb.append(" "); } } String sentence = sb.toString(); // sentence: "Java is fast"
Performance at Scale
insert, delete, and reverse
StringBuilder sb = new StringBuilder("Hello World"); // Insert at index 5 sb.insert(5, ","); // "Hello, World" // Delete index 5 to 7 (removes ", ") sb.delete(5, 7); // "HelloWorld" // Reverse the entire string sb.reverse(); // "dlroWolleH"
Capacity vs. Length
Common Mistakes
Question 1 of 5
Why is StringBuilder more efficient than using the + operator for string concatenation inside a loop?
Question 2 of 5
What is the default initial capacity of a StringBuilder created with new StringBuilder()?
Question 3 of 5
What does the following code print?
StringBuilder sb = new StringBuilder("ABCDE"); sb.delete(1, 3); System.out.println(sb);
Question 4 of 5
Which method converts a StringBuilder into an immutable String?
Question 5 of 5
What is the output of the following code?
StringBuilder sb = new StringBuilder("Java"); sb.insert(4, "Script"); sb.reverse(); System.out.println(sb.length());