(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(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);
That's called "Yoda Conditions" and the purpose is to prevent you from accidentally using assignment (=) instead of equality checking (==).
Videos
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.
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.
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
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.