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
This method returns true if the string is empty (length() is 0), and false if not. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ testing โ€บ string not empty test assertions in java
String Not Empty Test Assertions in Java | Baeldung
January 8, 2024 - Hamcrest is a well-known framework providing matchers that are commonly used for unit testing in the Java ecosystem. We can make use of the Hamcrest CoreMatchers class for empty String checking: assertThat(text, CoreMatchers.not(isEmptyString()));
๐ŸŒ
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"); } } }
๐ŸŒ
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.
๐ŸŒ
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. Another significant use case for isEmpty() is within conditional logic. Often, you might need to execute certain pieces of code only if a string is not 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 .
๐ŸŒ
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 ...
๐ŸŒ
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 - In Java, a built-in method is available to check if a String is empty before performing any operations. If you donโ€™t want to use this available method, alternatively you can check if the length of the String is zero. It will do the job for you. For the sake of this example, we are using the built-in method to see if the string is empty.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javaee โ€บ 7 โ€บ tutorial โ€บ bean-validation002.htm
21.2 Validating Null and Empty Strings - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all.