Combine Null and Empty Checks
In the previous step, we learned how to check if a variable is null
using the ==
operator. However, in Java, especially when dealing with String
objects, it's often necessary to check not only if a variable is null
but also if it's "empty". An empty string is a String
object that exists but contains no characters (e.g., ""
). A null
string, on the other hand, means the variable doesn't refer to any String
object at all.
Trying to call methods like isEmpty()
or length()
on a null
string will result in a NullPointerException
. Therefore, when you want to treat both null
and empty strings similarly (e.g., considering them as "blank" or "missing"), you need to check for both conditions.
The most common way to do this is to first check if the string is null
and then, if it's not null
, check if it's empty using the isEmpty()
method.
Let's modify our HelloJava.java
program to demonstrate combining these checks.
-
Open the HelloJava.java
file in the WebIDE editor.
-
Replace the current code with the following:
public class HelloJava {
public static void main(String[] args) {
String text1 = null;
String text2 = ""; // An empty string
String text3 = "Hello"; // A non-empty string
System.out.println("Checking text1 (null):");
if (text1 == null || text1.isEmpty()) {
System.out.println("text1 is null or empty.");
} else {
System.out.println("text1 is not null and not empty: " + text1);
}
System.out.println("\nChecking text2 (empty):");
// It's crucial to check for null first!
if (text2 == null || text2.isEmpty()) {
System.out.println("text2 is null or empty.");
} else {
System.out.println("text2 is not null and not empty: " + text2);
}
System.out.println("\nChecking text3 (not empty):");
if (text3 == null || text3.isEmpty()) {
System.out.println("text3 is null or empty.");
} else {
System.out.println("text3 is not null and not empty: " + text3);
}
}
}
In this updated code:
- We introduce three
String
variables: text1
(null), text2
(empty), and text3
(not empty).
- We use the logical OR operator (
||
) to combine the null
check (text == null
) and the empty check (text.isEmpty()
).
- The condition
text == null || text.isEmpty()
will be true if text
is either null
OR if text
is not null
AND text.isEmpty()
is true.
- Important: The
null
check (text == null
) must come first in the ||
condition. If text
is null
, the first part of the ||
condition (text == null
) is true, and Java uses "short-circuit evaluation" to skip the second part (text.isEmpty()
), thus preventing a NullPointerException
. If the isEmpty()
check came first and text
was null
, it would cause an error.
-
Save the file (Ctrl+S or Cmd+S).
-
Compile the program in the Terminal:
javac HelloJava.java
-
Run the program:
java HelloJava
You should see the following output:
Checking text1 (null):
text1 is null or empty.
Checking text2 (empty):
text2 is null or empty.
Checking text3 (not empty):
text3 is not null and not empty: Hello
This output shows that our combined check correctly identified both the null
string (text1
) and the empty string (text2
) as "null or empty", while correctly identifying the non-empty string (text3
).
This combined check (string == null || string.isEmpty()
) is a very common pattern in Java when you need to handle both null
and empty strings. Many libraries also provide utility methods for this, such as StringUtils.isEmpty()
or StringUtils.isBlank()
(which also checks for whitespace) in Apache Commons Lang, but understanding the basic combined check is fundamental.