StringBuilder

2018-06-28  本文已影响0人  茶瓯

String里面封装了char

public final class String{
     /** The value is used for character storage. */
      private final char value[];

   public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
    if (srcBegin < 0) {
        throw new StringIndexOutOfBoundsException(srcBegin);
    }
    if (srcEnd > value.length) {
        throw new StringIndexOutOfBoundsException(srcEnd);
    }
    if (srcBegin > srcEnd) {
        throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
    }
    //本地方法
    System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  }
}

public  class StringBuilder{
   public AbstractStringBuilder append(String str) {
    if (str == null)
        return appendNull();
    int len = str.length();
    //保证char的数组能满足需求
    ensureCapacityInternal(count + len);
    //调用String.getChars()方法
    str.getChars(0, len, value, count);
    count += len;
    return this;
    }
}
上一篇下一篇

猜你喜欢

热点阅读