String vs StringBuffer vs StringBuilder in Java
String is immutable; StringBuffer is mutable and thread-safe; StringBuilder is mutable and fastest. Comparison table, code proof and when to use each.
The one-line answer
Why String is the problem in loops
String s = "Java";
s = s + " Rocks"; // does NOT modify "Java"
// creates a brand NEW String object!
Every concatenation on a String creates a new object because String is immutable. In a loop of 10,000 iterations, that is 10,000 discarded objects — slow and garbage-heavy.
// BAD: creates thousands of temporary Strings
String report = "";
for (int i = 1; i <= 10000; i++) {
report = report + i + ",";
}
// GOOD: one mutable object, modified in place
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10000; i++) {
sb.append(i).append(",");
}
String report2 = sb.toString();
StringBuffer vs StringBuilder
StringBuffer buf = new StringBuffer("Hello"); // thread-safe
StringBuilder bld = new StringBuilder("Hello"); // fast
buf.append(" World"); // same methods on both:
bld.append(" World"); // append, insert, delete, reverse, replace
System.out.println(bld.reverse());
Both have identical methods. The only difference: every StringBuffer method is synchronized (safe when multiple threads modify it, but each call pays a locking cost). StringBuilder (added in Java 5) drops the locks — same API, much faster in single-threaded code, which is 95% of real usage.
Comparison table
| Point | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread-safe | Yes (immutable) | Yes (synchronized) | No |
| Speed | Slow in loops | Medium | Fastest |
| Since | Java 1.0 | Java 1.0 | Java 5 |
| Use when | Fixed text, keys, config | Multi-threaded string building | Everything else (default choice) |
Interview bonus: why is String immutable?
- String pool: literals are shared between variables — sharing is only safe if nobody can modify them.
- Security: file paths, URLs, class names are Strings — immutability prevents malicious modification after checks.
- HashMap keys: a key's hashcode must never change — immutability guarantees it, and lets String cache its hashcode.
One-line answer
Loops में String problem क्यों है
String s = "Java";
s = s + " Rocks"; // "Java" modify NAHI hota
// बिल्कुल NAYA String object बनता है!
String पर हर concatenation नया object बनाता है क्योंकि String immutable है. 10,000 iterations के loop में 10,000 फेंके गए objects — धीमा और garbage-heavy.
// BAD: हज़ारों temporary Strings बनते हैं
String report = "";
for (int i = 1; i <= 10000; i++) {
report = report + i + ",";
}
// GOOD: एक mutable object, in place modify
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10000; i++) {
sb.append(i).append(",");
}
String report2 = sb.toString();
StringBuffer vs StringBuilder
StringBuffer buf = new StringBuffer("Hello"); // thread-safe
StringBuilder bld = new StringBuilder("Hello"); // तेज़
buf.append(" World"); // दोनों पर same methods:
bld.append(" World"); // append, insert, delete, reverse, replace
System.out.println(bld.reverse());
दोनों के methods identical हैं. फर्क सिर्फ इतना: StringBuffer का हर method synchronized है (multiple threads के लिए safe, लेकिन हर call पर locking cost). StringBuilder (Java 5 में आया) locks हटा देता है — same API, single-threaded code में बहुत तेज़, जो 95% real usage है.
Comparison table
| Point | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread-safe | हां (immutable) | हां (synchronized) | नहीं |
| Speed | Loops में धीमा | Medium | सबसे तेज़ |
| कब से | Java 1.0 | Java 1.0 | Java 5 |
| कब use करें | Fixed text, keys, config | Multi-threaded string building | बाकी सब (default choice) |
Interview bonus: String immutable क्यों है?
- String pool: literals variables के बीच share होते हैं — sharing तभी safe है जब कोई modify न कर सके.
- Security: file paths, URLs, class names Strings हैं — immutability checks के बाद malicious modification रोकती है.
- HashMap keys: key का hashcode कभी नहीं बदलना चाहिए — immutability इसकी guarantee देती है, और String को hashcode cache करने देती है.
Frequently Asked Questions
What is the difference between String, StringBuffer and StringBuilder?
String is immutable; StringBuffer is mutable and thread-safe with synchronized methods; StringBuilder is mutable, not thread-safe, and the fastest of the three.
Which should I use in a loop: String or StringBuilder?
Always StringBuilder. String concatenation in a loop creates a new object every iteration, while StringBuilder modifies one object in place.
When is StringBuffer actually needed?
Only when multiple threads modify the same string builder object simultaneously; in single-threaded code StringBuilder is always the better choice.