What is the capacity of a StringBuffer ?
Is it necessary to set the size of the capacity when the program starts?And if not set,is cause the program to run slowly?
eg. more than 1000 length character use StringBuffer outputSource = new StringBuffer();
On the other hand,setting a relatively accurate value should will increate program computing performance?
I hope my question is clear.
Thanks advanced!

- 281
- 6
- 22
6 Answers
You should use the more recent StringBuilder
class. It's essentially the same, but without synchronization.
If you know beforehand the approximate size, it's more efficient to allocate the StringBuilder
with a sufficient capacity. This way it doesn't need to resize itself during the operations.
Note that unless you're using multiple operations to create long Strings, it won't really affect performance. A situation where defining the capacity might be useful is for example creating a String of 10,000 characters, 10 characters appended at a time. It would take 1000 append calls, and might require the internal char[]
to be resized multiple times.
However if you were to create a String of 10,000 characters with 2 appends, you might get only 2 resizings. This is unlikely an issue performance-wise.

- 72,141
- 5
- 83
- 121
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. 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.
Some important takeaways
If you overflow the capacity of the buffer it needs to allocate more memory, this will have some impact on performance depending on how the StringBuffer
is used.
Basically the capacity of the StringBuffer
is as much memory as you have assigned to your program.
If your not multi-threading use StringBuilder

- 93,289
- 19
- 159
- 189
StringBuffer
is thread safe. so it is run slower when it have large amount if data. instead of your application is single thread use StringBuilder
. it is fast access compared with StringBuffer
. but StringBuilder
is not thread safe.

- 3,116
- 3
- 18
- 22
Since Strings are immutable (cannot be changed), concatenating can be pretty time and memory consuming, since with every concatenation, the old version plus the String that needs to be appended are created as a new variable, having the old variable still on the heap ...
The StringBuilder holds an internal buffer, so that only the string that you want append needs to be copied.
If the StringBuilder runs out of space, it doubles up it's buffer (in this case the whole string needs to be copied). the reason why the buffer is doubled and not just extended to the size that matches the old string's size + the appended one's is that in this case the string would always have to be copied again. hence it doubles up.
It is also possible to provide the final buffer size in the constructor of the StringBuilder, which makes only sense if you already know how much will go into your StringBuilder
Hope this helps

- 8,665
- 12
- 47
- 70
You should prefer to use StringBuffer
class.
StringBuffer class is a mutable class unlike the String class which is immutable. Both the capacity and character string of a StringBuffer Class. StringBuffer can be changed dynamically. String buffers are preferred when heavy modification of character strings is involved (appending, inserting, deleting, modifying etc).
There are functions like capacity()
and ensureCapacity()
in StringBuffere class.
capacity() : Returns the current capacity of the String buffer.
ensureCapacity() : this method ensures minimum capacity of the String buffer.
You can go through following links to know brief about StringBuffer :

- 3,344
- 7
- 30
- 49
any how stringbuffer is mutable. any how it is not going to create any further object once it has been allocated to memory.
so its not affect on performance if you relatively define any size of strignbuffer

- 2,082
- 12
- 20