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
🌐
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 Android flavor targets Android ... Guavas Strings class comes with the method Strings.isNullOrEmpty: ... It checks whether a given string is null or empty, but it won’t check for whitespace-only strings....
🌐
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". Recognize the enhancements in Java 11 including new String methods.
🌐
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
March 4, 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.
🌐
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 - String string = "Hello there"; if (string == null || string.equals("") || string.trim().equals("")) System.out.println("String is null, empty or blank"); else System.out.println("String is neither null, empty nor blank"); In much the same fashion as the before, if the trimmed string is "", it was either empty from the get-go, or was a blank string with 0..n whitespaces: ... The Apache Commons is a popular Java library that provides further functionality.
🌐
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 - Java provides a built-in method to check for all those whitespaces in a String. Let’s look at an example on how to use that. public class Example2 { public static void main(String[] args) { // check if it is a "blank" string String myName = new String(" \t \n \t \t "); System.out.println("The String = " + myName); System.out.println("Is the String null? " + (myName == null)); System.out.println("Is the String empty?
🌐
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 - In Java, calling a method on a null reference will result in a NullPointerException, one of the most common runtime errors. To prevent this, it is crucial to check for null before performing operations on a variable. An empty String ("") has zero characters but still exists in memory, specifically in the heap where Java stores objects.
🌐
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. ...
Find elsewhere
🌐
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
You can check for empty String like this. The isEmpty() method returns true if the string is empty. String myString = ""; //empty string if(myString!=null && myString.isEmpty()){ System.out.println("This is an empty string"); }
🌐
Baeldung
baeldung.com › home › java › avoid check for null statement in java
Avoid Check for Null Statement in Java | Baeldung
April 8, 2019 - Although we eliminated the need for a null check on the caller of this API, we used it to return an empty response. To avoid this, Optional provides an ofNullable method that returns an Optional with the specified value, or empty, if the value is null:
🌐
TutorialsPoint
tutorialspoint.com › checking-for-null-or-empty-in-java
Checking for Null or Empty in Java.
We can check whether a particular String is empty or not, using the isBlank() method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not. The following is an example of checking if a string is null or empty ...
🌐
Medium
tamerardal.medium.com › simplify-null-checks-in-java-writing-clean-code-with-apache-commons-lang-3-e7d3aea207bd
Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3 | by Tamer Ardal | Medium
September 18, 2024 - In Java, null checks are generally done using == or !=. In addition, if we want to do an empty check, our condition will look like the following.
🌐
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.
🌐
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
Thanks for your comments Guys, I agree using a library function such as Apache Commons isBlank() is the right way to do null and empty check and everybody should do that in production only if that library is already in classpath. Though, It's important for Java beginners to know how they can do this in JDK as well.
🌐
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.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › check-if-a-string-is-empty-or-null-in-java
Check if a String is empty ("") or null in Java
Practice 3500+ coding problems and tutorials. Master programming challenges with problems sorted by difficulty. Free coding practice with solutions.
🌐
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)
if (testString==null) { doSomething(); } else { doAnotherThing(); } By default, the doAnotherThing method is called even when the user enters no data, because the testString element has been initialized with the value of an empty string. In order for the Bean Validation model to work as intended, ...
🌐
Java Guides
javaguides.net › 2024 › 05 › java-check-if-object-is-null-or-empty.html
Java: Check If Object Is Null or Empty
May 29, 2024 - This guide will cover various ways to check if different types of objects (like String, Collection, Map, and custom objects) are null or empty.