Check if StringBuilder is empty in Java [2 ways]

In this post, I will be sharing how to check if StringBuilder is empty or not in Java. There are two ways to achieve our goal:

1. Using the StringBuilder class length() method

2. Using String class isEmpty() method

Read Also: Convert String to StringBuilder in Java

Let's dive deep into the topic:

Check if StringBuilder is empty in Java

1. Using the length() method of the StringBuilder class


1.1 Syntax


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

 public int length()


1.2 Logic


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

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


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


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


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

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


Output:
Non-Empty StringBuilder object
Empty StringBuilder object


What does null and empty StringBuilder mean?


Empty StringBuilder means that an object is there but no characters present in it whereas null means that there is no StringBuilder 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. To get more clarity on this, you can find the example below:

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

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


Output:
Empty StringBuilder length is: 0
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.StringBuilder.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 StringBuilder 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