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!