短路问题
| 符号 | 说明 |
|---|---|
| & | 1.但与,如果前后都是布尔型,有假则假,但是如果符号前为false,符号后的判断会继续执行 2.如果该符号前后都是数字看作是位运算 |
| && | 1.双与,有假则假,但是有短路效果,如果符号前为false,符号后的判断就不会执行了 |
| | | 1.单或,如果前后都是布尔类型有真则真,但是如果符号前为true,符号前后都会执行 2.如果该符号前后都是数字看作是位运算 |
| || | 1.双或,有真则真,但是有短路效果,如果符号前为true,符号后的判断就不会执行了 |
package com.atguig.logic;public class Deom02Logic {public static void main(String[] args) {int a = 10;int b = 20;//boolean result01 = (++a > 100) && (++b > 10);// boolean result01 = (++a > 100) & (++b > 10);//boolean result01 = (++a < 100) | (++b > 10);boolean result01 = (++a < 100) || (++b > 10);System.out.println("result01=" + result01);System.out.println("a=" +a);System.out.println("b=" +b);}
}




6.三元运算符
1.格式:boolean表达式?表达式1:表达式2
2.执行流程:先判断如果是true就走?后面的表达式1;否则就走后面的表达式2.
package com.atguig.ternary;public class Demo01Ternary {public static void main(String[] args){//定义一个变量,表示小明的分数int score = 59;String result = score>60?"及格":"不及格";System.out.println(result);}
}
