Java String vs StringBuffer
Java String vs StringBuffer
In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes.
String Class
The String class represents character strings. All string literals in Java programs, such as "xyz", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable, they can be shared.
StringBuffer Class
StringBuffer is a thread-safe, mutable sequence of characters. It is the same as a String. The only difference is that it cannot be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type
Difference Between String and StringBuffer Class
String | StringBuffer |
String is an immutable class. | StringBuffer is a mutable class. |
Concatenation of string is slow in the String class. | Concatenation of string is fast in the StringBuffer class. |
Contents of the two strings can be compared using the equals() method, as the String class overrides the equals() method of the Object class. | Contents of the two strings cannot be compared using the equals() method, as the StringBuffer class does not override the equals() method of the Object class. |
Performance Comparison
The following comparison is based on the basis of the concatenation of string for the String class and the StringBuffer class.
FileName: PerformanceTestExample.java
// A basic Java program that compares the performance of String and StringBuffer public class PerformanceTestExample { // main method public static void main(String argvs[]) { long sTime = System.currentTimeMillis(); // creating an object of String String str = new String("Hi "); for (int j = 0; j < 10000; j++) { // doing the concatenation work str += ("Tutorial & Example"); } // displaying the time taken by String System.out.println("Total Time consumed by the String is: " + (System.currentTimeMillis() - sTime) + "ms"); // resetting the start time sTime = System.currentTimeMillis(); // creating an object of StringBuffer StringBuffer strBuffer = new StringBuffer("Hi "); for (int j = 0; j < 10000; j++) { // doing the concatenation work strBuffer.append("Tutorial & Example"); } // displaying the time taken by StringBuffer System.out.println("Total Time consumed by the StringBuffer is: " + (System.currentTimeMillis() - sTime) + "ms"); } }
Output:
Total Time consumed by the String is: 349ms Total Time consumed by the StringBuffer is: 2ms
Explanation: It is evident that the manipulation of a string in the String class is much slower as compared to the StringBuffer class. The reason behind this is the String class is immutable. Therefore, when a string in the String class is manipulated, the current string is not manipulated at all. Instead of manipulating the current string, memory for a new string is allocated, and a new string is generated. All these memory allocation and generation of the new string take time.
Because StringBuffer class is mutable, no new string is generated when the string manipulation is done. The current string is manipulated to get the desired output. Therefore, the manipulation is faster in the StringBuffer class as compared to the String class.
Hash Code Test for String and StringBuffer
We have learnt that when a string is manipulated in the String class, a new string is generated, and the same is not true for the StringBuffer class. Let’s confirm it with the help of an example.
FileName: HashCodeTestExample.java
// A basic Java program that performs the hash code test for String and StringBuffer public class HashCodeTestExample { // main method public static void main(String argvs[]) { System.out.println("Performing The Hash Code test for String: \n"); // creating an object of String String str = new String("Hi "); System.out.println("String is: " + str ); // printing the hashcode of the object str System.out.println("Hashcode of the string object is: " + str.hashCode()); // doing the concatenation work str += ("Tutorial & Example"); System.out.println("\nString is: " + str); // printing the hashcode of the object str System.out.println("Hashcode of the String object is: " + str.hashCode()); System.out.println("\nPerforming The Hash Code test for the StringBuffer: \n"); // creating an object of StringBuffer StringBuffer strBuffer = new StringBuffer("Hi "); System.out.println("String is: " + strBuffer ); // printing the hashcode of the object strBuffer System.out.println("Hashcode of the String Buffer object is: " + strBuffer.hashCode()); // doing the concatenation work strBuffer.append("Tutorial & Example"); System.out.println("\nString is: " + strBuffer ); // printing the hashcode of the object strBuffer System.out.println("Hashcode of the String Buffer object is: " + strBuffer.hashCode()); } }
Output:
Performing The Hash Code test for String: String is: Hi Hashcode of the string object is: 72479 String is: Hi Tutorial & Example Hashcode of the String object is: -1969955187 Performing The Hash Code test for the StringBuffer: String is: Hi Hashcode of the String Buffer object is: 1282788025 String is: Hi Tutorial & Example Hashcode of the String Buffer object is: 1282788025
Explanation: For the strings of the class String, the hash code is different when the string is manipulated, that shows the immutability of the class String. For the StringBuffer class, the hash code is same even after the manipulation showing that strings are not immutable in the StringBuffer class.