关于java中的AbstractStringBuilder中的append()方法理解(源码分析)

一、背景

1.关于AbstractStringBuilder是一个抽象实现类,所以不能直接new对象,接下来主要分析他的子类StringBuilder,来看下它底层主要是实现了什么把数据加上。

2.今天主要介绍的是StringBuilder类中的append()方法,接下来我们来看看源码吧。

二、源码解析append(int i)

1.进入AbstractStringBuilder的子类StringBuilder中,它复写了AbstractStringBuilder的append(int i)方法。

@Override
public StringBuilder append(int i) {
    super.append(i);
    return this;
}

2.接下来看下AbstractStringBuilder的具体实现类

public AbstractStringBuilder append(int i) {
    //如果传入的值为-2147483648,就添加-2147483648,返回当前对象本身
    if (i == Integer.MIN_VALUE) {
        append("-2147483648");
        return this;
    }
    //三元运算,返回的Integer.stringSize(i)的值为1
    int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
                                     : Integer.stringSize(i);
    int spaceNeeded = count + appendedLength;
    ensureCapacityInternal(spaceNeeded);
    //将i的值添加到value后
    Integer.getChars(i, spaceNeeded, value);
    count = spaceNeeded;
    //然后返回当前对象
    return this;
}

3.再去看下ensureCapacityInternal()方法具体实现了那些东西

private void ensureCapacityInternal(int minimumCapacity) {
   // 如果需要,则扩容,具体在newCapacity方法里
   if (minimumCapacity - value.length > 0) {
       value = Arrays.copyOf(value,newCapacity(minimumCapacity));
     }
 }

4.现在看下getChars()方法

static void getChars(int i, int index, char[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

        if (i < 0) {
            sign = '-';
            i = -i;
        }

        // Generate two digits per iteration
        while (i >= 65536) {
            q = i / 100;
        // really: r = i - (q * 100);
            r = i - ((q << 6) + (q << 5) + (q << 2));
            i = q;
            buf [--charPos] = DigitOnes[r];
            buf [--charPos] = DigitTens[r];
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i <= 65536, i);
        for (;;) {
            q = (i * 52429) >>> (16+3);
            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
            //在这一步添加我们的元素的,将i的值添加到之前的value后
            buf [--charPos] = digits [r];
            i = q;
            if (i == 0) break;
        }
        if (sign != 0) {
            buf [--charPos] = sign;
        }
    }

三、源码解析append(String str)

1.接着我们在看下怎么添加String类型的数据的append(String str)源码

public AbstractStringBuilder append(String str) {
   if (str == null)
      return appendNull();
      int len = str.length();
      ensureCapacityInternal(count + len);
      str.getChars(0, len, value, count);
      count += len;
      return this;
}

2.再去看下ensureCapacityInternal()方法具体实现了那些东西

private void ensureCapacityInternal(int minimumCapacity) {
   // 如果需要,则扩容,具体在newCapacity方法里
   if (minimumCapacity - value.length > 0) {
       value = Arrays.copyOf(value,newCapacity(minimumCapacity));
     }
 }

3.现在看下getChars()方法

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);
}

四、结束

1.今天源码就先看到这里吧!!!欢迎留言评论!!!

发布了122 篇原创文章 · 获赞 64 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/chenmingxu438521/article/details/100571990