Search code examples
javastringbuffer

What is the capacity of a StringBuffer?


When I run this code:

StringBuffer name = new StringBuffer("stackoverflow.com");
System.out.println("Length: " + name.length() + ", capacity: " + name.capacity());

it gives output:

Length: 17, capacity: 33

Obvious length is related to number of characters in string, but I am not sure what capacity is? Is that number of characters that StringBuffer can hold before reallocating space?


Solution

  • See: JavaSE 6 java.lang.StringBuffer capacity()

    But your assumption is correct:

    The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur