StringBuffer is a mutable sequence of characters available in the java.lang package. It allows text modification without creating a new StringBuffer object for each operation.
- Provides synchronized methods for thread-safe operations.
- Automatically expands its capacity when additional storage is required.
Example: Modifying a StringBuffer object.
public class Geeks{
public static void main(String[] args){
StringBuffer sb = new StringBuffer("Hello");
// Append text to the existing sequence
sb.append(" World");
System.out.println(sb);
}
}
Output
Hello World
Explanation: The append() method adds the specified text to the existing StringBuffer object. It modifies the same object instead of creating a new StringBuffer.
Hierarchy of StringBuffer
StringBuffer is a final class that extends AbstractStringBuilder and implements the Serializable, CharSequence, and Comparable<StringBuffer> interfaces. It inherits Appendable functionality through its superclass.

Constructors of StringBuffer Class
StringBuffer provides four constructors to create objects with different initial contents and capacities.
- StringBuffer(): Creates an empty buffer with an initial capacity of 16 characters.
- StringBuffer(int capacity): Creates an empty buffer with the specified initial capacity.
- StringBuffer(String str): Initializes the buffer with the given string and a capacity of str.length() + 16.
- StringBuffer(CharSequence seq): Initializes the buffer with the given character sequence and a capacity of seq.length() + 16.
Example: Creating StringBuffer objects using different constructors.
public class Geeks{
public static void main(String[] args){
StringBuffer sb1 = new StringBuffer();
System.out.println("Default capacity: " + sb1.capacity());
StringBuffer sb2 = new StringBuffer(50);
System.out.println("Custom capacity: " + sb2.capacity());
StringBuffer sb3 = new StringBuffer("Welcome");
System.out.println("String: " + sb3);
System.out.println("Capacity: " + sb3.capacity());
CharSequence cs = "Java";
StringBuffer sb4 = new StringBuffer(cs);
System.out.println("CharSequence: " + sb4);
}
}
Output
Default capacity: 16 Custom capacity: 50 String: Welcome Capacity: 23 CharSequence: Java
Explanation: The first two constructors create empty buffers with specified capacities, while the other constructors initialize buffers with existing character sequences and provide 16 additional characters of initial capacity.
Commonly Used StringBuffer Methods
StringBuffer provides methods to modify, access, search, and manage the capacity of a character sequence.
Example: Using StringBuffer methods.
public class Geeks{
public static void main(String[] args){
StringBuffer sb = new StringBuffer("Hello");
// Append text
sb.append(" World");
System.out.println("After append: " + sb);
// Insert text
sb.insert(5, ",");
System.out.println("After insert: " + sb);
// Replace characters
sb.replace(7, 12, "Java");
System.out.println("After replace: " + sb);
// Delete characters
sb.delete(5, 7);
System.out.println("After delete: " + sb);
// Reverse the sequence
sb.reverse();
System.out.println("After reverse: " + sb);
// Display length and capacity
System.out.println("Length: " + sb.length());
System.out.println("Capacity: " + sb.capacity());
}
}
Output
After append: Hello World After insert: Hello, World After replace: Hello, Java After delete: HelloJava After reverse: avaJolleH Length: 9 Capacity: 21
Explanation:
- append() adds text at the end of the sequence.
- insert() adds text at the specified position.
- replace() replaces characters within the specified range.
- delete() removes characters from the specified range.
- reverse() reverses the character sequence.
- length() returns the number of characters, while capacity() returns the allocated capacity.
Note: In methods such as insert(), replace(), and delete(), the start index is inclusive and the end index is exclusive.
Important Methods of StringBuffer
The following table lists commonly used methods of the StringBuffer class.
Method | Description |
|---|---|
| Appends the specified value to the end of the sequence. |
| Appends the specified Unicode code point. |
| Inserts the specified value at a given position. |
| Replaces characters within a specified range. |
| Removes characters within a specified range. |
| Removes the character at a specified index. |
| Reverses the character sequence. |
| Returns the number of characters in the sequence. |
| Returns the current allocated capacity. |
| Ensures that the buffer has at least the specified capacity. |
| Attempts to reduce the capacity to match the current length. |
| Returns the character at the specified index. |
| Replaces the character at the specified index. |
| Changes the length of the character sequence. |
| Copies characters into a specified character array. |
| Returns the index of the first occurrence of a substring. |
| Returns the index of the last occurrence of a substring. |
| Returns a new String containing the specified range. |
| Returns a CharSequence containing the specified range. |
| Converts the StringBuffer into a String. |
When to Use StringBuffer
- When multiple threads need to modify a shared character sequence.
- When frequent text modifications require synchronized operations.
- When the final size of a character sequence is not known in advance.
Individual synchronized operations are thread-safe, but compound operations may require additional synchronization. For single-threaded applications, StringBuilder is generally preferred because it avoids synchronization overhead.