Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Why the initial capacity of StringBuffer is 16?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though it is given in API doc but still i have doubt that why it is given like that? Why the initial capacity of StringBuffer is not zero?


 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While an initial capacity of 16 seems arbitrary (I'm sure its not), an inital capacity of 0 would be very inefficient.
The capacity determines the size of the internal char array, so if that char array would have room for 0 elements you could put nothing in it.
So everytime you'd append a String to a newly contructed StringBuffer / StringBuilder, it would first have to create a new char array of sufficient size - the length of the String.


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the default capacity is trying to get a balance between not allocating more space than is necessary, and avoiding reallocating space when the buffer gets too big. The best possible value is going to depend on what real usage patterns look like.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly, and that's why StringBuilder and StringBuffer provide an overloaded constructor and a method called ensureCapacity() that let you tweak the initial capacity and subsequent capacity increases to your specific needs.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

siva chaitanya wrote:Why the initial capacity of StringBuffer is 16?



Truly, the initial capacity of the string buffer is 16 plus the length of the string argument.

Regards,
Dan
 
Grow a forest with seedballs and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic