java if(null!=variable) 为什么不是 if(variable!=null)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4985074/
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
if(null!=variable) why not if(variable!=null)
提问by giri
Hi In our company they follow a strict rule of comparing with null values. When I code if(variable!=null)in code review I get comments on this to change it to if(null!=variable). Is there any performance hit for the above code? If anybody explains highly appreciated.
嗨,在我们公司,他们遵循与空值进行比较的严格规则。当我在代码中编写 if(variable!=null) 时,我会收到对此的评论以将其更改为if(null!=variable)。上述代码是否有任何性能损失?如果有人解释高度赞赏。
Thanks in advance
提前致谢
回答by JB Nizet
I don't see any advantage in following this convention. In C, where boolean types don't exist, it's useful to write
我认为遵循这个约定没有任何好处。在 C 中,布尔类型不存在,编写
if (5 == variable)
rather than
而不是
if (variable == 5)
because if you forget one of the eaqual sign, you end up with
因为如果你忘记了一个等号,你最终会得到
if (variable = 5)
which assigns 5 to variable and always evaluate to true. But in Java, a boolean is a boolean. And with !=, there is no reason at all.
它将 5 分配给变量并始终评估为真。但在 Java 中,布尔值是一个布尔值。有了 !=,就完全没有理由了。
One good advice, though, is to write
不过,一个好的建议是写
if (CONSTANT.equals(myString))
rather than
而不是
if (myString.equals(CONSTANT))
because it helps avoiding NullPointerExceptions.
因为它有助于避免 NullPointerExceptions。
My advice would be to ask for a justification of the rule. If there's none, why follow it? It doesn't help readability.
我的建议是要求对规则进行论证。如果没有,为什么要遵循它?它无助于可读性。
回答by Karl von Moor
This has nothing to do with performance. It's used to prevent that you assign accidentally instead of comparing. An assignment null = var
won't make any sense. But in Java var = null
also won't compile so the rule of turning them around doesn't make sense anymore and only makes the code less readable.
这与性能无关。它用于防止您意外分配而不是比较。一个任务null = var
没有任何意义。但是在 Java 中var = null
也不会编译,所以改变它们的规则不再有意义,只会降低代码的可读性。
回答by Joe Enos
No performance difference - the reason is that if you get used to writing (null == somevar)
instead of (somevar == null)
, then you'll never accidentally use a single equals sign instead of two, because the compiler won't allow it, where it will allow (somevar = null)
. They're just extending this to !=
to keep it consistent.
没有性能差异 - 原因是,如果您习惯于编写(null == somevar)
而不是(somevar == null)
,那么您永远不会意外地使用单个等号而不是两个等号,因为编译器不会允许它,而它会允许(somevar = null)
. 他们只是将其扩展!=
到保持一致。
I personally prefer (somevar == null)
myself, but I see where they're coming from.
我个人更喜欢(somevar == null)
我自己,但我知道他们来自哪里。
回答by a_horse_with_no_name
It's a "left-over" from old C-coding standards.
它是旧 C 编码标准的“遗留物”。
the expression if (var = null)
would compile without problems. But it would actually assign the value null to the variable thus doing something completely different. This was the source for very annoying bugs in C programs.
该表达式if (var = null)
可以毫无问题地编译。但它实际上会将值 null 分配给变量,从而做一些完全不同的事情。这是 C 程序中非常烦人的错误的来源。
In Java that expression does not compile and thus it's more a tradition than anything else. It doesn't erver any purpose (other than coding style preferences)
在 Java 中,该表达式不会编译,因此它比其他任何东西都更像是一种传统。它没有任何目的(除了编码风格偏好)