java (obj == null) vs (null == obj)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6883646/
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
(obj == null) vs (null == obj)?
提问by Luke Vo
My boss said I should use null == obj
, because it's better than obj == null
, but he didn't remember why to do this. Is there any reason for using null == obj
?
I feel it somehow... opposite!
我的老板说我应该使用null == obj
,因为它比 好obj == null
,但他不记得为什么要这样做。有什么使用理由null == obj
吗?
我感觉它以某种方式......相反!
After some search on Google, the only thing I found is:
在谷歌上搜索后,我唯一发现的是:
in C, it prevents you accidentally from typing (obj = null) in a conditional structure.
在 C 中,它可以防止您不小心在条件结构中键入 (obj = null)。
回答by Femaref
You can't accidently assign null
to obj
by typing obj = null
instead. However, that's a reminiscence from C times, in java, it is not possible, as the =
expression returns the right hand side of the assignment. As null
is not a boolean
, the compiler will complain.
您不能通过键入来意外地分配null
给。然而,这是对 C 时代的回忆,在 Java 中,这是不可能的,因为表达式返回赋值的右侧。由于不是,编译器会抱怨。obj
obj = null
=
null
boolean
I would try to explain it to my boss once, demonstrate it. If he still doesn't agree with you, just do it. It's a petty thing to fight about with your boss.
我会试着向我的老板解释一次,演示一下。如果他还是不同意你,那就去做吧。和老板吵架是件小事。
回答by Mohammad Suhaib
If you compile your file with if(null==obj)
, the generated byte code is if_acmpne
and in case of if(obj==null)
it is ifnonnull
. Now in if_acmpne
two operands are popped out of stack and checked they are not equal(in this case null
and obj
), and in ifnonnull
, only one operand is popped and checked if it is not null. From this it seems ifnonnull
is better as it involves popping of only one operand.
如果你用 编译你的文件if(null==obj)
,生成的字节码是if_acmpne
,如果if(obj==null)
是ifnonnull
. 现在,if_acmpne
两个操作数从堆栈中弹出并检查它们不相等(在本例中为null
和obj
),并且在 中ifnonnull
,如果不为空,则仅弹出并检查一个操作数。从这看起来ifnonnull
更好,因为它只涉及一个操作数的弹出。
References : http://www.artima.com/underthehood/flowP.html
回答by Olle Hallin
In Java, there is no difference.
在Java中,没有区别。
I prefer (obj==null) since it feels more natural.
我更喜欢 (obj==null) 因为它感觉更自然。
回答by Paul
I have never heard of this before, but it seems like the reasoning you gave is solid. I also think it feels backwards but, it should not have any issues other than "feeling" wrong. I don't think it would have any performance benefits or anything like that.
我以前从未听说过这个,但你给出的推理似乎是可靠的。我也认为它感觉向后但是,除了“感觉”错误之外,它不应该有任何问题。我认为它不会有任何性能优势或类似的东西。
回答by Bitmap
If you do pass a condition as such if (obj=null)
or if (null=obj)
in present day java IDE's, it will highlight it as syntax error, In addition - attempt to compile will signal the error.
如果您确实传递了这样的条件if (obj=null)
或if (null=obj)
在当今的 Java IDE 中,它会将其突出显示为语法错误,此外 - 尝试编译将发出错误信号。
Both (obj==null)
and (null==obj)
are acceptable, they both carry the same overhead, the later do not yield any performance whatsoever. Decision to using either depends on code style adopted to maintain uniform style across classes.
这两个(obj==null)
和(null==obj)
是可以接受的,他们都携带相同的开销,后来不产生任何任何性能。使用任何一种的决定取决于为保持跨类的统一风格所采用的代码风格。
回答by GullerYA
:)
:)
One thing I've found useful about this is that the below syntax (absolutely valid from my POV) is not causing spotbugs
' DC-DOUBLECHECK
warning, allowing to raise spotbugs
threshold without any special exclusion etc:
我发现对此有用的一件事是,以下语法(从我的 POV 中绝对有效)不会导致spotbugs
'DC-DOUBLECHECK
警告,允许在spotbugs
没有任何特殊排除的情况下提高阈值等:
if (defaultClient == null) {
synchronized (DEFAULT_CLIENT_INIT_LOCK) {
if (null == defaultClient) {
...
}
}
}