Sun GlassFish Enterprise Server 2.1 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);
str = tmp.toString();

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