I noticed that the capacity
method returns StringBuilder
capacity without a logic
way ... sometime its value is equals to the string length other time it's greater...
is there an equation for know which is its logic?
I noticed that the capacity
method returns StringBuilder
capacity without a logic
way ... sometime its value is equals to the string length other time it's greater...
is there an equation for know which is its logic?
I will try to explain this with some example.
length()
- the length of the character sequence in the builder since this stringbuilder doesn't contain any content, its length will be 0.capacity()
- the number of character spaces that have been allocated. When you try to construct a stringbuilder with empty content, by default it takes the initialize size as length+16 which is 0+16. so capacity would return 16 here.Note: The capacity, which is returned by the capacity() method, is always greater than or equal to the length (usually greater than) and will automatically expand as necessary to accommodate additions to the string builder.
The logic behind the capacity function:
This analysis is take from actual StringBuilder.java code
From the API:
Whenever you append something, there is a check to make sure that the updated StringBuilder won't exceed its capacity, and if it does, the internal storage of the StringBuilder is resized:
When data is added to it that exceeds its capacity it is re-sized according to the following formula:
See the
src.zip
file that comes with the JDK for more information. (Above snippets taken from the 1.6 JDK)You can go inside the JDK code and see how it works, it is based on a char array:
new char[capacity]
, it is similar to how theArrayList
works (When to use LinkedList over ArrayList?). Both use arrays to be 'hardware efficient', the trick is to allocate a large chunk of memory and work in it until you run out of memory and need the next big chunk to continue (expand/grow).Here's the logic: If you define a new instance of the
StringBuilder
class without a constructor, like sonew StringBuilder();
the default capacity is 16. A constructor can be either anint
or aString
. For aString
constructor, the default capacity is calculated like thisFor an
int
constructor, the capacity is calculated like thisIf a new
String
is appended to theStringBuilder
and the new length of theString
is greater than the current capacity, then the capacity is calculated like this:EDIT: Apologies - the below is information on .NET's StringBuilder, and is not strictly relevant to the original question.
http://johnnycoder.com/blog/2009/01/05/stringbuilder-required-capacity-algorithm/
StringBuilder allocates space for substrings you might add to it (much like List creates space the array it wraps). If you want the actual length of the string, use StringBuilder.Length.
When you append to the
StringBuilder
, the following logic happens:where
newCount
is the number of characters needed, andvalue.length
is the current size of the buffer.expandCapacity
simply increases the size of the backingchar[]
The
ensureCapacity()
method is the public way to callexpandCapacity()
, and its docs say: