How to Check if String is Empty Java

How to Check if String is Empty Java | Java provides many built-in functions to check whether the string is empty or not. To check whether the string is null or empty we can use the “==” operator or isEmpty() or isBlank() or length() methods and also we can create our own methods.

See the below example to Check If the String Is Empty Java
1. String = ""
The string is empty.
2. String = “Know Program”
The string is not empty.

Check if String is Empty Java

Here we are using the isNullEmpty() method which is a user-defined method to check If the string is null or empty or contains only whitespace or contains data. It covers most cases.

Note that the isBlank() method was introduced in Java 11, so if you are working with a prior JDK version then you should use string.trim().isEmpty(). The trim() method removes whitespace from the given string.

public class Main {
   public static void main(String[] args) {
      String string1 = null;
      String string2 = "";
      String string3 = "  ";
      String string4 = "Know Program";

      System.out.println("String1: " + isNullEmpty(string1));
      System.out.println("String2: " + isNullEmpty(string2));
      System.out.println("String3: " + isNullEmpty(string3));
      System.out.println("String4: " + isNullEmpty(string4));
   }

   public static String isNullEmpty(String string) {
      if (string == null) {
         return "NULL";
      } else if (string.isEmpty()) {
         return "EMPTY";
      } else if (string.isBlank()) {
         return "contains only whitespaces";
      } else {
         return "contains data";
      }
   }
}

Output:-

String1: NULL
String2: EMPTY
String3: contains only whitespaces
String4: contains data

How to Check String is Empty in Java using isEmpty()

Here we use the isEmpty() method to check if the string is empty Java. The isEmpty() method returns true if, and only if, length() is 0. Since isEmpty() is an instance method therefore we can’t call it on a string containing null, else we will get NullPointerException.

Check if String is Empty Java using isEmpty()

public class Main {
   public static void main(String[] args) {
      String string1 = null;
      String string2 = "";
      String string3 = "  ";
      String string4 = "Know Program";

      // System.out.println("String1: " + string1.isEmpty()); 
      // Error
      // We can't call instance method on null 
      
      System.out.println("String2: " + string2.isEmpty());
      System.out.println("String3: " + string3.isEmpty());
      System.out.println("String4: " + string4.isEmpty());
   }
}

Output:-

String2: true
String3: false
String4: false

How to Check String Empty in Java using length()

In the above program, we had seen that the isEmpty() method internally checks the length of the string therefore we can directly call the length() method. We were not able to call isEmpty() when the string was null, but here we will solve that problem.

Check if String is Empty Java using length()

public class Main {
   public static void main(String[] args) {
      String string1 = "Java Programming Language";
      String string2 = "";

      boolean result = (string1 == null || string1.length() == 0);
      System.out.println("String1 is null or empty?: " + result);

      if (string2 == null || string2.length() == 0) {
         System.out.println("String2 is either null or empty.");
      } else {
         System.out.println("String2 is neither null nor empty.");
      }
   }
}

Output:-

String1 is null or empty?: false
String2 is either null or empty.

Check if String is Empty Java using isBlank()

The isBlank() method was introduced in JDK 11 version and it returns true if the string is empty or contains only white space codepoints, otherwise false.

Check if String is Empty Java using isBlank()

public class Main {
   public static void main(String[] args) {
      String string1 = "";
      String string2 = "Know Program";

      boolean result = (string1 == null || string1.isBlank());
      System.out.println("String1 is null or blank?: " + result);

      if (string2 == null || string2.isBlank()) {
         System.out.println("String2 is either null or blank.");
      } else {
         System.out.println("String2 is neither null nor blank.");
      }
   }
}

Output:-

String1 is null or blank?: true
String2 is neither null nor blank.

Prior to the JDK 11 version, the same thing can be done by using string.trim().isEmpty() method. The trim() method removes whitespace. If the string contains only whitespace then after applying trim() resultant string contains an empty string, and isEmpty() returns true in that case. Let us see it through the example:-

Check if String is Empty Java using trim() & isEmpty()

public class Main {
   public static void main(String[] args) {
      String string1 = "";
      String string2 = "Know Program";

      boolean result = (string1 == null || string1.trim().isEmpty());
      System.out.println("String1 is null or blank?: " + result);

      if (string2 == null || string2.trim().isEmpty()) {
         System.out.println("String2 is either null or blank.");
      } else {
         System.out.println("String2 is neither null nor blank.");
      }
   }
}

Output:-

String1 is null or blank?: true
String2 is neither null nor blank.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *