Method 4 is best.

if(foo != null && foo.bar()) {
   someStuff();
}

will use short-circuit evaluation, meaning it ends if the first condition of a logical AND is false.

Answer from Jared Nielsen on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › avoid check for null statement in java
Avoid Check for Null Statement in Java | Baeldung
January 8, 2024 - ... Here, @NonNull makes it clear that the argument cannot be null. If the client code calls this method without checking the argument for null, FindBugs would generate a warning at compile time.
🌐
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.
🌐
sebhastian
sebhastian.com › java-is-not-null
Java - How to check if a variable or object is not null | sebhastian
March 18, 2022 - When the value of response is not null, then Java will run the println() method inside the if block above.
🌐
Coderanch
coderanch.com › t › 496486 › java › null-null-java
null and not null in java (Beginning Java forum at Coderanch)
Yes, the first == null test looks ... but that doesn't make any difference to the principle.] If it is null you choose the first option in your if-then-else, otherwise you choose the second option (after the else)....
🌐
Medium
medium.com › @mesfandiari77 › understanding-notnull-vs-nonnull-in-java-a-practical-guide-to-null-safety-5e179e918f37
Understanding @NotNull vs @Nonnull in Java: A Practical Guide to Null-Safety | by MEsfandiari | Medium
February 13, 2025 - The infamous NullPointerException (NPE) is one of the most common runtime errors developers face. To address this, Java and third-party libraries provide annotations like @NotNull and @Nonnull to indicate whether a variable, parameter, or return value can be null.
Find elsewhere
🌐
Baeldung
baeldung.com › home › spring › spring boot › using @notnull on a method parameter
Using @NotNull on a Method Parameter | Baeldung
May 11, 2024 - Our NullPointerException returns. Spring doesn’t enforce NotNull constraints when we invoke the annotated method from another method that resides in the same class. In this article, we learned how to use the @NotNull annotation on a method parameter in a standard Java application. We also learned how to use Spring Boot’s @Validated annotation to simplify our Spring Bean method parameter validation while also noting ...
🌐
Better Programming
betterprogramming.pub › checking-for-nulls-in-java-minimize-using-if-else-edae27016474
Checking for Nulls in Java? Minimize Using “If Else” | by Itır ...
January 26, 2022 - Lets say that you have Student.javawith fields such as id, name and classes. You can use put @Builder.Default before the related field and give it a default value. When an instance of this Student class is created, it will have “classes” as an empty list, not null.
🌐
Openjml
openjml.org › tutorial › Nullness
JML Tutorial - Nullable and non-null values and types
Accordingly, when ss is initialized or the target of an assignment, the value it is given must be provably not null. But thereafter the values can be assumed to be non-null. If there is no modifier, the default is non-null. Instead of these modifiers, one can use the Java annotations @NonNull ...
🌐
Educative
educative.io › answers › what-is-validatenotnull-in-java
What is Validate.notNull in Java?
The method returns the input value hello as it is not a null value. ... The method throws a NullPointerException as the input value is null.
Top answer
1 of 3
5

The dilemma

If a variable with null value gets used in your program causing a NullPointerException, this is clearly a situation in your program which you did not expect. You must ask yourself the question: "Did I not expect it because I didn't take into consideration the possibility of a null value or did I assume the value could never be null here?"

If the answer is the latter, the problem isn't because you didn't handle the null value. The problem happened earlier, and you're only seeing the consequence of that error on the particular line it's used. In this case, simply adding a if (variable != null) isn't going to cut it. You'll wind up skipping lines you were supposed to execute because the variable was null, and you'll ultimately hit a line further on where you again assumed it wouldn't be null.

When null should be used

As a general rule, return null only when "absent" is a possible return value. In other words, your data layer may search for a record with a specific id. If that record isn't found, you can either throw an exception or simply return null. You may do either, but I prefer not to throw exceptions in situations where the strong possibility exists. So you return null instead of a value.

The caller of this method, presumably written by you, knows the possibility exists that the record may not exist and checks for null accordingly. There is nothing wrong with this in this case, though you should handle this possibility as soon as possible as otherwise everywhere in your program you will need to deal with the possibility of a null value.

Conclusion

In other words, treat null as a legitimate value, but deal with it immediately rather than wait. Ideally in your program, you should ever only have to check if it is null once in your program and only in the place where such a null value is handled.

For every value you expect to be non-null, you need not add a check. If it is null, accept that there is an error in your program when it was instantiated. In essence, favor fail fast over fail safe.

2 of 3
8

Deciding whether or not null is a allowed as an object value is a decision that you must make consciously for your project.

You don't have to accept a language construct just because it exists; in fact, it is often better to enforce a strict rule against any nullvalues in the entire project. If you do this, you don't need checks; if a NullPointerException ever happens, that automatically means that there is a defect in your code, and it doesn't matter whether this is signalled by a NPE or by some other sanity check mechanism.

If you can't do this, for instance because you have to interoperate with other libraries that allow null, then you do have to check for it. Even then it makes sense to keep the areas of code where null is possible small if possible. The larger the project, the more sense it makes to define an entire "anti-corruption layer" with the only purpose of preserving stricter value guarantees than is possible elsewhere.

🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to check null in java (with pictures) - wikihow
How to Check Null in Java (with Pictures) - wikiHow
May 15, 2025 - Use “==” to check a variable’s value. A “==” is used to check that the two values on either side are equal. If you set a variable to null with “=” then checking that the variable is equal to null would return true.
🌐
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 equals() method compares the two given strings based on their content and returns true if they're equal or false if they are not: 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.
🌐
Sonar Community
community.sonarsource.com › sonarqube for ide › vs code
Declaring a non-null return value? - VS Code - Sonar Community
June 18, 2021 - Using VS Code and Java, I’m trying to set up a class that receives potentially null values, and verifies they are not null as part of instance construction. Here’s a simplified version (the real code uses a builder pattern). public class Job { private @NonNull String name; public Job(@Nullable String jobName) { this.name = checkNotNull(jobName, "Name cannot be null"); } } My checkNotNull() is… public static @NonNull T checkNotNull(@Nullable T value, @NonNull String message...
🌐
Educative
educative.io › answers › what-is-objectsnonnull-in-java
What is Objects.nonNull in Java?
If the passed object is null, then the method returns false. This method was introduced in Java 8. To use the nonNull method, import the following module. ... This method takes one parameter, Object obj, an object reference to be checked against ...
🌐
DataCamp
datacamp.com › doc › java › null
null Keyword in Java: Usage & Examples
This example demonstrates a null check before calling a method on the String object. It prevents a NullPointerException by ensuring the object is not null before invoking its methods.