开发者

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

This question already has answers here: ' ... != null' or 'null != ....' best performance? (16 answers) Closed 6 years ago.

In Java, which will be more effective, and what are the differences?

if (null == variable)

or

if (variable开发者_Python百科 == 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);


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


No difference.

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

Note that null is lowercase in Java.


no difference

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


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.

  • Difference between null==object and object==null
  • what is the difference between null != object and object!=null
  • which way is better “null != object” or “ object != null”?
  • Why does one often see “null != variable” instead of “variable != null” in C#?
  • ‘ … != null’ or ‘null != …’ best performance?


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


There is no material difference from a performance perspective.

However... what if you made a typo and missed a single equals character?

foo = null; // assigns foo to null at runtime... BAD!

versus

null = foo; // compile time error, typo immediately caught in editor,
developer gets 8 hours of sleep

This is one argument in favor of beginning an if test with null on the left hand side.

The second argument in favor of beginning an if test with null is that it's made very clear to the reader of the code that they are looking at a null test, even when the expression on the right of the equals sign is verbose.

@aiooba points this second argument out as well:

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


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.


There is not any difference。

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜