DEV Community

Discussion on: Effective Java: Beware the Performance of String Concatenation

Collapse
 
elmuerte profile image
Michiel Hendriks

You might also want to initialize the size of the StringBuilder when you have a rough idea of the resulting size. In the above example

StringBuilder result = new StringBuilder(numberOfIterations * m);
Enter fullscreen mode Exit fullscreen mode

(Assuming numberOfIterations or m is not negative)

Collapse
 
kylec32 profile image
Kyle Carter

Yeah, you are exactly right. This is a great further optimization.