JAVA源码解读之StringBuffer

来源:互联网 发布:淘宝怎么进我的店铺 编辑:程序博客网 时间:2024/05/18 01:43
/** * 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

 

原创粉丝点击