Bird
0
0

Why is StringBuilder preferred over StringBuffer in single-threaded applications?

hard📝 Conceptual Q10 of 15
Java - Strings and String Handling
Why is StringBuilder preferred over StringBuffer in single-threaded applications?
ABecause <code>StringBuilder</code> is faster due to lack of synchronization.
BBecause <code>StringBuilder</code> is synchronized and safer.
CBecause <code>StringBuffer</code> cannot be modified after creation.
DBecause <code>StringBuffer</code> does not support append.
Step-by-Step Solution
Solution:
  1. Step 1: Understand synchronization cost

    StringBuffer synchronizes methods, adding overhead.
  2. Step 2: Single-threaded use favors speed

    StringBuilder skips synchronization, making it faster in single-threaded contexts.
  3. Final Answer:

    Because StringBuilder is faster due to lack of synchronization. -> Option A
  4. Quick Check:

    Speed without sync = Because StringBuilder is faster due to lack of synchronization. [OK]
Quick Trick: No sync means faster in single-threaded apps [OK]
Common Mistakes:
  • Thinking StringBuilder is synchronized
  • Believing StringBuffer is immutable
  • Assuming StringBuffer lacks append

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes