Search code examples
javac#null

What is wrong in comparing a null with an object rather than an object with a null


I just found out that I can compare null with an Object like this,

if(null != Object)

Rather than comparing Object with null, like

Object != null

What may go wrong if use the former approach?

Is that legal? If not then why does compiler accept it?


Solution

  • There's one thing wrong about it - readability. If you want to write a clean code, you should care about the way it will be read in the future. It needs to be obvious, what it does and why it does a certain thing. If you place the "Object" to the right of the evaluation, it becomes less apparent what are you really doing. Well at least in my opinion...