`

未完 Operators | 运算符

阅读更多

待看:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

java运算符:
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html




&&与&、||与|区别:
&和|既可以做两个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);
	}
输出:
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);
	}
输出:
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);
	}
输出:
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);
	}
输出:
value: 10
count: 11

  • 大小: 7.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics