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?
See: JavaSE 6 java.lang.StringBuffer capacity()
But your assumption is correct:
Internally
StringBuffer
uses achar
array in order to store characters. Capacity is the initial size of thatchar
array.More INFO can be found from http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
It's the size of internal buffer. As Javadoc says:
"Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger."
http://download.oracle.com/javase/1.3/docs/api/java/lang/StringBuffer.html -see capacity() and ensurecapacity()
Ivan, just read the documentation for capacity() - it directly answers your question...
From http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html#capacity%28%29
Also from the same document