Difference Between String and StringBuffer

The preceding article is about the basic difference between String & StringBuffer.
 

Differences between String and StringBuffer

 
 
 
Here, the differences are listed below.
 
No String StringBuffer
1 String class is immutable StringBuffer class is mutable (modifiable)
2 Consumes more memory during concatenation Consumes less memory during concatenation
3 Slow performance Fast performance
4 Overrides equals() and hashcode() methods of the object class Doesn't override the equals() and hashcode() methods of the object class
 
Explanation of the preceding differences
 
Mutability: String is immutable (once created, it cannot be modified).
 
For example:
  1. String str = “Hello folks”;  
  2. Str = “Smile”;  
We can see that the value of str has been changed from “Hello folks” to “Smile” by which it looks like it is mutable. But the truth is that it is immutable, because in the first statement an object is created using the string literal “Hello folks” and in the second statement when we assigned the new string literal, in other words “Smile”, to the variable str. The object itself didn't change, instead a new object was created using the new string literal “Smile” and the reference to it is assigned to the variable str. So, both of the string literals or objects exist with different references.
 
StringBuffer is immutable (modifiable)
 
For example:
  1. StringBuffer str = new StringBuffer(“Good”);  
  2. Str.append(“night”);  
Here we can see that, in the first statement an object is created using the string literal “Good” and in the second statement the value of the object is changed from “Good” to “Goodnight” after concatenation. So here the object was modified instead of creating a new object like Strings.
 
Performance: StringBuffer is preferred over String for doing concatenation of strings because it is faster. In the case of String, when you concatenate strings, you are actually creating a new object every time since it is immutable and that makes it slower than StringBuffer.
 
Overrides: The String class overrides the equals() and hashcode() methods of the object class. So you can compare the contents of two strings using the equals() method. Whereas StringBuffer doesn't override the equals() and hashcode() methods of the object class. So you cannot compare the contents of two strings.
 
String & StringBuffer Performance test
 
Here, in this example, we will the test performance of String & StringBuffer on the basis of concatenation of two strings.
 
Example
  1. public class PerformanceTest {  
  2.     public static String StringConcat() {  
  3.         String str = "csharp";  
  4.         for (int i = 0; i < 10000; i++) {  
  5.             str = str + "corner";  
  6.         }  
  7.         return str;  
  8.     }  
  9.     public static String StringBufferConcat() {  
  10.         StringBuffer strBuff = new StringBuffer("csharp");  
  11.         for (int i = 0; i < 10000; i++) {  
  12.             strBuff.append("corner");  
  13.         }  
  14.         return strBuff.toString();  
  15.     }  
  16.     public static void main(String args[]) {  
  17.         long startTime = System.currentTimeMillis();  
  18.         StringConcat();  
  19.         System.out.println("Time taken for StringConcat: " + (System.currentTimeMillis() - startTime) + "ms");  
  20.         startTime = System.currentTimeMillis();  
  21.         StringBufferConcat();  
  22.         System.out.println("Time taken for StringBufferConcat: " + (System.currentTimeMillis() - startTime) + "ms");  
  23.     }  
  24. }
Output
 
 
Figure 1 Performance Test
 
In the preceding output, you can see that the time taken by StringBuffer is 0ms and that proves that it is faster than String (1344ms).
 
String & StringBuffer hashcode test
 
With this example, we can check the various hashcode values returned by the two classes.
 
Example
  1. public class HashCodeTest   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         System.out.println("Hashcode test of String:");  
  6.         String str = "csharp";  
  7.         System.out.println("HashCode value of 'csharp': " + str.hashCode());  
  8.         str = str + "community";  
  9.         System.out.println("HashCode value of 'community': " + str.hashCode());  
  10.         str = str + "members";  
  11.         System.out.println("HashCode value of 'members': " + str.hashCode());  
  12.   
  13.         System.out.println("Hashcode test of StringBuffer:");  
  14.         StringBuffer strbuff = new StringBuffer("csharp");  
  15.         System.out.println("HashCode value of 'csharp': " + strbuff.hashCode());  
  16.         strbuff.append("community");  
  17.         System.out.println("HashCode value of 'community': " + strbuff.hashCode());  
  18.         strbuff.append("members");  
  19.         System.out.println("HashCode value of 'members': " + strbuff.hashCode());  
  20.     }  
  21. }
Output
  
 
Figure 2 Hashcode Test
 
In the preceding output you can see that, String returns different hashcode values for different strings and that proves that it overrides the hashcode() method, but StringBuffer returns the same hashcode values for both of the strings.
 
Thank you, keep learning and sharing.