Difference Between String And StringBuffer in JAVA With Example

 Difference between String Class and String buffer class

  • String class is Immutable whereas String Buffer class is Mutable.
  • String class consumes more memory whenever we append too many strings, whereas String buffer consumes very less memory.
  • String class performance is low as compared to string Buffer class when we concatenate too many strings, as proven in the following program of performance testing.
  • Contents of String object can be compared by equals() method, as it overrides this method, whereas String buffer class do not override equals() method.

Performance Test of String and StringBuffer

Let us do a performance testing of String class and String Buffer Class and see what the result is. We have taken objects of both String class and String Buffer Class, than we have appended String value= “Android” to both for same time period, and checked time taken.

public class PerformanceTesting{
public static String concatinateString(){
String string = "Abhi";
for (int i=0; i<10000; i++){
string = string + "Android";
}
return string;
}

public static String concatinateStringBuffer(){
StringBuffer stringbuffer = new StringBuffer("Abhi");
for (int i=0; i<10000; i++){
stringbuffer.append("Android");
}
return stringbuffer.toString();
}
 
public static void main(String[] args){
long startTime = System.currentTimeMillis();
concatinateString();

System.out.println("Time taken for Concatination with String: "+(System.currentTimeMillis()-startTime)+"ms");

startTime = System.currentTimeMillis();

concatinateStringBuffer();

System.out.println("Time taken for Concatination with  StringBuffer: "+(System.currentTimeMillis()-startTime)+"ms");
}
}

Output:

Time taken for Concatination with String: 465ms

Time taken for Concatination with  StringBuffer: 1ms 

From output as shown above it is clear that String class takes more time than String Buffer class.


String and StringBuffer HashCode Test

Let us do Hash Code testing of String class and String Buffer Class and see what the result is. We have taken objects of both String class and String Buffer Class, than we have appended String value= “Android” to both objects. As shown in the following program.

public class HashCodeTesting{

public static void main(String args[]){
System.out.println("Hashcode testing of String:");
String string="Abhi";
System.out.println(string.hashCode());
string=string+"Android";
System.out.println(string.hashCode());

System.out.println("Hashcode testing of StringBuffer:");
StringBuffer stringbuffer=new StringBuffer("Abhi");
System.out.println(stringbuffer.hashCode());
stringbuffer.append("Android");
System.out.println(stringbuffer.hashCode());
}
}

Output:

Hashcode testing of String:

2033922

1496841997

Hashcode testing of StringBuffer:

31168322

31168322

From output it is clear that hashcode of String after appending is changed, whereas hashcode of String Buffer remains same after appending another string.

One thought on “Difference Between String And StringBuffer in JAVA With Example”