StringBuffer and StringBuilder are classes in Java used to create and modify mutable strings. Unlike the String class, their content can be changed without creating a new object. They are mainly used when frequent string modifications such as append, insert, or delete operations are required.
- StringBuffer is thread-safe and synchronized, whereas StringBuilder is not synchronized and not thread-safe.
- StringBuffer is slower in performance, whereas StringBuilder is faster and more efficient for single-threaded applications.
StringBuffer Class
StringBuffer is a mutable sequence of characters that is thread-safe. It is designed for multi-threaded environments where multiple threads may modify the same string object.
- StringBuffer is thread-safe because its methods are synchronized.
- Used when multiple threads access the same object.
- Safer for concurrent operations.
Syntax
StringBuffer sb = new StringBuffer("Hello");
class Test {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = sb1;
sb1.append(" World");
if (sb1 == sb2)
System.out.println("Same");
else
System.out.println("Not Same");
}
}
Output
Same
Explanation:
- sb1 and sb2 refer to the same StringBuffer object
- The append() method modifies the existing object
- No new object is created during modification
- Both references still point to the same memory location
StringBuilder
StringBuilder is a mutable sequence of characters similar to StringBuffer , but it is not thread-safe. It is optimized for single-threaded environments where performance is critical.
- Suitable for single-threaded applications.
- Does not use synchronized methods.
- Faster execution and better performance.
Syntax
StringBuilder sb = new StringBuilder("Hello");
class Test {
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = sb1;
sb1.append(" World");
if (sb1 == sb2)
System.out.println("Same");
else
System.out.println("Not Same");
}
}
Output
Same
Explanation:
- sb1 and sb2 reference the same object
- append() updates the existing StringBuilder instance
- No new object is created after modification
- Faster execution due to lack of synchronization
StringBuilder vs StringBuffer
Below is the key differences table of StringBuffer and StringBuilder.
| Feature | StringBuffer | StringBuilder |
|---|---|---|
| Introduced In | Java 1.0 | Java 1.5 |
| Thread Safety | Thread-safe | Not thread-safe |
| Performance | Slower due to synchronization | Faster because no synchronization |
| Synchronization | Methods are synchronized | Methods are not synchronized |
| Memory Efficiency | Slightly less efficient | More memory efficient |
| Execution Speed | Lower execution speed | Higher execution speed |
| Suitable For | Multi-threaded environments | Single-threaded environments |
| Data Safety | Provides data consistency between threads | No protection for concurrent access |
| Usage | Used when security and synchronization are required | Used when high performance is required |
| Preferred In | Banking, server-side applications | Competitive coding, desktop applications |
| Method Behavior | Safe for shared objects | Best for local objects |
| Modification Operations | Supports mutable string operations | Supports mutable string operations |
| Object Creation | Changes existing object without creating new one | Changes existing object without creating new one |
| Efficiency in Loops | Less efficient in loops | More efficient in loops |
| Common Methods | append(), insert(), delete(), reverse() | append(), insert(), delete(), reverse() |