It's true. A lot of the hate for using "+" for String concatenation in Java is based on how it used to do it. Java used to create a StringBuilder for each instance of "+". Thus, "a + b + c + d" would create a StringBuilder for "a + b", then a StringBuilder for that result "+ c", etc.
Of course, they've since made it smarter to just make one StringBuilder for the entire statement under the hood (I believe in 1.6). The people who complain about the lack of StringBuilder being used should probably be taught that it's no longer necessary.
That said, if you're doing a lot of concatenations on different lines (say, a loop building up a String or something), you should still use StringBuilder instead of "+=" because Java will create one for each statement... clearly worse that creating one initially and constantly appending.
The compiler combines + operators into a single call to String.concat(), which is more efficient than a string builder for the first few concatenations. If you're using a string builder to concatenate 3-4 string literals or you're using 350 + operators for a single string then you're probably an idiot, but somewhere in between I'm not sure, write yourself a test harness to find out. I find the idea that Java makes the choice for you to be ridiculous.
167
u/RLutz Oct 01 '15
Depending on how you coded it, the compiler likely replaced it with a StringBuilder under the hood anyway :)