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:
- String str = “Hello folks”;
- 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:
- StringBuffer str = new StringBuffer(“Good”);
- 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
- public class PerformanceTest {
- public static String StringConcat() {
- String str = "csharp";
- for (int i = 0; i < 10000; i++) {
- str = str + "corner";
- }
- return str;
- }
- public static String StringBufferConcat() {
- StringBuffer strBuff = new StringBuffer("csharp");
- for (int i = 0; i < 10000; i++) {
- strBuff.append("corner");
- }
- return strBuff.toString();
- }
- public static void main(String args[]) {
- long startTime = System.currentTimeMillis();
- StringConcat();
- System.out.println("Time taken for StringConcat: " + (System.currentTimeMillis() - startTime) + "ms");
- startTime = System.currentTimeMillis();
- StringBufferConcat();
- System.out.println("Time taken for StringBufferConcat: " + (System.currentTimeMillis() - startTime) + "ms");
- }
- }
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
- public class HashCodeTest
- {
- public static void main(String args[])
- {
- System.out.println("Hashcode test of String:");
- String str = "csharp";
- System.out.println("HashCode value of 'csharp': " + str.hashCode());
- str = str + "community";
- System.out.println("HashCode value of 'community': " + str.hashCode());
- str = str + "members";
- System.out.println("HashCode value of 'members': " + str.hashCode());
-
- System.out.println("Hashcode test of StringBuffer:");
- StringBuffer strbuff = new StringBuffer("csharp");
- System.out.println("HashCode value of 'csharp': " + strbuff.hashCode());
- strbuff.append("community");
- System.out.println("HashCode value of 'community': " + strbuff.hashCode());
- strbuff.append("members");
- System.out.println("HashCode value of 'members': " + strbuff.hashCode());
- }
- }
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.