What about isEmpty() ?

if(str != null && !str.isEmpty())

Be sure to use the parts of && in this order, because java will not proceed to evaluate the second part if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.

Beware, it's only available since Java SE 1.6. You have to check str.length() == 0 on previous versions.


To ignore whitespace as well:

if(str != null && !str.trim().isEmpty())

(since Java 11 str.trim().isEmpty() can be reduced to str.isBlank() which will also test for other Unicode white spaces)

Wrapped in a handy function:

public static boolean empty( final String s ) {
  // Null-safe, short-circuit evaluation.
  return s == null || s.trim().isEmpty();
}

Becomes:

if( !empty( str ) )
Answer from Colin Hebert on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-check-if-the-string-is-null-in-java
Program to check if the String is Null in Java - GeeksforGeeks
July 12, 2025 - The below example demonstrates how to check if a given string is null using the == relational operator. ... // Java Program to check if // a String is Null class StringNull { // Method to check if the String is Null public static boolean ...
🌐
CodeGym
codegym.cc › java blog › strings in java › java: check if string is null, empty or blank
Java: Check if String is Null, Empty or Blank
October 11, 2023 - Also, you can use the example below to check if a string is null or empty. public class Example1 { public static void main(String[] args) { // check if it is an "empty" string String myName = new String(); System.out.println("The String = " + myName); // not sure if the string is either null or empty System.out.println("Is the String null?
🌐
Blogger
javarevisited.blogspot.com › 2016 › 01 › how-to-check-if-string-is-not-null-and-empty-in-java-example.html
How to check if String is not null and empty in Java? Example
Since we are first doing a null check and then an empty check using the && operator, which is a short circuit AND operator. This operator will not check for emptiness if String is null hence no NPE. This is also a good trick to avoid NPE in Java.
🌐
Medium
medium.com › @ecetasci.iu › checking-for-null-or-empty-strings-in-java-19518fa1e553
Checking for Null or Empty Strings in Java | by Ece Tasci | Medium
February 25, 2025 - When working with Strings in Java, it is common to encounter situations where a String might be either null or empty. This can lead to unexpected errors, particularly when working with user input, API responses, or database records. To avoid these issues, performing proper checks before using a String in operations like concatenation, comparison, or storage is essential. One of the most common ways to ensure a String is valid before using it is by checking if(name != null && !name.isEmpty()).
🌐
Stack Abuse
stackabuse.com › java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - The equals() method compares the two given strings based on their content and returns true if they're equal or false if they are not: String string = "Hello there"; if (string == null || string.equals("") || string.trim().equals("")) System.out.println("String is null, empty or blank"); else ...
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › examples › check-if-a-string-is-empty-or-null
Java Program to Check if a String is Empty or Null | Vultr Docs
December 17, 2024 - public static boolean isNullOrEmpty(String str) { return str == null || str.isBlank(); } Explain Code · With Java 11, String.isBlank() does what StringUtils.isBlank() from Apache Commons does but without the need for an external library. Checking if a string is empty or null in Java can be approached in multiple ways, each serving different needs depending on your project requirements.
🌐
Reddit
reddit.com › r/java › cleanest way to check for null on a string?
Cleanest way to check for null on a String? : r/java
May 8, 2024 - The advantage with the switch is you can add more conditions easily and you can switch to @Nullable String later if you choose (I avoid Optional unless its API like Service). I admit though I would probably pick what u/Equivalent_Plan_3076 proposing if for sure you want to use Optional. I'm not a fan of u/papercrane java.util.Objects.toString(Object o, String nullDefault) because the second argument is @PolyNull and the JDK javadoc description barely makes it clear that it is.
🌐
Quora
quora.com › How-do-perform-a-Null-check-for-string-in-Java
How do perform a Null check for string in Java? - Quora
Answer (1 of 7): 4 Ways to check if String is null or empty in Java:- Here are my four solutions for this common problem. Each solution has their pros and cons and a special use case e.g. first solution can only be used from JDK7 on wards, second solution is the fastest way to check string is em...
🌐
Programiz
programiz.com › java-programming › examples › string-empty-null
Java Program to Check if a String is Empty or Null
This is because white spaces are treated as characters in Java and the string with white spaces is a regular string. Now, if we want the program to consider strings with white spaces as empty strings, we can use the trim() method. The method removes all the white spaces present in a string. ...
🌐
TestMu AI Community
community.testmu.ai › ask a question
What is the correct way to check if a String is empty or null in Java, and how can I avoid potential errors? - TestMu AI Community
March 4, 2025 - I’m parsing HTML data, and sometimes the string might be either null or an empty string. Here’s the condition I wrote to handle it: if(string.equals(null) || string.equals("")){ Log.d("iftrue", "seem to be true"); }
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › check-if-string-is-null-empty-or-blank-in-java
Check if String is Null, Empty or Blank in Java
Simply compare the string with null to check for null string. Use isEmpty() method of string class to check for empty string. The isEmpty() method returns true if the string does not contain any value. Use trim() and isEmpty() method together to check for blank string.
🌐
TutorialsPoint
tutorialspoint.com › checking-for-null-or-empty-in-java
Checking for Null or Empty in Java.
The following are the ways to check if a string is null or empty in Java: ... The isEmpty() method of the String class checks if a string is empty. It returns TRUE if the string has no characters, and FALSE otherwise. Following is an example of checking if a string is null or empty using the ...
🌐
Baeldung
baeldung.com › home › java › java string › checking for empty or blank strings in java
Checking for Empty or Blank Strings in Java | Baeldung
January 8, 2024 - The most convenient way is to use Apache Commons Lang, which provides helpers such as StringUtils.isBlank. If we want to stick to plain Java, we can use a combination of String#trim with either String#isEmpty or String#length.
🌐
Coderanch
coderanch.com › t › 693624 › java › check-null-empty-strings-time
Do we have any way to check null value and empty strings all the time ? (Features new in Java 8 forum at Coderanch)
If I go into the first class, what are you going to do about the middle name I haven't got? Mark it null? But in the second version, you can have an array with all middle names in; in my case you can use a zero‑length array. Abracadabra! No nulls More details in books like Effective Java by Joshua Bloch.
🌐
TutorialsPoint
tutorialspoint.com › check-if-a-string-is-not-empty-and-not-null-in-java
Check if a String is not empty ("") and not null in Java
JavaObject Oriented ProgrammingProgramming · Let’s say we have the following string − · String myStr1 = "Jack Sparrow"; Let us check the string now whether it is not null or not empty. if(myStr != null || myStr.length() != 0) { System.out.println("String is not null or not empty"); Live Demo ·
🌐
TestMu AI Community
community.testmuai.com › ask a question
Best Way to Check if a String is Null or Empty in Java - TestMu AI Community
March 19, 2025 - What is the best way to check if a String is null or empty in Java? I’m currently parsing HTML data, and sometimes the String can be null or empty when the expected word doesn’t match. To handle this, I wrote the following check: if(string.equals(null) || string.equals("")){ Log.d("iftrue", "seem to be true"); }else{ Log.d("iffalse", "seem to be false"); } However, when I remove string.equals(""), the condition doesn’t work correctly.