Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Java · 04 Jul 2026 · Hindi + English

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

String is immutable. StringBuffer is mutable + thread-safe (synchronized, slower). StringBuilder is mutable + not thread-safe (fastest).

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());
dlroW olleH

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

PointStringStringBufferStringBuilder
MutabilityImmutableMutableMutable
Thread-safeYes (immutable)Yes (synchronized)No
SpeedSlow in loopsMediumFastest
SinceJava 1.0Java 1.0Java 5
Use whenFixed text, keys, configMulti-threaded string buildingEverything 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

String immutable है. StringBuffer mutable + thread-safe है (synchronized, धीमा). StringBuilder mutable + non thread-safe है (सबसे तेज़).

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());
dlroW olleH

दोनों के 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

PointStringStringBufferStringBuilder
MutabilityImmutableMutableMutable
Thread-safeहां (immutable)हां (synchronized)नहीं
SpeedLoops में धीमाMediumसबसे तेज़
कब सेJava 1.0Java 1.0Java 5
कब use करेंFixed text, keys, configMulti-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.