Moz*_*ath 1 java syntax optimization performance null
在迄今为止发布的任何Java版本中是否存在性能差异?
foo != null
和
null != foo
?
我个人认为对象的上述两种形式的空检查的行为没有任何变化,但不确定哪一种表现更好.我知道差异会非常小,如果有的话,但是想知道为什么有人以这种方式编写了所有代码.
这来自旧C时代.它以foo == null(null == foo)开头.
在c语法有效(编译器没有抱怨):
if (foo = null) {
}
Run Code Online (Sandbox Code Playgroud)
为了防止这种微妙的错误,语法开始使用:
if (null == foo) {
}
Run Code Online (Sandbox Code Playgroud)
因为null是常量,所以if(null = foo){}
变成语法错误.
在java中,只有一个案例重要:
boolean someBoolean;
if (someBoolean = true) {
}
Run Code Online (Sandbox Code Playgroud)
是运行时错误,但编译
这个
boolean someBoolean;
if (true = someBoolean) {
}
Run Code Online (Sandbox Code Playgroud)
是编译错误.
没有性能问题,在某些情况下只有错误预防.
归档时间: |
|
查看次数: |
281 次 |
最近记录: |