You may now be wondering why we have two classes that do almost the same (StringBuffer and StringBuilder):
StringBuilder is a new class, added in
Java 5. It (more or less) replaces StringBuffer, and
you should always prefer StringBuilder above StringBuffer - StringBuffer should be regarded as a legacy class.
The difference is, as already said, that most methods in StringBuffer are synchronized, while they are not in StringBuilder. The engineers at Sun realized that for most applications, synchronization is not necessary, but it does add some runtime overhead.
This is similar to Vector and ArrayList, or Hashtable and HashMap.
Jim Hoglund wrote:So be sure to use StringBuilder unless you are working with shared data.
In that case, use StringBuffer.
To make this more precise: If you use it from multiple threads at the same time (which you almost never do), use StringBuffer. In all other cases (almost always), use StringBuilder.