Home » Java » object==null or null==object?

object==null or null==object?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I heard from somebody that null == object is better than object == null check

eg :

void m1(Object obj ) {
   if(null == obj)  // Is this better than object == null ? Why ?
       return ;
   // Else blah blah
}

Is there any reasons or this is another myth ?
Thanks for help.

Answers:

This is probably a habit learned from C, to avoid this sort of typo (single = instead of a double ==):

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

Questions:
Answers:

As others have said, it’s a habit learned from C to avoid typos – although even in C I’d expect decent compilers at high enough warning levels to give a warning. As Chandru says, comparing against null in Java in this way would only cause problems if you were using a variable of type Boolean (which you’re not in the sample code). I’d say that’s a pretty rare situation, and not one for which it’s worth changing the way you write code everywhere else. (I wouldn’t bother reversing the operands even in this case; if I’m thinking clearly enough to consider reversing them, I’m sure I can count the equals signs.)

What hasn’t been mentioned is that many people (myself certainly included) find the if (variable == constant) form to be more readable – it’s a more natural way of expressing yourself. This is a reason not to blindly copy a convention from C. You should always question practices (as you’re doing here 🙂 before assuming that what may be useful in one environment is useful in another.

Questions:
Answers:

This is not of much value in Java (1.5+) except when the type of object is Boolean. In which case, this can still be handy.

if (object = null) will not cause compilation failure in Java 1.5+ if object is Boolean but would throw a NullPointerException at runtime.

Questions:
Answers:

In Java there is no good reason.

A couple of other answers have claimed that it’s because you can accidentally make it assignment instead of equality. But in Java, you have to have a boolean in an if, so this:

if (o = null)

will not compile.

The only time this could matter in Java is if the variable is boolean:

int m1(boolean x)
{
    if (x = true)  // oops, assignment instead of equality

Questions:
Answers:

This also closely relates to:

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

which is convenient if you don’t want to deal with NPEs:

if (bar!=null && bar.equals("foo")) {

Questions:
Answers:

This trick supposed to prevent v = null kind of typos.

But Java allows only boolean expressions as if() conditions so that trick does not make much sense, compiler will find those typos anyway.

It is still valuable trick for C/C++ code though.

Questions:
Answers:

For the same reason you do it in C; assignment is an expression, so you put the literal on the left so that you can’t overwrite it if you accidentally use = instead of ==.

Questions:
Answers:

That is for people who prefer to have the constant on the left side.
In most cases having the constant on the left side will prevent NullPointerException to be thrown (or having another nullcheck). For example the String method equals does also a null check. Having the constant on the left, will keep you from writing the additional check. Which, in another way is also performed later. Having the null value on the left is just being consistent.

like:

 String b = null;
 "constant".equals(b);  // result to false
 b.equals("constant");  // NullPointerException
 b != null && b.equals("constant");  // result to false

Questions:
Answers:

Compare with the following code:

    String pingResult = "asd";
    long s = System.nanoTime ( );
    if ( null != pingResult )
    {
        System.out.println ( "null != pingResult" );
    }
    long e = System.nanoTime ( );
    System.out.println ( e - s );

    long s1 = System.nanoTime ( );
    if ( pingResult != null )
    {
        System.out.println ( "pingResult != null" );
    }
    long e1 = System.nanoTime ( );
    System.out.println ( e1 - s1 );

Output (After multiple executions):

null != pingResult
325737
pingResult != null
47027

Therefore, pingResult != null is the winner.

Questions:
Answers:

Above Output is not correct. Just run the both the block of code separately and you will get the actual result. Both will take the same time.

Also run the below code:

    String pingResult = "asd";
    long s = System.nanoTime ( );
    if ( null != pingResult )
    {
        //System.out.println ( "null != pingResult" );
    }
    long e = System.nanoTime ( );
    System.out.println ( e - s );

    long s1 = System.nanoTime ( );
    if ( pingResult != null )
    {
        //System.out.println ( "pingResult != null" );
    }
    long e1 = System.nanoTime ( );
    System.out.println ( e1 - s1 );