Java Program to Clear the StringBuffer

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: Clear the StringBuffer using setLength()

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: 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.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges