Java StringBuffer Class

In this tutorial you will learn about Java StringBuffer class and its important methods with examples.

StringBuffer in Java is used to create mutable strings. Here mutable means, we can modify or change the strings.

StringBuffer class is thread safe which means multiple threads can’t access the same object simultaneously.

Apart from StringBuffer, we can also make mutable strings using StringBuilder class. We will discuss about it in upcoming tutorials.

 

Java StringBuffer Constructors

StringBuffer class in Java provides following four constructors.

StringBuffer() – Creates an empty string with the default capacity of 16 characters.

StringBuffer(int size) – Creates an empty string with the specified capacity.

StringBuffer(String str) – Creates a string with an initial value contained by str. The total capacity of created string is the sum of length of initial value and 16 more characters.

StringBuffer(CharSequence chars) – Creates a string with an initial value contained by chars. It also reserves extra space for 16 more characters.

 

Note: When we do modification in a given string and if the length of new string exceeds the current capacity then the capacity is increased by (oldcapacity*2)+2.

Below I have discussed few important methods of StringBuffer class which are different from methods of String in Java.

 

Java StringBuffer Methods

StringBuffer length() and capacity() Methods

The length of string value present in StringBuffer object is calculated by length() method. The total capacity of an object is calculated by capacity() method. See below example code to understand it more clearly.

 

Syntax

 

Example

 

Output

StringBuffer length() and capacity() Methods

In above example str.length() gives output 5 because length of Hello is 5. While str.capacity() gives output 21 because space for 16 more characters is automatically added.

 

StringBuffer ensureCapacity() Method

This method ensures that the given capacity is the minimum to the current capacity. If the given capacity is more than the current capacity then the current capacity is increased by (oldcapacity*2)+2. For example if the current capacity is 20 then it will become (20*2)+2 = 42.

 

Syntax

 

Example

 

Output

StringBuffer ensureCapacity() Method

 

StringBuffer setLength() Method

As its name shows, it is used to set the length of string within an object of StringBuffer class. If the new length is less than the current length then the characters beyond the new length are lost.

Syntax

 

StringBuffer charAt() and setCharAt() Methods

The charAt() method is used to access a particular character in given string. It is same as we use for String class object. We can replace a particular character in a string with a new character using setCharAt() method.

 

Syntax

 

Example

 

Output

StringBuffer charAt() and setCharAt() Methods

StringBuffer append() Method

The append() method is used to add a string at the end of given string. It has several overloaded versions as given below.

 

Syntax

 

Example

 

Output

StringBuffer append() Method

StringBuffer insert() Method

The insert() method is used to insert a string in given string. Same as like append() method it also has various overloaded versions. We have to specify the index where we want to insert and the string to be inserted as arguments.

 

Syntax

 

Example

 

Output

StringBuffer insert() Method

StringBuffer reverse() Method

The characters within a StringBuffer object can be reversed using reverse() method.

 

Syntax

 

Example

 

Output

StringBuffer reverse() Method

StringBuffer replace() Method

The replace() method is used to replace a part of given string with another string. We have to just specify the startindex, endindex and the string with which we want to replace as arguments.

Note: Here the end index is one less than what we have specified in the argument i.e. endIndex – 1.

 

Syntax

 

Example

 

Output

StringBuffer replace() Method

StringBuffer delete() and deleteCharAt() Method

The delete() method is used to delete a sequence of characters from start to end index. The deleteCharAt() method is used to delete single character at specific index.

Note: Here the end index is one less than what we have specified in the argument i.e. endIndex – 1.

 

Syntax

 

Example

 

Output

StringBuffer delete() and deleteCharAt() Method

 

There are so many other methods of Java StringBuffer class. If you found anything incorrect or have any doubts regarding above tutorial then please comment below.

Leave a Reply

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