I\'m trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this
in byte code level there is not different and we are not compromising effeciancy there. In case of executing byte code level, it must go through non-inline operator overloading method for + by calling append. then in assembly language level (Java is written in C and C produces assemblies similar to assembly, there will be extra register call to store + method call in the stack and there will additional push. (in reality, cross-compiler might optimise + operator call, in that case making it no difference with efficiancy.)
It is a good practice to have one way to increase the readability. :)
All answers are pretty good and explanatory. But I felt exploration around other string concatenation techniques would also help like - Guava Joiner, Streams, String.format etc.
For complete details over performance of each concatenation technique java-string-concatenation-which-way-is-best.
In Brief, concatenation performance varies with no. of strings to concatenate. For example - to concatenate 1-10 strings, these techniques works best - StringBuilder, StringBuffer and Plus Operator. And to concatenate 100s of strings - Guava Joiner, apache's stringsUtils library also works great.
Please go through the above blog. It really explains performance efficiency very well.
Thanks.
Optimization is done automatically by the compiler.
The Java2 compiler will automatically convert the following:
String s = s1 + s2;
to
String s = (new StringBuffer()).append(s1).append(s2).toString();
Taken straight from the Java Best Practices on Oracles website.
If you concat exactly two Strings use String.concat (creates a new String by creating a new char-array that fits both Strings and copys both Strings' char arrays into it).
If you concat multiple (more than two) Strings in one line, use + or StringBuilder.append, it doesn't matter, since the compiler converts + to StringBuilder.append. This is good for multiple Strings because it maintains one char array that grows as needed.
If you concat multiple Strings over multiple lines create one StringBuilder and use the append-method. In the end when you are done appending Strings to the StringBuilder use it's .toString()-method to create a String out of it. For concatting in multiple lines this is faster than the second method, since the second method would create a new StringBuilder on each line, append the Strings and then cast back to String, while the third method only uses one StringBuilder for the whole thing.
+
operatorString s = s1 + s2
Behind the scenes this is translated to:
String s = new StringBuilder(s1).append(s2).toString();
Imagine how much extra work it adds if you have s1 + s2
here:
stringBuilder.append(s1 + s2)
instead of:
stringBuilder.append(s1).append(s2)
+
Worth to note that:
String s = s1 + s2 + s3 + ... +sN
is translated to:
String s = new StringBuilder(s1).append(s2).append(s3)...apend(sN).toString();
concat()
String s = s1.concat(s2);
String
creates char[]
array that can fit both s1
and s2
. Copies s1
and s2
contents to this new array. Actually requires less work then +
operator.
StringBuilder.append()
Maintains an internal char[]
array that grows when needed. No extra char[]
is created if the internal one is sufficiently big.
stringBuilder.append(s1.concat(s2))
is also performing poorly because s1.concat(s2)
creates an extra char[]
array and copies s1
and s2
to it just to copy that new array contents to internal StringBuilder
char[]
.
That being said you should use append()
all the time and append raw strings (your first code snippet is correct).
Use +
operator is best practice, it is also simple and readable.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.
Offical document: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html