标签:i++ als 异常处理 ++ int 左移 amp boolean 选择
java 位运算包括:左移( << )、右移( >> ) 、无符号右移( >>> ) 、位与( & ) 、位或( | )、位非( ~ )、位异或( ^ ),除了位非( ~ )是一元操作符外,其它的都是二元操作符。
逻辑运算符&、&&、|、||:
1.逻辑&的运算
boolean a = true;
boolean b = false;
int i = 10;
if(b&(i++)>0)
System.out.print(i); //输出11,即&的右边有进行运算
else
System.out.print(i);
2.短路&&的运算
boolean a = true;
boolean b = false;
int i = 10;
if(b&&(i1++)>0)
System.out.print(i); //输出10,即&&的右边没有运算
else
System.out.print(i);
小结一下:
&:不管&的左边是true还是false,右边都会进行运算
&&: 只要左边是false,右边就不会进行运算
一半情况下都会选择&&,因为这样可以提高效率,也可以进行异常处理,当右边产生异常的时候,同样可以跳过。
1.逻辑 | 的的运算
boolean a = true;
int i = 10;
if(a|(i++)>0)
System.out.print(i); //输出11,即的右边有进行运算
else
System.out.print(i);
2.短路 || 的运算
boolean a = true;
int i = 10;
if(a||(i++)>0)
System.out.print(i); //输出10,即||的右边没有运算
else
System.out.print(i);
小结:
| : 当左边为true时,右边同样进行运算
|| : 当左边为true时,右边不再进行运算
标签:i++ als 异常处理 ++ int 左移 amp boolean 选择
原文地址:http://www.cnblogs.com/lordcheng/p/7609423.html