关注Java细节 -- 对比StringBuffer类和StringBuilder类

来源:互联网 发布:部落冲突治疗升级数据 编辑:程序博客网 时间:2024/05/19 19:42

StringBuffer是一个从JDK1.0开始就存在的类,他就像String类一样,不过其内部成员是可以修改的。

StringBuilder是JDK1.5新引入的一个类,他和StringBuffer提供相同的接口。

 -----------------------------------------------------------

 

今天我们就来共同讨论一下这两个类的相同点和不同点:

 

相同点:

他们继承相同的父类: AbstractStringBuilder

他们实现相同的接口: java.io.Serializable, CharSequence

他们基本上对外提供相同的方法

 

不同点:

StringBuffer类是线程安全的,所有StringBuffer提供的public方法基本上都是synchronized的

StringBuilder类不是线程安全的,他的方法没有被synchronized锁修饰

 

 ------------------------------------------------------------

 

我们可以说,StringBuilder是StringBuffer的一个简单实现。

当在多线程的环境中使用时,我们应该首选StringBuffer。

如果只是在单线程环境中使用,那么我们应该选择StringBuilder,省略了同步的操作,其效率会比StringBuffer高一点。

 

 -------------------------------------------------------------

 

下面将JDK中两个类的注释贴在这里,希望能帮助大家更好的理解两个类。

 

StringBuffer

 

/**
 * A thread-safe, mutable sequence of characters.
 * A string buffer is like a
{@link 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.
 * <p>
 * String buffers are safe for use by multiple threads. The methods
 * are synchronized where necessary so that all the operations on any
 * particular instance behave as if they occur in some serial order
 * that is consistent with the order of the method calls made by each of
 * the individual threads involved.
 * <p>
 * The principal operations on a <code>StringBuffer</code> are the
 * <code>append</code> and <code>insert</code> methods, which are
 * overloaded so as to accept data of any type. Each effectively
 * converts a given datum to a string and then appends or inserts the
 * characters of that string to the string buffer. The
 * <code>append</code> method always adds these characters at the end
 * of the buffer; the <code>insert</code> method adds the characters at
 * a specified point.
 * <p>
 * For example, if <code>z</code> refers to a string buffer object
 * whose current contents are "<code>start</code>", then
 * the method call <code>z.append("le")</code> would cause the string
 * buffer to contain "<code>startle</code>", whereas
 * <code>z.insert(4, "le")</code> would alter the string buffer to
 * contain "<code>starlet</code>".
 * <p>
 * In general, if sb refers to an instance of a <code>StringBuffer</code>,
 * then <code>sb.append(x)</code> has the same effect as
 * <code>sb.insert(sb.length(),&nbsp;x)</code>.
 * <p>
 * Whenever an operation occurs involving a source sequence (such as
 * appending or inserting from a source sequence) this class synchronizes
 * only on the string buffer performing the operation, not on the source.
 * <p>
 * Every string buffer has a capacity. As long as the length of the
 * character sequence contained in the string buffer does not exceed
 * the capacity, it is not necessary to allocate a new internal
 * buffer array. If the internal buffer overflows, it is
 * automatically made larger.
 *
 * As of  release JDK 5, this class has been supplemented with an equivalent
 * class designed for use by a single thread,
{@link StringBuilder}.  The
 * <tt>StringBuilder</tt> 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.
 *
 * @author Arthur van Hoff
 * @version  1.99, 07/15/04
 * @see     java.lang.StringBuilder
 * @see     java.lang.String
 * @since   JDK1.0
 */

 

 

StringBuilder

 

/**
 * A mutable sequence of characters.  This class provides an API compatible
 * with <code>StringBuffer</code>, but with no guarantee of synchronization.
 * This class is designed for use as a drop-in replacement for
 * <code>StringBuffer</code> 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
 * <code>StringBuffer</code> as it will be faster under most implementations.
 *
 * <p>The principal operations on a <code>StringBuilder</code> are the
 * <code>append</code> and <code>insert</code> methods, which are
 * overloaded so as to accept data of any type. Each effectively
 * converts a given datum to a string and then appends or inserts the
 * characters of that string to the string builder. The
 * <code>append</code> method always adds these characters at the end
 * of the builder; the <code>insert</code> method adds the characters at
 * a specified point.
 * <p>
 * For example, if <code>z</code> refers to a string builder object
 * whose current contents are "<code>start</code>", then
 * the method call <code>z.append("le")</code> would cause the string
 * builder to contain "<code>startle</code>", whereas
 * <code>z.insert(4, "le")</code> would alter the string builder to
 * contain "<code>starlet</code>".
 * <p>
 * In general, if sb refers to an instance of a <code>StringBuilder</code>,
 * then <code>sb.append(x)</code> has the same effect as
 * <code>sb.insert(sb.length(),&nbsp;x)</code>.
 *
 * Every string builder has a capacity. As long as the length of the
 * character sequence contained in the string builder does not exceed
 * the capacity, it is not necessary to allocate a new internal
 * buffer. If the internal buffer overflows, it is automatically made larger.
 *
 * <p>Instances of <code>StringBuilder</code> are not safe for
 * use by multiple threads. If such synchronization is required then it is
 * recommended that
{@link java.lang.StringBuffer} be used.
 *
 * @author Michael McCloskey
 * @version  1.9, 07/16/04
 * @see         java.lang.StringBuffer
 * @see         java.lang.String
 * @since 1.5
 */

原创粉丝点击