Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java StringBuffer Class

public final class StringBuffer
extends Object
implements Serializable, CharSequence

  • • Like String class, StringBuffer is another class specifically used to represent and manipulate strings and which can be modified.
  • • That means 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.
  • StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously, So it is safe and will result in an order.
  • StringBuffer defines following constructors,

    1.StringBuffer():creates an empty string buffer with the initial capacity of 16.
    2.StringBuffer(String str): creates a string buffer with the specified string.
    3.StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.


difference between String and StringBuffer

class test {
public static void main(String[] args)
	String str = "Java";
	System.out.println(str);    //output: Java

	StringBuffer str = new StringBuffer("Java");
	System.out.println(str);    //outpuut: JavaRace


Output is such because String objects are immutable objects. Hence, if we concatenate on the same String object, it won't be changed(Output: Java). But StringBuffer creates mutable objects. Hence, it can be changed(Output: JavaRace). Important methods of StringBuffer class
The following methods are some most commonly used methods of StringBuffer class.


This method will concatenate the string representation of any type of data to the end of the invoking StringBuffer object. append() method has several overloaded forms.

StringBuffer append(String str)

StringBuffer append(int n)

StringBuffer append(Object obj)

The string representation of each parameter is appended to StringBuffer object.

StringBuffer str = new StringBuffer("SILAN");


This method inserts one string into another. Here are few forms of insert() method.

StringBuffer insert(int index,String str)

StringBuffer insert(int index,int num)

StringBuffer insert(int index,Object obj)

Here the first parameter gives the index at which position the string will be inserted and string representation of second parameter is inserted into StringBuffer object.

StringBuffer str = new StringBuffer("Java");

str.insert(4, 12345);


Output : Java12345


This method reverses the characters within a StringBuffer object.

StringBuffer str = new StringBuffer("JavaRace");



Output : ecaRavaJ


This method replaces the string from specified start index to the end index.

StringBuffer str = new StringBuffer("Hello World");

str.replace( 6, 11, "Java");


Output : Hello java


This method returns the current capacity of StringBuffer object.

StringBuffer str = new StringBuffer();

System.out.println( str.capacity() );

Output : 16

Note: Empty constructor reserves space for 16 characters. Therefore the output is 16.


This method is used to ensure minimum capacity of StringBuffer object.
If the argument of the ensureCapacity() method is less than the existing capacity, then there will be no change in existing capacity.
If the argument of the ensureCapacity() method is greater than the existing capacity, then there will be change in the current capacity using following rule: newCapacity = (currentCapacity*2) + 2.

StringBuffer str = new StringBuffer();
System.out.println( str.capicity());  //Output: 16
str.ensureCapacity(30);               //greater than the existing capacity
System.out.println( str.capacity());  //Output: 34 (currentcapacity*2) + 2.)
i.e(16*2)+2 = 34.

About the Author

Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc