可能的重复项:
What's the difference between | and || in Java?
我想知道&和&&的区别是什么?
几天后,我为一条if
语句写了一个条件,看起来像这样:
if(x < 50 && x > 0)
但是,我将&&更改为&,它没有显示任何错误。有什么关系呢?
示例:我编译了这个简单的程序:
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");
}
}
}
它打印"OK“和"Yup”。那么,如果它们都能工作,我使用哪一个有关系吗?
发布于 2011-08-26 03:23:05
&
是按位的。&&
是符合逻辑的。
&
对操作的两端进行评估。
&&
计算操作的左侧,如果为true
,则继续并计算右侧。
发布于 2011-08-26 03:22:14
&是比较每个操作数的位的按位AND运算符。
例如,
int a = 4;
int b = 7;
System.out.println(a & b); // prints 4
//meaning in an 32 bit system
// 00000000 00000000 00000000 00000100
// 00000000 00000000 00000000 00000111
// ===================================
// 00000000 00000000 00000000 00000100
&&是逻辑AND运算符,仅比较操作数的布尔值。它接受两个表示布尔值的操作数,并对它们进行惰性求值。
发布于 2011-08-26 03:18:54
&& ==逻辑与
&=按位与
https://stackoverflow.com/questions/7199666
复制相似问题