This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes StringBuffer doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringBuffer doubt" Watch "StringBuffer doubt" New topic
Author

StringBuffer doubt

Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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: 230
    
    2

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: 289

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: 247
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: 289

AbstractStringBuilder ? But it was StringBuffer.


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: 247
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: 289

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: 230
    
    2

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: 247
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: 247
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: 230
    
    2

Rameshwar Soni wrote:

See the comment above.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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: 230
    
    2

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: 247
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: 37874
    
  22
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: 37874
    
  22
Not a "beginning" question. Moving thread.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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: 11145
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
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: 247
Thanks fred and campbell
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: StringBuffer doubt
 
Similar Threads
StringBuffer
Wait question from K&B threads chapter
Crazy question
Doubt regarding StringBuffer
StringBuffer