In Java, String, StringBuffer and StringBuilder classes are widely used. In many scenarios, any of them can be used. However, there are minor differences between these classes.
String
String is immutable and defined as a final class. As all immutable objects are thread safe, String is thread safe too.
Part of String class source code in Java
1 2 3 4 5 6 7 |
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { ... } |
When a String object is created, it will be saved in the String Constant Pool. Let’s look at the code:
1 2 3 4 |
String str1 = "ABC"; String str2 = "EFG"; |
One more example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package net.tecbar.string; public class StringConstantPoolDemo { public static void main(String[] args) { String str1 = "ABC"; // str1 points to "ABC" String str2 = "EFG"; str1 = "EFG"; //str1 points to "EFG" System.out.println("equal? " + str1.equals(str2)); System.out.println("==? " + (str1==str2)); } } |
Output:
1 2 3 4 |
equal? true ==? true |
The diagram shows what happened:
step 1: when “ABC” is created, variable str1 is its reference;
step 2: when “EFG” is created, variable str2 is its reference;
step 3: when str1 refers to String “EFG”, “ABC” has no reference
Let’s see one more example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package net.tecbar.string; public class StringConstantPoolDemo { public static void main(String[] args) { String str1 = "ABC"; // str1 points to "ABC" String str2 = new String("ABC"); System.out.println("equal? " + str1.equals(str2)); System.out.println("==? " + (str1==str2)); } } |
output
1 2 3 4 |
equal? true ==? false |
The diagram shows what happened:
- String literals with same values will refer to the same String object
- String objects created using new operator will be different from literals
StringBuilder
StringBuilder is a mutable sequence of characters. The object created through StringBuilder is stored in the heap. StringBuilder provides an API compatible with StringBuffer, but with no guarantee of synchronization. The class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations [Java Doc].
StringBuffer
StringBuffer is mutable. The object created through StringBuffer is stored in the heap. StringBuffer has the same interface as StringBuilder. However, the methods in StringBuffer are synchronized, and StringBuffer object is thread safe. As each method is synchronized in StringBuffer, the performance of StringBuffer will be hit a little bit compared to StringBuilder.
Conclusion
The difference between String, StringBuffer and StringBuilder includes:
- String is immutable while StringBuffer and StringBuilder is mutable object
- StringBuffer is synchronized and thread safe while StringBuilder is not.
- If the text will be accessed by single thread, use StringBuilder to replace StringBuffer to get better performance
- If the text is not going to change, use String (over StringBuilder / StingBuffer).