This way the compiler is the same by default. So it won’t be slow
Another example:
String a= "a";
String b = "b";
String s2= a+b;
The s2 produced like this is very slow because the memory is requested three times.
I hope you will adopt it and give me points if you are satisfied. . I am a new member and hope to serve you all! ! ! (I really want to give -1 point to the first answer, just give it a few random answers)
The first type is not just a slow problem, but a code that looks like shit.
new StringBuilder("a").append("b").append("c");
new StringBuilder("a" + "b" + "c");
If you use it like the above, it is better to directly use "a" + "b" + "c"因为没有使用到StringBuilder的任何特性,加上JDK’s compilation and optimization. The final code will become as follows.
new StringBuilder(new StringBuilder("a").append("b").append("c").toString());
I don’t understand the second one at all. What is the meaning of using StringBuilder? Isn't it just a string in itself?
As long as the splicing character '+' is not used between objects, there is basically no difference between using the splicing character '+' between strings and not using it; and once the string object is generated, it will be modified to generate another object with all strings Splicing between objects will be very slow;
public static void main(String[] args) {
fun1(10000000);
} private static void fun1(int length){
long min1 = new Date().getTime();
System.out.println(min1);
//1
for(int i=0;i<length;i++){
new StringBuffer("字符1;" +
"字符2;" +
"字符3;" +
"字符4;" +
"字符5;");
}
long min2 = new Date().getTime();
System.out.println(min2);
System.out.println(min2-min1);
} private static void fun2(int length){
long min1 = new Date().getTime();
System.out.println(min1);
//2
for(int i=0;i<length;i++){
new StringBuffer("字符1;字符2;字符3;字符4;字符5;");
}
long min2 = new Date().getTime();
System.out.println(min2);
System.out.println(min2-min1);
} private static void fun3(int length){
long min1 = new Date().getTime();
System.out.println(min1);
//3
for(int i=0;i<length;i++){
String aa = "字符1;" +
"字符2;" +
"字符3;" +
"字符4;" +
"字符5;";
new StringBuffer(aa);
}
long min2 = new Date().getTime();
System.out.println(min2);
System.out.println(min2-min1);
} private static void fun4(int length){
long min1 = new Date().getTime();
System.out.println(min1);
//3
for(int i=0;i<length;i++){
String aa = "字符1;" ;
aa +="字符2;" ;
aa +="字符3;" ;
aa +="字符4;" ;
aa +="字符5;";
new StringBuffer(aa);
}
long min2 = new Date().getTime();
System.out.println(min2);
System.out.println(min2-min1);
These two methods of creating objects are faster and slower. In fact, they can be converted into the following two methods to compare faster and slower:
String a = "Test";
String b = "T" + "e" + "s" + "t";
Of course these two methods are equally fast. In the JVM, "T" + "e" + "s" + "t" are actually processed as "Test". Let’s now take a look at the following string splicing method:
String a = "Te";
String b = a + "st";
The above method is the least efficient method of string splicing and is extremely not recommended, because when this kind of string splicing is done, the JVM actually generates a new StringBuilder, then calls the append method, and then performs toString. The speed is simply It’s a slow dog (dog: I’m lying on the gun again...) So to sum up: the two new StringBuilder methods written by the subject are actually the same method, the efficiency is the same, and they are straightforward.
Answer: Just as fast
I hope you will adopt it and give me points if you are satisfied. . I am a new member and hope to serve you all! ! ! (I really want to give -1 point to the first answer, just give it a few random answers)
The first type is not just a slow problem, but a code that looks like shit.
If you use it like the above, it is better to directly use
"a" + "b" + "c"
因为没有使用到StringBuilder
的任何特性,加上JDK
’s compilation and optimization. The final code will become as follows.I don’t understand the second one at all. What is the meaning of using
StringBuilder
? Isn't it just a string in itself?javap -c
看看,编译器应该会把你用+
The connected string constants are spliced together, and the two writing methods should run the same.Constants can be used directly +
Variables can also be used +
If you care about performance, check this out: http://calvin1978.blogcn.com/...
As long as the splicing character '+' is not used between objects, there is basically no difference between using the splicing character '+' between strings and not using it; and once the string object is generated, it will be modified to generate another object with all strings Splicing between objects will be very slow;
public static void main(String[] args) {
}
private static void fun1(int length){
}
private static void fun2(int length){
}
private static void fun3(int length){
}
private static void fun4(int length){
}
These two methods of creating objects are faster and slower. In fact, they can be converted into the following two methods to compare faster and slower:
Of course these two methods are equally fast. In the JVM, "T" + "e" + "s" + "t" are actually processed as "Test".
Let’s now take a look at the following string splicing method:
The above method is the least efficient method of string splicing and is extremely not recommended, because when this kind of string splicing is done, the JVM actually generates a new StringBuilder, then calls the append method, and then performs toString. The speed is simply It’s a slow dog (dog: I’m lying on the gun again...)
So to sum up: the two new StringBuilder methods written by the subject are actually the same method, the efficiency is the same, and they are straightforward.