1

Possible Duplicate:
Null check in Java

I'm just wondering what's the difference between

if (null == something) {...}

and

if (something == null) {...}

assuming that in both cases something is the same object. Presumably, there should be non, except for readability of the code.

Community
  • 1
  • 1
Denys S.
  • 6,358
  • 12
  • 43
  • 65
  • No diff. considering behaviors and performance – jmj Oct 27 '10 at 07:15
  • 1
    This question shows again that the simpler the question, the higher the noise. 2 wrong answers of the first 4 answers, because everyone rushes to give it a shot. – Daniel Oct 27 '10 at 07:21
  • 1
    See [Null check in Java](http://stackoverflow.com/questions/2369226/null-check-in-java) – Matthew Flaschen Oct 27 '10 at 07:24
  • @John That's not Yoda Syntax. Yoda syntax is constant.equals(variable). For == it doesn't matter. – Joeri Hendrickx Oct 27 '10 at 08:49
  • I also had a similar problem [here is what I got as answers](http://stackoverflow.com/questions/2398588/null-or-null-best-performance) – asela38 Oct 27 '10 at 07:38

4 Answers4

7

The statements are completly equivalent.

Differences occure only when using equals, and then null is never used explicitely.

if( "A".equals(someString) ) { ... }

is null safe, since it doesn't fail if someString is null.

if( someString.equals("A") ) { ... }

will obviously fail when someString is null.

Daniel
  • 27,718
  • 20
  • 89
  • 133
  • Well, the question is not about equals method. – Denys S. Oct 27 '10 at 08:49
  • Yes, but the questioner had the vague idea in mind that changing the operands of an comparison would result in differences, so I pointed out, where he might have seen that concept and where it is appliable. – Daniel Oct 27 '10 at 08:58
6

There is no difference.

The idea of putting the constant first helps guard against accidental assignment in the condition. For example in some languages it is valid to say:

if (i = 42) {
    ...
}

i is assigned and the condition is true. If you didn't mean to do this, there is no compiler error and it can be difficult to find.

If you instead always put the constant first:

if (42 == i) {
    ...
}

Then the day you accidentally do:

if (42 = i) {
    ...
}

A compiler error will alert you immediately that you are attempting to assign to a constant.

Chris Wallis
  • 1,263
  • 8
  • 20
  • 1
    It's also possible to assign in the condition of a Java `if` statement, if the type of the variable being assigned is `boolean` – Tom Crockett Oct 27 '10 at 07:38
  • good answer to where it came from http://stackoverflow.com/questions/2369226/null-check-in-java – Denys S. Oct 27 '10 at 08:56
3

Check out this StackOverflow question: object==null or null==object?. It covers the answer of your question.

Community
  • 1
  • 1
Steve
  • 18,660
  • 4
  • 34
  • 27
0

There is no difference.

This is just to guard against the common mistake of using = in place of ==.

if (true = something) {...}

gives an error but

if (something = true) {...}

does not, assuming something is a boolean variable.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 1
    -1 The second will also give an error. Again, this is Java not C. – Daniel Oct 27 '10 at 07:20
  • @Daniel: not if `something` is a boolean (which it is if you are assigning true to it). Your down vote is erroneous. – JeremyP Oct 27 '10 at 08:11
  • @JeremyP: My initial answer was wrong: `if (something = null)` :) – codaddict Oct 27 '10 at 08:39
  • @codaddict: OK, but the down vote is still erroneous *now*. – JeremyP Oct 27 '10 at 08:42
  • Bug in stackoverflow? I just tried to undownvote, but my vote is locked now, because "the answer has not been edited" (which seems wrong). – Daniel Oct 27 '10 at 09:00
  • @Daniel : It might be someone else -1. – Denys S. Oct 27 '10 at 15:24
  • @codaddict there is no boolean with boolean comparison in java as of common use. Assuming that something is boolean value, common use is like next: if (something) {...}, so we're talking only about null values which means that this answer is still missing the point. – Denys S. Oct 27 '10 at 15:25