Combine Null and Empty Checks
In the previous step, we learned how to check if a string is null. However, in many real-world scenarios, you might also need to check if a string is empty (has zero characters) or contains only whitespace. A string that is null, empty, or contains only whitespace is often considered "blank" or "effectively empty".
Checking for both null and empty strings is a common requirement. You can combine these checks using the logical OR operator (||).
Let's modify our previous program to include checks for both null and empty strings.
-
Open the NullCheck.java file in the WebIDE editor.
-
Modify the main method to include an empty string and combine the checks. Replace the existing main method with the following code:
public class NullCheck {
public static void main(String[] args) {
String myString1 = "Hello";
String myString2 = null;
String myString3 = ""; // This is an empty string
String myString4 = " "; // This string contains only whitespace
System.out.println("Checking myString1:");
if (myString1 == null || myString1.isEmpty()) {
System.out.println("myString1 is null or empty");
} else {
System.out.println("myString1 is not null and not empty");
}
System.out.println("\nChecking myString2:");
if (myString2 == null || myString2.isEmpty()) {
System.out.println("myString2 is null or empty");
} else {
System.out.println("myString2 is not null and not empty");
}
System.out.println("\nChecking myString3:");
if (myString3 == null || myString3.isEmpty()) {
System.out.println("myString3 is null or empty");
} else {
System.out.println("myString3 is not null and not empty");
}
System.out.println("\nChecking myString4:");
// Note: isEmpty() does not check for whitespace
if (myString4 == null || myString4.isEmpty()) {
System.out.println("myString4 is null or empty");
} else {
System.out.println("myString4 is not null and not empty");
}
}
}
In this updated code:
- We added
myString4 which contains only whitespace.
- We use the condition
myString == null || myString.isEmpty() to check if a string is either null or empty. The isEmpty() method is a built-in String method that returns true if the string has a length of 0.
- Important: The
isEmpty() method can only be called on a non-null string. If you try to call isEmpty() on a null string, you will get a NullPointerException. Therefore, it is crucial to check for null first using myString == null before calling myString.isEmpty(). The logical OR operator (||) is "short-circuiting", meaning if the first condition (myString == null) is true, the second condition (myString.isEmpty()) is not evaluated, preventing the NullPointerException.
-
Save the NullCheck.java file.
-
Compile the modified program in the Terminal:
javac NullCheck.java
-
Run the compiled program:
java NullCheck
You should see output similar to this:
Checking myString1:
myString1 is not null and not empty
Checking myString2:
myString2 is null or empty
Checking myString3:
myString3 is null or empty
Checking myString4:
myString4 is not null and not empty
Notice that myString4 (which contains only whitespace) is considered "not null and not empty" by this check. If you need to also consider strings with only whitespace as "blank", you can use the isBlank() method (available since Java 11).
Let's quickly update the code to use isBlank() for myString4:
-
Modify the check for myString4 in NullCheck.java:
// ... (previous code) ...
System.out.println("\nChecking myString4 with isBlank():");
if (myString4 == null || myString4.isBlank()) {
System.out.println("myString4 is null or blank");
} else {
System.out.println("myString4 is not null and not blank");
}
}
}
-
Save the file, compile, and run again:
javac NullCheck.java
java NullCheck
The output for myString4 should now be:
Checking myString4 with isBlank():
myString4 is null or blank
This demonstrates how to combine checks for null and empty/blank strings, which is a very common pattern in Java programming.