Stringbuffer in java

Posted on Updated on

StringBuffer is used to create mutable string object. StringBuffer is similar to string except that it is mutable. Purpose of StringBuilder is to over the problem of immutability.

Here we’ll be discussing about methods and features of string buffer.

Constructors of String Buffer-

Constructors

Description

StringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
StringBuffer(int capacity)
It constructs a string buffer with no characters in it and with the specified initial capacity.

Parameters:

capacity – the initial capacity.

Throws:

NegativeArraySizeException – if the capacity argument is less than 0.

StringBuffer(String str)
It constructs a string buffer initialized to the contents of the specified 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.

Throws:

NullPointerException – if str is null

StringBuffer(CharSequence seq)
It constructs a string buffer that contains the same characters as the specified CharSequence. The initial capacity of the string buffer is 16 plus the length of the CharSequence argument.

If the length of the specified CharSequence is less than or equal to zero, then an empty buffer of capacity 16 is returned.

Parameters:

seq – the sequence to copy.

Throws:

NullPointerException – if seq is null

Methods of Sting Buffer:

 

public StringBuffer append(char c) Appends the string representation of the char argument to this sequence and thus increasing the size by 1.

char c- “c” is character type data variable

public StringBuffer append(char[] str) Appends the string representation of the char array argument to this sequence and thus increasing the size of string buffer by the size of length of str array of data type char.

char[] str- str is an array of data type char.

public StringBuffer append(char[] str, int offset, int len) Appends the string representation of a subarray of the char array argument to this sequence. Characters from str, starting with index specified with offset and number of characters specified by len is appended to the sequence and thus increasing the length by len.

char[] str – str is an array of data type char.

int offset- offset is starting index from which characters will be picked up.

int len- len specifies the number of characters which will be appended to the sequence

public char charAt(int index) Returns the char value in this sequence at the specified index. The first char value is at index 0, the next at index 1, and so on, as in array indexing.
public int capacity() Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.
public void ensureCapacity(int minimumCapacity) Ensures the minimum capacity of String Buffer as specified by minimumCapacity
public StringBuffer delete(int start, int end) Removes the characters in substring specified by start and end index. The characters which are removed are from starting index: “start” and till terminal index: “end-1” or  to the end of the sequence if no such character exists. If start and end are equal then no changes are made.

Exception:

StringIndexOutOfBoundsException – if start is negative, greater than length(), or greater than end.

public StringBuffer deleteCharAt(int index) Delete the character specified at given index and thus reducing the size of string by 1.

Exception:

StringIndexOutOfBoundsException – if the index is negative or greater than or equal to length().

public StringBuffer insert(int offset,

char c)

Insert the character specified by char c at the starting index position specified by offset. This method is overload with different parameters as such: insert(int offset, boolean b), insert(int offset, char[] str), insert(int index, char[] str, int offset, int len).
public StringBuffer insert(int index, char[] str, int offset, int len) Insert the characters specified by str array of data type char with starting index specified by offset and number of characters specified by len.
public StringBuffer replace(int start, int end, String str) Replaces the characters from string on which it’ll be called with starting index: start and terminal index: end by string specified by str.
public StringBuffer reverse() Reverse the string by replacing the specified string with reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation.

Example “\uDC00\uD800” produces “\uD800\uDC00” .

Features of Stringbuffer-

  1. It is thread safe, i.e. it can be accessed by multiple threads.
  2. It’s methods are synchronized.
  3. Principle operation are append and insert like in case of String Builder, which are overloaded to accept any type of data.
  4. It is not fast as compared with String builder.

Size of Stringbuffer:

String buffer has initial capacity of 16, which increase dynamically by below explained formula-
Intial size: oldcapacity;

Incremented size:  [(2*oldcapacity)+2];

i.e. if the initial size is 16 then incremented size will be [(2*16)+2]= 34 and so on.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.