&&与&的区别?
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