1.&&和&的区别?
a:最终结果一样。
b:&&具有短路效果。左边是false,右边不执行.
&是无论左边是false还是true,右边都会执行.
&&的效果:
int a = 10;
int b = 20;
int c = 30;
System.out.println(a < b && b < c); //true && true = true
System.out.println(a < b && b > c); //true && false = false
System.out.println(a > b && b < c); //false && true = false
System.out.println(a > b && b > c); //false && false = false
&的效果:
int a = 10;
int b = 20;
int c = 30;
System.out.println(a < b & b < c); //true & true = true
System.out.println(a < b & b > c); //true & false = false
System.out.println(a > b & b < c); //false & true = false
System.out.println(a > b & b > c); //false & false = false
&&与&的区别:
int x = 3;
int y = 4;
System.out.println((++x == 3) & (++y == 4)); //false & false = false
System.out.println("x = " + x); //x = 4
System.out.println("y = " + y); //y = 5
System.out.println("---------------------------");
System.out.println((++x == 3) && (++y == 4)); //false && false = false
System.out.println("x = " + x); //x = 4
System.out.println("y = " + y); //y = 4
同理:
||与|的区别:
a:最终的结果是一样
b:||具有短路效果,左边为true,右边不执行