Lesson Completion
Back to course

StringBuffer vs StringBuilder: Thread-Safety Trade-off

Beginner
15 minutes4.8Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: StringBuffer is thread-safe (synchronized), StringBuilder is not. Both are mutable, same API—choose based on concurrency needs.

Like choosing between a shared Google Doc (StringBuffer—multiple editors safe) vs local notepad (StringBuilder—single user, faster)!


2. Conceptual Clarity

Quick Comparison

FeatureStringBuilderStringBuffer
Thread-safe❌ No✅ Yes
Performance✅ Faster⚠️ Slower
SynchronizedNoYes
IntroducedJava 5Java 1.0
Use caseSingle-threadedMulti-threaded
graph LR Decision{Multi-threaded?} Decision -- No --> SB["StringBuilder<br/>(faster)"] Decision -- Yes --> SBF["StringBuffer<br/>(thread-safe)"] style SB fill:#2E7D32 style SBF fill:#F57C00

3. Interactive & Applied Code

java
public class StringBufferVsBuilder { public static void main(String[] args) { // === SAME API === StringBuilder builder = new StringBuilder(); StringBuffer buffer = new StringBuffer(); builder.append("Hello").append(" World"); buffer.append("Hello").append(" World"); System.out.println(builder); // "Hello World" System.out.println(buffer); // "Hello World" // === SINGLE-THREADED: Use StringBuilder === StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.append(i); // ✅ Faster, no sync overhead } // === MULTI-THREADED: Use StringBuffer === StringBuffer sharedBuffer = new StringBuffer(); Thread t1 = new Thread(() -> { for (int i = 0; i < 100; i++) { sharedBuffer.append("A"); } }); Thread t2 = new Thread(() -> { for (int i = 0; i < 100; i++) { sharedBuffer.append("B"); } }); t1.start(); t2.start(); // StringBuffer ensures no data corruption // === PERFORMANCE COMPARISON === int iterations = 100000; // StringBuilder (faster) long start1 = System.nanoTime(); StringBuilder perf1 = new StringBuilder(); for (int i = 0; i < iterations; i++) { perf1.append("x"); } long time1 = System.nanoTime() - start1; // StringBuffer (synchronized overhead) long start2 = System.nanoTime(); StringBuffer perf2 = new StringBuffer(); for (int i = 0; i < iterations; i++) { perf2.append("x"); } long time2 = System.nanoTime() - start2; System.out.println("StringBuilder: " + time1 / 1_000_000 + "ms"); System.out.println("StringBuffer: " + time2 / 1_000_000 + "ms"); // StringBuilder is typically 10-20% faster } }

⚠️ Common Mistakes

Mistake #1: Using StringBuffer in single-threaded code

java
// ❌ Unnecessary synchronization overhead StringBuffer sb = new StringBuffer(); // ✅ Use StringBuilder when single-threaded StringBuilder sb = new StringBuilder();

Mistake #2: Using StringBuilder in multi-threaded code

java
// ❌ Race conditions possible StringBuilder shared = new StringBuilder(); // Multiple threads appending = data corruption // ✅ Use StringBuffer for thread-safety StringBuffer shared = new StringBuffer();

4. The "Interview Corner"

🏆 Q1: "When to use StringBuffer?" Answer: Only when multiple threads modify the same buffer. Otherwise, StringBuilder is always preferred.

🏆 Q2: "Why was StringBuilder added in Java 5?" Answer: Most string building is single-threaded. StringBuilder removes synchronization overhead for better performance in common cases.

🏆 Q3: "String vs StringBuilder vs StringBuffer?"

  • String: Immutable, thread-safe (shared safely)
  • StringBuilder: Mutable, fast, NOT thread-safe
  • StringBuffer: Mutable, slower, thread-safe

🎓 Key Takeaways

✅ StringBuilder: Default choice (faster)
✅ StringBuffer: Only for multi-threaded access
✅ Same API, same methods
✅ ~10-20% performance difference
✅ String is immutable, both are mutable

Topics Covered

Java FundamentalsStrings

Tags

#java#strings#string-manipulation#string-methods#beginner-friendly

Last Updated

2025-02-01