Java位运算和逻辑运算的区别实例,请参考下面代码,希望对你有所帮助
代码如下:
public class Test {
public static void main(String[] args) {
// 逻辑运算符执行的是短路求值,当左边操作数可以推断出表达式的值,就不再执行 了
来源gaodai#ma#com搞@代~码网 int x, y = 10;
if (((x = 0) == 0) || ((y = 20) == 20)) {
System.out.println(y);// 输出10
}
public static void main(String[] args) {
// 逻辑运算符执行的是短路求值,当左边操作数可以推断出表达式的值,就不再执行 了
来源gaodai#ma#com搞@代~码网 int x, y = 10;
if (((x = 0) == 0) || ((y = 20) == 20)) {
System.out.println(y);// 输出10
}
// 位操作运算不管值是如何,任何参与运算的表达式都会被执行求值
int a, b = 10;
if (((a = 0) == 0) | ((b = 20) == 20)) {
System.out.println(b);// 输出20
}
}
}
以上就是Java位运算和逻辑运算的区别实例的详细内容,更多请关注gaodaima搞代码网其它相关文章!