Sun Java System Application Server Enterprise Edition 8.1 2005Q2 Performance Tuning Guide

Use StringBuffer to Concatenate Strings

To improve performance, instead of using string concatenation, use StringBuffer.append().

String objects are immutable—they never change after creation. For example, consider the following code:

String str = "testing";
str = str + "abc";

The compiler translates this code as:

String str = "testing";
StringBuffer tmp = new StringBuffer(str);
tmp.append("abc");
str = tmp.toString();

Therefore, copying is inherently expensive and overusing it can reduce performance significantly.