Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Capacity in String Builder

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the purpose of saying a empty StringBuilder has a capacity of 16 chars. The 16 isn't a limit because I can add as many characters as I want. I can't access anything in the capacity because there is nothing there. In what situation would the capacity effect how I write code? Thanks!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of the capacity of the StringBuilder class is nothing but performance optimization.

Behind the scenes of a StringBuilder you'll find a char array. When you invoke operations like append, insert,... the char array needs to be expanded (e.g. when you append 10 chars and the char array has only room left for 5 chars).
Each time the array needs to be resized, it has a (little) effect on performance. So that's why the capacity isn't expanded 1 by 1 and the initial capacity of empty StringBuilder is 16.
Why not an initial capacity of 1000? Then you need a few (or none) expansions. Memory usage! Each StringBuilder (even if it contains just "yes" or "no") will take a big chunck of memory, because the char array is initialized to hold 1000 chars...

Hope it helps!
Kind regards,
Roel
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. So as a programmer I never have to worry about the capacity? Similar to not having to worry about garbage collection? Do I just need to be aware of it for the Java certification test? Thanks again!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I like to add ArrayList uses an Object array behind the scenes and has for exactly the same reasons a capacity as well. The default value of capacity (when you use the no-arg constructor) is 10.
javadocs ArrayList wrote:ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.


Kendall Ponder wrote:So as a programmer I never have to worry about the capacity? Similar to not having to worry about garbage collection?

I guess in 99% of your code, you won't worry about the capacity of a StringBuilder. Although you can change the initial capacity if you know for example the StringBuilder will contain large strings. Then you could increase the capacity at construction time (and limit the number of expansions of the char array). The same applies of course to the ArrayList as well. If you know you need to store 100 elements, you will have better performance using new ArrayList<>(100); (than if you would use new ArrayList<>();).

Kendall Ponder wrote:Do I just need to be aware of it for the Java certification test? Thanks again!

You definitely need to know about the capacity for the exam. I definitely remember a question on my exam about the capacity, can't remember if it was about StringBuilder and/or ArrayList But you just need some basic knowledge, if you know everything what I said in this thread, you'll be good for the exam.

A short summary:
  • ArrayList and StringBuilder use a capacity (to improve performance).
  • The initial capacity of ArrayList is 10, for StringBuilder it is 16.
  • Both classes define a constructor to create an instance with a specified initial capacity
  • If you create an ArrayList with a capacity of 5 and you add 2 elements, list.size() will return 2 (not 5). The same applies (of course) to StringBuilder and its length method.

  • You don't need to know the underlying algorithm to expand the capacity when you insert/add more elements than the underlying array can hold.

    Hope it helps!
    Kind regards,
    Roel
     
    Dasa Beg
    Greenhorn
    Posts: 5
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi guys,

    have a short question regarding to the wording ("initial capacity") with an example.

    Given:

    StringBuilder sb = new StringBuilder("Test");

    When we mention the StringBulider "initial capacity", does that mean the capacity of the object is 16 (without considering the literal "Test" ---> also 16 + 4 = 20)?

    Or do we, for this example, have to consider the argument during the instantation (new StringBuilder("Test")), so that the initial capacity for this object would be 20?

    An other example:

    StringBuilder sb1 = new StringBuilder("CodeRanch");

    Is the "initial capacity" for this object 16 or 16 + 9 = 25?

    Also to consider, if the construcot argument is a "int" value.

    StringBulider sb2 = new StringBuilder(128);

    What is the "initial capacity" of this StringBuilder object?

    Thanks in advance!
    Best regards

     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dasa Beg wrote:StringBuilder sb = new StringBuilder("Test");

    When we mention the StringBulider "initial capacity", does that mean the capacity of the object is 16 (without considering the literal "Test" ---> also 16 + 4 = 20)?

    Or do we, for this example, have to consider the argument during the instantation (new StringBuilder("Test")), so that the initial capacity for this object would be 20?

    Check StringBuilder's javadoc: The initial capacity of the string builder is 16 plus the length of the string argument. So for sb, the initial capacity is 20.

    Dasa Beg wrote:StringBuilder sb1 = new StringBuilder("CodeRanch");

    Is the "initial capacity" for this object 16 or 16 + 9 = 25?

    And sb1's initial capacity is 25.

    Dasa Beg wrote:StringBulider sb2 = new StringBuilder(128);

    What is the "initial capacity" of this StringBuilder object?

    Again we have a look at the javadoc of the appropriate constructor of StringBuilder: Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument. So for sb2, the initial capacity is 128.

    And a little code snippet to illustrate that above values are correct:Output:
    sb : capacity[20] length[4]
    sb1: capacity[25] length[9]
    sb2: capacity[128] length[0]


    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic