views:

86

answers:

4

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?

+1  A: 

This function does something different than you expect - it gives you the max number of chars this StringBuilder instance memory can hold at this time.

String Builder must read

InsertNickHere
A: 

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.

Alex Humphrey
This article is about C#, isn't it?
Andreas_D
Yep. The formula is similar to Java but not exactly the same.
Catchwa
My apologies - I saw StringBuilder and assumed .NET.
Alex Humphrey
+3  A: 

When you append to the StringBuilder, the following logic happens:

if (newCount > value.length) {
    expandCapacity(newCount);
}

where newCount is the number of characters needed, and value.length is the current size of the buffer.

expandCapacity simply increases the size of the backing char[]

The ensureCapacity() method is the public way to call expandCapacity(), and its docs say:

Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:

  • The minimumCapacity argument.
  • Twice the old capacity, plus 2.

If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

Bozho
yes but if I have:StringBuilder str = new StringBuilder(); // capacity 16str.append("1111111111111111111"); capacity 32 length 19According to the equation why capacity is not 16 * 2 + 2 = 34??
xdevel2000
+1  A: 

From the API:

Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

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:

int len = str.length();
int newCount = count + len;
if (newCount > value.length)
  expandCapacity(newCount);

When data is added to it that exceeds its capacity it is re-sized according to the following formula:

void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;
    if (newCapacity < 0) {
        newCapacity = Integer.MAX_VALUE;
    } else if (minimumCapacity > newCapacity) {
    newCapacity = minimumCapacity;
}
    value = Arrays.copyOf(value, newCapacity);
}

See the src.zip file that comes with the JDK for more information. (Above snippets taken from the 1.6 JDK)

Catchwa
Into JDK 7 source there is no more + 2 chars only the new value * 2 !!!
xdevel2000
Interesting! Maybe they took it out as an optimisation?
Catchwa
Maybe, however into the jdk 7 documentation that is not yet updated!
xdevel2000