Java में String vs StringBuffer vs StringBuilder
String immutable है; StringBuffer mutable और thread-safe; StringBuilder mutable और सबसे तेज़. Comparison table, code proof और कब कौन-सा use करें.
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
String, StringBuffer और StringBuilder में क्या अंतर है?
String immutable है; StringBuffer mutable और synchronized methods के साथ thread-safe है; StringBuilder mutable, non thread-safe और तीनों में सबसे तेज़ है.
Loop में क्या use करें: String या StringBuilder?
हमेशा StringBuilder. Loop में String concatenation हर iteration में नया object बनाता है, जबकि StringBuilder एक ही object को in place modify करता है.
StringBuffer असल में कब चाहिए?
सिर्फ तब जब multiple threads एक ही string builder object को simultaneously modify करें; single-threaded code में हमेशा StringBuilder बेहतर है.