DWQA QuestionsCategory: QuestionsStringBuffer's capacity() method
prj719000 asked 19 mins ago

StringBuffer str=new StringBuffer();
StringBuffer str1=new StringBuffer(3);
System.out.println(str.capacity()); //16
System.out.println(str1.capacity()); //8
Why is the result of str1.capacity 8?,You may be mistaken, the output should be:

16
3

Look at the source code:

// minimumCapacity is the length of characters currently stored + the length of characters to be appended
    // value.length is the current capacity
    // So new capacity=max(2*old capacity+2, the length of characters after appending)
    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);
    }

It just expands the capacity when appending. In addition to the default of 16 when initializing, you can set as many as you want.,

To show you the constructor in JDK 1.8:
Two constructors of StringBuffer, inherited from the constructor of the parent class AbstractStringBuilder:
public StringBuffer() {
    super(16);
}
public StringBuffer(int capacity) {
    super(capacity);
}
The constructor of AbstractStringBuilder:
AbstractStringBuilder(int capacity) {
    value = new char[capacity];
}

,Run it hereJava online compilation and executionOperating environment: java v1.7.0_80

prj719000 replied 20 hours ago

The result of str1.capacity() is 8, because the capacity() constructor of StringBuffer creates a 16-character buffer by default, which is stored in the internal array,
When you call the default constructor, the buffer is 16 bytes, for example: str.capacity(), when your byte length exceeds 16, according to (initial size + 1)2 increase so(3+1)2
Because the initialization is 3 bytes, when str1.append() appends 7 bytes, it is (3+1)2. If you append to 9 bytes, because it has exceeded (initial size + 1)2 length, so the result will become 9

3 Answers
magooxx answered 20 hours ago

You may be mistaken, the output should be:

16
3

Look at the source code:

// minimumCapacity is the length of characters currently stored + the length of characters to be appended
    // value.length is the current capacity
    // So new capacity=max(2*old capacity+2, the length of characters after appending)
    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);
    }

It just expands the capacity when appending. In addition to the default of 16 when initializing, you can set as many as you want.

prj719000 replied 20 hours ago

But my output in the console is 8

magooxx replied 20 hours ago

I suggest you try again

Bodhi Rising Light answered 20 hours ago
To show you the constructor in JDK 1.8:
Two constructors of StringBuffer, inherited from the constructor of the parent class AbstractStringBuilder:
public StringBuffer() {
    super(16);
}
public StringBuffer(int capacity) {
    super(capacity);
}
The constructor of AbstractStringBuilder:
AbstractStringBuilder(int capacity) {
    value = new char[capacity];
}
lihua9081 answered 20 hours ago

Run it hereJava online compilation and executionOperating environment: java v1.7.0_80