Skip to main content

How to Check if a String Is null or Empty in Java

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A String your program needs can be missing in two ways: it can be null, no string at all, or empty, "". Java's usual check covers both in one condition, value == null || value.isEmpty(), and the order of the two parts is what keeps it from crashing.

Syntax​

Syntax
if (<value> == null || <value>.isEmpty()) {
// <value> is null or ""
}
PartWhat it does
<value> == nulltrue when there's no string at all.
||Logical OR. It stops at the first true, so the right side only runs when <value> isn't null.
<value>.isEmpty()true when the string has no characters.

The JDK 25 documentation for isEmpty: "Returns true if, and only if, length() is 0." Calling it on null isn't possible, because there's no string to ask; that's what the null check in front prevents.

The Example​

The example runs three values through one check: a name, an empty string and null.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Check​

Create a file named Main.java:

Main.java, which defines describe(String value) returning empty or missing when value is null or empty and has text: followed by the value otherwise, and prints it for Ada, an empty string and null.

Main.java
public class Main {
static String describe(String value) {
if (value == null || value.isEmpty()) {
return "empty or missing";
}
return "has text: " + value;
}

public static void main(String[] args) {
System.out.println(describe("Ada"));
System.out.println(describe(""));
System.out.println(describe(null));
}
}

Step 2: Run It​

Terminal
java Main.java
Output
has text: Ada
empty or missing
empty or missing

A zsh terminal where java Main.java prints has text: Ada and then empty or missing twice.

For null, value == null was already true, so || never called isEmpty(). The command is the same in zsh, bash and PowerShell.

Why the Order Matters​

Swap the two checks, value.isEmpty() || value == null, and null crashes the program before the null check is reached:

Output
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "<local1>" is null

The message names the variable <local1> here because java Main.java compiles without local variable names. Compiled with javac -g, the same code said because "value" is null.

Strings of Spaces​

" ".isEmpty() is false: three spaces are three characters. isBlank(), available since Java 11, returns true for a string that is empty or only white space, so value == null || value.isBlank() treats spaces as missing too.

Common Mistakes​

MistakeWhat happensDo this instead
Checking isEmpty() before nullNullPointerException: Cannot invoke "String.isEmpty()" because … is nullTest == null first
Using | instead of ||Both sides always run, so null still crashesUse ||
Comparing with value == ""Compares objects, not text, so it can be false for an empty stringUse isEmpty()

Troubleshooting​

SymptomLikely causeFix
NullPointerException on the check lineA method was called on nullPut value == null first
A field of spaces passes the checkisEmpty() counts spaces as charactersUse isBlank()

FAQ​

What does || do when the left side is true?​

It skips the right side, because the answer is already true. That's called short-circuit evaluation.

Is there a built-in helper for both?​

Not in String itself. Many projects write a small helper method like describe above.

Conclusion​

value == null || value.isEmpty() checks for a missing or empty string safely, because || skips isEmpty() when value is null. Use isBlank() instead of isEmpty() when spaces should count as empty.

Additional Resources​