未完 Operators | 运算符

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

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


[b][color=red]&&与&、||与|区别:[/color][/b]
&和|既可以做两个boolean间运算符也可以做两个int间运算符;即:
[quote]
boolean & boolean //逻辑与、Logical AND、Boolean AND
boolean | boolean //逻辑或、Logical OR、 Boolean OR
int & int //按位与
int | int //按位或
[/quote]
当左右操作数为两个boolean时,&和|为Boolean Operators ;当左右操作数为int时,他们是Bitwise Operators。

作为Boolean Operators的&和|与&&和||的区别:
[img]http://dl.iteye.com/upload/attachment/192427/0cd0bab7-d1f7-3f8d-90d8-26e1fdc8b993.png[/img]
&&:Conditional AND、Short-circuit AND
||:Conditional OR、Short-circuit OR
&: Logical AND、Boolean AND
|: Logical OR、 Boolean OR
[b][color=red]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.[/color][/b]
例子:
Conditional && and Logical &:
[quote]
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);
}
输出:
[color=red]value: 9
count: 10[/color]

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);
}
输出:
[color=red]value: 9
count: 11[/color]
[/quote]

Conditional || and Logical |:
[quote]
	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);
}
输出:
[color=red]value: 10
count: 10[/color]

	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);
}
输出:
[color=red]value: 10
count: 11[/color]
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值