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
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_isempty.asp
Java String isEmpty() Method
String myStr1 = "Hello"; String myStr2 = ""; System.out.println(myStr1.isEmpty()); System.out.println(myStr2.isEmpty()); ... The isEmpty() method checks whether a string is empty or not.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-stringutilsisnotempty-in-java
What is StringUtils.isNotEmpty in Java?
isNotEmpty() is a static method of the StringUtils class that is used to check if the given string is not empty.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ String โ€บ isEmpty
Java String isEmpty() - Check If Empty | Vultr Docs
December 17, 2024 - The isEmpty() method in Java is a straightforward and efficient way for checking whether a given string has a length of zero, which means it is an empty string. This method is part of the String class in Java's standard library, making it readily ...
๐ŸŒ
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 String is blank, so it's obviously neither null nor empty. Now, based just on the length, we can't really differentiate between Strings that only contain whitespaces or any other character, since a whitespace is a Character. Note: It's important to do the null-check first, since the short-circuit OR operator || will break immediately on the first true condition.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ list-isempty-method-in-java-with-examples
List isEmpty() method in Java with Examples - GeeksforGeeks
December 3, 2024 - // Java Progrm to Implement // List isEmpty() Method import java.util.*; class Main { public static void main (String[] args) { // New empty list created List<Integer> l=new ArrayList<>(); // Implementing isEmpty() Method if(l.isEmpty()) ...
๐ŸŒ
Medium
medium.com โ€บ dev-java โ€บ stringutils-isnotempty-vs-stringutils-isnotblank-in-java-491f10680879
Java String Checks: Understanding isNotEmpty vs isNotBlank | by Anil R | Dev Java | Medium
April 23, 2025 - Java String Checks: Understanding isNotEmpty vs isNotBlank When working with strings in Java, itโ€™s common to perform checks to see whether a string is not empty or not blank. Apache Commons Lang โ€ฆ
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-isempty
Java String isEmpty()
Java String isEmpty() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string isempty in java etc.
๐ŸŒ
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
public class Demo { public static void main(String[] args) { String myStr = "Jack Sparrow"; boolean res; if(myStr != null || myStr.length() != 0) { System.out.println("String is not null or not empty"); } else { System.out.println("String is null or empty"); } } }
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ string-empty-null
Java Program to Check if a String is Empty or Null
class Main { public static void main(String[] args) { // create a string with white spaces String str = " "; // check if str1 is null or empty System.out.println("str is " + isNullEmpty(str)); } // method check if string is null or empty public static String isNullEmpty(String str) { // check if string is null if (str == null) { return "NULL"; } // check if string is empty else if (str.trim().isEmpty()){ return "EMPTY"; } else { return "neither NULL nor EMPTY"; } } }
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 10 โ€บ java-string-isempty-method-with-example
Java String isEmpty() method
public class Example{ public static void main(String args[]){ String str1 = null; String str2 = "beginnersbook"; if(str1 == null || str1.isEmpty()){ System.out.println("String str1 is empty or null"); } else{ System.out.println(str1); } if(str2 == null || str2.isEmpty()){ System.out.println("String str2 is empty or null"); } else{ System.out.println(str2); } } }
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 417319 โ€บ java โ€บ elegant-check-field-empty
Any elegant way to check that field is not empty? (Java in General forum at Coderanch)
Do you mean null, or perhaps the empty string (""), or what? I wouldn't go catching NullPointerExceptions if you can use simple if statements though. And if you're that lazy about the checks, just write a method that does all the work for you. SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6 How To Ask Questions How To Answer Questions ... It is better to set up constructors so the fields are not left null after the object is instantiated anyway.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-array-empty-check
Java Array Empty Check - GeeksforGeeks
July 23, 2025 - In Java, an array is considered non-empty, if it is not null and its length is greater than 0.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-string-isempty-method-example
Java String isEmpty() Method with Example - GeeksforGeeks
November 20, 2024 - // Java program to demonstrate ... To ensure that the user input is not empty before processing it, we can use isEmpty() method of String class....
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ java-string-isempty-method-explained-a731faf082aa
Java String isEmpty() Method Guide | Medium
June 23, 2024 - If the user inputs an empty string, the program will display a message indicating that the name cannot be empty. This simple check makes sure that the required data is provided before proceeding further.
๐ŸŒ
Java67
java67.com โ€บ 2014 โ€บ 09 โ€บ right-way-to-check-if-string-is-empty.html
Right way to check if String is empty in Java with Example | Java67
Another popular and faster way to check if String is empty or not is by checking its length like if String.length() = 0 then String is empty, but this is also not null safe. A third common way of checking the emptiness of String in Java is comparing it with empty String literal like "".equals(str), this method is not as fast as the previous two but it is null safe, you don't need to check for null, in case of null it will return false.