4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
FugueWeb
  • 744
  • 1
  • 9
  • 19
  • `=` is the assignment operator, you mean `==` (equality operator) – Bart Kiers Sep 21 '13 at 21:00
  • 2
    @Reimeus: Exactly what this convention [used to] prevent -- with the "constant" (`null`) on the left, the idea is that you will encounter a compile time error if you forget that second equals sign, as it would be an invalid assignment otherwise. – Cᴏʀʏ Sep 21 '13 at 21:00
  • 2
    In that way writes the code yoda – nachokk Sep 21 '13 at 21:01
  • e.g. because you avoid aassignment and comparison mistakes. As you have a constant on the peft side and get a compiler warning `"bar" = foo` vs `foo = "bar"` I think it's also called Yoda style – Moritz Roessler Sep 21 '13 at 21:03

5 Answers5

11

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.

tbodt
  • 16,609
  • 6
  • 58
  • 83
5

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.

DrA
  • 437
  • 2
  • 11
4

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.

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • with Strings it makes total Sense to put the "contstant" first to avoid nullpointers. In any other case, its just a matter of your preferences. i prefer (object == null) – Daniel Bo Sep 21 '13 at 21:09
2

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...)

Augustus Francis
  • 2,694
  • 4
  • 22
  • 32
1

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 ==

ayane_m
  • 962
  • 2
  • 10
  • 26