null == foo versus foo == null [duplicate]

2019-02-23 23:37发布

问题:

This question already has an answer here:

  • Which is more effective: if (null == variable) or if (variable == null)? [duplicate] 9 answers

This may just be a style question, but I'm reading a Java coding book ('Programming Android') and the writer all declares null first before a variable method, a practice I am not familiar with. For example:

if (null == foo) {
    //code here
}

or

if (null != foo) {
    //code here
}

instead of

if (foo == null) {
    //code here
}

I can't see how the order would make a difference semantically/syntactically, or am I wrong here? Really just curious.

回答1:

It's probably a habit left over from C/C++. In C, you would put constants on the left, because if you mistyped = instead of == there would be an error because you can't assign something to a constant. In Java, this is unnecessary because if (foo = null) also gives an error, which says that an object reference isn't a boolean.



回答2:

This is a holdover from C/C++. It was advantages to put the value on the left of the == operator in case you accidently used the assignment = operator. The C compiler will catch the 14 = var as an error, but var = 14 will compile, when you meant to type var == 14. There is not much reason to do this in Java, but some still do it.



回答3:

Sometimes order saves you from null pointer exception e.g. if a String variable is coming from somewhere and you compare it like this:

if(foo.equals("foo")){
}

then you might get Null pointer exception. On the other hand if you do it like this:

if("foo".equals(foo)){
}

then you not only achieve your purpose but you also avoid a null pointer exception in case String foo was null.



回答4:

No difference.

Second one is merely because C/C++ where programmers always did assignment instead of comparing.

E.g.

// no compiler complaint at all for C/C++
// while in Java, this is illegal.
if(a = 2) {
}
// this is illegal in C/C++
// and thus become best practice, from C/C++ which is not applicable to Java at all.
if(2 = a) {
}

While java compiler will generate compilation error.. There is no really different between two form. There is no performance issue but there are following notes:

  1. First form is readable for code reader, because people usually read codes Left-To-Right.

  2. Second form is better for code writer, because in java = operator is for assignment and == operator is for test equivalent, but people usually using in if statement = instead of ==, by second approch developer getting Compile-Time-Error because null can't use in Left-Side of a assignment statement.

ADDED

if (object = null) {

The convention of putting the constant on the left side of == isn't really useful in Java since Java requires that the expression in an if evaluate to a boolean value, so unless the constant is a boolean, you'd get a compilation error either way you put the arguments. (and if it is a boolean, you shouldn't be using == anyway...)



回答5:

There is no difference, and

if (foo == null)
    enter code here

is the prefered way; however in C, you would put constants to the left since there would be an error if you used = instead of ==



标签: java null