I read about the way Java works with += operator, using StringBuilder. Is it the same with a ("a" + "b") operation?
No. It's no the same using StringBuilder than doing "a" + "b".
In Java, String instances are immutable.
So, if you do:
String c = "a" + "b";
You are creating new Strings every time you concatenate.
On the other hand, StringBuilder is like a buffer that can grow as it needs when appending new Strings.
StringBuilder c = new StringBuilder();
c.append("a");
c.append("b"); // c is only created once and appended "a" and "b".
Rule of the thumb is (changed thanks to the comments I got):
If you are going to concatenate a lot (i.e., concatenate inside a loop, or generating a big XML formed by several string concatenated variables), do use StringBuilder. Otherwise, simple concatenation (using + operator) will be just fine.
Compiler optimizations also play a huge role when compiling this kind of code.
Here's further explanation on the topic. And more StackOVerflow questions on the issue:
Yes, it's the same, but the compiler can additionally optimize concatenations of literals before issuing the code, so "a"+"b"
can be just issued as "ab"
directly.
Strings are more commonly concatenated with the + operator, as in
"Hello," + " world" + "!"
If you’re only building long strings, then using StringBuilder is recommended. For shorter strings, with fixed creations (such as res = a + " " + b + " : " + c;) it should be okay. For iterations over a group of values, I’d seriously recommend StringBuilder.
For concatenating a fixed number of strings in one expression with +
, the compiler will produce code using a single StringBuilder
.
E.g. the line
String d = a + b + c;
results in the same bytecode as the line
String d = new StringBuilder().append(a).append(b).append(c).toString();
when compiled using the javac compiler. (The Eclipse compiler produces somewhat more optimized code by invoking new StringBuilder(a)
, thus saving one method call.)
As mentioned in other answers, the compiler will concatenate string literals like "a" + "b"
into one string itself, producing bytecode that contains "ab"
instead.
As mentioned everywhere on the net, you should not use +
to build up one string within a loop, because you are copying the beginning of the string over and over to new strings. In this situation you should use one StringBuilder
which you declare outside the loop.
"a" + "b"
operation
Though readable, easy to format and straight forward, concatenating strings with "+" is considered to be bad in Java.
Each time you append something via '+' (String.concat()) a new String is created, the old String content is copied, the new content is appended, and the old String is discarded. The bigger the String gets the longer it takes - there is more to copy and more garbage is produced. Note: if you are just concatenating a few (say 3,4) strings and not building a string via a loop or just writing some test application, you could still stick with "+"
Using
StringBuilder
When performing extensive String manipulation (or appending through a loop), replacing "+" with StringBuilder
.append is likely recommended. The intermediate objects mentioned in case of "+" are not created during append()
method call.
Also to be noted that optimizations in the Sun Java compiler, which automatically creates StringBuilders
(StringBuffers
< 5.0) when it sees String concatenations. But that is just Sun Java compiler.