StringBuffer

  1. If the content is going to change frequently then it is not recommended to use String because for every modification a new String object will be created.
  2. We can handle this type of requirement by using StringBuffer.If we are performing any modification.It will take place in the existing object only nstead of creating new object

Constructors of StringBuffer

Constructor Description
String Buffer sb=new String Buffer() Creates an empty String Buffer object with default initial Capacity 16.
If the String Buffer reaches it’s maximum capacity then a new String Buffer object will be created with
New Capacity=(current Capacity+1)*2;
String Buffer sb=new String Buffer() Creates an empty String Buffer object with specified initial Capacity.
StringBuffer sb=new StringBuffer(String s) Create an equivalent StringBuffer object for the given String object with capacity=s.length()+16.

Example of StringBuffer Constructor

public class Test{
public static void main(String args[])
StrinBuffer sb=new StringBuffer(“arch”);
System.out.println(sb.capacity(); 
}
}
/*output:
20
*/

Important methods of StringBuffer:

  1. int length()
    It returns the number of characters in the StringBuffer object.
  2. int capacity()
    It returns the capacity of StringBuffer object
  3. char charAt(int index)
    It returns the character at the given specified index
    Example:

    public class Test
    {
    public static void main(String[] args) 
    {
    StringBuffer sb=new StringBuffer("arch");
    System.out.println(sb.charAt(3));//h
    System.out.println(sb.charAt(10));//java.lang.StringIndexOutOfBoundsException
    }
    }
    
  4. void setCharAt(int index,char ch)
    for replacing the character located at specified index with the provide character
  5. StringBuffer append(x)
    x may be boolean,byte,int,long,float,double,char,character array,String or another StringBuffer.It will be added to the StringBuffer object.
    Example:

    public class Test
    {
    public static void main(String[] args) 
    {
    StringBuffer sb=new StringBuffer("arch");
    System.out.println(sb.append("technologies"));
    }
    }
    /*
    output:arch technologies
    */
    
  6. StringBuffer insert(int index,x)
    x may be boolean,byte,short,int,long,float,double,char,character array,String or StringBuffer.It will be inserted into the StringBuffer at the position represented by i
    Example:

    public class Test
    {
    public static void main(String[] args) 
    {
    StringBuffer sb=new StringBuffer("arch");
    System.out.println(sb.insert(4,"Technologies"));
    }
    }
    /*
    arch technologies
    */
    
  7. StringBuffer delete(int start,int end)
    This method deletes the character from start to (end-1) indexes.
    Example:

    public class Test
    {
    public static void main(String[] args) 
    {
    StringBuffer sb=new StringBuffer("arch");
    System.out.println(sb.delete(2,3));
    }
    }
    /*
    output:arh
    */
    
  8. StringBuffer deleteCharAt(int index)
  9. StringBuffer reverse()
    This reverse the character sequence in the StringBuffer.
  10. void setLength(int length)
    Example:

    public class Test {
    public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("archtechnologies");
    sb.setLength(5);
    System.out.println(sb);
    }
    }
    /*
    output:archt
    */
    
  11. int indexOf(String str)
  12. int lastIndexOf(String str)
  13. String substring(int i)
  14. String substring(int i,int j)
  15. StringBuffer replace(int I,int j,String str)
    This replace the characters from i to j-1 by the String str in the StringBuffer
  16. String toString()
    This converts the StringBuffer object into a String object this will enable to use String class methods on StringBuffer object after it’s conversion

Chaining of methods:
For most of the methods in String and StringBuffer the return types are same String and StringBuffer only,After applying any method on the result we are allowed to call another method which forms chaining of methods

  1. Example: sbm1().m2().m3().m4()….
    All these method calls will be executed from left to right.
  2. Example:
    public class Demo {
    
        public static void main(String args[]) {
    
            StringBuffer sb = new StringBuffer("test");
            sb.append("software").insert(2, "abcd").reverse().delete(3, 7);
            System.out.println(sb);
        }
    }
    /*
    output:
    erastsdcbaet
    */
    

Note:All methods which are available in StringBuffer are synchronized which effects performance of the system .To resolve this sun people introduced StringBuillder in 1.5 version.
What is the difference b/w String and StringBuffer classes?

String StringBuffer
String class objects are immutable and hence their contents cannot be modified. String Buffer class objects are mutable ,So they can be modified.
Moreover the methods that directly manipulate data of the object are not available in String class. Moreover the methods that directly manipulate data of the object are not available in StringBuffer class.

Note:

  1. along with String ,wrapperclasses like Character,Byte,Integer, Float,Double,Long ..are immutable.
  2. BigInteger,BigDecimal are also immutable.

Differences between StringBuffer and StringBuillder

StringBuffer StrindBuilder
All methods are synchronized String Builder object is not thread safe
performance is low performance is high
1.0 version 1.5 version

Note:-

  1. If the content is going to change frequently and thread safty is required then we should go for StringBuffer
  2. If the content is going to change frequently but thread safty is not required then we should go for StringBuilder
  3. If the content won’t change frequently then highly recommended to use String