• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

StringBuilder capacity method -- not updating capacity?

 
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic