Null == foo versus foo == null

It may just be a style question, but I am reading a Java coding book (“Android Programming”), and the author all declares null first before the variable method, with a practice that I am not familiar with. For instance:

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

or

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

instead

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

I don’t see how order will matter semantically / syntactically, or am I wrong here? Actually just curious. Thanks.

+4
source share
5 answers

This is probably a habit left over from C / C ++. In C, you put the constants on the left, because if you make a mistake = instead of == , there will be an error, because you cannot assign something to a constant. In Java, this is not necessary because if (foo = null) also gives an error that says that the reference to the object is not logical.

+11
source

This is an excerpt from C / C ++. It was beneficial to put the value to the left of the == operator if you accidentally used the assignment operator =. The C compiler will catch 14 = var as an error, but var = 14 will compile when you would like to enter var == 14. There is not much reason to do this in Java, but some still do.

+5
source

Sometimes an order saves you from a null pointer exception, for example. if the String variable comes from somewhere, and you compare it like this:

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

then you can get a null pointer exception . On the other hand, if you do it like this:

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

then you will not only achieve your goal, but also avoid the null pointer exception if String foo was null .

+4
source

No difference.

The second is simply because C / C ++, where programmers always performed assignments instead of comparisons.

eg.

 // 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 the java compiler generates a compilation error. There is no difference between the two forms. There is no performance issue, but the following notes:

  • The first form is read to read code, because people usually read codes from left to right.

  • The second form is better for a code writer, because in java = operator is for assignment and == operator for test equivalent, but people are usually used in the expression if = instead of ==, according to the second convention, the developer gets compilation-time-error, because null cannot use the left side assignment instructions.

ADDED

 if (object = null) { 

The convention of putting a constant on the left side of == is not really useful in Java, since Java requires that the expression in if evaluate to a boolean value, so if the constant is not boolean, you will get a compilation error, how do you put the arguments. (and also if this is a boolean, you should not use == in any case ...)

+2
source

It makes no difference, but

 if (foo == null) enter code here 

is a preferred method; however, in C you put the constants to the left, since an error would occur if you used = instead of ==

+1
source

All Articles