- &&与&的区别
1、最终结果一样。
2、&&具有短路效果。左边是false,右边则不执行。
class ysf { public static void main(String[] args) { 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 } }
- ||与|的区别
1、最终的结果一样
2、||具有短路效果,左边为true,右边不执行
class ysf {
public static void main(String[] args) { 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 = true System.out.println(a > b || b < c); //false && true = true System.out.println(a > b || b > c); //false && false = false } }
- &,|,^,~的用法
&:有0则0
| :有1则1
^:相同则0,不同则1~:按位取反
class ysf {
public static void main(String[] args) { int a = 10; int b = 20; int c = 30; System.out.println(6 & 3); //2 System.out.println(6 | 3); //7 System.out.println(6 ^ 3); //5 System.out.println(~6); //-7 } }