Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 38

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. StringBuilder = mutable and fast. StringBuffer = mutable and thread-safe.

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());
Hello World!

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());
Hello World

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);
Dry Run — dekhiye har step kaise chalta hai (i = 0 se shuru)
Stepi ki valueConditionKya huai ab
100 < 1000 → trueloop body chalta haii → 1
211 < 1000 → trueloop body chalta haii → 2
322 < 1000 → trueloop body chalta haii → 3
433 < 1000 → trueloop body chalta haii → 4
544 < 1000 → trueloop body chalta haii → 5
655 < 1000 → trueloop body chalta haii → 6
766 < 1000 → trueloop body chalta haii → 7
877 < 1000 → trueloop body chalta haii → 8
988 < 1000 → trueloop body chalta haii → 9
1099 < 1000 → trueloop body chalta haii → 10
111010 < 1000 → trueloop body chalta haii → 11
121111 < 1000 → trueloop body chalta haii → 12
131212 < 1000 → trueloop body chalta haii → 13
141313 < 1000 → trueloop body chalta haii → 14
151414 < 1000 → trueloop body chalta haii → 15
161515 < 1000 → trueloop body chalta haii → 16
171616 < 1000 → trueloop body chalta haii → 17
181717 < 1000 → trueloop body chalta haii → 18
191818 < 1000 → trueloop body chalta haii → 19
201919 < 1000 → trueloop body chalta haii → 20
212020 < 1000 → trueloop body chalta haii → 21
222121 < 1000 → trueloop body chalta haii → 22
232222 < 1000 → trueloop body chalta haii → 23
242323 < 1000 → trueloop body chalta haii → 24
252424 < 1000 → trueloop body chalta haii → 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

PointStringStringBuilderStringBuffer
MutableNoYesYes
Thread-safeYes (immutable)NoYes
SpeedSlow to buildFastestSlightly slower
Use forFixed textMost buildingMulti-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 with toString() 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?
String is immutable (cannot change). StringBuilder and StringBuffer are mutable (can change without new objects). StringBuilder is fast but not thread-safe; StringBuffer is thread-safe but slightly slower. Use StringBuilder for most text-building.
Why is StringBuilder faster than String for concatenation?
Because String is immutable, every + 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?
Thread-safe means multiple threads can use it at once without corrupting the data, because its methods are synchronized. This safety adds a small speed cost, which is why StringBuilder (not synchronized) is faster.
When should I use StringBuilder vs StringBuffer?
Use StringBuilder in normal single-threaded code (the common case) for speed. Use StringBuffer only when multiple threads will modify the same text object at the same time and you need thread safety.
Should I use String or StringBuilder in a loop?
Use StringBuilder in a loop. Building a String with += inside a loop creates many temporary objects and is slow; StringBuilder.append() is far more efficient.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicArrayList vs LinkedList Your progress: 38 / 61 (62%) · Lesson 39 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free