StringBuffer
StringBuffer is a mutable, thread-safe class in Java used to create and manipulate strings. It is designed for scenarios where string content changes frequently and multiple threads may access the same object. This topic is important for interviews, especially when comparing String, StringBuilder, and StringBuffer.
What Is StringBuffer?
- A mutable sequence of characters
- Part of java.lang package
- Thread-safe (synchronized methods)
- Slower than StringBuilder, faster than repeated String concatenation
StringBuffer sb = new StringBuffer("Java");
Why StringBuffer Exists
- String → Immutable (inefficient for frequent changes)
- StringBuilder → Mutable but not thread-safe
- StringBuffer → Mutable and thread-safe
Use case: Multi-threaded environments where string modification is required.
Key Characteristics of StringBuffer
- Mutable
- Synchronized (thread-safe)
- Allows modification without creating new objects
- Methods are synchronized → performance overhead
How StringBuffer Works (Conceptual)
StringBuffer sb = new StringBuffer("Java");
sb.append(" World");
- Same object is modified
- No new object created (unlike String)
Commonly Used Constructors
StringBuffer sb1 = new StringBuffer(); // default capacity 16
StringBuffer sb2 = new StringBuffer("Java"); // capacity = 16 + length
StringBuffer sb3 = new StringBuffer(50); // custom capacity
Capacity Concept (Important)
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // 16
When capacity is exceeded:
newCapacity = (oldCapacity * 2) + 2
Commonly Used StringBuffer Methods
append()
sb.append(" Java");
insert()
sb.insert(4, " Core");
replace()
sb.replace(0, 4, "Core");
delete()
sb.delete(0, 4);
reverse()
sb.reverse();
length() and capacity()
sb.length();
sb.capacity();
Example Program
StringBuffer sb = new StringBuffer("Java");
sb.append(" Programming");
sb.insert(4, " Core");
sb.replace(0, 4, "Core");
sb.reverse();
System.out.println(sb);
StringBuffer vs StringBuilder vs String
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread Safety | Yes | No | Yes |
| Performance | Slow (changes) | Fastest | Slower than SB |
| Synchronization | N/A | No | Yes |
| Use Case | Read-only | Single-thread | Multi-thread |
When to Use StringBuffer
- Multi-threaded applications
- Shared string modification
- Thread safety is more important than performance
When NOT to Use StringBuffer
- Single-threaded applications
- Performance-critical code
- Simple string concatenation
Common Beginner Mistakes
- Using StringBuffer unnecessarily
- Assuming it is faster than StringBuilder
- Ignoring synchronization overhead
- Confusing immutability with thread safety
Interview-Ready Answers
Short Answer
StringBuffer is a mutable and thread-safe class used to modify strings in multi-threaded environments.
Detailed Answer
In Java, StringBuffer allows modification of string content without creating new objects and ensures thread safety through synchronized methods. It is suitable for multi-threaded applications but has lower performance compared to StringBuilder.
Key Takeaway
Use StringBuffer only when thread safety is required. For most modern applications, prefer StringBuilder unless multiple threads modify the same string.