This question already has an answer here:
I want to know differences between two conditional expressions null == var
and var == null
in if statement of Java.
This question already has an answer here:
I want to know differences between two conditional expressions null == var
and var == null
in if statement of Java.
You can also have a look at this:
Which is more effective: if (null == variable) or if (variable == null)?
There is no difference.Simply a matter of style.
And the first one called as yoda style
Although both conditional expressions
null == var
andvar == null
are equvilent. But suppose if you introduces bug by misspell==
(check equality ) as=
(assignment) thenvar = null
doesn't give you can error and introduce a bug in your code. Whereas you writenull == var
and suppose misspellsnull = var
it produce a compilation time error. And a compilation time error is a way better than a runtime error. That's one of the advantages of the Yoda conditional form.But I also suggest you read Criticism of Yoda continuations:
Many Programmers hate it, as it has to mentally re-reverse it to understand it. (Others obviously don't have that issue). The practice is referred to as "Yoda conditions". Most compilers can be persuaded to warn about assignments in conditions anyway.
No, there's not. Both statements are true exactly when
var
isnull
.There are no difference. for example consider the following piece of code
output
This means that there are no difference