Is there a difference between null != something
and something != null
in Java. And if there is a difference then which one should I use and why??
I just want to point out that the "Yoda condition" rationale for doing this (in languages like C & C++) does not apply in this (Java) case.
Java does not allow general expressions to be used as statements, so both
something == null;
and
null == something;
would be compilation errors.
The types of something == null
and something = null
are different; boolean and some reference type respectively. In this case, it means that both:
if (something = null) {
...
}
and
if (null = something) {
...
}
would be compilation errors.
In fact, I can't think of a realistic example where null == something
would be compilation error and something == null
would not. Hence, it doesn't achieve anything in terms of mistake-proofing.