views:

422

answers:

8

hi expert, want to ask, in java if(NULL==variables) or if(variables==NULL) which will be more effective and what are the difference,

thanks

+9  A: 

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

Interestingly enough however, the compiled bytecode (as emitted by Suns 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);
aioobe
+1 good find... but performance-wise it's still the same? :)
Bozho
I don't know. It would be cool to inspect the output of the JIT compiler for, say, the x86 implementation of suns java, and figure it out once and for all :D You know how to do that?
aioobe
no. :) (I still wonder why this answer isn't more upvoted..)
Bozho
I can downvote yours, Bozho, if that helps ;)And i can't prove it, but i believe different versions of the JDK would produce differently optimized byte code. I.e. i don't see why another compiler shouldn't produce the same byte code for both.
Stroboskop
@Stroboskop, well, java compilers never put *that* much effort into optimizing the bytecode. Basically, most compilers just have a quite naive translation of java-code to bytecode, for the simple reason that the optimization should be deferred to the JIT compilation in which hardware etc is known. That is, I doubt the produced machine code will be different for the two cases. I mean, they're semantically equivalent, and simple enough to be compiled into the most efficient machine code possible anyway.
aioobe
+6  A: 

No difference.

if (variable == null) is (imo) a better programming style.

Note that null is lowercase in Java.

Bozho
Bruno Rothgiesser
+5  A: 

no difference

(null == variables) was sometimes used in good old times (C language) to avoid writing: (variable = NULL) by mistake

dfens
+2  A: 

The first is a hang over from C where it is perfectly legal to write if(variable = NULL)

Robert
Same with Java..
Martin Dürrmeier
that's not true: null is not of type boolean, so it is not legal
Robert
+5  A: 

That's called "Yoda Conditions" and the purpose is to prevent you from accidentally using assignment (=) instead of equality checking (==).

scompt.com
+2  A: 

Short answer: no difference.

Longer answer: there is stylistic difference which is rather subjective. Some people argue constants should be in the left as a defensive style just in case you mistyped == into =. Some people argue constants should be in the right because it's more natural and readable.

A well designed language combined with a good compiler and static analysis tools, paranoia can be minimized, so you should write the most readable and natural code, which would be the constant on the right.

Related questions

Please use the search function next time.

polygenelubricants
good collection :)
Bozho
A: 

There is not any difference。

Christ
A: 

My opinion: Don't care about such insignificant performance optimizations. If you have bad performance, find and target the real problems/bottlenecks in your code.

chiccodoro