String Buffer Constructor 

  • String Buffer defines these three constructors:
  • String Buffer( )
  • String Buffer(int size)
  • String Buffer(String str)

The default constructor (the one with no parameters) reserves room for 16 characters without reallocation ..The second version accepts an integer argument that explicitly sets the’size of the buffer. The third version accepts a String argument that sets the initial contents of the String Buffer object and reserves room for 16 more characters with String Buffer allocates room for 16 additional characters when nonspecific buffer length is requested, because reallocation is a costly prices:> in terms of time. Also, frequent reallocation can fragment memory. By allocating room for a few extra characters, String Buffer reduces the number of reallocation that take place.

length( ) and caplet( )

The current length of a Shining Buffer can be found via the lengthy ) method, while the total allocated capacity can be found through the capacity ) method. They have. the following general forms: . into length( ) ; capacity Here is an example: Here is the output of this program, which shows how String reserves extra space for additional manipulations: Since sub is initialized with the string “Hello” when it is created, its length is 5. Its capacity is 21 because room for is additional characters is automatically added, ensure capacity ( ) If you want to reallocate room for a certain number of characters after a String Buffer has been constructed, you can use censure Capacity( ) to set the size of the buffer, This is useful if you know in advance that you will be appending a large number of small strings to a String Buffer. ensure<;,capacity{ ) has this general for in void ensure Capacity(int capacity) Here, capacity specifies the size of the buffer.

[vfb id=1]

Share This