String StringBuilder StringBuffer difference

String Vs StringBuilder Vs StringBuffer:

String: 
String is immutable, means instance of an object can not be modified without creating the new object.
String object will be created once it does not exist in the string pool.

[java]
String a ="javadomain";
String b = "javadomain"; // object already present in the string pool, so it will make a reference instead of creating the new object.
System.out.println("String a hashCode : "+a.hashCode());
System.out.println("String b hashCode : "+b.hashCode());
System.out.println("a==b : "+(a==b));
System.out.println("a.equals(b) : "+(a.equals(b)));
[/java]

Output:
String a hashCode : -229515930
String b hashCode : -229515930
a==b : true
a.equals(b) : true

If we are creating the object explicitly then it wll react like below,

[java]
String a =new String("javadomain");
String b = new String("javadomain");
System.out.println("String a hashCode : "+a.hashCode());
System.out.println("String b hashCode : "+b.hashCode());
System.out.println("a==b : "+(a==b));
System.out.println("a.equals(b) : "+(a.equals(b)));
[/java]

Output:
String a hashCode : -229515930
String b hashCode : -229515930
a==b : false
a.equals(b) : true

String, StringBuffer and StringBuilder is explained in the below program,

[java]public class StringStringBuilderStringBuffer {

public static void main(String[] args) {
String stringVar = "hello";
stringVar.concat(" world"); //String is immutable so must be assigned to new object, since it wont allow us to change the existing object reference without creating new object
System.out.println("String variable : "+stringVar);
System.out.println("String Variable after concat : "+stringVar);

StringBuilder stringBuilderVar = new StringBuilder("hello");
stringBuilderVar.append(" world"); //StringBuilder is mutable, so can be modified without creating new object.
System.out.println("StringBuilder Variable : "+stringBuilderVar);
System.out.println("StringBuilder Variable after append : "+stringBuilderVar);

StringBuffer stringBufferVar = new StringBuffer("hello");
stringBufferVar.append(" world"); //StringBuffer is also mutable, so can be modified without creating the new object.
System.out.println("StringBuffer Variable : "+stringBufferVar);
System.out.println("StringBuffer Variable after append : "+stringBufferVar);

}

}[/java]

Output:

String variable : hello
String Variable after concat : hello
StringBuilder Variable : hello world
StringBuilder Variable after append : hello world
StringBuffer Variable : hello world
StringBuffer Variable after append : hello world

StringBuilder Vs StringBuffer :

StringBuffer: 

  • StringBuffer is mutable, means instance of an object can be modified without creating new object.
  • StringBuffer is synchronized.
  • StringBuffer is thread safe.
  • StringBuffer is slow than StringBuilder.

StringBuilder:

  • StringBuilder is also mutable, means in StringBuilder also instance of an object can be modified without creating new object.
  • StringBuilder is not synchronized
  • StringBuilder is not thread safe.
  • StringBuilder is faster than StringBuffer, since it is not synchronized and not thread safe.

StringBuilder is faster than StringBuffer, you can understand this term by the below program,

[java]public class StringBufferVsStringBuilder{

public static void main(String[] args){
long startTimeStringBuffer = 0;
long startTimeStringBuilder = 0;
long endTimeStringBuffer = 0;
long endTimeStringBuilder = 0;
int value = 93483493;
int stringBuff = 0;
int stringBuil = 0;
{
StringBuffer sBuffer = new StringBuffer();
startTimeStringBuffer = System.currentTimeMillis();
while(stringBuff< value){
sBuffer.append("");
stringBuff++;
}
endTimeStringBuffer = System.currentTimeMillis();
System.out.println("Total Time taken by StringBuffer is :"+(endTimeStringBuffer-startTimeStringBuffer));
}
{
StringBuilder sBuilder = new StringBuilder();
startTimeStringBuilder = System.currentTimeMillis();
while(stringBuil< value){
sBuilder.append("");
stringBuil++;
}
endTimeStringBuilder = System.currentTimeMillis();
System.out.println("Total Time taken by StringBuilder is :"+ (endTimeStringBuilder-startTimeStringBuilder));
}

}
}[/java]

Output:

Total Time taken by StringBuffer is :2300
Total Time taken by StringBuilder is :1051

Leave a Reply