String vs StringBuffer vs StringBuilder
In this lesson: Difference between String, StringBuffer and StringBuilder — mutability, thread safety, speed — table and code. In Hindi & English.
Three ways to hold text
Java has three classes for text: String, StringBuilder and StringBuffer. They differ in whether they can change (mutability), how fast they are, and whether they are thread-safe. Choosing the right one matters when you build or edit a lot of text.
String — immutable
String s = "Hello"; s = s + " World"; // creates a NEW string each time
A String cannot be changed. Every + makes a brand-new String object. That is fine for a few joins, but slow if you do it many times in a loop.
StringBuilder — fast and mutable
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
sb.append("!");
System.out.println(sb.toString());
StringBuilder changes the same object in place with append() — no new objects each time. It is the fast, standard choice for building strings, especially inside loops.
StringBuffer — thread-safe
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb.toString());
StringBuffer works just like StringBuilder but its methods are synchronized, making it safe when several threads edit the same object. That safety makes it a little slower.
Why performance differs
// slow: new String every loop turn String s = ""; for (int i = 0; i < 1000; i++) s += i; // fast: one object, edited in place StringBuilder b = new StringBuilder(); for (int i = 0; i < 1000; i++) b.append(i);
| Step | i ki value | Condition | Kya hua | i ab |
|---|---|---|---|---|
| 1 | 0 | 0 < 1000 → true | loop body chalta hai | i → 1 |
| 2 | 1 | 1 < 1000 → true | loop body chalta hai | i → 2 |
| 3 | 2 | 2 < 1000 → true | loop body chalta hai | i → 3 |
| 4 | 3 | 3 < 1000 → true | loop body chalta hai | i → 4 |
| 5 | 4 | 4 < 1000 → true | loop body chalta hai | i → 5 |
| 6 | 5 | 5 < 1000 → true | loop body chalta hai | i → 6 |
| 7 | 6 | 6 < 1000 → true | loop body chalta hai | i → 7 |
| 8 | 7 | 7 < 1000 → true | loop body chalta hai | i → 8 |
| 9 | 8 | 8 < 1000 → true | loop body chalta hai | i → 9 |
| 10 | 9 | 9 < 1000 → true | loop body chalta hai | i → 10 |
| 11 | 10 | 10 < 1000 → true | loop body chalta hai | i → 11 |
| 12 | 11 | 11 < 1000 → true | loop body chalta hai | i → 12 |
| 13 | 12 | 12 < 1000 → true | loop body chalta hai | i → 13 |
| 14 | 13 | 13 < 1000 → true | loop body chalta hai | i → 14 |
| 15 | 14 | 14 < 1000 → true | loop body chalta hai | i → 15 |
| 16 | 15 | 15 < 1000 → true | loop body chalta hai | i → 16 |
| 17 | 16 | 16 < 1000 → true | loop body chalta hai | i → 17 |
| 18 | 17 | 17 < 1000 → true | loop body chalta hai | i → 18 |
| 19 | 18 | 18 < 1000 → true | loop body chalta hai | i → 19 |
| 20 | 19 | 19 < 1000 → true | loop body chalta hai | i → 20 |
| 21 | 20 | 20 < 1000 → true | loop body chalta hai | i → 21 |
| 22 | 21 | 21 < 1000 → true | loop body chalta hai | i → 22 |
| 23 | 22 | 22 < 1000 → true | loop body chalta hai | i → 23 |
| 24 | 23 | 23 < 1000 → true | loop body chalta hai | i → 24 |
| 25 | 24 | 24 < 1000 → true | loop body chalta hai | i → 25 |
| … loop isi tarah chalta rahता hai | ||||
The String loop creates a thousand throwaway objects; the StringBuilder loop reuses one. For heavy text-building, this difference is huge.
Comparison
| Point | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutable | No | Yes | Yes |
| Thread-safe | Yes (immutable) | No | Yes |
| Speed | Slow to build | Fastest | Slightly slower |
| Use for | Fixed text | Most building | Multi-thread building |
Common mistakes
- Using String
+=in a loop. Slow — use StringBuilder. - Using StringBuffer everywhere. Its thread safety is wasted in single-threaded code; StringBuilder is faster.
- Forgetting
toString(). Convert a builder back to a String withtoString()when needed. - Expecting String to change in place. It never does — it is immutable.
Summary
- String is immutable; every change makes a new object.
- StringBuilder is mutable and fast — the default for building text.
- StringBuffer is mutable and thread-safe but slightly slower.
- Use StringBuilder in loops instead of String
+=. - Pick StringBuffer only when multiple threads edit the same text.
Next, revise text basics in Java strings, or apply them in reverse a string.
Frequently Asked Questions
What is the difference between String, StringBuffer and StringBuilder?
Why is StringBuilder faster than String for concatenation?
+ creates a new object, which is slow in a loop. StringBuilder edits the same object in place with append(), avoiding all those extra objects.What does thread-safe mean for StringBuffer?
When should I use StringBuilder vs StringBuffer?
Should I use String or StringBuilder in a loop?
+= inside a loop creates many temporary objects and is slow; StringBuilder.append() is far more efficient.