-1

I tried the constructors of the StringBuffer class in Java but I was confused with the working of StringBuffer(int initial) constructor.

I know that it gives an initial capacity to the buffer but I have a doubt that, when the initial capacity provided is actually less than the length of the object, how does the capacity increase in such case?

I tried two cases where in first one:

StringBuffer str = new StringBuffer(11);
str.append("Hello World!");
System.out.println(str.length() + " " + str.capacity()); //prints 12 24

that is the capacity becomes twice as that of the length of the string.

and in second one:

StringBuffer str = new StringBuffer(10);
str.append("Hello World!");
System.out.println(str.length() + " " + str.capacity()); //prints 12 22

that is the capacity becomes length + initial buffer capacity

So why such different increase of Buffer size. Initial capacity is less than the string length in both the cases so either it should double to the length in both cases that is becomes 24 or it adds up to the initial buffer that is becomes 23 in the first case and 22 in the second. I read the documentation too and various other blogs but all I could find is the basic use of the constructors and functions of StringBuffer class.

Please help me out. Thank you.

  • 1
    Side note: you should be using `StringBuilder` instead of `StringBuffer` in modern Java. – Mick Mnemonic Jan 09 '18 at 09:17
  • It has to do with how the new space is allocated. It is a function of the currently allocated space, which is not the same in both of your examples – Tim Biegeleisen Jan 09 '18 at 09:20
  • Okay. I haven't read about it in detail still, so when I do I'll follow your advice. But since I was reading StringBuffer for now so asked this doubt :) @MickMnemonic – Kaustubh Srivastava Jan 09 '18 at 09:21
  • the source code of Java classes are normally distributed together with the JDK - should be possible to find the implementation there. – user85421 Jan 09 '18 at 09:33
  • I found an answer where it was said that the size increases in a way as (previous capacity + 1) * 2 which I can understand works with values whose increased value accommodates the string. But I don't think so it will work for values whose increased value is also not sufficient to accommodate the string. – Kaustubh Srivastava Jan 09 '18 at 09:39
  • Okay, I'll search there. Thank you @CarlosHeuberger – Kaustubh Srivastava Jan 09 '18 at 09:40
  • or just read the [javadoc](https://docs.oracle.com/javase/9/docs/api/java/lang/StringBuffer.html#ensureCapacity-int-) : "The new capacity is the larger of: ° The minimumCapacity argument. ° Twice the old capacity, plus 2. " – user85421 Jan 09 '18 at 09:40

1 Answers1

5

Please refer to the source code of StringBuffer and AbstractStringBuilder:

private int newCapacity(int minCapacity) {
    // overflow-conscious code
    int newCapacity = (value.length << 1) + 2;
    if (newCapacity - minCapacity < 0) {
        newCapacity = minCapacity;
    }
    return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
        ? hugeCapacity(minCapacity)
        : newCapacity;
}

Please go through these codes and you will get it

For the first block, initial value.length = 11, and (value.length << 1) + 2 = 24; For the secondblock, initial value.length = 10, and (value.length << 1) + 2 = 22;

Hash Jang
  • 599
  • 3
  • 11