+
Stringing is the most common method of splicing.
String jeremy = "Jeremy";
String tsai = "Tsai";
String jeremytsai = jeremy + tsai;
Observation byte code
L0
LINENUMBER 12 L0
LDC "Jeremy"
ASTORE 1
L1
LINENUMBER 13 L1
LDC "Tsai"
ASTORE 2
L2
LINENUMBER 14 L2
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 1
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 2
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 3
We are not difficult to find,String str = a + b
The JDK compiler helped us optimize to become the following sentences when compiling bytecode:
String jeremytsai = new StringBuilder().append(jeremy).append(tsai);
Differential and C ++ computing symbols are loaded. This is just JDK’s internal optimized syntax sugar. Java itself does not have the saying of operators. But pay attention to this grammar sugar, only effective for the same line, for example:
s = hello + world + jeremy + tsai;
Note: They are all variables.
If split
s = hello;
s += world;
s += jeremy;
s += tsai;
View byte code:
L5
LINENUMBER 15 L5
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 5
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 2
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 5
L6
LINENUMBER 16 L6
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 5
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 3
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 5
L7
LINENUMBER 17 L7
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 5
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 4
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 5
can be seen that each stitching will create a StringBuilder. Therefore, this is just grammatical sugar. It cannot be counted as a sign of the symbol. If you do n’t pay attention, it will affect the efficiency, because we cannot use it in daily writing.
The+
number of stitching is long, and there is business logic in the middle.
Concat method is a method for stitching string string stringed by String.
String jeremy = "Jeremy";
String tsai = "Tsai";
String jeremytsai = jeremy.concat(tsai);
source code description is as follows:
Connect the specified string to the end of the string.
If the length of the parameter string is 0, return this String object.
Otherwise, return a String object, which represents a character sequence that is connected to the character sequence represented by the string of the string of this String object.
source code
public String concat(String str) {
int otherLen = str.length(); // Get the length of the parameter string
if (otherLen == 0) {
return this; // The length of the parameter is 0, and return to yourself
}
int len = value.length; // Get your length
char buf[] = Arrays.copyOf(value, len + otherLen); // Get a character array that contains the current character sequence, the sum of the two is the sum of the two
str.getChars(buf, len); // Start with the length of the sequence of the current character, write the character sequence of the parameter into the buf character array
return new String(buf, true); // Create a new String object and return.
}
is the same as description.
String jeremy = "jeremy";
String tsai = "tsai";
String jeremytsai = new StringBuilder().append(jeremy).append(tsai).toString();
View bytecode
L0
LINENUMBER 13 L0
LDC "jeremy"
ASTORE 1
L1
LINENUMBER 14 L1
LDC "tsai"
ASTORE 2
L2
LINENUMBER 15 L2
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 1
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 2
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 3
can be seen, and+
The stitching is consistent.
first know, String is an unchanged object in Java, so each stitching is to generate a new String object. In order to solve the frequent memory -opening consumption resources, only there is it.StringBuilder
Class. exist
During the+
During the splicing process, JDK’s default optimization became StringBuilder to improve operational efficiency.
But there is another problem here. When there are only two string stitching to generate a new string, the advantage of this default optimization cannot be reflected. Because I only needed three string spaces, and the default optimization of StringBuilder, I also needed a space of StringBuilder to open up more space. It will definitely affect performance. Program test.
// Define the array to be spliced
String jeremy = "Jeremy";
String tsai = "Tsai";
// Then record the time consumption of each stitching respectively
String jeremytsai1 = jeremy + tsai;
String jeremytsai2 = jeremy.concat(tsai);
result, as I expected:
------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 100 -nano -seconds Concat stitching Jeremytsai execution time: 200 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 200 -nano -seconds Concat stitching Jeremytsai execution time: 100 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 100 -nano -seconds Concat stitching Jeremytsai execution time: 200 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 100 -nano -seconds Concat stitching Jeremytsai execution time: 200 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 200 -nano -seconds Concat stitching Jeremytsai execution time: 100 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 0 nano -seconds Concat stitching Jeremytsai execution time: 100 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai execution time is: 100 -nano -seconds Concat splicing Jeremytsai execution time: 300 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 100 -nano -seconds Concat splicing Jeremytsai execution time: 100 nan seconds
------------------------------------------------------------------------------------------------------------------------------------------------
+/StringBuilder stitching Jeremytsai's execution time is: 100 -nano -seconds Concat splicing Jeremytsai execution time: 0 nan seconds second second seconds
In the 100,000 stitching Jeremytsai,+/StringBuilder is fast,: 29510, and the number of CONCAT is: 70490
cyclic stitching is a special stitching, and its form is generally:
string results;
for(cycle condition) {
string middle amount;
// Calculationresult+=}
In this case, the default optimization of JDK is very clumsy, for example:
String jeremytsai = "";
for (int i = 0; i < 100; i++) {
jeremytsai += "JeremyTsai\n";
}
View source code
L0
LINENUMBER 15 L0
LDC ""
ASTORE 1
L1
LINENUMBER 16 L1
ICONST_0
ISTORE 2
L2
FRAME APPEND [java/lang/String I]
ILOAD 2
BIPUSH 100
IF_ICMPGE L3
L4
LINENUMBER 17 L4
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 1
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
LDC "JeremyTsai\n"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 1
L5
LINENUMBER 16 L5
IINC 2 1
GOTO L2
can be seen,
The default optimization of the+
number makes a new StringBuilder inside each cycle for stitching, which will greatly reduce performance. In the same way, Concat is the same. Each splicing will generate a new String object, which will frequently open up the space and the efficiency is not high.
Therefore, the string stitching in the cycle body is recommended to useStringBuilder
StringBuilder jeremytsai = new StringBuilder();
for (int i = 0; i < 100; i++) {
jeremytsai.append("jeremy").append("tsai\n");
}
The string stitching in the non -circulating body, if it is only two string stitching, it is recommended to useconcat
。
Polygonal or cycling stitching string is preferredStringBuilder
, improve efficiency, can also be programmed chain. Don’t be too dependent on+
Settlement grammar candy, but simple stitching is still recommended. After all, it can save a lot of code.