Best practices / performance: mixing StringBuilder.append with String.concat

I am trying to understand what is the best practice, and why to concatenate string literals and variables for different cases. For example, if I have code like this

StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA") .append(B_String).append("CCCCCCCCCCC").append(D_String) .append("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE") .append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); 

Is this a way to do this? From this post, I noticed that the + operator in Strings creates a new StringBuilder instance, concatenates the operands and returns a String transformation, which seems a lot more work than just calling .append() ; so if this is true, then this is out of the question. But what about String.concat() ? Is it right to use .append() for each concatenation? Or just for variables, and literals can be added using .concat() ?

 StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA") .append(B_String.concat("CCCCCCCCCCC")).append(D_String .concat("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE") .concat("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")); 

What are the general best practice and performance rules for these situations? Is my assumption correct on + and is it really easy not to use it?

+65
java string stringbuilder string-concatenation
Apr 09 2018-12-12T00:
source share
7 answers

+ operator

 String s = s1 + s2 

Behind the scenes, this translates to:

 String s = new StringBuilder(s1).append(s2).toString(); 

Imagine how much extra work he adds if you have s1 + s2 here:

 stringBuilder.append(s1 + s2) 

instead:

 stringBuilder.append(s1).append(s2) 

A few lines with +

It is worth noting that:

 String s = s1 + s2 + s3 + ... +sN 

translates to:

 String s = new StringBuilder(s1).append(s2).append(s3)...apend(sN).toString(); 

concat()

 String s = s1.concat(s2); 

String creates an array of char[] , which can match either s1 or s2 . Copy s1 and s2 contents to this new array. Actually less work is needed than the + operator.

StringBuilder.append()

It supports the internal char[] array, which grows when necessary. No extra char[] is created if the inside is large enough.

 stringBuilder.append(s1.concat(s2)) 

also works poorly because s1.concat(s2) creates an additional char[] array and copies s1 and s2 it only to copy this new contents of the array into the internal StringBuilder char[] .

In doing so, you should use append() all the time and add raw lines (your first piece of code is correct).

+139
Apr 09 '12 at 20:00
source share

The compiler optimizes + concatenation.

So

 int a = 1; String s = "Hello " + a; 

converted to

new StringBuilder().append("Hello ").append(1);

There is a great topic here explaining why you should use the + operator.

+13
Apr 09 '12 at 19:57
source share

You should always use append .

concat create a new line so i like + .

If you concat or use + with 2 final lines, the JVM can do the optimization, so that it will be the same as when added in this case.

+2
Apr 09 2018-12-12T00:
source share

Optimization is performed automatically by the compiler.

The Java2 compiler automatically converts the following:

 String s = s1 + s2; 

to

 String s = (new StringBuffer()).append(s1).append(s2).toString(); 

Taken directly from Java Best Practices on the Oracles website.

+2
May 12 '14 at 19:41
source share

If you concatenate exactly two strings, use String.concat (creates a new String, creating a new char -array that fits both strings and copies both strings and char into it).

If you execute several (more than two) lines on one line, use + or StringBuilder.append, it does not matter, since the compiler converts + to StringBuilder.append. This is useful for multiple lines, as it supports a single char array, which grows as needed.

If you concatenate multiple lines over multiple lines, create one StringBuilder and use the append method. In the end, when you finish adding strings to StringBuilder, use its .toString () method to create a string from it. For concatting on multiple lines, this is faster than the second method, because the second method created a new StringBuilder on each line, added the lines and then returned String, while the third method uses only one StringBuilder for everything.

+1
Jan 27 '14 at 10:03
source share

Using the + operator is best practice; it is also simple and readable.

The Java language provides special support for the string operator concatenation (+), and for converting other objects to strings. String concatenation is done through StringBuilder (or StringBuffer) and its add method.

White paper: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

0
Aug 10 '16 at 3:34
source share

in byte code, the level is no different, and we do not compromise efficiency there. If the byte code level is executed, it must go through the in-inline overload method for + by calling append. then at the assembly language level (Java written in C and C creates assemblies similar to an assembly, there will be an additional register call to store + method call on the stack and an additional push will be added. (in fact, the cross-compiler can optimize + the operator call, in in this case, making no distinction with efficiency.)

Good practice is one way to increase readability. :)

0
Sep 26 '17 at 14:26
source share



All Articles