Difference between String and StringBuffer or String vs StringBuffer is a very commonly asked java interview question.
Both String and StringBuffer classes are used to store and manage sequence of characters but still there are some differences between them that I have explained in this tutorial.
Difference between String and StringBuffer in Java
Mutability
String class is immutable. It means once an object is assigned a value it can’t be changed.
Example:
String str = "I love"; //first object is created here str = str + "Java"; //second object is created
In above case total two objects are created. First object is created when we assign “I love” to str. When we concatenated value of str with “Java”, second object is created.
StringBuffer is mutable. It means we can change the value assigned to an object.
Example:
StringBuffer strBuffer = new StringBuffer("I love"); //an object is created here strBuffer = strBuffer.append("Java"); //existing object's value changed
In above case only one object is created.
HashCode Test
Let’s make one program to prove that String is immutable and StringBuffer is mutable.
package com; public class HashCodeTest { public static void main(String...s){ String str = "I Love"; StringBuffer strBuffer = new StringBuffer("I Love"); System.out.println("String"); System.out.println("Before Append: " + str.hashCode()); str = str + "Java"; System.out.println("After Append: " + str.hashCode()); System.out.println("\nStringBuffer"); System.out.println("Before Append: " + strBuffer.hashCode()); strBuffer = strBuffer.append("Java"); System.out.println("After Append: " + strBuffer.hashCode()); } }
Output
String
Before Append: 2121855241
After Append: -952474933
StringBuffer
Before Append: 1704856573
After Append: 1704856573
In above example see that in case of String the hashcode of object before and after append is different. This shows String class object is immutable. While in case of StringBuffer the hashcode of object before and after append is same. This shows StrinBuffer class object is mutable.
Performance
String performance is low and consumes more memory than StringBuffer. In below example I am appending “Java” 1000 times to String and StrinBuffer object value. You can see the time taken to append in String is more than StringBuffer.
Performance Test
package com; public class PerformanceTest { public static void main(String...s){ String str = "I Love"; StringBuffer strBuffer = new StringBuffer("I Love"); long time; int i; time = System.currentTimeMillis(); for(i = 0; i < 1000; ++i){ str = str + "Java"; } System.out.println("Time taken for concatenation in String = " + (System.currentTimeMillis() - time) + "ms"); time = System.currentTimeMillis(); for(i = 0; i < 1000; ++i){ strBuffer = strBuffer.append("Java"); } System.out.println("Time taken for concatenation in StringBuffer = " + (System.currentTimeMillis() - time) + "ms"); } }
Output
Time taken for concatenation in String = 16ms
Time taken for concatenation in StringBuffer = 0ms
Comment below if you found anything incorrect or missing in above tutorial for String vs StringBuffer in Java.