StringBuilder Class in Java

Last Updated : 7 Aug 2026

In Java, StringBuilder class belongs to java.lang package. It is used to create mutable (modifiable) String. It is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.

We should use StringBuffer class only when thread safety is compulsory; otherwise, prefer StringBuilder for improved performance.

Hierarchy of StringBuilder Class

StringBuilder Class in Java

StringBuilder Class Constructors

ConstructorDescription
StringBuilder()It creates an empty String Builder with the initial capacity of 16.
StringBuilder(String str)It creates a String Builder with the specified string.
StringBuilder(int length)It creates an empty StringBuilder with the specified capacity as length.

StringBuilder Class Methods

MethodDescription
public StringBuilder append(String s)It is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.
public StringBuilder insert(int offset, String s)It is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.
public StringBuilder replace(int startIndex, int endIndex, String str)It is used to replace the string from specified startIndex and endIndex.
public StringBuilder delete(int startIndex, int endIndex)It is used to delete the string from specified startIndex and endIndex.
public StringBuilder reverse()It is used to reverse the string.
public int capacity()It is used to return the current capacity.
public void ensureCapacity(int minimumCapacity)It is used to ensure the capacity at least equal to the given minimum.
public char charAt(int index)It is used to return the character at the specified position.
public int length()It is used to return the length of the string i.e. total number of characters.
public String substring(int beginIndex)It is used to return the substring from the specified beginIndex.
public String substring(int beginIndex, int endIndex)It is used to return the substring from the specified beginIndex and endIndex.

Java StringBuilder Examples

Let's see the examples of different methods of StringBuilder class.

1) Example: StringBuilder.append() Method

The append() method concatenates the given argument with this String.

Java

public class Main {  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello ");  
sb.append("Java");//now original string is changed  
System.out.println(sb);//prints Hello Java  
}  
}  
Compile and Run

Output:

Hello Java

2) StringBuilder.insert() Method

The insert() method inserts the given string with this string at the given position.

Java

public class Main {  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello ");  
sb.insert(1,"Java");//now original string is changed  
System.out.println(sb);//prints HJavaello  
}  
}  
Compile and Run

Output:

HJavaello

3) StringBuilder.replace() Method

The replace() method replaces the given string from the specified beginIndex and endIndex.

Java

public class Main {  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello");  
sb.replace(1,3,"Java");  
System.out.println(sb);//prints HJavalo  
}  
}  
Compile and Run

Output:

HJavalo

4) StringBuilder.delete() Method

The delete() method deletes the string from the specified beginIndex to endIndex.

Java

public class Main {  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello");  
sb.delete(1,3);  
System.out.println(sb);//prints Hlo  
}  
}  
Compile and Run

Output:

Hlo

5) StringBuilder.reverse() Method

The reverse() method reverses the current string.

Java

public class Main {  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello");  
sb.reverse();  
System.out.println(sb);//prints olleH  
}  
}  
Compile and Run

Output:

olleH

6) StringBuilder.capacity() Method

The capacity() method returns the current capacity of the Builder. The default capacity of the Builder is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2) + 2. For example, if your current capacity is 16, it will be (16*2)+2=34.

Java

public class Main {    
public static void main(String args[]){    
StringBuilder sb=new StringBuilder();    
System.out.println(sb.capacity());//default 16    
sb.append("Hello");    
System.out.println(sb.capacity());//now 16    
sb.append("Java is my favourite language");    
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2    
}    
}    
Compile and Run

Output:

16
16
34

7) StringBuilder.ensureCapacity() Method

The ensureCapacity() method ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example, if your current capacity is 16, it will be (16*2)+2=34.

Java

public class Main {    
public static void main(String args[]){    
StringBuilder sb=new StringBuilder();    
System.out.println(sb.capacity());//default 16    
sb.append("Hello");    
System.out.println(sb.capacity());//now 16    
sb.append("Java is my favourite language");    
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2    
sb.ensureCapacity(10);//now no change    
System.out.println(sb.capacity());//now 34    
sb.ensureCapacity(50);//now (34*2)+2    
System.out.println(sb.capacity());//now 70    
}    
}    
Compile and Run

Output:

16
16
34
34
70