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.
This is probably a habit learned from C, to avoid this sort of typo (single
=instead of a double==):The convention of putting the constant on the left side of
==isn’t really useful in Java since Java requires that the expression in anifevaluate to abooleanvalue, so unless the constant is aboolean, you’d get a compilation error either way you put the arguments. (and if it is a boolean, you shouldn’t be using==anyway…)