Java 基础知识-逻辑运算符

&&与&的区别?

1、&&与&的最终结果一样。

2、&&具有短路效果。左边是false,右边不执行。

 举个栗子:

&

public class test {
	public static void main(String[] args) {
		int x = 3;
		int y = 4;
		System.out.println((++x == 3) & (++y == 4));
		System.out.println("x = " + x);
		System.out.println("y = " + y);
	}
}
输出结果为:
false
x = 4
y = 5

&&

public class test {
	public static void main(String[] args) {
		int x = 3;
		int y = 4;
		System.out.println((++x == 3) && (++y == 4));
		System.out.println("x = " + x);
		System.out.println("y = " + y);
	}
}

输出结果为:

false
x = 4
y = 4

猜你喜欢

转载自blog.csdn.net/qq_36847713/article/details/80716609
今日推荐