Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuilder , how is increased capacity?  RSS feed

 
Gheyath Nasani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



and is there any equation for  how to ''Capacity increased '' ?
 
Otto Zeimer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking how capacity is increased?  every time you append anything to it.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
    --------------------------
     
    Campbell Ritchie
    Marshal
    Posts: 55734
    163
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
    • 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: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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
    • Quote
    • Report post to moderator
    Thanks for all , i found answer in JavaDocs
     
    Gheyath Nasani
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • 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--
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!