Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object==null or null==object?

Tags:

java

null

I heard from somebody that null == object is better than object == null check

eg :

void m1(Object obj ) {    if(null == obj)  // Is this better than object == null ? Why ?        return ;    // Else blah blah } 

Is there any reasons or this is another myth ? Thanks for help.

like image 643
Jijoy Avatar asked Mar 03 '10 06:03

Jijoy


1 Answers

This is probably a habit learned from C, to avoid this sort of typo (single = instead of a double ==):

if (object = null) { 

The convention of putting the constant on the left side of == isn't really useful in Java since Java requires that the expression in an if evaluate to a boolean value, so unless the constant is a boolean, you'd get a compilation error either way you put the arguments. (and if it is a boolean, you shouldn't be using == anyway...)

like image 88
Laurence Gonsalves Avatar answered Oct 04 '22 00:10

Laurence Gonsalves