StringBuilder capacity()

stringbuilder capacity java
stringbuilder capacity c#
stringbuilder capacity vs length
stringbuilder length
stringbuilder max capacity java
stringbuilder c#
string capacity java
stringbuilder initial capacity

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?

StringBuilder capacity() in Java with Examples, capacity() - the number of character spaces that have been allocated. When you try to construct a stringbuilder with empty content, by default it� The capacity() method of StringBuilder Class is used to return the current capacity of StringBUilder object. The capacity is the amount of storage available to insert new characters. Syntax: public int capacity() Return Value: This method returns the current capacity of StringBuilder Class.

I will try to explain this with some example.

public class StringBuilderDemo {
     public static void main(String[] args) {
         StringBuilder sb = new StringBuilder();
         System.out.println(sb.length());
         System.out.println(sb.capacity());
     }
}

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:

  1. If you don't initialize stringbuilder with any content, default capacity will be taken as 16 characters capacity.
  2. If you initialize stringbuilder with any content, then capacity will be content length+16.
  3. When you add new content to stringbuilder object, if current capacity is not sufficient to take new value, then it will grow by (previous array capacity+1)*2.

This analysis is take from actual StringBuilder.java code

StringBuilder.Capacity Property (System.Text), This example demonstrate the use of capacity() method of StringBuilder class. import java.lang.StringBuilder; class StringBuilderCapacity{ public static void� The java.lang.StringBuilder.capacity() method returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur. The following example shows the usage of java.lang.StringBuilder.capacity() method

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

StringBuilder capacity(), insert(sb.length(), x) . Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not� If you initialize stringbuilder with any content, then capacity will be content length+16. When you add new content to stringbuilder object, if current capacity is not sufficient to take new value, then it will grow by (previous array capacity+1)*2. This analysis is take from actual StringBuilder.java code

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.

StringBuilder.capacity() Method, Length and Capacity. The StringBuilder class, like the String class, has a length() method that returns the length of the character sequence in the builder. In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). 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.

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)

StringBuilder (Java Platform SE 7 ), The java.lang.StringBuilder.capacity() method returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond � The maximum capacity for this implementation is Int32.MaxValue. However, this value is implementation-specific and might be different in other or later implementations. You can explicitly set the maximum capacity of a StringBuilder object by calling the StringBuilder(Int32, Int32) constructor.

The StringBuilder Class (The Java™ Tutorials > Learning the Java , Understand how the StringBuilder type manages its capacity as data is added. void Main() { using (var writer = new StreamWriter("data.txt")) { StringBuilder� Since the StringBuilder class is a drop-in replacement for StringBuffer class, this applies to the StringBuffer capacity as well. What is StringBuilder capacity (or StringBuffer capacity)? Java StringBuilder and StringBuffer classes maintain an internal character array to store the contents. As and when we add more content to the StringBuilder

Java.lang.StringBuilder.capacity() Method, Basically the capacity() method gives the allowable number of characters this StringBuilder object can still accommodate. Method Syntax. public� StringBuilder Append. Program: This is a simple console program in C# that loops through 257 integers, 0 to 256 inclusive. And: It writes the Capacity and Length of the StringBuilder, and then appends one character. Note: The output shows in the first column the Capacity. And in the second column, we see the buffer Length.

C# StringBuilder Capacity, The ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.

Comments
  • Why do you care about the capacity? It automatically grows to accomodate whatever is necessary. You can play with it to improve performance, but it's still asymptotically linear.
  • There are questions about capacity vs. length in the OCA exam so to some people the issue does mean a lot.
  • yes but if I have: StringBuilder str = new StringBuilder(); // capacity 16 str.append("1111111111111111111"); capacity 32 length 19 According to the equation why capacity is not 16 * 2 + 2 = 34??
  • Note that this is an implementation detail, the doc does not guarantee that the append methods call ensureCapacity.
  • This article is about C#, isn't it?
  • Yep. The formula is similar to Java but not exactly the same.
  • My apologies - I saw StringBuilder and assumed .NET.
  • Interesting! Maybe they took it out as an optimisation?
  • Maybe, however into the jdk 7 documentation that is not yet updated!