if(x)是否与if(x == true)相同? [英] Is if(x) same as if(x==true)?
本文介绍了if(x)是否与if(x == true)相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,假设我有一个if语句和一个布尔值x:
Ok, so let's say I have an if statement and a boolean x:
if (x) {
// some stuff
}
这是怎么回事?这和if(x == true)
一样吗?
What happens here? Does this mean the same thing as if(x == true)
?
推荐答案
是
if(x) {
}
简明等同于
if(x == true) {
}
正如@Sotirios指出的那样,它们在字节码级别上是不同的.考虑以下Java类:
As @Sotirios points out, they are different at the bytecode level. Consider the following Java class:
class Test {
public void foo() {
boolean x = true;
if(x == true) {
}
}
}
发射:
public void foo();
Code:
0: iconst_1
1: istore_1
2: iload_1
3: iconst_1
4: if_icmpne 7
7: return
vs
class Test {
public void foo() {
boolean x = true;
if(x) {
}
}
}
发出:
public void foo();
Code:
0: iconst_1
1: istore_1
2: iload_1
3: ifeq 6
6: return
我认为这与程序的性能或正确性无关.
I don't think this has any bearing on the performance or correctness of the program.
这篇关于if(x)是否与if(x == true)相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文