I used to see a lot of discussion about Java string stitching on the Internet. See some Java developers in suggestions for novice programmers like the following:
Do not use the + number stitching string, and use the StringBuffer or StringBuilder append () method to stitch the string.
However, using the + number stitching string is really so annoying, is not the use of the + number stitching string is not a bit desirable?
By looking at parts of the string class in the Java API documentation, we can see the following fragment:
The Java language provides special support for string concatenation symbols ("+") and for converting other objects to strings. string concatenation is implemented through the StringBuilder (or StringBuffer) class and its Append method. String conversions are implemented through the ToString method, which is defined by the Object class and can be inherited by all classes in Java. ”
This passage clearly tells us that using the + number stitching string in Java actually uses the StringBuffer or StringBuilder and its Append method to implement it.
In addition to the Java API documentation, we can use the tool to view the byte code commands for class files to get these answers. For example, code:
public static void Main (string[] args) {
String a = "Hello";
String B = "World";
String str = a + B + "!";
System.out.println (str);
The corresponding bytecode commands are viewed from the tool as follows:
From the bytecode command, we can see clearly that the following code we wrote
String str = a + B + "!";
was converted by the compiler to resemble the following statement:
String str = new StringBuilder (string.valueof (a)). Append (b). Append ("!"). ToString ();
Not only that, the Java compiler is also a relatively clever compiler, when the + number stitching is the whole string literal, the Java compiler will be at compile time intelligently convert it to a complete string. For example:
public static void Main (string[] args) {
String str = ' Hello ' + ' world ' + ', java! ';
System.out.println (str);
The Java compiler directly concatenation this literal string, converting it to a complete string at compile time.
Even if there are variables in the string with the + number stitching, the Java compiler merges the first string literal into a string.
public static void Main (string[] args) {
String java = ", java!";
String str = "Hello" + "world" + Java;
System.out.println (str);
From the above, for a similar string str = str1 + str2 + str3 + STR4, this will be a one-time concatenation of multiple strings of operations, using the + number for stitching is completely no problem.
In Java, a string object is immutable (immutable). In code, you can create multiple aliases for one string object. But these aliases are all references that are the same.
For example, S1 and S2 are aliases to "droidyue.com" objects, and aliases hold references to real objects. So S1 = s2
String S1 = "droidyue.com";
String s2 = S1;
System.out.println ("S1 and S2 has the same reference =" + (S1 = = s2));
And in Java, the only operator that is overloaded is the concatenation of strings. +,+=. In addition, the Java designer does not allow other operators to be overloaded.
In Java, the only operator that is overloaded is the concatenation of strings. +,+=. In addition, the Java designer does not allow other operators to be overloaded.
As we all know, before the Java 1.4 version, string stitching can use StringBuffer, starting with Java 1.5, we can use StringBuilder to stitch strings. The main difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe, is suitable for multithreaded operation strings, StringBuilder is thread insecure and is suitable for single-threaded operation strings. However, most of our string concatenation operations are done in a single thread, so using StringBuilder is beneficial for performance improvement.
Before Java 1.4, the compiler used StringBuffer to handle string concatenation of + numbers; Starting with Java 1.5, the compiler used StringBuilder to handle the string with the + number stitching in most cases.
When we write code in the context of JDK 1.4, it is advisable to use the + number for this one-time concatenation of multiple strings. Thus, when the JDK is upgraded to version 1.5 and above, the compiler will automatically convert it to StringBuilder to stitch the string, thereby increasing the string concatenation efficiency.
Of course, the recommended use of the + number stitching string is also limited to the concatenation of multiple strings in a single statement. If you scatter a string in more than one statement, it is still recommended that you use StringBuffer or StringBuilder. For example:
public static void Main (string[] args) {
String java = ", java!";
String str = "";
STR + + "Hello";
str = "World";
str = java;
System.out.println (str);
The compiler compiles the byte commands as follows:
From the above picture we can know that each + number stitching statements, all created a new StringBuilder object. This condition is especially evident under cyclic conditions, resulting in relatively large performance losses. Therefore, the concatenation of strings in multiple statements is strongly recommended to be handled using StringBuffer or StringBuilder.
About the optimization brought about by using StringBuilder
In addition, when using StringBuffer or StringBuilder, we can further improve performance by using the following methods (for example, in StringBuilder, StringBuffer is similar to this).
1. Predict the maximum length of the final string to be obtained.
The default length of the StringBuilder internal Char array is 16, and when we append append the string beyond this length, StringBuilder expands the internal array capacity to meet the need. In this process, StringBuilder creates a new, larger-capacity char array and copies the data from the original array into the new array. If we can roughly predict the maximum length of the string to be finally spliced, we can specify the initial capacity of the appropriate size when creating the StringBuilder object. For example, we need to splice to get a string containing 100 letters a. You can write the following code:
StringBuilder sb = new StringBuilder (m);
for (int i = 0; i < i++) {
sb.append (' a ');
}
System.out.println (SB);
Balance the situation to create a StringBuilder that is appropriate for the initial capacity.
2. For a single character, use the char type as much as possible instead of the string type.
Sometimes we need to append a single character (for example: a) after the string, and use the
Rather than using: