Difference between String, StringBuilder, and StringBuffer

๐Ÿ’ก Concept: String, StringBuilder, StringBuffer

These are classes used for string manipulation in Java, with differences in mutability, synchronization, and performance.

๐Ÿ“˜ Quick Intro

String is immutable, StringBuilder is mutable and unsynchronized, StringBuffer is mutable and synchronized.

๐Ÿง  Analogy

Think of String as a printed page (unchangeable), StringBuilder as a notepad (editable), and StringBuffer as a synchronized notepad (thread-safe).

๐Ÿ”ง Technical Explanation

  • String objects are immutable; changes create new objects.
  • StringBuilder allows fast, mutable string manipulation without synchronization.
  • StringBuffer is synchronized and thread-safe but slower.
  • Use StringBuilder in single-threaded, StringBuffer in multi-threaded contexts.
  • String is preferable for constants and fixed values.

๐ŸŽฏ Use Cases

  • โœ… Use String for fixed, immutable text.
  • โœ… Use StringBuilder for efficient string modification in single-threaded scenarios.
  • โœ… Use StringBuffer when thread safety is required during string changes.

๐Ÿ’ป Example: Using StringBuilder and StringBuffer


// String is immutable
String s = "Hello";
// StringBuilder is mutable
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
// StringBuffer is thread-safe
StringBuffer sbuf = new StringBuffer("Hello");
sbuf.append(" World");

โ“ Interview Q&A

Q1: Is String mutable?
A: No, it is immutable.

Q2: What is StringBuilder?
A: A mutable string class without synchronization.

Q3: How is StringBuffer different?
A: It is synchronized and thread-safe.

Q4: When to use StringBuilder?
A: In single-threaded scenarios for performance.

Q5: When to use StringBuffer?
A: When thread safety is needed.

Q6: Does StringBuilder have synchronization?
A: No.

Q7: Can String objects be changed?
A: No, new objects are created.

Q8: Which is faster, StringBuilder or StringBuffer?
A: StringBuilder.

Q9: What happens when you concatenate strings with +?
A: Internally uses StringBuilder.

Q10: Are StringBuffer methods synchronized?
A: Yes.

๐Ÿ“ MCQs

Q1. Is String mutable?

  • Yes
  • No
  • Sometimes
  • Depends

Q2. What is StringBuilder?

  • Immutable
  • Mutable without synchronization
  • Mutable with synchronization
  • Thread-safe

Q3. How is StringBuffer different?

  • Not synchronized
  • Synchronized and thread-safe
  • Immutable
  • Faster than StringBuilder

Q4. When to use StringBuilder?

  • Multi-threaded
  • Single-threaded scenarios
  • Never
  • Only with StringBuffer

Q5. When to use StringBuffer?

  • Single-threaded
  • When thread safety needed
  • Never
  • For constants

Q6. Does StringBuilder have synchronization?

  • Yes
  • No
  • Sometimes
  • Always

Q7. Can String objects be changed?

  • Yes
  • No
  • Sometimes
  • Depends

Q8. Which is faster, StringBuilder or StringBuffer?

  • StringBuilder
  • StringBuffer
  • String
  • StringBuffer and StringBuilder equal

Q9. What happens when concatenating with +?

  • Creates new String
  • Uses StringBuilder
  • Uses StringBuffer
  • Throws error

Q10. Are StringBuffer methods synchronized?

  • No
  • Yes
  • Sometimes
  • Never

๐Ÿ’ก Bonus Insight

Choosing the right string class impacts performance and thread safety in Java applications.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: