StringBuilder
StringBuilder is a mutable class in Java used to create and modify strings efficiently. It is designed for high-performance string manipulation in single-threaded scenarios and is one of the most commonly used alternatives to String. This is a very common interview topic, especially in comparison with String and StringBuffer.
What Is StringBuilder?
- A mutable sequence of characters
- Part of the java.lang package
- Not thread-safe (no synchronization)
- Faster than String and StringBuffer
StringBuilder sb = new StringBuilder("Java");
Why StringBuilder Exists
- String → Immutable (creates new objects on modification)
- StringBuffer → Thread-safe but slower
- StringBuilder → Fast and mutable (no synchronization overhead)
Best use case: Single-threaded applications with frequent string modifications.
Key Characteristics of StringBuilder
- Mutable (content can change)
- Not synchronized (not thread-safe)
- High performance
- Same API as StringBuffer
How StringBuilder Works (Conceptual)
StringBuilder sb = new StringBuilder("Java");
sb.append(" World");
• Same object is modified
• No new object created
• Memory-efficient for repeated changes
Constructors of StringBuilder
StringBuilder sb1 = new StringBuilder(); // capacity = 16
StringBuilder sb2 = new StringBuilder("Java"); // 16 + length
StringBuilder sb3 = new StringBuilder(50); // custom capacity
Capacity Concept (Important for Interviews)
StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity()); // 16
Capacity Growth Formula
newCapacity = (oldCapacity * 2) + 2
Commonly Used StringBuilder Methods
append()
sb.append(" Java");
sb.append(8);
sb.append(true);
insert()
sb.insert(4, " Core");
replace()
sb.replace(0, 4, "Core");
delete() / deleteCharAt()
sb.delete(0, 4);
sb.deleteCharAt(2);
reverse()
sb.reverse();
length() and capacity()
sb.length();
sb.capacity();
Example Program
StringBuilder sb = new StringBuilder("Java");
sb.append(" Programming");
sb.insert(4, " Core");
sb.replace(0, 4, "Core");
sb.delete(0, 5);
System.out.println(sb);
String Concatenation vs StringBuilder (Performance)
Using String (Inefficient)
String s = "";
for (int i = 0; i < 1000; i++) {
s = s + i;
}
• Creates many temporary objects
• Poor performance
Using StringBuilder (Efficient)
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
sb.append(i);
}
• Single object
• Much faster
StringBuilder vs StringBuffer vs String
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread Safety | Yes | No | Yes |
| Synchronization | N/A | No | Yes |
| Performance | Slow | Fastest | Slower |
| Use Case | Read-only | Single-thread | Multi-thread |
When to Use StringBuilder
- Frequent string modifications
- Single-threaded applications
- Performance-critical code
- Loops and string concatenation
When NOT to Use StringBuilder
- Multi-threaded shared access
- When immutability is required
- When thread safety is mandatory
Common Beginner Mistakes
- Using StringBuilder in multi-threaded code
- Assuming it is thread-safe
- Forgetting capacity planning
- Using String concatenation in loops
Interview-Ready Answers
Short Answer
StringBuilder is a mutable, non-thread-safe class used for fast string manipulation.
Detailed Answer
In Java, StringBuilder allows modification of string content without creating new objects. It is not synchronized, making it faster than StringBuffer, and is ideal for single-threaded applications where performance is critical.
Key Takeaway
Use StringBuilder for fast, mutable string operations in single-threaded scenarios. Avoid it when thread safety or immutability is required.