Check if StringBuffer is empty in Java [2 ways]

In this post, I will be sharing how to check if StringBuffer is empty or not in Java. I have already shared the difference between String, StringBuilder, and StringBuffer in detail here. There are two ways to achieve our goal:

1. Using the StringBuffer class length() method

2. Using isEmpty() method of String class

Read Also: Convert String to StringBuffer in Java

Let's dive deep into the topic:

Check if StringBuffer is empty in Java

1. Using the StringBuffer class length() method


1.1 Syntax


According to Oracle docs, the StringBuffer class in Java contains a length() method that returns the character count. The length() method syntax is given below:

 public int length()


1.2 Logic


We can easily check if the StringBuffer is empty or not by using the length() method. If the length of the StringBuffer object is zero then it is empty otherwise not. Please find the code below:

 public class StringBufferEmptyCheck {
    public static void main(String args[]) {
      // Creating the StringBuffer Object    
      StringBuffer sb = new StringBuffer("Alive is Awesome");
      
      // Checking StringBuffer object is empty or not using length() method
      if (sb.length() == 0) {
          System.out.println(sb.toString() + " StringBuffer object is Empty ");
      }
      else {
          System.out.println(sb.toString() + " StringBuffer object is Not-empty");
      }
     
      // Clearing content of the StringBuffer object
      sb.setLength(0);
      
      if(sb.length() == 0) {
          System.out.println("Empty StringBuffer object");
      }
      else {
          System.out.println("Non-Empty StringBuffer object");
      }
    }
}


Output:
Alive is Awesome StringBuffer object is Not-empty
Empty StringBuffer object


2. Using the isEmpty() method of the String class


We can check whether StringBuffer is empty or not by using the isEmpty() method of the String class. For this, first, we need to convert the StringBuffer object to String using the toString() method as shown below in the example:

 public class StringBufferEmptyCheck2 {
    public static void main(String args[]) {
      // Creating the StringBuffer Object    
      StringBuffer sb1 = new StringBuffer("Be in Present");
      
      // Checking StringBuffer object is empty or not using isEmpty() method
      if (sb1.toString().isEmpty()) {
          System.out.println("Empty StringBuffer object");
      }
      else {
          System.out.println("Non-Empty StringBuffer object");
      }
     
      // Clearing content of the StringBuffer object
      sb1.setLength(0);
      
      if(sb1.toString().isEmpty()) {
          System.out.println("Empty StringBuffer object");
      }
      else {
          System.out.println("Non-Empty StringBuffer object");
      }
    }
}


Output:
Non-Empty StringBuffer object
Empty StringBuffer object


What does null and empty StringBuffer mean?


Empty StringBuffer means that an object is there but no characters present in it whereas null means that there is no StringBuffer object at all in the heap memory for the given reference variable. If we call a method on the null, then it will throw NullPointerException as shown below in the example:

 public class StringBufferEmptyCheck3 {
    public static void main(String args[]) {
        /* Below line constructs an empty StringBuffer Object
        with no characters in it */
        StringBuffer sb1 = new StringBuffer();
        System.out.println("Empty StringBuffer length is:  " + sb1.length());

        // Assigning null to the StringBuffer object
        sb1 = null;
        System.out.println(sb1.length());
    }
}


Output:
Empty StringBuffer length is: 0
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.StringBuffer.length()" because "<local1>" is null


That's all for today. Please mention in the comments in case you have any questions related to how to check if StringBuffer is empty or not in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry