For me both are same almost. I use if(var == null) this way only.
this habhits[if(null == value)] comes from C language . example,in C if statement validate 1 as true and 0 as false
so when you say accidently if(i = 2) insteadof if(i == 2) , you get different output.
but in Java if staement condition must be a boolean , so it is not a problem [except Boolean].
note: only when you are comparing a *Boolean* value you need to be careful because if(Boolean = null) will pass the compiler check.
There are reasons for preferring if (null == value) in C/C++ because you will get a compiler error if you write = by mistake instead of ==. That is because C cannot distinguish 0 for false and 0 for null and will accept (value = null) as the same as false. Only in C you usually say NULL, rather than null. But you can't writeif (NULL = value) even in C.
In Java, however, the compiler will never allow if (null), so you will always get a compiler error if you write if (value = null) instead of == null. In that case there is no need to prefer if (null == value) and I would agree with Maneesh and Manish.
You should, however, always avoid == false and == true, because it is possible to change values with = unintentionally.
Campbell Ritchie wrote:There are reasons for preferring if (null == value) in C/C++ because you will get not a compiler error if you write = by mistake instead of ==.
There, I fixed it. C and C++ allow it.
There is in Java one reference type that does allow this to compile since Java 5.0: Boolean. Consider the following example:
In Java 1.4 and before this would give a compiler error. Because of auto-boxing this will now compile but lead to a NullPointerException. The reason is because this is what happens:
- null is assigned to b
- b is auto-unboxed from Boolean to boolean, but auto-unboxing null references throws an NPE.