我只是想知道&和&&之间的区别是什么?
几天后,我为if
声明写了条件,看起来像这样:
if(x < 50 && x > 0)
但是,我将&&更改为just &,并且没有显示任何错误。有什么不同?
示例:我编译了这个简单的程序:
package anddifferences;
public class Main {
public static void main(String[] args) {
int x = 25;
if(x < 50 && x > 0) {
System.out.println("OK");
}
if(x < 50 & x > 0) {
System.out.println("Yup");
}
}
}
它打印了“确定”和“是”。那么,如果两者都起作用,我使用哪一个就无关紧要?
&
是按位的。&&
是合乎逻辑的。
&
评估操作的双方。
&&
评估操作的左侧,如果为true
,它将继续并评估右侧。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句