java 哪个性能更好: test != null 或 null != test

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10494563/
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-10-31 01:18:44  来源:igfitidea点击:

Which has better performance: test != null or null != test

javanull

提问by Sandeep Nair

Consider the following two lines of code

考虑以下两行代码

if (test ! = null)

and

if (null != test)

Is there is any difference in above two statements, performance wise? I have seen many people using the later and when questioned they say its a best practice with no solid reason.

在性能方面,上述两个陈述有什么区别吗?我见过很多人使用后者,当被问到时,他们说这是一种没有充分理由的最佳实践。

回答by assylias

They are exactly the same. The second one can make sense when using equals:

它们完全一样。第二个在使用 equals 时有意义:

if("bla".equals(test))

can never throw a NullPointerExceptionwhereas:

永远不能抛出一个NullPointerExceptionwhile:

if(test.equals("bla")) 

can.

能够。

回答by Pau Kiat Wee

No difference.

没有不同。

Second one is merely because C/C++ where programmers always did assignment instead of comparing.

第二个仅仅是因为 C/C++ 程序员总是进行赋值而不是比较。

E.g.

例如

// no compiler complaint at all for C/C++
// while in Java, this is illegal.
if(a = 2) {
}
// this is illegal in C/C++
// and thus become best practice, from C/C++ which is not applicable to Java at all.
if(2 = a) {
}

While java compiler will generate compilation error.

而java编译器会产生编译错误。

So I personally prefer first one because of readability, people tend to read from left to right, which read as if test is not equal to nullinstead of null is not equal to test.

所以我个人更喜欢第一个因为可读性,人们倾向于从左到右阅读,if test is not equal to null而不是阅读null is not equal to test

回答by Tomasz Nurkiewicz

There is no performance difference but I personally find the second one being confusing.

没有性能差异,但我个人认为第二个令人困惑。

回答by IProblemFactory

Second one looks for me like "Yoda condition", i.e. if(2 == x) ...and is much less readable.

第二个看起来像“尤达条件”,即if(2 == x) ...,可读性要低得多。

回答by Ewald

It's best practice to avoid some basic typo's that most modern IDE's will pick up, because sometimes you want to do comparisons between more complex types that are not null and end up doing accidental assignments. So the pattern remains the same, but I've never seen this linked to performance and have never seen it generate special byte code.

最好的做法是避免大多数现代 IDE 会出现的一些基本拼写错误,因为有时您想在非空的更复杂类型之间进行比较,并最终进行意外分配。所以模式保持不变,但我从未见过这与性能相关,也从未见过它生成特殊的字节码。

The idea is to have the static, known value first, so you can't throw any kind of weird exception when you perform the comparison.

这个想法是首先拥有静态的已知值,因此在执行比较时不能抛出任何奇怪的异常。

Neither of the two methods are "more correct" though, so it's entirely up to you to decide what you wish to use.

不过,这两种方法都不是“更正确的”,因此完全由您决定要使用什么。

回答by Alex Stybaev

there is no difference. But in second way you avoid the typo like test = null. Beacause in second way you'll get compiler error.

没有区别。但是在第二种方式中,您可以避免像test = null. 因为在第二种方式中你会得到编译器错误。

回答by JB Nizet

It's not a best practice to use the latter one. Both are equivalent, but the former is easier to read.

使用后者不是最佳实践。两者是等价的,但前者更容易阅读。

This "best practice" comes from C where boolean don't exist. Integers are used instead, and if (foo = 1)is not a syntax error, but is completely different from if (foo == 1).

这种“最佳实践”来自不存在布尔值的 C。取而代之的if (foo = 1)是整数,这不是语法错误,而是与if (foo == 1).

In Java, only boolean expressions can be inside an if, and this practice doesn't make much sense.

在 Java 中,if 中只能使用布尔表达式,这种做法没有多大意义。

回答by Sam

There is no really different between two form. There is no performance issue but there are following notes:

两种形式之间没有真正的区别。没有性能问题,但有以下注意事项:

  • First form is readable for code reader, because people usually read codes Left-To-Right.
  • Second form is better for code writer, because in java =operator is for assignment and ==operator is for test equivalent, but people usually using in ifstatement =instead of ==, by second approch developer getting Compile-Time-Errorbecause null can't use in Left-Sideof a assignment statement.
  • 第一种形式对于代码阅读器来说是可读的,因为人们通常从左到右阅读代码。
  • 第二种形式更适合代码编写者,因为在 Java 中,=运算符用于赋值,而==运算符用于等效测试,但人们通常使用 inif语句=而不是==,由第二个开发人员获取,Compile-Time-Error因为 null 不能在Left-Side赋值语句中使用。