1

Has there ever been a performance difference in any of the java versions released to date between

foo != null

and

null != foo?

I personally do not think there is any change in behavior of the above two forms of null check of an object, but not sure about which one performs better. I know the difference will be very low, if any, but was wondering why someone had written all the code in that fashion.

MozenRath
  • 9,652
  • 13
  • 61
  • 104
  • I don't there is a performance difference. If there is, it should be very hard to measure. – Peter Jaloveczki Jun 17 '13 at 08:46
  • There should be no performance difference but form readability perspective foo != null is better. Because generally on the left side we put what we want to compare against the value on the right side. Generally we will be comparing our object like foo against null. Logically speaking there is no point of checking null against foo. – Juned Ahsan Jun 17 '13 at 08:48
  • 1
    See [here](http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2430307#2430307) for more about Yoda conditions. In certain C circles they were quite popular as a way to avoid accidental assignments instead of comparisons (the compiler errors because the constant is not an lvalue). Performancewise I'd be very surprised to see they perform differently. – fvu Jun 17 '13 at 09:01
  • I found a pretty interesting result (detailed in my answer). – Steve P. Jun 17 '13 at 09:19

6 Answers6

6

There should be no performance difference but form readability perspective foo != null is better. Because generally on the left side we put what we want to compare against the value on the right side. Generally we will be comparing our object like foo against null.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
5

This came from old C times. And it started with foo == null (null == foo).

In c syntax was valid (compiler did not complain):

if (foo = null) {
}

To prevent this subtle error syntax was started to use:

if (null == foo) {
}

Because null is Constant this if (null = foo) { }

becomes syntax error.

In java there is only one case where this matters:

boolean someBoolean;
if (someBoolean = true) {
}

is runtime error, but compiles

This

boolean someBoolean;
if (true = someBoolean) {
}

is compile error.

There is no performance issues, only error prevention in some cases.

timoras
  • 98
  • 11
4

Equality is symmetric. So the order won't matter semantically.

Conventional style says you put the thing you're checking first and the constant last. This follows (at least many European) linguistic forms, e.g. "is x null?" not "is null x?".

At one point in history, people used to write if (NULL == x) in C. This was so that the compiler would catch an accidental syntax error if (NULL = x) by saying "you can't assign to a constant". Standard code quality tools can now pick up these types of errors, so this practise is now obsolete.

Joe
  • 46,419
  • 33
  • 155
  • 245
3

The fashion is about yoda style. This is only matter of personal preferences.

"Yoda Conditions"— using if (constant == variable) instead of if (variable == constant), like if (4 == foo). Because it's like saying "if blue is the sky" or "if tall is the man".

More on SO

Community
  • 1
  • 1
2

No there is no difference between the two forms.

No matter what, there are the following instructions - load the value of foo, compare with 0 (null) and read the comparison result.

Now, if it was the equals() method, that would be different :)

polomo12
  • 95
  • 1
  • 6
2
public class Stack
{
    public static void main(String[] args)
    {
        Object obj0 = null;
        Object obj1 = new Object();
        long start;
        long end;
        double difference;  
        double differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (obj0 == null);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);
        differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (null == obj0);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);
        differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (obj1 == null);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);
        differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (null == obj1);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);      
    }
}

enter image description here

In the case when the Object was initialized, nothing is gleaned; however, when the Object is null, in almost every average case run, obj0 == null was faster than null == obj0. I ran 21 additional executions, and in 20 of them this was the case.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • 1
    This led me to do some testing and make a post in another [thread](http://stackoverflow.com/questions/17144457/null-object-and-null-null-comparison-efficiency). I'm trying to figure out what the comparison is so much faster when the `Object` is initialized as opposed to when it is `null`. – Steve P. Jun 17 '13 at 09:45
  • ur results arent exactly unbiased to the way you wrote code and the order in which you call the different test cases. I was way too hasty to accept something that i thought was possible but isn't actually true. – MozenRath Jun 17 '13 at 10:31