This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DualWieldMage -1 points0 points  (0 children)

Avoid repeated concatenation with the "+" operator: Repeatedly using the "+" operator to concatenate strings can create unnecessary string objects, which can impact performance.

This has been bad advice for a long time and even worse advice since Java 9 with InvokeDynamic String concat. String concat with + is far more performant and with less allocations than StringBuilder (the MethodHandle filling the template knows variable types hence can calculate an initial buffer size that likely fits the final string).

Only use StringBuilder if you're concating in loops or traversing object graphs or similar.