if (null == variable) 和 if (variable == null) 哪个更有效?

发布于 2024-09-05 18:47:53 字数 134 浏览 3 评论 0原文

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

if (null == variable)

或者

if (variable == null)

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

if (null == variable)

or

if (variable == null)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

人│生佛魔见 2024-09-12 18:47:53

(类似于这个问题:null==object 和 object 之间的区别==null)

我想说这两个表达式之间的性能绝对没有区别。

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

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

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

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

然而,对于某些情况,我倾向于将 null 放在前面。例如下面的情况:

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

(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);
帅气尐潴 2024-09-12 18:47:53

这就是所谓的“尤达条件”,其目的是防止您意外使用赋值 (=) 而不是相等检查 (==)。

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

-黛色若梦 2024-09-12 18:47:53

没有区别。

if (variable == null) 是(我认为)更好的编程风格。

请注意,null 在 Java 中是小写的。

No difference.

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

Note that null is lowercase in Java.

小ぇ时光︴ 2024-09-12 18:47:53

没有区别

(null ==变量)
有时在美好的时光中使用(C语言)
避免写:
(variable = NULL) 错误

no difference

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

明天过后 2024-09-12 18:47:53

简短回答:没有区别。

更长的答案:存在相当主观的风格差异。有些人认为常量应该位于左侧,作为一种防御方式,以防万一您将 == 错误地输入到 = 中。有些人认为常量应该在右边,因为它更自然和可读。

一门设计良好的语言加上良好的编译器和静态分析工具,可以最大限度地减少偏执,因此您应该编写最易读和最自然的代码,这将是右侧的常量。

相关问题

请下次使用搜索功能。

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.

零時差 2024-09-12 18:47:53

第一个是 C 语言的遗留问题,在 C 语言中编写 if(variable = NULL) 是完全合法的。

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

烟织青萝梦 2024-09-12 18:47:53

从性能角度来看没有实质性差异。

但是...如果您犯了一个拼写错误并错过了一个等于字符怎么办?

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

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

This is one argument赞成开始左侧为 null 的 if 测试。

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

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

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

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

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);
白芷 2024-09-12 18:47:53

我的观点:不要关心这种无关紧要的性能优化。如果您的性能不佳,请找到并瞄准代码中的真正问题/瓶颈。

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.

风吹短裙飘 2024-09-12 18:47:53

没有任何区别。

There is not any difference。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文