java.lang
Class StringBuffer
继承关系:
java.lang.Object
java.lang.StringBuffer
A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls
String buffers are safe for use by multiple threads
//
java.lang
Class StringBuilder
继承关系:
java.lang.Object
java.lang.StringBuilder
A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
/
StringBuffer具有线程同步。而StringBuilder不具同步性。但是StringBuilder执行效率比StringBuffer更高。在单线程和不要求线程安全的程序中用StringBuilder更有效率。