It is supposed to be generally preferable to use a StringBuilder
for string concatenation in Java. Is this always the case?
What I mean is this: Is the overhead of creating a StringBuilder
object, calling the append()
method and finally toString()
already smaller then concatenating existing strings with the +
operator for two strings, or is it only advisable for more (than two) strings?
If there is such a threshold, what does it depend on (perhaps the string length, but in which way)?
And finally, would you trade the readability and conciseness of the +
concatenation for the performance of the StringBuilder
in smaller cases like two, three or four strings?
Explicit use of StringBuilder
for regular concatenations is being mentioned as obsolete at obsolete Java optimization tips as well as at Java urban myths.