The String,StringBuilder, and StringBuffer classes have long been a classic problem in Java
A. Simple comparison
- String: character constant
- StringBuilder: character variable
- StringBuffer: character variable
String is a constant and is declared final class. All properties are final, so once a String is created, it cannot be changed. The StringBuilder/StringBuffer classes are variable types that can be changed, and they are both provided to solve the problem of strings having too many intermediate objects due to concatenation.
-
Running speed StringBuilder > StringBuffer > String
-
Thread safety: StringBuffer
-
Non-thread-safe: StringBuilder
StringBuilder is not much different from StringBuffer in nature, but because StringBuilder removes the thread-safe part that StringBuffer has, it effectively reduces overhead. Therefore, StringBuilder is the preferred string concatenation operation for most situations
String Processes a String
Example 1:
String s = "abcd";
s = s + "fgh";
Copy the code
A lot of people who do String manipulation like this mistakenly think that strings are mutable.
Instead, the JVM handles this code like this: first it creates an S object and assigns “abcd”. Then, on the second line of code, it creates another S object and assigns “abcdfgh”. Then the first S object is garbage collected.
So the first s has not changed, and the second S is the new object
Example 2:
String STR = "This is only a" + "simple" +"test";Copy the code
This code is equivalent to String STR = “This is only a simple test”;
Example 3:
String str2 = "This is only a";
String str3 = "simple";
String str4 = "test";
String str1 = str2 +str3 + str4;
Copy the code
This code will also be treated as in example 1
StringBuilder/StringBuffer constructor features
Array.copyof () : char[] : char[] : char[] : char[] : char[] : char[] , of course, this affects performance, so you can customize the size of an object as needed when you create it
Source code: // Default 16 characters publicStringBuilder() { super(16); } public StringBuilder(int capacity) {super(capacity); }Copy the code
String and StringBuilder handle String concatenation comparison
We all know that StringBuilder is recommended for String concatenation, but is StringBuilder always recommended instead of String concatenation? Obviously not.
Example 1:
String str = "123";
String str1 = str + "456";
String str2 = new StringBuilder().append(str).append("def").toString();
Copy the code
In this case, there is little difference in efficiency between the two treatments
After JDK1.5, String concatenation is automatically converted to StringBuilder by the compiler and append is called. The toString method of StringBuilder is called to return a recreated String. Because of this optimization scheme, the processing efficiency of the two classes is not very different in this case. In Java 9, for more uniform string manipulation optimization, StringConcatFactory is provided as a unified entry point that optimizes string concatenation operations.
Example 2:
String str = "";
for (int i = 0; i < 1000; i++) {
str += "12345";
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 1000; i++) {
stringBuilder.append("12345");
}
Copy the code
In this case, StringBuilder is faster
Each time a “+” is executed in the loop, a String object is created, so there is a lot of object creation and recycling overhead.
Simply put, string concatenation of the same string object is done in a loop, and StringBuilder is preferred
Example 3
String str1 = "123" + "456" + "789";
String str2 = new StringBuilder("123").append("456").append("789").toString();
Copy the code
In this case, String is faster
We know that String str1 = “123” + “456” + “789”; String str1 = “123456789”; StringBuilder calls the Append method multiple times.