Java Program to Clear the StringBuffer

Java Program to Clear the StringBuffer

In this example, we will learn to clear the string buffer using the delete() and setLength() method and creating a new StringBuffer object in Java.

Example 1: Java program to clear using StringBuffer using delete()

class Main {
  public static void main(String[] args) {

    // create a string buffer
    StringBuffer str = new StringBuffer();

    // add string to string buffer
    str.append("Java");
    str.append(" is");
    str.append(" popular.");
    System.out.println("StringBuffer: " + str);

    // clear the string
    // using delete()
    str.delete(0, str.length());
    System.out.println("Updated StringBuffer: " + str);
  }
}

Output

StringBuffer: Java is popular.
Updated StringBuffer: 

In the above example, we have used the delete() method of the StringBuffer class to clear the string buffer.

Here, the delete() method removes all the characters within the specified index numbers.


Example 2: Java Program to clear the StringBuffer using setLength() method

class Main {
  public static void main(String[] args) {

    // create a string buffer
    StringBuffer str = new StringBuffer();

    // add string to string buffer
    str.append("Java");
    str.append(" is");
    str.append(" awesome.");
    System.out.println("StringBuffer: " + str);

    // clear the string
    // using setLength()
    str.setLength(0);
    System.out.println("Updated StringBuffer: " + str);
  }
}

Output

StringBuffer: Java is awesome.
Updated StringBuffer

Here, the setLength() method changes the character sequences present in StringBuffer to a new character sequence. And, set the length of the new character sequence to 0.

Hence, the older character sequence is garbage collected.

Note: The setLength() method completely ignores the character sequence present in the string buffer. However, the delete() method accesses the character sequence and deletes it. Hence, setLength() is more faster than delete().


Example 3: Java Program to clear the StringBuffer by creating a new object

class Main {
  public static void main(String[] args) {

    // create a string buffer
    StringBuffer str = new StringBuffer();

    // add string to string buffer
    str.append("Java");
    str.append(" is");
    str.append(" awesome.");
    System.out.println("StringBuffer: " + str);

    // clear the string
    // using new
    // here new object is created and assigned to str
    str = new StringBuffer();
    System.out.println("Updated StringBuffer: " + str);
  }
}

Output

StringBuffer: Java is awesome.
Updated StringBuffer:

Here, new StringBuffer() creates a new string buffer object and assigns the previous variable to the new objects. In this case, the previous object will be there. But it won't be accessible so it will be garbage collected.

Since, every time instead of clearing the previous string buffer, a new string buffer is created. So it is less efficient in terms of performance.