Correct way to check for null or empty or string containing only spaces is like this:

if(str != null && !str.trim().isEmpty()) { /* do your stuffs here */ }
Answer from Pradeep Simha on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ String โ€บ isEmpty
Java String isEmpty() - Check If Empty | Vultr Docs
December 17, 2024 - This code snippet checks if the username string is empty and provides feedback accordingly, crucial for avoiding processing of invalid or empty inputs. Handle input data where operations depend on whether strings are empty or not. Employ isEmpty() to conditionally process data only when strings are not empty.
๐ŸŒ
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
An empty Java String is considered ... One of the most popular ways of checking whether String is empty or not is the String class' isEmpty() method, this looks perfect right, it's readable and returns boolean if String is empty ...
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ java-string-isempty-method-explained-a731faf082aa
Java String isEmpty() Method Guide | Medium
June 23, 2024 - Learn about Java's isEmpty() method to efficiently check if a string is empty. Perfect for beginners and useful for technical interviews.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ shorter (efficient) way to check if a string is null or empty?
Shorter (efficient) way to check if a string is null or empty? : r/learnjava
February 28, 2020 - [The Java String class has an isEmpty method](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty() ... That's correct, but his left hand condition will check for that, if its true the right hand condition will not be evaluated ... This is TERRIBLE advice. The point of evaluating a variable is so that the program doesn't crash. Yet, your advice is to let the application crash. That is in no way better than checking that the variable is null or empty.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 203109 โ€บ java โ€บ Efficient-Check-Empty-String
Efficient Way to Check Empty String (Performance forum at Coderanch)
We use (string != null || string.trim().length() == 0) to check if string is empty. Calling a method which actually iterate through string and checks for non-whitespace character .
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ program-to-check-if-the-string-is-empty-in-java
Program to check if the String is Empty in Java - GeeksforGeeks
July 12, 2025 - We can simply check if the string is empty or not using the isEmpty() method of String class Syntax: ... Print true if the above condition is true. Else print false. Below is the implementation of the above approach: ... // Java Program to check ...
๐ŸŒ
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()).
๐ŸŒ
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. ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 10 โ€บ java-string-isempty-method-with-example
Java String isEmpty() method
Java String isEmpty() method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false.
๐ŸŒ
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.
๐ŸŒ
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 - if (StringUtils.isNotEmpty("")) ... for string manipulation. In this article, we have used some of these methods such as isEmpty(), equals(), StringUtils.isEmpty() and length() to check if the String is null, empty or blank....
๐ŸŒ
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 - Here, StringUtils.isBlank() checks if str is null, empty, or made solely of whitespace (spaces, tabs, new line characters). Very useful when the mere presence of whitespace should also classify the string as "empty".
๐ŸŒ
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
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. The trim() method remove all whitespaces and then isEmpty() checks if the string contains ...
๐ŸŒ
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.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ empty-string-java
Working with Java Empty String: A Detailed Guide
February 20, 2024 - So, letโ€™s dive in and start mastering empty strings in Java! In Java, you can create an empty string like this: String str = ""; and check if a string is empty using str.isEmpty().
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-string-isempty-method-example
Java String isEmpty() Method with Example - GeeksforGeeks
November 20, 2024 - ... // Java program to demonstrate isEmpty() method import java.util.*; public class StringIsEmpty { public static void main(String[] args) { // Declare and initialize strings String s1 = ""; String s2 = "Java"; // Check if the strings are empty ...