java.lang.StringBuffer class creates mutable object of sequence of characters .
StringBuffer represents growable and writable character sequence.StringBuffer may have characters and substring inserted in the middle or appended to the end.
Whenever a StringBuffer object is created then JRE(Java Runtime Environment ) creates a default array for it.
The default capacity of this array is 16.

  • StringBuffer class Constructor

    Object of StringBuffer class can be created by using following constructors.

    Constructor Description
    public StringBuffer() it creates the object with empty value.
    public StringBuffer(int capacity) it creates the object with specified capacity.
    public StringBuffer(String string) it creates the object with specified string value.
    Example)
    drag_handle
     //creating the object of StringBuffer class 
    
    class StringBufferTest {
    
        public static void main(String[] args) {
            StringBuffer br = new StringBuffer();
    
            StringBuffer br1 = new StringBuffer(20);
    
            StringBuffer br2 = new StringBuffer("I love java programming language");
    
            System.out.println("Capacity of br = " + br.capacity());
            System.out.println("Capacity of br1 = " + br1.capacity());
            System.out.println("Capacity of br2 = " + br2.capacity());
    
        }
    }
    
    Output:
    Capacity of br = 16
    Capacity of br1 = 20
    Capacity of br2 = 48
  • Methods of StringBuffer class

    There are many useful methods in StringBuffer class to perform the operation with strings.

    Method Description Syntax
    length() used to find the length of StringBuffer. public int length()
    capacity() used to obtain the allocated capacity of StringBuffer. public int capacity()
    append() used to append or concatenate given string or primitive value with string buffer object . public StringBuffer append(String str)
    replace() used to replace any part of StringBuffer object with given string. public StringBuffer replace(int start , int end-1 , String str)
    delete() used to delete a part of StringBuffer object according to given start and end. public StringBuffer delete(int start , int end-1)
    insert() used to insert any given string inside current StringBuffer object. public StringBuffer(int start , int end-1,String str)
    reverse() used to reverse any given StringBuffer object. public StringBuffer reverse()
    deleteCharAt() used to delete the character at specified index from current StringBuffer Object. public StringBuffer deleteCharAt(int charIndex)
    ensureCapacity() used to increase the capacity of StringBuffer object. public void ensureCapacity(int capacity)
    toString() used to convert StringBuffer object to String. public String toString()
    codePointAt() used to obtain the unicode of the character at given index. public int codePointAt(int index);
    • Note : All the above methods of the StringBuffer class are synchronized type ,that means these methods are thread safe.

    • In the next chapter ,we will see the example of these methods and how to use it in the program.

What next?

The next topic is StringBuffer Class Methods

Share this page