«Back to Home

Core Java

Topics

StringBuffer Class In Java

StringBuffer Class
 
In Java, we know that the string is immutable. It cannot be changed but a new instance is created. Due to this, we use StringBuffer and StringBuilder class for mutable class.
 
StringBuffer class is used to create mutable string, which means we can modify the class through StringBuffer class in Java.
 
StringBuffer class is same as a String class, except it is mutable. It can be changed easily.
 
In other words, we can say that StringBuffer is a mutable sequence of characters. It is like a string but the contents of the StringBuffer can be modified after creation.
 
StringBuffer class has a capacity to hold the number of characters. It increases automatically when more contents are added to it.
 
In Java, StringBuffer class is thread-safe, which means that multiple threads cannot access it at the same time. Thus, it is safe and it will give the result in an order.
 
Three important constructors of StringBuffer class are,
 
StringBuffer()

This constructor creates a blank string buffer with the initial capacity of 16.
 
StringBuffer(String str)

This constructor creates a string buffer with the specified string.
 
StringBuffer(int capacity)

This constructor creates a blank string buffer with the specified capacity as length.
 
There are many important methods of StringBuffer class in Java, which are,
 
public synchronized StringBuffer append(String s)

This method is used to append the particular string with this string. The append() method is overloaded like append(int), append(float), append(double) etc.
 
public synchronized StringBuffer insert(int offset, String s)

This method is used to insert the particular string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, int), insert(int, float) etc.
 
public synchronized StringBuffer replace(int startIndex, int endIndex, String str)

This method is used to replace the string from particular startIndex and endIndex.
 
public synchronized StringBuffer delete(int startIndex, int endIndex)

This method is used to delete the string from the particular startIndex and endIndex.
 
public synchronized StringBuffer reverse()

This method is used to reverse the string in Java.
 
public int capacity()

This method is used to return the current capacity of the string.
 
public void ensureCapacity(int minimumCapacity)

This method is used to ensure the capacity i.e. at least equal to the given minimum.
 
public char charAt(int index)

This method is used to return the character at the particular position.
 
public int length()

This method is used to return the length of the string (means the total number of characters).
 
public String substring(int beginIndex)

This method is used to return the substring from the particular beginIndex.
 
public String substring(int beginIndex, int endIndex)

This method is used to return the substring from the particular beginIndex and endIndex.
 
Let's see an examples of methods of StringBuffer class.
 
StringBuffer append() method

The append() method is used to concatenate the particular argument with this string.
 
Let’s see an example, given below.
 
Code
  1. public class StringBufferMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer n = new StringBuffer("Peter ");  
  4.         n.append("William");  
  5.         System.out.println(n);  
  6.     }  
  7. }  
17

Output

18

In the example, mentioned above, the original string is changed and gives the output.
 
StringBuffer insert() method

The insert() method is used to insert the particular string with this string at the specified position.
 
Let’s see an example, given below.
 
Code
  1. public class StringBufferMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer n = new StringBuffer("Mia ");  
  4.         n.insert(1"Alexa");  
  5.         System.out.println(n);  
  6.     }  
  7. }  
19

Output

20
 
In the example, shown above, the original string is changed and gives the output.
 
StringBuffer replace() method

The replace() method is used to replace the particular string from the specified beginIndex and endIndex.
 
Let’s see an example, given below.
 
Code
  1. public class StringBufferMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer n = new StringBuffer("Lily ");  
  4.         StringBuffer n1 = new StringBuffer("Lily ");  
  5.         n.replace(12"Jaxon");  
  6.         n1.replace(14"Jaxon");  
  7.         System.out.println(n);  
  8.         System.out.println(n1);  
  9.     }  
  10. }  
21

Output

22

StringBuffer delete() method

The delete() method is used to delete the string from the particular beginIndex to endIndex.
 
Let’s see an example, given below.
 
Code
  1. public class StringBufferMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer n = new StringBuffer("Jackson");  
  4.         n.delete(15);  
  5.         System.out.println(n);  
  6.     }  
  7. }  
23

Output

24

StringBuffer reverse() method

The reverse() method is used to reverse the current string.
 
Let’s see an example, given below.
 
Code
  1. public class StringBufferMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer name = new StringBuffer("David");  
  4.         name.reverse();  
  5.         System.out.println(name);  
  6.     }  
  7. }  
25

Output

26

StringBuffer capacity() method

The capacity() method is used to return the current capacity of the buffer.
 
The default capacity of the buffer is 16 and If the number of character increases from its current capacity, it increases the capacity by (old capacity*2) +2.
 
Let’s see an example, given below.
 
Code
  1. public class StringBufferMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer n = new StringBuffer();  
  4.         System.out.println(n.capacity());  
  5.         n.append("William Shakespeare");  
  6.         System.out.println(n.capacity());  
  7.         n.append("William Shakespeare is a famous writer");  
  8.         System.out.println(n.capacity());  
  9.     }  
  10. }  
27

Output

28

StringBuffer ensureCapacity() method

The ensureCapacity() method is used to ensure that the given capacity is the minimum to the current capacity and if it is greater than the current capacity, it increases the capacity by (old capacity*2) +2.
 
Let’s see an example, given below.
 
Code
  1. public class StringBufferMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer c = new StringBuffer();  
  4.         System.out.println(c.capacity());  
  5.         c.append("William Shakespeare");  
  6.         System.out.println(c.capacity());  
  7.         c.append("William Shakespeare is a famous writer");  
  8.         System.out.println(c.capacity());  
  9.         c.ensureCapacity(60);  
  10.         System.out.println(c.capacity());  
  11.         c.ensureCapacity(150);  
  12.         System.out.println(c.capacity());  
  13.     }  
  14. }  
29

Output

30
 
Summary

Thus, we learnt that StringBuffer class is same as String class, except it is mutable. It can be changed easily and also learnt its important methods in Java.