views:

127

answers:

5

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.

+7  A: 

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
Well, the question is not about equals method.
den-javamaniac
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
+3  A: 

Check out this StackOverflow question: http://stackoverflow.com/questions/2369226/null-check-in-java. It covers the answer of your question.

Steve
Great link, explains where it came from.
den-javamaniac
A: 

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.

codaddict
-1 The second will also give an error. Again, this is Java not C.
Daniel
Oops, thanks for pointing.
codaddict
@Daniel: not if `something` is a boolean (which it is if you are assigning true to it). Your down vote is erroneous.
JeremyP
@JeremyP: My initial answer was wrong: `if (something = null)` :)
codaddict
@codaddict: OK, but the down vote is still erroneous *now*.
JeremyP
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
@Daniel : It might be someone else -1.
den-javamaniac
@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.
den-javamaniac
+6  A: 

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 use:

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 good explanation
Daniel
It's also possible to assign in the condition of a Java `if` statement, if the type of the variable being assigned is `boolean`
pelotom
Noted - edited. Thanks!
Chris Wallis
good answer to where it came from http://stackoverflow.com/questions/2369226/null-check-in-java
den-javamaniac
A: 

I also had a similar problem here is what I got as answers

asela38