1

Which one is recommended and to be used to check the Object null-ness?

null != Object

or

Object != null

and other way

null == Object

or

Object == null

...and is there any difference between them?

Mahendra Athneria
  • 1,203
  • 3
  • 16
  • 32
  • 1
    No difference. I've never heard that one of the options would be officially recommended. – home Sep 28 '11 at 06:51
  • 1
    This, or a slight variation of it, has been asked a number of times here and on programmers.SE http://programmers.stackexchange.com/questions/16908/doesnt-if-0-value-do-more-harm-than-good http://stackoverflow.com/questions/797162/is-there-any-difference-between-ifa-5-or-if5-a-in-c/797170 http://stackoverflow.com/questions/271561/why-does-one-often-see-null-variable-instead-of-variable-null-in-c – Tyler Sep 28 '11 at 06:53
  • Why did you put your entire question in a block quote? I've updated the formatting. (And removed the salutation and sign-off, community standards here are not to include them. [I don't agree with the standards, but that's neither here nor there.]) – T.J. Crowder Sep 28 '11 at 06:53
  • 1
    "... but that's neither here nor there". So why mention it? :-) – Stephen C Sep 28 '11 at 07:05
  • 1
    Actually all conditions are wrong since you're doing a null check on `Object` (capital `O`) (you know where I'm going with this) ;-) – Buhake Sindi Sep 28 '11 at 07:05

2 Answers2

5

(In)equality is commutative, so there is no difference.

Historically the former stems from C to avoid accidentally assigning a value in a conditional statement, however that mostly applies to ==, not !=. Also Java requires the condition in a conditional statement to have a boolean value, so the only place where it could go wrong nowadays would be

if (a == false) ...

if you accidentally omit one of the =. A rare case, I guess (though probably not so much, given what students frequently write in their first two terms). Joonas also points out another (more obscure) case in the comments.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    I prefer `null != Object` because if you later need to change it to `==`, you won't accidentally write `Object = null` (which would even compile inside Java `if` statement if `Object`'s type is `Boolean`). – Joonas Pulakka Sep 28 '11 at 06:56
  • Not only `if (a == null)` but also `if (a = null)` *does* compile if `a` is a `Boolean`. At runtime, it tries to unbox the `null`, resulting in NPE. – Joonas Pulakka Sep 28 '11 at 07:21
  • That's what I wanted to say with »accidentally omit one of the `=`«. You don't write assignments in conditionals intentionally, usually. – Joey Sep 28 '11 at 07:25
1

It's always more readable to use

Object != null

because that reads as "the object is not null", which is literally what the condition is. The only case where you want to swap the two is to avoid accidentally using

Object = null

which will return true even though it is not the desired behavior, when you wanted to say

Object == null

but in reality not only do modern tools catch these kinds of mistakes, but wide use of the reverse can actually be an impediment to anyone who has to read the code.

donnyton
  • 5,874
  • 9
  • 42
  • 60
  • How is `Object = null` a mistake that could be catched? It's a perfectly valid statement. A machine has no way to guess that you meant `==`. – Joonas Pulakka Sep 28 '11 at 07:01
  • 1
    A machine may not be able to tell for sure, but some code analysis tools will notice if a programmer tries to assign values in a conditional and spit out a warning. Of course, it is still up to the programmer to use and pay attention to such tools... – donnyton Sep 28 '11 at 07:49