本文共 2094 字,大约阅读时间需要 6 分钟。
java运算符:
&和|既可以做两个boolean间运算符也可以做两个int间运算符;即:
boolean & boolean //逻辑与、Logical AND、Boolean AND
boolean | boolean //逻辑或、Logical OR、 Boolean OR
int & int //按位与
int | int //按位或
当左右操作数为两个boolean时,&和|为Boolean Operators ;当左右操作数为int时,他们是Bitwise Operators。
作为Boolean Operators的&和|与&&和||的区别:
&&:Conditional AND、Short-circuit AND
||:Conditional OR、Short-circuit OR
&: Logical AND、Boolean AND
|: Logical OR、 Boolean OR
Conditional && will not evaluate the right-hand operand if the left-hand operand is false. Conditional || omits the evaluation of the right-hand operand when the left-hand operand is true. 例子:
Conditional && and Logical &:
- public static void main(String[] args) {
- int value = 8;
- int count = 10;
- int limit = 11;
- if (++value%2==0 && ++count<limit) {
- }
- System.out.println("value: " + value);
- System.out.println("count: " + count);
- }
public static void main(String[] args) { int value = 8; int count = 10; int limit = 11; if (++value%2==0 && ++count
输出:
value: 9 count: 10 - public static void main(String[] args) {
- int value = 8;
- int count = 10;
- int limit = 11;
- if (++value%2==0 & ++count<limit) {
- }
- System.out.println("value: " + value);
- System.out.println("count: " + count);
- }
public static void main(String[] args) { int value = 8; int count = 10; int limit = 11; if (++value%2==0 & ++count
输出:
value: 9 count: 11 Conditional || and Logical |:
- public static void main(String[] args) {
- int value = 9;
- int count = 10;
- int limit = 11;
- if (++value%2==0 || ++count<limit) {
- }
- System.out.println("value: " + value);
- System.out.println("count: " + count);
- }
public static void main(String[] args) { int value = 9; int count = 10; int limit = 11; if (++value%2==0 || ++count
输出:
value: 10 count: 10 - public static void main(String[] args) {
- int value = 9;
- int count = 10;
- int limit = 11;
- if (++value%2==0 | ++count<limit) {
- }
- System.out.println("value: " + value);
- System.out.println("count: " + count);
- }
public static void main(String[] args) { int value = 9; int count = 10; int limit = 11; if (++value%2==0 | ++count
输出:
value: 10 count: 11 转载地址:http://mblmi.baihongyu.com/