if (null == variable) 和 if (variable == null) 哪个更有效?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
(类似于这个问题:null==object 和 object 之间的区别==null)
我想说这两个表达式之间的性能绝对没有区别。
然而有趣的是,两种情况下编译后的字节码(由 OpenJDK javac 发出)看起来有点不同。
对于
boolean b = variable == null
:对于
boolean b = null == variable
:正如 @Bozho 所说,
variable == null
是最常见、默认和首选的样式。然而,对于某些情况,我倾向于将
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
:For
boolean b = null == variable
: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:这就是所谓的“尤达条件”,其目的是防止您意外使用赋值 (
=
) 而不是相等检查 (==
)。That's called "Yoda Conditions" and the purpose is to prevent you from accidentally using assignment (
=
) instead of equality checking (==
).没有区别。
if (variable == null)
是(我认为)更好的编程风格。请注意,
null
在 Java 中是小写的。No difference.
if (variable == null)
is (imo) a better programming style.Note that
null
is lowercase in Java.没有区别
(null ==变量)
有时在美好的时光中使用(C语言)
避免写:
(variable = NULL)
错误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.
第一个是 C 语言的遗留问题,在 C 语言中编写
if(variable = NULL)
是完全合法的。The first is a hang over from C where it is perfectly legal to write
if(variable = NULL)
从性能角度来看没有实质性差异。
但是...如果您犯了一个拼写错误并错过了一个等于字符怎么办?
与
This is one argument赞成开始左侧为 null 的 if 测试。
支持以 null 开始 if 测试的第二个论点是,即使等号右侧的表达式很冗长,代码的读者也可以清楚地知道他们正在查看 null 测试。
@aiooba 也指出了第二个论点:
There is no material difference from a performance perspective.
However... what if you made a typo and missed a single equals character?
versus
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:
我的观点:不要关心这种无关紧要的性能优化。如果您的性能不佳,请找到并瞄准代码中的真正问题/瓶颈。
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。