StringBuffer的capacity的处理方法有点特别。看下面的StringBuffer的方法    private char value[];    public StringBuffer(int length) {
value = new char[length];
shared = false;
    }    public synchronized int capacity() {
return value.length;
    }
    /**
     * This implements the expansion semantics of ensureCapacity but is
     * unsynchronized for use internally by methods which are already
     * synchronized.
     *
     * @see java.lang.StringBuffer#ensureCapacity(int)
     */
    private void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;
        if (newCapacity < 0) {
            newCapacity = Integer.MAX_VALUE;
        } else if (minimumCapacity > newCapacity) {
    newCapacity = minimumCapacity;
}

char newValue[] = new char[newCapacity];
System.arraycopy(value, 0, newValue, 0, count);
value = newValue;
shared = false;
    }    public synchronized StringBuffer append(String str) {
if (str == null) {
    str = String.valueOf(str);
} int len = str.length();
int newcount = count + len;
if (newcount > value.length)
    expandCapacity(newcount);
str.getChars(0, len, value, count);
count = newcount;
return this;
    }现在明白为什么是20了吗? 因为加入的字符串没有超过20,所以还是你一开始初始化的值20。当只有超过20的时候才会增加capacity。而且是两倍的增加。