StringBuffer in Java

Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe.


Java StringBuffer Constructor

The StringBuffer class has the following four constructors.

1) StringBuffer(): The parameter less constructor that creates an empty String Buffer and provides the initial size of the buffer for 16 characters.

2) StringBuffer(int s): It is a parameterized constructor that builds an empty String Builder and provides the initial size (s) of the buffer specified in the argument.

4) StringBuffer(String st): Another parametrized constructor that makes a non-empty String Buffer using the content provided in the argument. It also allocates space for 16 more characters.

Java Program

The following code uses the above-mentioned constructors to create objects of the StringBuffer class.

FileName: StringBufferExample.java

// A Java program to demonstrate StringBuffer

Output:


Explanation: The last statement of the output shows the capacity as 34. The size of the string “Tutorial & Example” is 18, and room for 16 more characters is provided by the

 constructor making the total capacity as 18 + 16 = 34.

Java StringBuffer Class Methods
The StringBuffer class has a lot of inbuilt methods. A few of them are mentioned below.

Method NameDescription
public synchronized StringBuffer append(String str)Used to add string str at the end of the string on which the append() method is called. The append() method is overloaded with primitive data type like append(int), append(float), etc.
public synchronized StringBuffer replace(int start, int end, String s)Replaces the specified string from index start to index end using the string s.
public synchronized StringBuffer delete(int start, int end)Used to delete the character from the start index to the end index from the specified string.
public int capacity()Renders the capacity of the buffer
public synchronized StringBuffer reverse()Modifies the given string by reversing the order of its character
public void ensureCapacity(int capacity)Ensures that the minimum capacity of the buffer is always greater than or equal to the capacity specified in the parameter
public int length()Returns the total number of characters present in the string.
public String substring(int i)Return the substring whose ending is in the last character of the specified  substring, and the first character is the character positioned at the index i, which is mentioned in the argument.
public String substring(int sIndex, int eIndex)Returns the substring which starts from the index sIndex, and ends at eIndex.
public char charAt(int i)Returns the character present at the specified index i.


Let’s understand the methods one by one through Java programs.

The append() method

FileName: StringBufferMethodsExample.java

Output:

Explanation: It is evident that the append() method inserts the specified string at the end of the given string.
The charAt() method
FileName: StringBufferMethodsExample1.java

Output:

Explanation: In the program, the variable ch stores the character present at the index i.

The replace() method

FileName: StringBufferMethodsExample2.java

Output:

Explanation: The replace() method considers the indices just before the end index, starting from the start index. Thus, as per the above program, the replace() method considers indices from 9  to (11 – 1), i.e., 9 to 10. Thus, characters ‘r’ and ‘l’ are omitted, and the output confirms the same.

The delete() method

FileName: StringBufferMethodsExample3.java

Output:

Explanation: Similar to the replace() method, the delete() method considers the index, which is just before the specified ending index as its last index (endIndex – 1 in our case). The start index is startIndex. Hence, the characters ‘H’, ‘e’, ‘l’, ‘l’  from the given string get erased, which is evident by looking at the output.

The capacity() method

FileName: StringBufferMethodsExample4.java

Output:

Explanation: Similar to the StringBuilder class, in StringBuffer also, calling the parameter less constructor gives the buffer capacity 16.

The ensureCapacity() method

FileName: StringBufferMethodsExample5.java

Output:

Explanation: In the above program, the ensureCapacity() method takes minCapacity as 40 in its argument. Therefore, the current buffer capacity has to be greater than or equal to the value fetched by the variable minCapacity.

The reverse() method

FileName: StringBufferMethodsExample6.java

Output:

Explanation: In the program, the variable newSb stores the reversed string that is getting displayed in the output. The reverse() method comes in handy when one wants to check for palindromic strings.

The length() method

FileName: StringBufferMethodsExample7.java

Output:

Explanation: The length() method counts the total number of characters present in the input string.

The substring() method – I

FileName: StringBufferMethodsExample8.java

Output:

Explanation: In the above program, we have only specified the starting index of the substring. The last index of the substring automatically becomes the last index of the given string.

The substring() method – II

FileName: StringBufferMethodsExample9.java

Output:

Explanation: The substring() method used in the above program is another version that requires the ending index too. Thus, whatever is present between the starting and ending index gets printed on the console. Note that the starting and the ending index have to be within the range of the input string; otherwise, the StringIndexOutOfBoundsException is raised.

Pin It on Pinterest

Share This