StringBuilder class is used to create mutable (modifiable) string. It is same as StringBuffer class except that it is non-synchronized.

StringBuilder Constructors

  • StringBuilder ( ), creates an empty StringBuilder and reserves room for 16 characters.
  • StringBuilder ( int size ), create an empty string and takes an integer argument to set capacity of the buffer.
  • StringBuilder ( String str ), create a StringBuilder object and initialize it with string str.

Example

// Program to demonstrate difference between String, StringBuilder and StringBuffer 
class StingTypes { 

  // Concatenates to String 
  public static void concat1(String s1) { 
    s1 = s1 + "example"; 
  } 
  
  // Concatenates to StringBuilder 
  public static void concat2(StringBuilder s2) { 
    s2.append("example"); 
    
  } 
  
  // Concatenates to StringBuffer 
  public static void concat3(StringBuffer s3) { 
    s3.append("example"); 
  } 
  
  public static void main(String[] args) { 
  
    String s1 = "Hello"; 
    concat1(s1); 
    
    //s1 is not changed 
    System.out.println("String: " + s1); 
    
    StringBuilder s2 = new StringBuilder("Hello"); 
    concat2(s2); 
    
    //s2 is changed 
    System.out.println("StringBuilder: " + s2); 
    
    StringBuffer s3 = new StringBuffer("Hello"); 
    concat3(s3); 
    
    //s3 is changed 
    System.out.println("StringBuffer: " + s3); 
  } 
  
}

 

Exaplanation :-

  1. Concat1 : We pass a string and perform “s1 = s1 + ”example”. The string passed from main() is not changed, this is due to the fact that String is immutable. Altering the value of string creates another object and s1 in concat1() stores reference of new string. References s1 in main() and cocat1() refer to different strings. ‘s1’ in main after Concat1 still points to original string as referrnce are passed by value.
  2. Concat2 : We pass a string and perform “s2.append(“..”)”, which changes the actual value of the string (in main()) to “Helloexample”. This is due to the simple fact that StringBuilder is mutable and hence changes its value.
  3. Concat3 : StringBuffer is similar to StringBuilder except one difference that StringBuffer is thread safe, i.e., multiple threads can use it without any issue. The thread safety brings a penalty of performance.

Important functions :

  • StringBuilder append(String str)
    Appends the specified string to this character sequence.
  • char charAt(int index)
    Returns the char value in this sequence at the specified index.
  • StringBuilder delete(int start, int end)
    Removes the characters in a substring of this sequence.
  • StringBuilder deleteCharAt(int index)
    Removes the char at the specified position in this sequence.
  • int indexOf(String str)
    Returns the index within this string of the first occurrence of the specified substring.
  • StringBuilder insert(int offset, String str)
    Inserts the string into this character sequence.
  • int length()
    Returns the length (character count).
  • StringBuilder replace(int start, int end, String str)
    Replaces the characters in a substring of this sequence with characters in the specified String.
  • StringBuilder reverse()
    Causes this character sequence to be replaced by the reverse of the sequence.
  • void setCharAt(int index, char ch)
    The character at the specified index is set to ch.
  • String substring(int start, int end)
    Returns a new String that contains a subsequence of characters currently contained in this sequence.