Java

Java Check if String is Null, Empty or Blank

In Java programming, there can be a requirement for the developer to locate and omit unnecessary values. For instance, tracing the values accumulating the memory and affecting the code flow. In such instances, checking if a string is “null”, “empty” or “blank” in Java assists in eliminating or modifying the contained entries in the code.

This write-up will discuss the approaches to check the string for “null”, “empty” or “blank” in Java.

How to Check if String is “Null”, “Empty” or “Blank” in Java?

To verify the string is null, empty, or blank, apply the “null” reserved keyword, the “isEmpty()” method, or the “isBlank()” method, respectively.

The “null” keyword checks if the value is “null”. The “isEmpty()” method checks whether a string is empty or not and returns a boolean outcome based on that and the “isBlank()” method returns true if the provided string is empty or accumulates only white spaces.

Note: The isEmpty()” and “isBlank()” methods return the corresponding outcome as a boolean value, i.e., “true/false”.

Example 1: Check if String is Null, Empty, or Blank in Java Using “if/else” Statement

In this example, the discussed approaches can be utilized in combination with the “if/else” statement to check for the null, empty, or blank string:

String string1 = null;
String string2 = "";
String string3 = "  ";
if (string1 == null) {
 System.out.println("The first string is null!");
} else {
   System.out.println("The first string is not null");
}
if (string2.isEmpty() == true) {
 System.out.println("The second string is empty!");
} else {
   System.out.println("The second string is not empty");
}
if (string3.isBlank() == true) {
 System.out.println("The third string is blank!");
} else {
   System.out.println("The third string is not blank");
}

 

In the above lines of code, apply the following steps:

  • Firstly, initialize the string with “null”, keep it “empty” and “blank”, respectively, and specify their data type, i.e., “String”.
  • In the next step, apply the “if/else” statement to check for the “null” string via the “null” keyword.
  • Now, associate the “isEmpty()” and “isBlank()” methods with the initialized strings to check if the corresponding string is empty or blank, respectively, and return the boolean value based on that.

Output

In the above output, it can be seen that the former condition is “true” in each of the cases since the string values are “null”, “empty” and “blank”, respectively.

Example 2: Check if String is Null, Empty, or Blank in Java Using a User-defined Function

In this particular example, a check can be applied to the string for the discussed conditions via the user-defined function:

public static void main(String[] args) {
String string1 = null;
String string2 = "";
String string3 = " ";
System.out.println("The first string is: " + isNullEmptyBlank(string1));
System.out.println("The second string is: " + isNullEmptyBlank(string2));
System.out.println("The third string is: " + isNullEmptyBlank(string3));
}
public static String isNullEmptyBlank(String string) {
 if (string == null) {
  return "NULL";
}
 else if(string.isEmpty()){
  return "EMPTY";
}
 else if(string.isBlank()){
  return "BLANK";
}
 else {return string;}}

 

According to this code block, perform the below-provided steps:

  • Likewise, initialize the strings in the same sequence, as discussed.
  • After that, invoke the function “isNullEmptyBlank()” by passing each of the initialized strings, as its argument one by one.
  • Now, define the function “isNullEmptyBlank()” and specify its return type as “String”.
  • The function parameter corresponds to the string that needs to be evaluated for the required conditions.
  • In its(function) definition, apply the “if/else” statements for each of the passed strings and return the corresponding string value based on that.

Output

This outcome implies that each of the passed strings is evaluated appropriately.

Conclusion

To check the string to be null, empty, or blank in Java, apply the “null” reserved keyword, the “isEmpty()” method, or the “isBlank()” method, respectively. This can be achieved by simply applying the discussed methods in the “if/else” statement or via the “user-defined” function. This blog demonstrated the approaches to checking for the string being null, empty, or blank.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.