11

As we know, there is a attribute in StringBuilder called capacity, it is always larger than the length of StringBuilder object. However, what is capacity used for? It will be expanded if the length is larger than the capacity. If it does something, can someone give an example?

1
  • Exactly the same uses as the capacity of an ArrayList. Commented Dec 19, 2014 at 8:17

4 Answers 4

13

You can use the initial capacity to save the need to re-size the StringBuilder while appending to it, which costs time.

If you know if advance how many characters would be appended to the StringBuilder and you specify that size when you create the StringBuilder, it will never have to be re-sized while it is being used.

If, on the other hand, you don't give an initial capacity, or give a too small intial capacity, each time that capacity is reached, the storage of the StringBuilder has to be increased, which involves copying the data stored in the original storage to the larger storage.

Sign up to request clarification or add additional context in comments.

2 Comments

what if the capacity is too large? And can you tell me what the StringBuffer is used for typically?
@Jing If you specify a too large capacity, you are allocating more memory than you need, so it's wasteful. It's the same as creating an array that is larger than what you actually need. StringBuffer is the thread safe version of StringBuilder, so it should only be used if multiple threads can modify the same instance of a StringBuffer.
4

The string builder has to store the string that is being built somewhere. It does so in an array of characters. The capacity is the length of this array. Once the array would overflow, a new (longer) array is allocated and contents are transferred to it. This makes the capacity rise.

If you are not concerned about performance, simply ignore the capacity. The capacity might get interesting once you are about to construct huge strings and know their size upfront. Then you can request a string builder with a capacity being equal to the expected size (or slightly larger if you are not sure about the size).

Example when building a string with a content size of 1 million:

StringBuilder sb = new StringBuilder(1000000);
for(int i = 0; i < 1000000; i++){
   sb.append("x");
}

Initializing the string builder with one million will make it faster in comparison to a default string builder which has to copy its array repeatedly.

2 Comments

Can you tell me what typically the StringBuilder is used for?
@Jing: To build strings dynamically, i.e., whenever you want to assemble a string piece by piece, then use a string builder.
3

The answer is: performance. As the other answers already say, StringBuilder uses an internal array of some original size (capacity). Every time the building up string gets to large for the array to hold it, StringBuilder has to allocate a new, larger array, copy the data from the previous array to the new one and delete the previous array.

If you know beforehand what size the resulting string might be and pass that information to the constructor, StringBuilder can create a large enough array right away and thus can avoid the allocating and copying.

While for small strings, the performance gain is negelectible, it make quite a difference if you build really large strings.

Comments

3

StringBuilder is backed by an array of characters. The default capacity is 16 + the length of the String argument. If you append to the StringBuilder and the number of characters cannot be fit in the array, then the capacity will have to be changed which will take time. So, if you have some idea about the the number of characters that you might have, initialize the capacity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.