Nearby lessons
111 of 125Java - StringBuffer and StringBuilder
📌 What You Will Learn
- Why String objects are immutable
- String vs StringBuffer vs StringBuilder
- The most important methods of String
- == vs equals() — the classic interview question
- How to split, join and manipulate text
StringBuffer and StringBuilder is a core concept of the Java language. This lesson explains String vs StringBuffer vs StringBuilder, String vs StringBuffer — Question and Answer and StringBuffer — Constructors and Methods with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
String vs StringBuffer vs StringBuilder
If you need to change text many times, do not use String — every change creates a new object, which is slow. Use StringBuffer or StringBuilder instead:
| Point | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Immutable? | Yes | No | No |
| Thread safe? | Yes (immutable) | Yes (synchronized) | No |
| Speed | Slow when changed often | Medium | Fastest |
| Java version | JDK 1.0 | JDK 1.0 | Java 5 |
| When to use | Text that never changes | Multi-threaded changes | Single-threaded changes (most common) |
Trainer's Note: Trainer rule of thumb: use String for normal values, StringBuilder when you are building text in a loop (like a report), and StringBuffer only when multiple threads share the same buffer.
Example01
String vs StringBuffer — Question and Answer
| Point | String | StringBuffer |
|---|---|---|
| Mutability | Immutable (cannot change) | Mutable (can change) |
| Storage | String Constant Pool | Heap (for the buffer object) |
| Changing content | Creates a new object each time | Changes the same object |
| Performance with changes | Slow | Fast |
| Thread safe? | Yes (immutable) | Yes (synchronized) |
StringBuffer — Constructors and Methods
StringBuffer (and its sibling StringBuilder) has three constructors:
| Constructor | What it does |
|---|---|
| new StringBuffer() | Empty buffer with initial capacity 16 |
| new StringBuffer(int capacity) | Empty buffer with your own capacity |
| new StringBuffer(String str) | Buffer with the given text; new capacity = 16 + text length |
And the key methods (StringBuilder has the same methods):
| Method | What it does | Example |
|---|---|---|
| append(data) | Add text at the end | sb.append("Hi") |
| insert(index, str) | Insert text at a position | sb.insert(1, "-") |
| delete(start, end) | Remove a range of characters | sb.delete(1, 3) |
| reverse() | Reverse the whole content | sb.reverse() |
| replace(start, end, str) | Replace a range with text | sb.replace(1, 3, "X") |
| capacity() | Current capacity of the buffer | sb.capacity() |
| length() | Number of characters currently held | sb.length() |
| ensureCapacity(n) | Set a minimum capacity (min 16) | sb.ensureCapacity(100) |
Example03
📝 Key Takeaways
- String is a class, not a primitive; it is immutable.
- Immutability gives security, thread safety and pool caching.
- Use StringBuilder (fastest, not thread-safe) or StringBuffer (thread-safe) when text changes often.
- == compares references; .equals() compares content. Always use .equals() for Strings.
- Override toString() to print objects meaningfully.
- Text blocks (Java 15+) make multi-line strings clean.
🧠 Test Your Knowledge
2 QuestionsProgress: 0 / 2