| Author |
StringBuffer doubt
|
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
I was reading about StringBuffer class and i came to know that the default capacity when using the constructor with no parameters is 16. i.e.
Capacity of s is 16 characters. But when we add the 17th character then the capacity increases by (2 * previous capacity + 2) i.e. (2 * 16 + 2)=34. Therefore after adding the 17th character the capacity is 34.
Similarly when 35th character is added the capacity would be (2 * 34 + 2)=70.
But when i tried the below code i got the results for object sb1 correct but why not for object sb2. Thanks in advance.
|
 |
Raymond Tong
Ranch Hand
Joined: Aug 15, 2010
Posts: 156
|
|
You may have missed part of the code which I have attached below
If needed capacity (minimumCapacity) > calculated new capacity (newCapacity),
it would set the final new capacity to minimumCapacity.
i.e. 35 > 34, set the capacity to 35
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
Hi Rameshwar
i think the capacity calculation is diffrent when i saw the code for AbstractStringBuilder the Capcity will be calculated like this
Where insert or any append code call this method
where str is 35chars, so len = 35
and count =0, newcount =35
when call expandCapacity(35)
newCapacity = (0+1)*2 = 2
35>2
newCapacity =35
value.length = 35
I think this will solve our doudt
|
-Chiru
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
Thanks Raymond and Chiranjeevi for your replies. But i haven't yet understood, sorry i am not that good in Java.
Chiranjeevi Kanthraj wrote:
i think the capacity calculation is diffrent when i saw the code for AbstractStringBuilder the Capcity will be calculated like this
AbstractStringBuilder ? But it was StringBuffer.
Chiranjeevi Kanthraj wrote:
What is value.length ?
Chiranjeevi Kanthraj wrote:
Where insert or any append code call this method
where str is 35chars, so len = 35
and count =0, newcount =35
when call expandCapacity(35)
newCapacity = (0+1)*2 = 2
35>2
newCapacity =35
value.length = 35
[code=java]
How count=0 ?
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
AbstractStringBuilder is the super class of the StringBuffer
What is value.length ?
value is the char[] which holds the chars of the StringBuffer
How count=0
if you see the insert method implementation you will come to know. count is the inital length of the value.
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
Thanks for your reply. But when i did the calculation using the method which you gave, sometimes the calculation comes right and sometimes not.
For the below code the calculation is incorrect
Here you can see after adding 35 characters the capacity is 70
Whereas for the below code
Here after 35 characters the capacity is 35 which is correct according to your method.
Why that difference?
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
Sorry for the last reply count is not length of the value means not 0, its 17 or length of the String which we inserting
|
 |
Raymond Tong
Ranch Hand
Joined: Aug 15, 2010
Posts: 156
|
|
To simplify the logic in few words,
when doubling the current size (+2) is enough for storage, expand it with this size.
Otherwise, add the size with the additional String's length.
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
I think its getting really though for ME. I am not getting my calculation correctly.
My question is Why capacity is 70 when 35 characters were added
Why capacity is 35 when 35 characters were added
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
Raymond Tong wrote:
when doubling the current size (+2) is enough for storage, expand it with this size.
How would we know that ? I mean to say 35 characters could be easily stored in an capacity of 35 or its double 70. ??
|
 |
Raymond Tong
Ranch Hand
Joined: Aug 15, 2010
Posts: 156
|
|
Rameshwar Soni wrote:
See the comment above.
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
Hi Raymond, thanks for your reply. I think you might be getting furious on me because i still have doubt because i tried the below code and applied your way of calculation
and i got wrong answer
i am really sorry.
|
 |
Raymond Tong
Ranch Hand
Joined: Aug 15, 2010
Posts: 156
|
|
Rameshwar Soni wrote:
I didn't make the calculation very clear.
Check the detailed calculation above.
Why would you interested about the logic of StringBuffer?
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
Thank you very much Raymond Tong for your reply. Your detailed calculation was very helpful.
This calculation was never there in Java documentation, it was your calculation which helped me.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
Rameshwar Soni wrote: . . . This calculation was never there in Java documentation, . . .
You don't need that calculation. You only need to know the public interface of a class, not its implementation details. You can easily work out that the largest array possible is 2147483647 code points, but you will probably run out of JVM memory before you get that large.
And why does everybody use StringBuffer when StringBuilder gives better performance? By the way: that last StringBuilder link will give you its default capacity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
Not a "beginning" question. Moving thread.
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
Campbell Ritchie wrote:You only need to know the public interface of a class, not its implementation details.
What do you mean by that? Can you give an example.
Campbell Ritchie wrote: You can easily work out that the largest array possible is 2147483647 code points, but you will probably run out of JVM memory before you get that large.
Yes that was great, i never knew that. So basically what would be the largest size array that we can make? Does that differ as per the JVM ?
Thanks for your reply.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
Rameshwar Soni wrote:
Campbell Ritchie wrote:You only need to know the public interface of a class, not its implementation details.
What do you mean by that? Can you give an example.
If I get a collection reference, and want the third item in it, all I should need to know is to call:
myObject = myCollection.getItem(3);
I don't need to know if the collection is an array and has direct access to the third item, or if it is a linked list and they jump from node to node to node, or a map and they use the 3 as the key to pull it out.
All I need to know is that I call the getItem, pass it a positive integer, and it returns the item I want.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
Rameshwar Soni wrote: . . what would be the largest size array that we can make? Does that differ as per the JVM ? . . .
Try here, and you can see the sort of thing you can try. It probably differs with the amount of memory available, which you can alter with the options for the java tool when you start the JVM.
|
 |
Rameshwar Soni
Ranch Hand
Joined: Feb 03, 2011
Posts: 246
|
|
|
Thanks fred and campbell
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
You're welcome
|
 |
 |
|
|
subject: StringBuffer doubt
|
|
|