码迷,mamicode.com
首页 > 其他好文 > 详细

StringBuffer类

时间:2014-09-28 13:14:12      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   java   strong   for   

  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类

标签:style   blog   http   color   io   ar   java   strong   for   

原文地址:http://www.cnblogs.com/wanghui390/p/3997816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!