Search code examples
javaoopcomparison

(obj == null) vs (null == obj)?


My boss said I should use null == obj, because it's better than obj == null, but he didn't remember why to do this. Is there any reason for using null == obj?
I feel it somehow... opposite!

After some search on Google, the only thing I found is:

in C, it prevents you accidentally from typing (obj = null) in a conditional structure.


Solution

  • You can't accidently assign null to obj by typing obj = null instead. However, that's a reminiscence from C times, in java, it is not possible, as the = expression returns the right hand side of the assignment. As null is not a boolean, the compiler will complain.

    I would try to explain it to my boss once, demonstrate it. If he still doesn't agree with you, just do it. It's a petty thing to fight about with your boss.