String vs StringBuilder
String vs StringBuilder
In this section, we will discuss the comparison between Java String and StringBuilder class.
String
In Java, a string is treated as an object that represents a sequence of characters. The String class is present in java.lang package. In Java, a String object is always immutable.
StringBuilder
The StringBuilder class is used to create mutable strings. The StringBuilder is same as the StringBuffer class. The difference is StringBuilder class is not synchronized whereas, the StringBuffer class is synchronized.
The following table highlights the key points by comparing the String class with the StringBuilder classin Java.
String | StringBuilder |
String is an immutable class. | StringBuilder is a mutable class. |
Manipulation of string is slow in the String class. | Concatenation of string is quick in StringBuilder class as compared to the StringBuffer class. |
Comparison of two strings can be made using the equals() method. | Similar to StringBuffer, the StringBuilder class does not override the equals() method of the Object class. Hence, a comparison of two strings using the equals() method can not be made. |
Performance Difference Between String and StringBuffer
The following comparison is based on the basis of the concatenation of strings for the String class and the StringBuilder class.
FileName: PerformanceTestExample.java
// A basic Java program that compares the performance of String and StringBuilder 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 the StringBuilder class StringBuilder strBuilder = new StringBuilder("Hi "); for (int j = 0; j < 10000; j++) { // doing the concatenation work strBuilder.append("Tutorial & Example"); } // displaying the time taken by StringBuilder System.out.println("Total Time consumed by the StringBuilder is: " + (System.currentTimeMillis() - sTime) + "ms"); } }
Output:
Total Time consumed by the String is: 612ms Total Time consumed by the StringBuilder is: 5ms
Explanation: Similar to StringBuffer, the StringBuilder class also takes less time as compared to the String class. The reason behind this is the time for doing the appending work is less, as the StringBuilder class is also mutable, whereas, for the String class, the time consumption is more as the String class is immutable. The immutable property of the String class leads to the creation of a new string whenever the string is manipulated. The creation of the new string, upon manipulation, is absent in the StringBuilder class.
Hash Code Test for String and StringBuilder
In the previous example, we have learned that whenever a string is manipulated, a new string is created for the String class. However, the same is not true for the StringBuilder 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 StringBuilder 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("Hello "); System.out.println("String is: " + str ); // printing the hash code of the object str System.out.println("Hash code of the string object is: " + str.hashCode()); // doing the concatenation work str += ("Tutorial & Example "); System.out.println("\nString is: " + str); // printing the hash code of the object str System.out.println("Hash code of the String object is: " + str.hashCode()); System.out.println("\nPerforming The Hash Code test for the StringBuilder: \n"); // creating an object of StringBuilder StringBuilder strBuilder = new StringBuilder("Hello "); System.out.println("String is: " + strBuilder ); // printing the hash code of the object strBuilder System.out.println("Hash code of the String Builder object is: " + strBuilder.hashCode()); // doing the concatenation work strBuilder.append("Tutorial & Example"); System.out.println("\nString is: " + strBuilder ); // printing the hash code of the object strBuilder System.out.println("Hash code of the String Builder object is: " + strBuilder.hashCode()); } }
Output:
Performing The Hash Code test for String: String is: Hello Hash code of the string object is: -2137068114 String is: Hello Tutorial & Example Hash code of the String object is: -443899836 Performing The Hash Code test for the StringBuilder: String is: Hello Hash code of the String Builder object is: 314337396 String is: Hello Tutorial & Example Hash code of the String Builder object is: 314337396
Explanation: It is evident by looking at the output that, for the StringBuilder class, the hash code remains the same even after the manipulation (append work) of the string. Because of the immutability feature of the String class, the hash code changes every time when the manipulation of string is done.