首页 > 代码库 > StringBuffer类

StringBuffer类

  void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
          将字符从此字符串复制到目标字符数组。

//不能被继承

public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence{}

 

StringBuffer类继承AbstractStringBuilder,AbstractStringBuilder中有两个属性:

/**     * The value is used for character storage.     */    char[] value;    /**     * The count is the number of characters used.     */    int count;

几个重要的方法:

1 public AbstractStringBuilder append(String str) {2         if (str == null) str = "null";3         int len = str.length();4         ensureCapacityInternal(count + len);5         str.getChars(0, len, value, count);6         count += len;7         return this;8     }
private void ensureCapacityInternal(int minimumCapacity) {        // overflow-conscious code        if (minimumCapacity - value.length > 0)            expandCapacity(minimumCapacity);    }
void expandCapacity(int minimumCapacity) {        int newCapacity = value.length * 2 + 2;        if (newCapacity - minimumCapacity < 0)            newCapacity = minimumCapacity;        if (newCapacity < 0) {            if (minimumCapacity < 0) // overflow                throw new OutOfMemoryError();            newCapacity = Integer.MAX_VALUE;        }        value = Arrays.copyOf(value, newCapacity);    }

 

StringBuffer类