Friday, June 17, 2022

Check String Null or Empty in Java

In this post we’ll see what options are there to check if a String is null or empty in Java.

1. Using isEmpty() method in Java to check if String is empty. This method returns true if length() is 0. Note that isEmpty() method is added in Java 6.

public class StringLength {

 public static void main(String[] args) {
  String str1 = "Test";
  String str2 = "";
  String str3 = "   ";

  System.out.println("String str1 null or empty- " + isStringNullorEmpty(str1));
  System.out.println("String str2 null or empty- " + isStringNullorEmpty(str2));
  System.out.println("String str3 null or empty- " + isStringNullorEmpty(str3));
 }
 // Method to check whether passed String is null or empty
 private static boolean isStringNullorEmpty(String str){
  if(str == null || str.isEmpty()) {
   return true;
  }
  return false;
 }
}

Output

String str1 null or empty- false
String str2 null or empty- true
String str3 null or empty- false

Important points to note here-

  • Because of the short circuiting behavior of the conditional operators null should be checked first so that second part is not even checked if the passed String is null. That prevents the NullPointerException if isEmpty() method is called on a null String.
  • In the example str3 has only white spaces but length is not 0 so it is not considered empty String. In such scenario you can use strip() method to strip the spaces before using isEmpty() method or you can use isBlank() method Java 11 onward.
With strip() method to strip white spaces-
private static boolean isStringNullorEmpty(String str){
 if(str == null || str.strip().isEmpty()) {
  return true;
 }
 return false;
}

2. Using length() method in Java to check if String is empty, for empty string length should be 0. If String with whitespaces should be considered empty then use strip() method to strip spaces.

public class StringLength {

 public static void main(String[] args) {
  String str1 = "Test";
  String str2 = "";
  String str3 = null;

  System.out.println("String str1 null or empty- " + isStringNullorEmpty(str1));
  System.out.println("String str2 null or empty- " + isStringNullorEmpty(str2));
  System.out.println("String str3 null or empty- " + isStringNullorEmpty(str3));
 }
 // Method to check whether passed String is null or empty
 private static boolean isStringNullorEmpty(String str){
  if(str == null || str.length() == 0) {
   return true;
  }
  return false;
 }
}

Output

String str1 null or empty- false
String str2 null or empty- true
String str3 null or empty- true

3. Java 11 onward there is also isBlank() method to check if String is empty in Java. This method returns true if the string is empty or contains only white spaces, otherwise false.

public class StringLength {

 public static void main(String[] args) {
  String str1 = "Test";
  String str2 = "";
  String str3 = "   ";

  System.out.println("String str1 null or empty- " + isStringNullorEmpty(str1));
  System.out.println("String str2 null or empty- " + isStringNullorEmpty(str2));
  System.out.println("String str3 null or empty- " + isStringNullorEmpty(str3));
 }
 // Method to check whether passed String is null or empty
 private static boolean isStringNullorEmpty(String str){
  if(str == null || str.isBlank()) {
   return true;
  }
  return false;
 }
}

Output

String str1 null or empty- false
String str2 null or empty- true
String str3 null or empty- true

As you can see now str3 returns true even without using strip() method.

That's all for this topic Check String Null or Empty in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. String Pool in Java
  2. StringJoiner Class in Java With Examples
  3. Java String substring() Method - Getting Substring
  4. Converting String to int in Java
  5. Java String Interview Questions And Answers

You may also like-

  1. Package in Java
  2. How to Create Password Protected Zip File in Java
  3. CallableStatement Interface in Java-JDBC
  4. Method Reference in Java
  5. Java ThreadLocal Class With Examples
  6. Passing Arguments to getBean() Method in Spring
  7. Configuring DataSource in Spring Framework
  8. Python Installation on Windows

No comments:

Post a Comment