Difference between String and StringBuffer

There are many differences between String and StringBuffer.

No. String StringBuffer
1 String class is immutable. StringBuffer class is mutable.
2 String is slow and drinks more memory when you concat too numerous strings because every time it creates new instance. StringBuffer is fast and consumes less memory when you cancat strings.

Performance Test of String and StringBuffer

public class ConcatTest{
    public static String concatWithString()    {
        String t = "Java";
        for (int i=0; i<10000; i++){
            t = t + Portal";
        }
        return t;
    }
    public static String concatWithStringBuffer(){
        StringBuffer sb = new StringBuffer("Java");
        for (int i=0; i<10000; i++){
            sb.append("Portal");
        }
        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");
    }
}