StringBuffer Class in Java

StringBuffer Class

  • String class is used to create a string of fixed length, but StringBuffer class creates strings of flexible length which can be modified.
  • It is a thread-safe, mutable sequence of characters.
  • A string buffer is like a string, but can be modified in the same memory location.

StringBuffer Class Constructors

The StringBuffer class has four constructors.
  • public StringBuffer()
  • public StringBuffer(int capacity)
  • public StringBuffer(String str)
  • public StringBuffer(CharSequence cs)
1. public StringBuffer () : It constructs the empty StringBuffer object with initial capacity of 16 characters.

For example:
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // 16

2. public StringBuffer(int capacity) : It creates a string buffer object with no character in it and is specified with the given initial capacity.

The initial capacity cannot be less than zero, otherwise it throws NegativeArraySizeException.

For example:
StringBuffer sb = new StringBuffer(5);
System.out.println(sb.capacity());  //5

3. public StringBuffer( String str) : It creates string buffer object with this String object data. The initial capacity of the string buffer is 16, plus the length of the string arguments.

The value of str cannot be null, otherwise it throws NullPointerExeption.

For example:
StringBuffer sb = new StringBuffer("Hello");
System.out.println(sb.capacity()); //21

4. public StringBuffer(CharSequence cs) : It creates the string buffer object that contains same character as the specified charSequence. The initial capacity of the string buffer is 16, plus the length of the CharSequence.

For example:
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer(sb1);
System.out.println(sb1.capacity());  //21
System.out.println(sb2.capacity());  //21
System.out.println(sb1 == sb2);  //false

StringBuffer Methods

List of the some important methods supported by StringBuffer.

MethodsDescription
public StringBuffer append(String s)Append the specified string with the given string.
public insert(int offset, String s)Insert the specified string with given string at the specified position.
public StringBuffer reverse()Returns the reverse of string.
public delete(int start, int end)Delete substring from specified start index to end index.
replace(int start, int end, String str)Replace the characters in a substring of current sequence with the characters in the specified new string.
int capacity()Returns the current capacity of the string.
void ensureCapacity(int minimumCapacity)Ensure the capacity at least equal to given minimum capacity.
int length()Returns the length of StringBuffer object. This method is used to count the characters in specified string.
String toString()Returns a string representing the data in this sequence.
String substring(int start)Returns a new string that contains the sub sequence of the characters currently contained in this string.

Example 1 : A program to illustrate the StringBuffer methods

public class StringBufferDemo
{
      public static void main(String args[])
      {
             // append
             StringBuffer sb1 = new StringBuffer("Welcome");
             StringBuffer sb2 = sb1.append(" Java");
             System.out.println("sb2 = "+sb2);
             System.out.println("Length of sb2 = "+sb2.length());
             //insert
             StringBuffer sb3 = new StringBuffer("10Aug1992");
             sb3.insert(2,"-");
             sb3.insert(6,"-");
             System.out.println("sb3 = "+sb3);
      }
}


Output:
sb2 = Welcome Java
Length of sb2 = 12
sb3 = 10-Aug-1992

Example 2 : A program to illustrate the StringBuffer methods

public class StringBufferDemo
{
      public static void main(String args[])
      {
             StringBuffer sb1 = new StringBuffer("Java Tutorials");
             StringBuffer sb2 = new StringBuffer("Java");
             StringBuffer sb3 = new StringBuffer("ABC");
             //delete
             sb1.delete(0, 5);
             System.out.println("After delete sb1= "+sb1);
             //reverse
             sb2.reverse();
             System.out.println("Reverse of sb2= "+sb2);
             //charAt()
             for (int i = 0; i <= sb3.length()-1; i++)
             {
                    System.out.println("Character at inedx "+i+" is: "+sb3.charAt(i));
             }
      }
}


Output:
After delete sb1 = Tutorials
Reverse of sb2 = avaJ
Character at inedx 0 is: A
Character at inedx 1 is: B
Character at inedx 2 is: C