StringBuffer and StringBuilder in Java
Introduction
StringBuilder and StringBuffer hold text that can be edited after creation. Both support operations such as appending a name or replacing a label. Their important difference is synchronization, not the text they can represent.
Why Do We Need StringBuffer and StringBuilder?
Remember that String is immutable.
For example:
String text = "Hello";
text = text + " Java";
text = text + " World";Each modification produces a new String object.
When many modifications are required, repeatedly creating new String objects can be inefficient.
StringBuilder and StringBuffer provide mutable character sequences.
StringBuilder
StringBuilder is a mutable sequence of characters.
Example
StringBuilder builder = new StringBuilder("Hello");
builder.append(" Java");
System.out.println(builder);Output
Hello JavaThe existing StringBuilder object is modified.
Modifying a StringBuilder
StringBuilder builder = new StringBuilder("Hello");
builder.append(" Java");
builder.insert(5, " World");
builder.replace(0, 5, "Hi");The object can be changed using its methods.
Common StringBuilder Methods
append()
Adds content to the end.
StringBuilder builder = new StringBuilder("Hello");
builder.append(" Java");
System.out.println(builder);Output
Hello Javainsert()
Inserts content at a specified position.
StringBuilder builder = new StringBuilder("Hello");
builder.insert(5, " Java");
System.out.println(builder);Output
Hello Javadelete()
Removes characters from a specified range.
StringBuilder builder = new StringBuilder("Hello Java");
builder.delete(5, 10);
System.out.println(builder);Output
Helloreplace()
Replaces characters in a specified range.
StringBuilder builder = new StringBuilder("Hello Java");
builder.replace(6, 10, "World");
System.out.println(builder);Output
Hello Worldreverse()
Reverses the sequence.
StringBuilder builder = new StringBuilder("Java");
builder.reverse();
System.out.println(builder);Output
avaJStringBuffer
StringBuffer also represents a mutable sequence of characters.
Example
StringBuffer buffer = new StringBuffer("Hello");
buffer.append(" Java");
System.out.println(buffer);Output
Hello JavaStringBuffer provides many of the same operations as StringBuilder.
StringBuilder vs StringBuffer
The main difference is synchronization.
StringBuffer provides synchronized operations for shared mutable text. A sequence of separate operations may still need one external lock when the whole sequence must be atomic.
StringBuilder is not synchronized.
Therefore, for ordinary single-threaded string manipulation, StringBuilder is generally preferred because it avoids the synchronization overhead associated with StringBuffer.
| Feature | StringBuilder | StringBuffer |
|---|---|---|
| Mutable | Yes | Yes |
| Synchronized | No | Yes |
| Thread-safe for its synchronized operations | No | Yes |
| Typical single-threaded use | Yes | Less common |
| Performance in ordinary use | Generally faster | Generally slower due to synchronization |
String vs StringBuilder
String text = "Hello";A String is immutable.
StringBuilder builder = new StringBuilder("Hello");A StringBuilder is mutable.
For repeated modifications:
StringBuilder builder = new StringBuilder();
builder.append("Java");
builder.append(" is");
builder.append(" powerful");This is often more appropriate than repeatedly concatenating strings.
Converting StringBuilder to String
A StringBuilder can be converted to a String using toString().
StringBuilder builder = new StringBuilder("Hello Java");
String text = builder.toString();
System.out.println(text);When Should You Use StringBuilder?
Use StringBuilder when:
- You need to modify text repeatedly.
- You are building a String inside a loop.
- You need append, insert, delete, or replace operations.
- The code does not require synchronized access to the same buffer.
Example
StringBuilder result = new StringBuilder();
for (int i = 1; i <= 5; i++) {
result.append(i);
}
System.out.println(result);Output
12345When Should You Use StringBuffer?
Use StringBuffer when you specifically need its synchronized behavior for shared mutable text in concurrent code. For example: StringBuffer buffer = new StringBuffer();
buffer.append("Java"); buffer.append(" Programming");
Key Takeaway
String is immutable, while StringBuilder and StringBuffer are mutable character sequences. StringBuilder is generally preferred for ordinary single-threaded string manipulation, while StringBuffer provides synchronized operations for situations where that behavior is required.
See you in next page....🙂