JAVA源码解读之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(), 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
 * @see     java.lang.StringBuilder
 * @see     java.lang.String
 * @since   JDK1.0
 */
  • 这是一个线程安全的,可变的字符串
  • StringBuffer类似于String,但是他是可以修改的。这个字符串可以通过调用方法来修改其长度与内容。
  • StringBuffer是线程安全的,可以线程同步使用,在一些必要的同步方法中是使用synchronized的修饰的。
  • StringBuffer的基本操作就是插入,insert和append。他们是被重载的,因此可以使用与任何数据类型的插入,每一个重载方法都有效地将给定数据类型转化为字符类型,然后再插入到StringBuffer中。其中append插入数据到字符串的串尾,insert插入数据到字符串的指定位置
  • 举例子,z是一个StringBuffer对象,z代表的字符串是start,如果调用z.append("le"),现在的字符串是startle。如果调用的是z.insert(4,"le"),现在的字符串就是starlet
  • sb是一个StringBuffer对象,sb.append(x)等价于sb.insert(sb.length(),x)
  • 当操作源字符串时,这时候的操作将是被同步修饰执行的,而不是同步源字符串
  • 每一个StringBuffer都有一个容量。如果这个容量可以容纳字符串的话,将不会扩容,但是容量不够存储字符串的话,sb对象将会自动扩容
  • jdk1.5 发行之后,有一个等价的对象被补充-StringBuilder。但是StringBuilder不支持线程同步,他支持StringBuffer的所有方法,速度更快。推荐使用StringBuilder

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Narasimha_Karumanchi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值