6

Possible Duplicates:
What's the comparison difference?
Null check in Java

Most of the developers have the habit of writing the null checking with null in the left hand side.like,

if(null == someVariable)

Does this help any way? According to me this is affecting the readability of the code.

Community
  • 1
  • 1
Sujith
  • 1,127
  • 1
  • 13
  • 20

3 Answers3

15

No, it has no purpose whatsoever in Java.

In C and some of its related languages, it was sometimes used to avoid making this mistake:

if (someVariable = null)

Note the = rather than ==, the author has inadvertently assigned null to someVariable rather than checking for null. But that will result in a compiler error in Java.

Even in C, any modern compiler will have an option to treat the if (someVariable = null) as a warning (or even an error).

Stylistically, I agree with you — I wouldn't say "if 21 you are, I will serve you a drink" (unless I'd already had a couple several and was doing my Yoda impersonation). Mind you, that's English; for all I know it would make perfect sense in other languages, in which case it would be perfectly reasonable style for speakers of those languages.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @TJCrowder, to say it has `no purpose whatsoever in java` would be slightly wrong....If the `someVariable` is a `Boolean`, it doesn't throw a compile error...:) You'll need to Yoda that out...but otherwise java cleans up for you. :) – st0le Nov 13 '10 at 18:11
  • 1
    @st0le: Wow, I'm astonished to find that you're right about that. I did not know that (and have checked it). Thank you! But I'd counter with: Don't test booleans that way, there's no purpose whatsoever to `if (someVariable == true)`, simply use `if (someVariable)` (and of course the `!someVariable` instead of comparing to `false`). (And the fact you can have an inadvertent assignment supports using that style.) The only time I'd use `=` or `!=` with booleans would be if I were comparing two variables (not comparing to a constant), and the construct wouldn't help me there. :-) – T.J. Crowder Nov 13 '10 at 22:04
  • @TJCrowder, well ofcourse, no decent java programmer will actually use that...I was merely throwing an exception, pun intended ;) – st0le Nov 14 '10 at 03:54
  • @st0le: Yeah, thanks again. I was so surprised when I wrote a test case for it and it passed. :-) – T.J. Crowder Nov 14 '10 at 06:29
5

It used to help in 'the olden days' when C compilers would not complain about missing an =, when wanting ==:

// OOps forgot an equals, and got assignment
if (someVariable = null) 
{
}

Any modern C#/Java/C++/C compiler should raise a warning (and hopefully an error).

Personally, I find

if (someVariable == null) 
{
}

more readable than starting with the null.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
1

In your case, I don't see any merit in doing that way. But I prefer the following...

if("a string".equals(strVariable))
{
}

over this..

if(strVariable != null && strVariable.equals("a string"))
{
}
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
  • @T.J. Crowder - yes, it is defined in the contract. From the very link you provided: "For any non-null reference value x, x.equals(null) should return false." – Cowan Oct 29 '10 at 21:40
  • @T.J. Crowder & @Cowan, yes I went back as far as JDK 1.1.1 and it is defined in the contract. http://www.tns.lcs.mit.edu/manuals/java-1.1.1/api/java.lang.Object.html#equals%28java.lang.Object%29 – Rosdi Kasim Oct 30 '10 at 07:41
  • 1
    @Cowan & @Rosdi: I can't believe I've missed that all these years. Thanks. I've removed the earlier comment. Proof that you never stop learning. **Man** do those docs need rewording, all of the unnecessary reiteration of "for any non-null x" totally obscures the remaining points. And sadly, I've met quite a number of contract-breaking implementations of `equals` over the years. :-( – T.J. Crowder Oct 30 '10 at 08:02