StringBuffer in Java
Last Updated : 10 Jan 2026
Java StringBuffer class is thread-safe, i.e., multiple threads cannot access it simultaneously. So, it is safe and will result in an order.
Constructors of the StringBuffer Class
The StringBuffer class provides multiple constructors to create mutable string objects with different initial states and capacities.
1. StringBuffer()
This constructor creates an empty StringBuffer object with a default initial capacity of 16 characters.
Syntax:
It has the following syntax:
2. StringBuffer(String str)
This constructor creates a StringBuffer object initialized with the specified string.
Syntax:
It has the following syntax:
3. StringBuffer(int capacity)
This constructor creates an empty StringBuffer object with the specified initial capacity.
Syntax:
It has the following syntax:
Example
The following example demonstrates how to create StringBuffer objects using different constructors:
Output:
sb1 (default): "", Capacity: 16 sb2 (with string): "Hello", Capacity: 21 sb3 (with capacity 50): "", Capacity: 50
Explanation:
- sb1 is created using the default constructor with an initial capacity of 16.
- sb2 is created with the string "Hello", which adds 5 characters plus the default extra capacity, making a total of 21.
- sb3 is created with a specified capacity of 50, ready to hold up to 50 characters without resizing.
Methods of the StringBuffer class
The following tables shows the commonly used methods with their descriptions of the StringBuffer class:
| Modifier and Type | Method | Description |
|---|---|---|
| public synchronized StringBuffer | append(String s) | It is used to append the specified string to this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double), etc. |
| public synchronized StringBuffer | insert(int offset, String s) | It is used to insert the specified string into this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double), etc. |
| public synchronized StringBuffer | replace(int startIndex, int endIndex, String str) | It is used to replace the string from the specified startIndex and endIndex. |
| public synchronized StringBuffer | delete(int startIndex, int endIndex) | It is used to delete the string from the specified startIndex and endIndex. |
| public synchronized StringBuffer | reverse() | is used to reverse the string. |
| public int | capacity() | It is used to return the current capacity. |
| public void | ensureCapacity(int minimumCapacity) | It is used to ensure the capacity is at least equal to the given minimum. |
| public char | charAt(int index) | It is used to return the character at the specified position. |
| public int | length() | It is used to return the length of the string, i.e. total number of characters. |
| public String | substring(int beginIndex) | It is used to return the substring from the specified beginIndex. |
| public String | substring(int beginIndex, int endIndex) | It is used to return the substring from the specified beginIndex and endIndex. |
StringBuffer Class Methods Examples
Here are the StringBuffer class's methods along with appropriate examples.
1. StringBuffer Class append() Method
The append() method concatenates the given argument with this String. The following example demonstrates the use of append() method.
Output:
Hello Java
2. StringBuffer insert() Method
The insert() method inserts the given String into this string at the given position. The following example demonstrates the use of insert() method.
Output:
HJavaello
3. StringBuffer replace() Method
The replace() method replaces the given String from the specified beginIndex and endIndex. The following example demonstrates the use of replace() method.
Output:
HJavalo
4. StringBuffer delete() Method
The delete() method of the StringBuffer class deletes the String from the specified beginIndex to endIndex. The following example demonstrates the use of delete() method.
Output:
Hlo
5. StringBuffer reverse() Method
The reverse() method of the StringBuffer class reverses the current String. The following example demonstrates the use of reverse() method.
Output:
olleH
6. StringBuffer capacity() Method
The capacity() method of the StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of characters increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example, if your current capacity is 16, it will be (16*2)+2=34.
The following example demonstrates the use of capacity() method.
Output:
16 16 34
7. StringBuffer ensureCapacity() Method
The ensureCapacity() method of the StringBuffer class ensures that the given capacity is the minimum of the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example, if your current capacity is 16, it will be (16*2)+2=34. The ensureCapacity() method of the StringBuffer class guarantees that the specified capacity is at least the current capacity. If the specified capacity exceeds the current capacity, it increases the capacity to (oldcapacity* 2) + 2. For instance, if the current capacity is 16, it will be calculated as (16* 2) + 2 = 34.
The following example demonstrates the use of ensureCapacity() method.
Output:
16 16 34 34 70
Next TopicStringbuilder-in-java