Win a copy of Terraform in Action this week in the Cloud 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

StringBuffer length

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain the following from Sun's Java Tutorial:
Question : What is the initial capacity of the following string buffer?
StringBuffer sb = new StringBuffer("Able was I ere I saw Elba.");
Answer : It's the length of the initial string + 16: 26 + 16 = 42.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API:
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.
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roopa,
The following paragraph from the API doc, explains the length of the StringBuffer created.


From the Java 2 API DOC...

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.


Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited August 30, 2001).]
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is 16 the default capacity of the string buffer.

Originally posted by Thomas Paul:
From the API:
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.


 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roopa Bagur:
Is 16 the default capacity of the string buffer.

If you expect to pass the certification test then you really need to be able to read the API:
StringBuffer
public StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People perceive things in a different way... this forum is for less experienced people to learn from the experienced ones in order to help them pass their certification...
So do not tell what I should be able to do.. just tell me politely what to do & how to go about doing that...

Originally posted by Thomas Paul:

Originally posted by Roopa Bagur:
[b]Is 16 the default capacity of the string buffer.

If you expect to pass the certification test then you really need to be able to read the API:
StringBuffer
public StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
[/B]


 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roopa Bagur:
People perceive things in a different way... this forum is for less experienced people to learn from the experienced ones in order to help them pass their certification...
So do not tell what I should be able to do.. just tell me politely what to do & how to go about doing that...

Huh? Then I will politely say that if you want to be able to pass the certification then you must be able to read and understand the API. The way to go about doing it is to download it from the sun website and use it as a resource.
Based on your answer you seemed to have read a hostility into my response which simply wasn't there.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic