Stringbuilder in Java

Posted on Updated on

StringBuilder is mutable sequence of characters which is used to create mutable string i.e. they can be modified or say their state can be changed unlike in case of immutable objects as string.

While performing various operation with String, the only thing bother is immutability of strings. This led to introduction of Stringbuilder and Stringbuffer. Here in blog we’ll be discussing about Stringbuilder.

Constructors of String Builder:

Constructors Description
StringBuilder() Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(int capacity) Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

capacity – the initial capacity.

Exception: NegativeArraySizeException – if the capacity argument is less than zero.

StringBuilder(String str) Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument.

str – the initial contents of the buffer.

Exception: NullPointerException – if str is null

StringBuilder(CharSequence seq) Constructs a string builder that contains the same characters as the specified CharSequence. The initial capacity of the string builder is 16 plus the length of the CharSequence argument.

seq – the sequence to copy.

Exception: NullPointerException – if seq is null

The principle operation of String builder are insert and append which are overloaded to accept any type of data. Insert method is used to insert data at any specified index however append method always add data at end of builder.

Methods:

Methods Description
public StringBuilder 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 StringBuilder append(char[] str) Appends the string representation of the char array argument to this sequence and thus increasing the size of string builder by the size of length of str array of data type char.

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

public StringBuilder 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 Builder as specified by minimumCapacity.
public StringBuilder 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 StringBuilder 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 StringBuilder 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 StringBuilder 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 StringBuilder 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 StringBuilder 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” .
Key Point:
  1. It is faster to operate as multiple thread can access it at a time.
  2. It is not thread safe
  3. Doesn’t guarantee synchronization.
Size of Stringbuilder:

Default size of string builder is 16 which grows dynamically. The increment is done by below mentioned rule:

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.