Best practices/performance: mixing StringBuilder.append with String.concat
+
operator
String 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)
Multiple strings with +
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).
The compilier optimize the + concatenation.
So
int a = 1;
String s = "Hello " + a;
is transformed into
new StringBuilder().append("Hello ").append(1).toString();
There an excellent topic here explaining why you should use the + operator.