when to use StringBuilder in java

Posted by kostja on Stack Overflow See other posts from Stack Overflow or by kostja
Published on 2011-01-10T08:52:30Z Indexed on 2011/01/10 9:53 UTC
Read the original article Hit count: 176

It is supposed to be generally preferable to use a StringBuilder for String concatenation in Java. Is it always the case?

What i mean is :

Is the overhead of creating a StringBuilder object, calling the append() method and finally toString() smaller then concatenating existing Strings with + for 2 Strings already or is it only advisable for more Strings?

If there is such a threshold, what does it depend on (the String length i suppose, 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 2, 3, 4 Strings?

© Stack Overflow or respective owner

Related posts about java

Related posts about string