Note that if you are using Java 5 or newer, you should use StringBuilder
instead of StringBuffer
. From the API documentation:
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread,
StringBuilder
. TheStringBuilder
class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
In practice, you will almost never use this from multiple threads at the same time, so the synchronization that StringBuffer
does is almost always unnecessary overhead.