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
StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA")
.append(B_String).append("CCCCCCCCCCC").append(D_String)
.append("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
.append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
Is this the way to do it? From this post, I noticed that the +
operator on Strings creates a new instance of StringBuilder, concatenates the operands, and returns a String conversion, which seems like a lot more work than just calling .append()
; so if that's true, then that is out of the question. But what about String.concat()
? Is it proper to use .append()
for every concatenation? Or just for variables, and literals are okay to append with .concat()
?
StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA")
.append(B_String.concat("CCCCCCCCCCC")).append(D_String
.concat("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
.concat("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
What are the general rules for best practices and performance for going about these situations? Is my assumption correct on +
and it should really just not be used?
+
operatorBehind the scenes this is translated to:
Imagine how much extra work it adds if you have
s1 + s2
here:instead of:
Multiple strings with
+
Worth to note that:
is translated to:
concat()
String
createschar[]
array that can fit boths1
ands2
. Copiess1
ands2
contents to this new array. Actually requires less work then+
operator.StringBuilder.append()
Maintains an internal
char[]
array that grows when needed. No extrachar[]
is created if the internal one is sufficiently big.is also performing poorly because
s1.concat(s2)
creates an extrachar[]
array and copiess1
ands2
to it just to copy that new array contents to internalStringBuilder
char[]
.That being said you should use
append()
all the time and append raw strings (your first code snippet is correct).The compilier optimize the + concatenation.
So
is transformed into
new StringBuilder().append("Hello ").append(1).toString();
There an excellent topic here explaining why you should use the + operator.
Use
+
operator is best practice, it is also simple and readable.Offical document: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
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. :)
You should always use
append
.concat
create a new String so it's pretty like+
I think.If you
concat
or use+
with 2 final String the JVM can make optimisation so it's the same as doing append in this case.Optimization is done automatically by the compiler.
The Java2 compiler will automatically convert the following:
to
Taken straight from the Java Best Practices on Oracles website.