What is the capacity of a StringBuffer?
Asked Answered
A

14

12

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?

Awhile answered 4/11, 2011 at 15:19 Comment(0)
T
9

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

Trimaran answered 4/11, 2011 at 15:22 Comment(0)
H
3

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.

Hanhhank answered 4/11, 2011 at 15:22 Comment(0)
V
3

Yes, you're correct, see the JavaDoc for more information:

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.

Vern answered 4/11, 2011 at 15:22 Comment(0)
S
2

Internally StringBuffer uses a char array in order to store characters. Capacity is the initial size of that char array.

More INFO can be found from http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html

Shostakovich answered 4/11, 2011 at 15:21 Comment(0)
D
2

From http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html#capacity%28%29

public int capacity()

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

Also from the same document

As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

Degrease answered 4/11, 2011 at 15:21 Comment(0)
B
2

Yes, it's exactly that. You can think of StringBuffer as being a bit like a Vector<char> in that respect (except obviously you can't use char as a type argument in Java...)

Broadbent answered 4/11, 2011 at 15:21 Comment(0)
L
2

Taken from the official J2SE documentation

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

Its generally length+16, which is the minimum allocation, but once the number of character ie its size exceed the allocated one, StringBuffer also increases its size (by fixed amount), but by how much amount will be assigned,we can't calculate it.

Ligature answered 4/11, 2011 at 15:21 Comment(0)
J
2

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.

From: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/StringBuffer.html

Jurado answered 4/11, 2011 at 15:22 Comment(0)
T
2

StringBuffer has a char[] in which it keeps the strings that you append to it. The amount of memory currently allocated to that buffer is the capacity. The amount currently used is the length.

Toreutic answered 4/11, 2011 at 15:22 Comment(0)
E
1

"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()

Esme answered 4/11, 2011 at 15:25 Comment(0)
R
1

Capacity is amount of storage available for newly inserted characters.It is different from length().The length() returns the total number of characters and capacity returns value 16 by default if the number of characters are less than 16.But if the number of characters are more than 16 capacity is number of characters + 16. In this case,no of characters=17 SO,Capacity=17+16=33

Regular answered 26/6, 2016 at 12:3 Comment(0)
R
0

Ivan, just read the documentation for capacity() - it directly answers your question...

Reasoning answered 4/11, 2011 at 15:22 Comment(2)
I already red it, and it says "capacity is the amount of storage" but I wasn't sure what is measure unit for this amount. As some approved this measure unit is "number of characters". Thanks.Kythera
Quote: Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.Reasoning
H
0

The initial capacity of StringBuffer/StringBuilder class is 16. For the first time if the length of your String becomes >16. The capacity of StringBuffer/StringBuilder class increases to 34 i.e [(16*2)+2]

But when the length of your String becomes >34 the capacity of StringBuffer/StringBuilder class becomes exactly equal to the current length of String.

Haulage answered 10/7, 2017 at 12:29 Comment(0)
C
0

It is already too late for the answer, But hoping this might help someone.

When we use default constructor of StringBuffer then the capacity amount get allocated is 16

StringBuffer name = new StringBuffer();
System.out.println("capacity: " + name.capacity()); /*Output - 16*/

But in case of String argument Constructor of StringBuffer the capacity calculation is like below

StringBuffer sb = new StringBuffer(String x);

Capacity = default StringBuffer Capacity + x.length()

Solution:

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

Capacity Calculation: capacity = 16 + 17

Chanson answered 2/2, 2020 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.