Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

StringBuffer Capacity

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java");
System.out.println(sb.capacity());

}
}
The default capacity of a StringBuffer is 16.According to what I understand, when the StringBuffer is initialized to "java" ,the capacity should still be 16 as the length of String java is less than 16.
It prints the capaictey as 20.why so?Please expalin!!
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there
this is from the java docs:
StringBuffer
public StringBuffer(String str)
Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string. The initial capacity of the string buffer is 16 plus the length of the string argument.
Parameters:
str - the initial contents of the buffer.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If anyone can explain about StringBuffer capacity it would be great help for everybody.
I have seen in other discussion about the confusion for StringBuffer's capacity.
Thank u in advance
Rakesh
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer has 3 constructors
StringBuffer() has a capactity of 16.
StringBuffer(int capactity) sets it to the desired capacity.
StringBuffer(String s) sets capactity = 16 + s.length()
the length() method returns the length of the string (different from capactiy).
capacity() returns the capactity of the Sb.
ensureCapacity(minCapacity) - used to ensure the current capacitry is atleast as much as the mincapacity. If not it is set to the max(mincapacity,currentcapacity*2 +2)
The sb expands automatically if required.
When a string is appended capacity is increased(if req) to max(currentcapacity*2+2, req string length)
the setLength(int length) method truncates the string or adds null chars as required. The length is modified to the requested length but the capacity is left unaffected.

 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic