StringBuilder Class in java

StringBuilder class is same as StringBuffer class except for important difference that is StringBuilder class is not synchronized ,which means this class is not thread safe class. It is available since JDK 1.5.


StringBuilder Constructors

Constructor Description
public StringBuilder() creates an empty StringBuilder and reserves room for 16 characters.
public StringBuilder(int length) create an empty string and takes an integer argument to set capacity of the buffer.
StringBuilder ( String str ) creates a string Builder with the specified string.

Methods of StringBuilder class

Method Description
public StringBuilder append(String s) 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) 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) is used to replace the string from specified startIndex and endIndex-1.
public StringBuilder delete(int startIndex, int endIndex) is used to delete the string from specified startIndex and endIndex.
public StringBuilder reverse() is used to reverse the string.
public int capacity() is used to return the current capacity.
public void ensureCapacity(int minimumCapacity) is used to ensure the capacity at least equal to the given minimum.
public char charAt(int index) is used to return the character at the specified position.
public String substring(int beginIndex) is used to return the substring from the specified beginIndex.
public int length() is used to return the length of the string i.e. total number of characters.

Difference between StringBuffer and StringBuilder

StringBuffer Class StringBuilder Class
StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously. StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously.
StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.

Performance Test of StringBuffer and StringBuilder

public class ConcatTest {
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		StringBuffer sb = new StringBuffer("only");
		for (int i = 0; i < 10000; i++) {
			sb.append("javatech");
		}
		System.out.println("Time taken by StringBuffer: "
				+ (System.currentTimeMillis() - startTime) + "ms");
		startTime = System.currentTimeMillis();
		StringBuilder sb2 = new StringBuilder("only");
		for (int i = 0; i < 10000; i++) {
			sb2.append("javatech");
		}
		System.out.println("Time taken by StringBuilder: "
				+ (System.currentTimeMillis() - startTime) + "ms");
	}
}
Time taken by StringBuffer: 16ms
Time taken by StringBuilder: 0ms

Difference between String and StringBuffer

String Class StringBuffer Class
String class is immutable. StringBuffer class is mutable.
String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. StringBuffer class doesn't override the equals() method of Object class.
String is slow and consumes more memory when you concat too many strings because every time it creates new instance. StringBuffer is fast and consumes less memory when you cancat strings.

  Important Point:

Performance Test of String and StringBuffer


class ConcatTest{  
    public static String concatWithString()    {  
        String t = "only";  
        for (int i=0; i<10000; i++){  
            t = t + "javatech";  
        }  
        return t;  
    }  
    public static String concatWithStringBuffer(){  
        StringBuffer sb = new StringBuffer("only");  
        for (int i=0; i<10000; i++){  
            sb.append("javatech");  
        }  
        return sb.toString();  
    }  
    public static void main(String[] args){  
        long startTime = System.currentTimeMillis();  
        concatWithString();  
        System.out.println("Time taken by Concating with String: "+(System.currentTimeMillis()-startTime)+"ms");  
        startTime = System.currentTimeMillis();  
        concatWithStringBuffer();  
        System.out.println("Time taken by Concating with  StringBuffer: "+(System.currentTimeMillis()-startTime)+"ms");  
    }  
} 
Time taken by Concating with String: 1798ms
Time taken by Concating with  StringBuffer: 3ms