逻辑运算符:&& ||
位运算符:&与、| 或、~非、^异或
1、逻辑运算会出行短路现象,即在多个逻辑运算表达式共同作用下,如果中途已经明确逻辑运算结果,则后续逻辑表达式不会再进行运算。
int b = 2, c = 3; boolean result = (b == 2) || (c++ == 3); System.out.println(c); System.out.println(result); result = (b != 2) && (c++ == 3); System.out.println(c); System.out.println(result);
结果:
3 true 3 false
2、位运算符&、|、^ 可以作用于布尔型,但~不行
3、char,byte 或者short 数值位运算时,运算结果自动转型为int,如下程序则会编译报错:Type mismatch: cannot convert from int to short
short x=1,y=2,z; z=x&y;
浙公网安备 33010602011771号