StringBuffer class in Java

In this example you will learn about StringBuffer class. This example explains how you can use functions provided by the StringBuffer class like append, insert, reverse, setCharAt, charAt, length, deleteCharAt, substring, delete, capacity etc. to manipulate the string operation in your program.

The StringBuffer class is used to represent characters that can be modified. This is simply used for concatenation or manipulation of the strings. 

StringBuffer is mainly used for the dynamic string concatenation which enhances the performance. A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. There are some functions used in the given example. All the functions have been explained below with example :


This is the append() function used for the concatenate the string in string buffer. This is better to use for dynamic string concatenation. This function works like a simple string concatenation such as : String str = str + “added string”;.


This is the insert() function used to insert any string or character at the specified position in the given string.


This is the reverse() function used to reverse the string present in string buffer.


This is the setCharAt() function which is used to set the specified character in buffered string at the specified position of the string in which you have to set the given character.


This is the charAt() function which is used to get the character at the specified position of the given string.


This is the substring() function which is used to get the sub string from the buffered string from the initial position to end position (these are fixed by you in the program).


This is the deleteCharAt() function which is used to delete the specific character from the buffered string by mentioning that’s position in the string.


This is the length() function is used to finding the length of the buffered string.


This is the delete() function is used to delete multiple character at once from n position to m position (n and m are will be fixed by you.) in the buffered string.


This is the capacity() function is used to know about the current characters kept which is displayed like : number of characters + 6.

Code for the program :



public class bufferString {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(;
String all_str;
try {
System.out.print(“Enter Your Name:  “);
all_str = in.readLine();
all_str += “, This is the example of BufferString class and it’s functions.”;

// Create a object of StringBuffer class
StringBuffer strbuffer = new StringBuffer();
strbuffer.delete(0, all_str.length());

// append()
strbuffer.append(“Hello, My Name is”);
strbuffer.append(” Rahul”);
// print Hello, My Name is Rahul

// insert()
strbuffer.insert(5, ” Java Programme “);
// print Hello, My Name is Java Programme Rahul

// reverse()
System.out.print(“Please Check Reversed string : “);
// print luhal emmargorp avaJ si emaN yM ,olleH
// print Hello, My Name is Java Programme Rahul

// setCharAt()
strbuffer.setCharAt(5, ‘ ‘);
// prit Hello My Name is Java Programme Rahul

// charAt()
System.out.print(“Character at 8th position : “);
// print M

// substring()
System.out.print(“Substring from position 8 to 12 : “);
System.out.println(strbuffer.substring(8, 12));
// print y Nam

// deleteCharAt()
// print Hell My Name is Java Programme Rahul

// capacity()
System.out.print(“Capacity of object : “);

// delete() and length()
strbuffer.delete(6, strbuffer.length());
// no anything
} catch (StringIndexOutOfBoundsException e) {

Enjoy Code.