(Similar to this question: Difference between null==object and object==null)

I would say that there is absolutely no difference in performance between those two expressions.

Interestingly enough however, the compiled bytecode (as emitted by OpenJDKs javac) looks a bit different for the two cases.

For boolean b = variable == null:

 3: aload_1               // load variable
 4: ifnonnull 11          // check if it's null
 7: iconst_1              // push 1
 8: goto 12           
11: iconst_0              // push 0
12: istore_2              // store

For boolean b = null == variable:

 3: aconst_null           // push null
 4: aload_1               // load variable
 5: if_acmpne 12          // check if equal
 8: iconst_1              // push 1
 9: goto 13
12: iconst_0              // push 0
13: istore_2              // store

As @Bozho says, variable == null is the most common, default and preferred style.

For certain situations however, I tend to put the null in front. For instance in the following case:

String line;
while (null != (line = reader.readLine()))
    process(line);
Answer from aioobe on Stack Overflow
🌐
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 - You can also use “!=” to check that a value is NOT equal. ... Use an “if” statement to create a condition for the null. The result of the expression will be a boolean (true or false) value.
🌐
Quora
quora.com › What-is-the-best-way-to-check-if-a-variable-is-null-before-trying-to-access-its-value-in-Java
What is the best way to check if a variable is null before trying to access its value in Java? - Quora
Answer (1 of 5): I̲n̲ ̲J̲a̲v̲a̲ ̲,̲ ̲t̲h̲e̲ ̲s̲t̲a̲n̲d̲a̲r̲d ̲w̲a̲y ̲i̲s̲ ̲t̲o̲ ̲j̲u̲st̲ ̲d̲o̲ ̲a̲ ̲s̲t̲r̲a̲i̲g̲h̲t̲f̲o̲r̲w̲a̲r̲d̲ ̲n̲u̲l̲l̲ ̲c̲h̲e̲c̲k̲ ̲w̲i̲t̲h̲ ̲`̲i̲f̲ ̲(̲v̲a̲r̲i̲a̲b̲l̲e̲ ̲=̲=̲ ...
🌐
Baeldung
baeldung.com › home › java › core java › check if all the variables of an object are null
Check If All the Variables of an Object Are Null | Baeldung
January 8, 2024 - In this tutorial, we’ll learn four approaches to check if all variables of an object are null. The null value in Java means the absence of a variable’s value. Technically, a variable containing null doesn’t point to any position in memory or wasn’t initialized yet. That can only occur with instance variables. Primitive variables such as int, double, and boolean can’t hold null. Checking for null variables in our programs is ...
🌐
Medium
suraj-batuwana.medium.com › how-to-check-if-all-the-variables-of-an-object-are-null-88ad67a70ea2
How to check If All the Variables of an Object Are Null | by Suraj Batuwana | Medium
November 9, 2023 - String text = // some operation that may return null if (text != null) { // perform operations on text } else { // handle the case where text is null } In summary, checking for null in Java is a fundamental practice for building robust, reliable, and maintainable software by preventing runtime errors, improving code readability, and enhancing overall program stability.
🌐
Javatpoint
javatpoint.com › how-to-check-null-in-java
How to Check null in Java
How to Check null in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
DEV Community
dev.to › scottshipp › better-null-checking-in-java-ngk
Better Null-Checking in Java - DEV Community
January 11, 2019 - As a motivating example, finding a user’s zip code in their account might look similar to this in standard Java: That’s three null checks in the space of ten lines. Using my solution, which I’ve released as part of a library called Mill, the motivating example can be converted to: There would then be a single null check afterward. Or, if an empty string or another default value might be more applicable, there is an alternative getOrDefault() method.
🌐
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 - The below example demonstrates how to check if a given string is null using the == relational operator. ... // Java Program to check if // a String is Null class StringNull { // Method to check if the String is Null public static boolean ...
Find elsewhere
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.

🌐
Baeldung
baeldung.com › home › java › avoid check for null statement in java
Avoid Check for Null Statement in Java | Baeldung
January 8, 2024 - We can use these annotations over any method, field, local variable, or parameter. This makes it explicit to the client code whether the annotated type can be null or not. ... 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. Developers generally rely on IDEs for writing Java ...
🌐
Baeldung
baeldung.com › home › java › java numbers › check if an integer value is null or zero in java
Check if an Integer Value Is Null or Zero in Java | Baeldung
January 8, 2024 - Java 8 introduced the Optional type, so if our Java version is 8 or later, we can also implement this check using a static method from the Optional class: public static boolean usingOptional(Integer num) { return Optional.ofNullable(num).orElse(0) == 0; } As we don’t know if the given Integer variable is null or not, we can build an Optional instance from it using Optional.ofNullable(num).
🌐
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
May 16, 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.
🌐
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. You can also use the Objects.nonNull() method to check whether a variable is not null.
🌐
Java2Blog
java2blog.com › home › core java › java basics › check if object is null in java
Check if Object Is Null in Java - Java2Blog
November 29, 2023 - The most basic and efficient way to check if an object is null is by using the == operator. ... Object myObject = null;: This line declares a variable named myObject of the type Object, which is a class in Java.
Top answer
1 of 3
10

Why is my code broken here?

When the Java compiler see's the type listed again, RepairDetails repairDetails = new RepairDetails();

it assumes you are trying to define a variable. However, you already have that variable accessible in the outer scope.

The correct code simply drops the type, allowing you to assign repairDetails.

if(repairDetails == null){
    repairDetails = new RepairDetails();
}

Which turns repairDetails = new RepairDetails(); into an assignment, instead of a variable declaration.


(Tangentally related, but a question that usually follows after someone runs into this issue and thinks they have seen it before)

Why is it okay when the outer scope was a member, or super class member?

https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html has a section that mentions declaring member variables, but local variables behave similarly, except they are contained to their scope of the method / braces that they are defined in, rather then the class.

There are a few times that Java allows clashing variable names from scopes, is when it's a local variable overriding a member variable. (a parameter is also a local variable) , or there is inheritance involved.

It's known as hiding. https://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html

methods can also be hidden. https://docs.oracle.com/javase/tutorial/java/IandI/override.html

But the behavior isn't what you expect here either.

The reason why this is unavoidable, is that members can be overriden by inheritance, and a super class may define a member that was previously used in local scope on a child class.

So the Java designers allowed hiding, as it's a lesser of the 2 evils, considering that you can disambiguate between super members, class members, and locals by prefixing the variable name with

  • member: this.repairDetails ,
  • super member: super.repairDetails ,
  • local: repairDetails
2 of 3
6

The Problem is that you are trying to define a new Variable.

change the

if(repairDetails == null){
    RepairDetails repairDetails = new RepairDetails();
}

to

if(repairDetails == null){
    repairDetails = new RepairDetails();
}

and it should work.

You don't need the type definition a second time and that is why you are getting a error.

🌐
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
If you are a complete beginner then I suggest you first go through a comprehensive course like The Complete Java Masterclass on Udemy to learn more about core Java basics as well as such gems from Java API. Here are my three solutions to this common problem. Each solution has its pros and cons and a special use case like the first solution can only be used from JDK 7 onward, the second is the fastest way to check if String is empty and the third solution should be used if your String contains whitespaces. This is the most readable way to check for both whether String is null or not and whether String is empty or not.