• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuilder , how is increased capacity?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



and is there any equation for  how to ''Capacity increased '' ?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking how capacity is increased?  every time you append anything to it.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gheyath Nasani wrote:and is there any equation for  how to ''Capacity increased '' ?

I'm using jdk1.8.0_91. I don't know how it is internally implemented in other version, may vary.

I don't think you need to know how internally It is implemented because It may change in next implementations.

Since you have asked
You have to add src.zip source to your IDE to see this internal source code. It is available in your jdk installation directory.

Actually internally It uses an array of char type to store the contents of this String.
  • Default capacity of non-argument constructor of StringBuilder is 16
  • capacity of parameterized constructor of StringBuilder is 16 + String length  Or 16 + CharSequence length


  • For appending It uses following formula
    Initially count is 0
    where count is current number of chars in that array of char i.e. char[] value  and len is the length of String we passed as an argument to append() method.
    value.length is length of array of char i.e. length of value[]




    Example:
    Output:
    ********Method One Starts********
    Start:
    Length: 0
    Capacity: 16
    --------------------------
    After adding String of length 17 :
    Length: 17
    Capacity: 34
    --------------------------
    After adding String of length 18 more:
    Length: 35
    Capacity: 70
    ********Method Two Starts********
    Start:
    Length: 18
    Capacity: 34
    --------------------------
    After adding String of length 17 more:
    Length: 35
    Capacity: 70
    --------------------------
     
    Marshal
    Posts: 79405
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Go through the StringBuilder documentation and you will find at least two ways to set the capacity of your StringBullder.
     
    Otto Zeimer
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ganesh,  your answer is great, but I believe you over complicated your answer .  Imho OP wanted a quick answer for his simplistic question. Otherwise he would have search google instead posting here
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Otto Zeimer wrote:Ganesh,  your answer is great, but I believe you over complicated your answer .  Imho OP wanted a quick answer for his simplistic question. Otherwise he would have search google instead posting here

    Looking at OP's example, OP intentionally appended String of length 17 means OP knows default capacity of StringBuilder() is 16 and after printing capacity got 34 output so wanted to know how It increments so asked is there any equation for  how to ''Capacity increased '' ?

    To know how It increments you need to go through this source code that's what I think.

    Or may be I misunderstood OP as you said
     
    Gheyath Nasani
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for all , i found answer in JavaDocs
     
    Gheyath Nasani
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Gheyath Nasani wrote: i found answer in JavaDocs

    This site can’t be reached !

    this will be work http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#capacity--
     
    reply
      Bookmark Topic Watch Topic
    • New Topic