• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuilder capacity method -- not updating capacity?

 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So this has been puzzling me. Perhaps I'm misusing it:


and my output is:

; capacity is: 16
cat; capacity is: 19
wolf; capacity is: 20
terrible horse; capacity is: 16
cats sleep too much; capacity is: 19
wolf-powered snowblower; capacity is: 42

Why would sb3 be the only StringBuilder that is updating its capacity? Am I missing something in my code?

Thanks!
-Mark
 
Paul Clapham
Sheriff
Posts: 21554
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what the API documentation has to say about that:

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.


So, based on what you did to those three internal buffers, does that help to explain the behaviour?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Justison wrote:Why would sb3 be the only StringBuilder that is updating its capacity? Am I missing something in my code?

The capacity is one of the internals of the StringBuilder. And as Paul mentioned the capacity will not be updated on every occasion.

The StringBuilder uses behind the scenes a char array to store its content. If the size of the array is not big enough to store new content, the capacity is increased and a new array is created (with the new capacity) and the old array is copied into a new one. So each time the internal buffer (the char array) has to be made larger, it has a very, very slight influence on the performance. That's why if you know a StringBuilder will hold at least 100 characters, you should create a StringBuilder with an initial capacity of 100 instead of using the default initial capacity. It will improve performance (because you have fewer "internal buffer made larger" operations).

What do you need to know for the exam about the capacity? This post to the rescue!

Note: the ArrayList uses a capacity as well (for exactly the same reason). It's also covered in the aforementioned post.

Hope it helps!
Kind regards,
Roel
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies! I see exactly where I went wrong. sb1 and sb2 were within their limits after the appends but not sb3 which is why its capacity increased.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing to remember is that the capacity is not the same as the size: the capacity is how many characters the StringBuilder has room for, and the size is the actual number of characters that are currently stored in the StringBuilder.

Normally, you don't need to worry about the capacity, because it grows automatically when you put more characters in the StringBuilder.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic