To check if a string is null in Java, use the == operator to compare the string reference directly with null.

if (str == null) {
    System.out.println("String is null");
}

This is the standard and most direct way to perform a null check. Always check for null first to avoid a NullPointerException when calling methods like isEmpty() or length() on a null reference.

Copyif (myString != null && !myString.isEmpty()) {
  // doSomething
}

As further comment, you should be aware of this term in the equals contract:

From Object.equals(Object):

For any non-null reference value x, x.equals(null) should return false.

The way to compare with null is to use x == null and x != null.

Moreover, x.field and x.method() throws NullPointerException if x == null.

Answer from polygenelubricants on Stack Overflow
🌐
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)
May 1, 2018 - I also know that we have null value check and Empty string check methods inside JAVA already. But i want to know we need to make program all the time to check for null values and empty strings manually. This is increasing the number of lines of program as well.
🌐
Netjstech
netjstech.com › 2019 › 08 › check-string-null-or-empty-java.html
Check String Null or Empty in Java | Tech Tutorials
June 17, 2022 - public class StringLength { public ... if(str == null || str.length() == 0) { return true; } return false; } } ... 3. Java 11 onward there is also isBlank() method to check if String is empty in Java....
Discussions

Cleanest way to check for null on a String?
If all you're doing is returning an Optional, use the features of the Optional: return Optional.ofNullable(someObject.get("someKey")); If you need to do some additional processing on the string value, call .map() on the Optional. More on reddit.com
🌐 r/java
11
3
May 8, 2024
[Java] How do you check if an input string is null?
I've tried "if s == null" check but that doesn't work. Did you put that check before or after your reference to s.length and s.charAt? Also, Java requires the condition to be in parentheses, so assuming you literally had if s == null, that might be your issue. Try if (s == null) return null; More on reddit.com
🌐 r/learnprogramming
8
3
January 6, 2021
Shorter (efficient) way to check if a string is null or empty?
[The Java String class has an isEmpty method]( https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty() More on reddit.com
🌐 r/learnjava
12
2
March 4, 2020
In Java, should I use (String ) isEmpty() or isBlank() when checking an XML object for an empty value?
What behavior do you want if the xml parser emits " " as the config value? More on reddit.com
🌐 r/AskProgramming
4
1
September 26, 2022
🌐
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 - To check if a string is null in Java, we can use the "==" operator that directly compares the string reference with null.
🌐
Mkyong
mkyong.com › home › java › java – check if a string is empty or null
Java - Check if a String is empty or null - Mkyong.com
April 29, 2019 - In Java, we can use (str != null && !str.isEmpty()) to make sure the String is not empty or null. ... package com.mkyong; public class StringNotEmpty { public static void main(String[] args) { System.out.println(notEmpty("")); // false ...
🌐
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.
🌐
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 - public class Example { public static void main(String[] args) { // check if it is a null string String myName = null; String nullString = null; if (myName == null) { // print if the string is null System.out.println("The String = " + myName); ...
Find elsewhere
🌐
Java Guides
javaguides.net › 2019 › 04 › check-if-string-is-empty-or-null-in-java-utility-methods.html
How to Check if a String is Null or Empty in Java
August 2, 2023 - It returns true if the length of ... true System.out.println(str3 != null && str3.isEmpty()); // Output: false } } ... The isBlank() method is introduced in Java 11, and it not only checks for empty strings but also considers strings ...
🌐
Codemia
codemia.io › knowledge-hub › path › checking_if_a_string_is_empty_or_null_in_java
Checking if a string is empty or null in Java
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java string › checking if a stringbuilder is null or empty
Checking if a StringBuilder Is Null or Empty | Baeldung
August 25, 2024 - Checking whether a StringBuilder is null isn’t a challenge to us. In Java, we can verify any object is null using theObject == null:
🌐
Programiz
programiz.com › java-programming › examples › string-empty-null
Java Program to Check if a String is Empty or Null
Become a certified Java programmer. Try Programiz PRO! ... class Main { public static void main(String[] args) { // create null, empty, and regular strings String str1 = null; String str2 = ""; String str3 = " "; // check if str1 is null or empty System.out.println("str1 is " + isNullEmpty(str1)); // check if str2 is null or empty System.out.println("str2 is " + isNullEmpty(str2)); // check if str3 is null or empty System.out.println("str3 is " + isNullEmpty(str3)); } // 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.isEmpty()){ return "EMPTY"; } else { return "neither NULL nor EMPTY"; } } }
🌐
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 - String#isEmpty was introduced with Java 6. For Java 5 and below, we can use String#length instead: boolean isEmptyString(String string) { return string == null || string.length() == 0; } In fact, String#isEmpty is just a shortcut to String#length. Both String#isEmpty and String#length can be used to check for empty strings. If we also want to detect blank strings, we can achieve this with the help of String#trim.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-check-if-a-string-is-empty-or-null
Java program to check if a string is empty or null
November 8, 2024 - Print that it's an empty string if true. Otherwise, print that the string is neither null nor empty. ... In the main, define a string variable input_string and set it to null. Print the value of input_string. Call the isNullEmpty method with input_string as an argument to check its state.
🌐
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)
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, you must set the context parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true in the web deployment descriptor file, web.xml: <context-param> <param-name> javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL </param-name> <param-value>true</param-value> </context-param>
🌐
DEV Community
dev.to › scottshipp › better-null-checking-in-java-ngk
Better Null-Checking in Java - DEV Community
January 11, 2019 - That’s kind of what Optional was made for and if you have the option (ok, ok, I know this is getting a little heavy-handed) go for it. What I have found, though, is that there are plenty of situations where awkward null-checking has been replaced with still-awkward usage of Optional, such as this example from a popular open-source Java tool: That’s really no different than the null-check in this twin code: String pathsStr = get(PATHS_KEY); if (pathsStr != null) { ObjectMapper objectMapper = new ObjectMapper(); try { String[] paths = objectMapper.readValue(pathsStr, String[].class);
🌐
GUVI
guvi.in › hub › java-programs-tutorial › is-string-empty
Java Program to Check If a String is Empty or Null
Use length() to check whether the entered string is null or not. If the length of the entered string is 0 it is an empty string. Display the result. ... The below example illustrates the implementation of the above algorithm.
🌐
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.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
Checking for Null or Empty or White Space Only String in Java | InfoWorld
September 2, 2011 - /** * Demonstrate checking for String that is not null, not empty, and not white * space only using standard Java classes. * * @param string String to be checked for not null, not empty, and not white * space only. * @return {@code true} if provided String is not null, is not empty, and * has at least one character that is not considered white space.
🌐
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...