Java 哪个更有效:if (null == variable) 还是 if (variable == null)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3021195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:39:17  来源:igfitidea点击:

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

javanullif-statement

提问by Apache

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

在Java中,哪个更有效,有什么区别?

if (null == variable)

or

或者

if (variable == null)

采纳答案by aioobe

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

(类似于这个问题:null==object 和 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.

然而,有趣的是,编译后的字节码(由 OpenJDKs javac 发出)在这两种情况下看起来有点不同。

For boolean b = variable == null:

对于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:

对于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 == nullis the most common, default and preferred style.

正如@Bozho 所说,variable == null是最常见、默认和首选的样式。

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

但是,对于某些情况,我倾向于将 放在null前面。例如在以下情况下:

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

回答by Bozho

No difference.

没有不同。

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

if (variable == null)是(imo)更好的编程风格。

Note that nullis lowercase in Java.

请注意,这null是 Java 中的小写字母。

回答by dfens

no difference

没有不同

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

(null == variables)有时在美好的旧时代使用(C 语言)以避免编写: (variable = NULL)错误

回答by Robert

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

第一个是 C 的悬而未决,在那里写是完全合法的 if(variable = NULL)

回答by Edward Dale

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

这就是所谓的“Yoda 条件”,目的是防止您不小心使用赋值 ( =) 而不是等式检查 ( ==)。

回答by polygenelubricants

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.

下次请使用搜索功能。

回答by Code.....

There is not any difference。

没有任何区别。

回答by chiccodoro

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.

我的观点:不要在意这种微不足道的性能优化。如果您的性能不佳,请查找并定位代码中的真正问题/瓶颈。

回答by Robert Christian

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.

这是支持在左侧使用 null 开始 if 测试的一个论点。

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.

支持以 null 开始 if 测试的第二个论点是,代码的读者非常清楚他们正在查看 null 测试,即使等号右侧的表达式很冗长。

@aiooba points this second argument out as well:

@aiooba 也指出了第二个论点:

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

但是,对于某些情况,我倾向于将 null 放在前面。例如在以下情况下:

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