Topics Index

Strings > StringBuffer >
Siva Nookala - 20 Feb 2017
StringBuffer class enables you to represent and manipulate sequence of characters. Unlike String class, StringBuffer class is mutable. That is, you can change the contents of a StringBuffer object. When you modify a string of StringBuffer class, you are not creating a new String object, but rather operating directly on the original string itself. For this reason, the StringBuffer class offers a different set of methods than the String class, all of which operate directly on the buffer that contains the string. The instances of StringBuffer hold the sequence of characters in some internal array of characters. The StringBuffer has a capacity which is the number of characters it can hold without requiring reallocation. When the length of characters in the StringBuffer overflow and exceed the capacity, the capacity is automatically increased. The following are the constructors of StringBuffer class:

//  To create an empty StringBuffer with a default initial capacity of 16 characters
StringBuffer sb = new StringBuffer();
//  To create a StringBuffer from a String
StringBuffer sb = new StringBuffer(“Merit Campus”);
//  To create an empty StringBuffer with an initial capacity of 100 characters
StringBuffer sb = new StringBuffer(100);
Now let us look at the following program and learn some of the methods of StringBuffer class :
class StringBufferBasics
    public static void main(String arg[])
        StringBuffer sb = new StringBuffer("Merit Campus");
        sb.setCharAt(5, '*');  //LINE A
        StringBuffer sb1 = new StringBuffer("C++,Html,C");
        sb1.replace(4, 8,"Java");    //LINE B
        StringBuffer sb2 = new StringBuffer("Merit");



In the above program, we initially created an instance of the StringBuffer class to hold the sequence of characters "Merit Campus". To replace any single character of the string, we can use setCharAt method which is shown in LINE A. To replace a sequence of characters, replace method can be used as shown in LINE B. To add characters at the end of the string, append method can be used and to insert characters in between the string insert method can be used. All these methods are available in different versions to enable us to add different types of data (float, int, double, char, boolean, String…). A version of insert is shown above, which inserts specified character at specified position.

  • Place the below code before LINE A and check the output. It should be Merit Campus online Java tutorial.
    sb.append(" online Java tutorial.");
  • Place the below code before LINE A and check the output.

    The output should be supmaC tireM. Since the reverse method reverses each word in the String.
  • Can use objects of StringBuffer class instead of String class, in situations where contents of the string change frequently.
  • The StringBuffer is thread-safe. There is a class by name StringBuilder Class In Java that has same methods as that of StringBuffer but it is not thread-safe. Use of StringBuilder is recommended over StringBuffer for better performance when thread-safety is not needed.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App