Which Is More Effective: If (Null == Variable) or If (Variable == Null)

Which is more effective: if (null == variable) or if (variable == null)?

(Similar to this question: Difference between null==object and object==null)

I would say that there is absolutely no difference in performance between those two expressions.

Interestingly enough however, the compiled bytecode (as emitted by OpenJDKs javac) looks a bit different for the two cases.

For boolean b = variable == null:

 3: aload_1               // load variable
4: ifnonnull 11 // check if it's null
7: iconst_1 // push 1
8: goto 12
11: iconst_0 // push 0
12: istore_2 // store

For boolean b = null == variable:

 3: aconst_null           // push null
4: aload_1 // load variable
5: if_acmpne 12 // check if equal
8: iconst_1 // push 1
9: goto 13
12: iconst_0 // push 0
13: istore_2 // store

As @Bozho says, variable == null is the most common, default and preferred style.

For certain situations however, I tend to put the null in front. For instance in the following case:

String line;
while (null != (line = reader.readLine()))
process(line);

if(value == null) vs if(null == value)

I doubt that there's a measurable performance advantage either way. I'll bet the person who told you this didn't have any hard data.

As far as I know, it's a historical artifact, a technique from C and C++ to guard against this mistake:

if (c = null) {
}

The compiler will catch this if you reverse the arguments, since you can't assign something to null.

Difference between null!=variable and variable!=null

No difference.

But people quite often write "abc".equals(foo), instead of foo.equals("abc"), to avoid null point exception.

Why does one often see null != variable instead of variable != null in C#?

It's a hold-over from C. In C, if you either use a bad compiler or don't have warnings turned up high enough, this will compile with no warning whatsoever (and is indeed legal code):

// Probably wrong
if (x = 5)

when you actually probably meant

if (x == 5)

You can work around this in C by doing:

if (5 == x)

A typo here will result in invalid code.

Now, in C# this is all piffle. Unless you're comparing two Boolean values (which is rare, IME) you can write the more readable code, as an "if" statement requires a Boolean expression to start with, and the type of "x=5" is Int32, not Boolean.

I suggest that if you see this in your colleagues' code, you educate them in the ways of modern languages, and suggest they write the more natural form in future.

... != null' or 'null != ....' best performance?

Comparing the generated bytecodes is mostly meaningless, since most of the optimization happens in run time with the JIT compiler. I'm going to guess that in this case, either expression is equally fast. If there's any difference, it's negligible.

This is not something that you need to worry about. Look for big picture optimizations.

Which has better performance: test != null or null != test

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.

So I personally prefer first one because of readability, people tend to read from left to right, which read as if test is not equal to null instead of null is not equal to test.

Should `!var` or `var == NULL` be used?

The difference between:

!var

and

var == NULL

is in the second case the compiler have to issue a diagnostic if var is not of a pointer type and NULL is defined with a cast (like (void *) 0).

Also (as pointed by @bitmask in the comments) to use the NULL macro, you need to include a standard header that defines the NULL macro. In C the NULL macro is defined in several headers for convenience (like stddef.h, stdio.h, stdlib.h, string.h, etc.).

Otherwise the two expressions are equivalent and it is just a matter of taste. Use the one you feel more confortable at.

And for your second question if (var) is the same as if (var != NULL) with the difference noted above.

Checking for null - what order?

The second version ( null == str ) is called a yoda condition.

They both result in the same behavior, but the second one has one advantage: It prevents you from accidentally changing a variable, when you forget one =. In that case the compiler returns an error at that row and you're not left with some weird behavior of your code and the resulting debugging.

null == foo versus foo == null

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.

how to check if the variable is null?

you can use .equals("") or .isEmpty()

check check if the variable is null



Related Topics



Leave a reply



Submit