• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does the StringBuilder's object capacity is 16 + no of Characters?  RSS feed

 
Siddhant Agarwal
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering, when we create an instance of StringBuilder, by default the capacity is taken as 16. However, after I sb.append("abcd"), when I check the capacity value again prints 20? Why is that so? What is stored in the initially allocated memory space?
 
Siddhant Agarwal
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering, when we create an instance of StringBuilder, by default the capacity is taken as 16. However, after I execute sb.append("abcd"), the capacity value changes to 20? Why is that so? What is stored in the initially allocated memory space?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it? How do you know? That is not specified in the documentation, so it might change from implementation to implementation. If you add 64 characters, is the capacity still 80?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37181
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code prints the following when I run it:
16 0
16 4


The initial capacity of a StringBuilder is 16 unless you specify otherwise. And the length starts empty.  The capacity only increases as needed so it doesn't change in this example.

Can you show the code you used to test? And what JDK you are using? I ran it with Java 8 on Mac.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do the 0 and 4 come from? Are you printing the length as well? Those figures suggest the capacity does not increase from 16 to 20.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37181
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Where do the 0 and 4 come from? Are you printing the length as well? Those figures suggest the capacity does not increase from 16 to 20.

Edited. I decided to add the length after pasting the code here. Yes, my run suggests the capacity doesn't increase from 16 to 20.  I'd be surprised if it did. I would expect capacity jumps to be bigger than that!
 
Zachary Griggs
Ranch Hand
Posts: 82
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the StringBuilder source code: with a default constructor, capacity starts at 16

With a constructor that initializes to a String: capacity is size + 16

When appending: it only extends the capacity if the new length would exceed it.

When it DOES need to extend the capacity, it does so by taking the current size of the StringBuilder +1 and multiplying that by 2. If that's not enough, then it expands to exactly the amount of space needed.


Given the example from your post: I find it very likely that you were constructing a StringBuilder with "abcd" which would give a capacity of 20, not appending it.
Luckily you don't really need to worry about any of this. The only time I would explicitly set the capacity of a StringBuilder is if I'm going to create and use them like, thousands of times in a loop, or read in a file line-by-line that has 10,000 lines or something; then the performance might be significant.
 
Siddhant Agarwal
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the incorrect information in the post. As Zachary said, it happens when I create an instance of StringBuilder and pass a value in the constructor. That is when it prints the capacity as 20. It doesn't do the same when we do an append. As seen in the source code for StringBuilder it passes the string's length + 16 to the base, causing the capacity to be 20. I agree with Zachary, that we need not worry about the capacity at all in most of the scenarios. However, I was wondering is there any reason behind setting the capacity to size of string + 16, in the constructor of this class?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37181
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that makes sense. The 16 extra is to give you some "slack" to add more characters without a resize. There's also a constructor that takes the desired capacity to any value you like. So if you are expecting 1000 characters to show up later, you can make it 1000.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:. . . I would expect capacity jumps to be bigger than that!
To 32? That is what I would expect, too.
OP: Why not go through the source code and see whether you can verify that sort of increase. The fact that ZG has already done that and is about to get a cow is no excuse for not doing it yourself
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the idea of a StringBuilder is to alter a String, it has to guess how much space to reserve for adding to the original text, and 0x10 is a good default

16 is of course 0x10 if you print it in decimal.
 
Siddhant Agarwal
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation. I did try it out before posting the last reply and it does increase the capacity to 20, when we pass "abcd" to the constructor while creating an instance of StringBuilder. However, if I do an append, it does not increase the capacity unless and until it is required.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!