• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

a StringBuffer Question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i=...//?
String s="123456";
StringBuffer sb=new StringBuffer(s);
sb.setLength(i);
//sb.capacity()=?;
case 1: when i<=22,sb.capacity()=16+s.length=22;
//that's ok
case 2: when 23<=i<=46, sb.capacity=(oldcapacity+1)*2=46;
// that's ok
case 3: when i>=47,sb.capacity()=sb.length();
//that's ?? why??
How does sb.setLength() affect sb.capacity()?
who can tell me?
Thanks a lot!!!
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike-
I'd like to help you out -- but I don't understand the "pseudo-code" you wrote above -- can you re-write it as an actual runnable piece of code?
Also, check out the API for StringBuffer, it might help you out with the relationship between .capacity(), .setLength(), .ensureCapacity() and .lenth().
 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

case ONE:
when i<=22,sb.capacity()=16+s.length=22;
--that's ok,I know it.
case TWO:
when 23<=i<=46, sb.capacity=(oldcapacity+1)*2=46;
--that's ok,I know it too.
case THREE:
when i>=47,sb.capacity()=sb.length();
--that's ?? why??I don't know it.
How does sb.setLength() affect sb.capacity()?
I have read the API ,but did not find the answer.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a little test program:

Then ran this using:

The result was:

So it seems a simple rule for sb.capacity. Namely, start with an initial capacity of 21. Whenever i in sb.setLength(i) is more than the current capacity, then keep doubling the capacity until sb.capacity is larger than i
Hope this helps :-)
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding more to what Neil said
Try this piece of code !

o/p is
i: 0, sb.length(): 0, sb.capacity: 21
i: 100, sb.length(): 100, sb.capacity: 100
i: 200, sb.length(): 200, sb.capacity: 202
i: 300, sb.length(): 300, sb.capacity: 406
i: 400, sb.length(): 400, sb.capacity: 406
i: 500, sb.length(): 500, sb.capacity: 814
i: 600, sb.length(): 600, sb.capacity: 814
i: 700, sb.length(): 700, sb.capacity: 814
i: 800, sb.length(): 800, sb.capacity: 814
i: 900, sb.length(): 900, sb.capacity: 1630
Note the second line of output-
i: 100, sb.length(): 100, sb.capacity: 100
When length is changed to hundred, the capacity is also changed to the hundred & not just the double of oldCapacity+1.
The fact is that the smallest capacity increase is defined by the following:
newCapacity = (oldCapacity + 1) * 2.
But in case the length is increased, more than (oldCapacity + 1) * 2 in one shot than new capacity is made equal to the new length.
This is my understanding.. Pls correct if I am wrong !!
[ December 10, 2002: Message edited by: shweta mathur ]
[ December 10, 2002: Message edited by: shweta mathur ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, a little correction :
The initial capacity of StringBuffer is 16.
You initialize the StringBuffer with a String( "12345"). So the capacity of StringBuffer is 16 + 5 = 21.
Michael
 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


so, I still can't understand it.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Capacity growing rule is:
newCapacity = (currentCapacity + 1) * 2,
and also, in Sun's implementation of StringBuffer, the algorithm for StringBuffer.setLength(int newLength) is:
if (newLength > (currentLength + 1) * 2)
newCapacity = newLength;
thus,
1 int i=200 ;
2 String s="123456";
3 StringBuffer sb=new StringBuffer(s); //sb.length() is 6
4 sb.setLength(i); // 200 > (6 + 1) * 2
5 System.out.println(sb.capacity()); //print 200
But,
1 String s = "123456";
2 StringBuffer sb = new StringBuffer(s);
3 for(int i = 0; i <= 400 ; i++) {
4sb.setLength(i);
5System.out.println(i+","+sb.capacity());
6//when i=200, sb.capacity()=382;???
7 }
here, you see, sb.capacity() is 382 when i is in between 191 and 382, because, for example
200 < (191 + 1) * 2,
so the capacity will follow its own growing rule, that is,
sb.capacity() is 766 after currentLength becomes 383, one greater than currentCapacity.

======
Wish this helps, but I don't think it is a good idea for a good API to expose its detailed implementation :-)

-cb
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic