In Java, the StringBuffer class is employed for crafting String objects that are mutable, meaning they can be modified. Unlike the String class, the StringBuffer class in Java allows for changes to be made to the object.
Note: 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
| Constructor | Description |
|---|---|
| StringBuffer() | It creates an empty String buffer with the initial capacity of 16. |
| StringBuffer(String str) | It creates a String buffer with the specified string.. |
| StringBuffer(int capacity) | It creates an empty String buffer with the specified capacity as length. |
Methods 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. |
What is a mutable String?
A mutable string is a string that can be altered or modified. The classes StringBuffer and StringBuilder are utilized for constructing mutable strings.
1) StringBuffer Class append Method
The append function combines the provided parameter with the current String.
Example
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
Output:
Hello Java
2) StringBuffer insert Method
The method insert adds the specified String into the current string at the specified position.
Example
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
Output:
HJavaello
3) StringBuffer replace Method
The replace method substitutes a specified string starting from a designated beginIndex up to an endIndex with a new string.
Example
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}
}
Output:
HJavalo
4) StringBuffer delete Method
The delete function in the StringBuffer class removes a portion of the String starting from the designated beginIndex up to the endIndex.
Example
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
Output:
5) StringBuffer reverse Method
The StringBuffer class provides a reverse method that is used to reverse the characters in the existing String object.
Example
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
}
}
Output:
6) StringBuffer capacity Method
The capacity function in the StringBuffer class provides the existing capacity of the buffer. Initially, the buffer has a default capacity of 16. When the number of characters surpasses the current capacity, the capacity is expanded by (oldcapacity2)+2. For instance, with a starting capacity of 16, the updated capacity will be (162)+2=34.
Example
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
Output:
16
16
34
7) StringBuffer ensureCapacity Method
The ensureCapacity function in the StringBuffer class ensures that the provided capacity is set as the minimum required capacity. In cases where the provided capacity is larger than the current capacity, the function increases the capacity by a factor of (oldcapacity2)+2. To illustrate, if the current capacity is 16, the new capacity would be (162)+2=34. This method in the StringBuffer class guarantees that the specified capacity is at minimum the same as the current capacity. If the specified capacity surpasses the current capacity, the function enlarges the capacity to (oldcapacity2)+2. For instance, if the current capacity is 16, the updated capacity would be (162)+2=34.
Example
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}
}
Output:
16
16
34
34
70