&之间的差异放大器;和&功放;&安培; [英] Difference between & and &&
问题描述
我始终认为,&放大器;&功放;在Java
运算符用于验证是否其两个布尔操作数是真正
和&放大器;
运算符是用来做两个整数类型位运算
I always thought that &&
operator in Java is used for verifying whether both its boolean operands are true
, and the &
operator is used to do Bit-wise operations on two integer types.
最近,我才知道,&安培;
运营商也可以用来验证其两个布尔操作数是否真正
中,唯一的区别是它检查即使在LHS操作数是假的RHS操作数。
Recently I came to know that &
operator can also be used verify whether both its boolean operands are true
, the only difference is that it checks the RHS operand even if the LHS operand is false.
是&功放;在Java
运算符重载的内部?或者是有这背后一些其他的概念?
Is the &
operator in Java internally overloaded? Or is there some other concept behind this?
推荐答案
&安培; < - 验证两个操作数结果
&功放;&安培; < - 如果第一个操作数计算结果为假,因为其结果将是错误的停止评估
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
&安培; (1 / X&GT; 1)
&LT; - 这意味着评估
然后评估((X = 0!) 1 / X&GT; 1)
然后执行&放;.的问题是,对于x = 0,这将抛出异常。
(x != 0) & (1/x > 1)
<-- this means evaluate (x != 0)
then evaluate (1/x > 1)
then do the &. the problem is that for x=0 this will throw an exception.
&放大器;&安培; (1 / X&GT; 1)
&LT; - 这意味着评估(!X = 0)
,只如果这是真的,那么评估(1 / X&GT; 1)
所以如果你有X = 0,那么这是完全安全的,不会抛出,如果任何异常的计算结果为假(X = 0!)整个事情的直接计算结果为假,而不评估(1 / X&GT; 1)。
(x != 0) && (1/x > 1)
<-- this means evaluate (x != 0)
and only if this is true then evaluate (1/x > 1)
so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1)
.
编辑:
exprA | exprB
&LT; - 这意味着评估 exprA
然后评估 exprB
然后执行 |
exprA | exprB
<-- this means evaluate exprA
then evaluate exprB
then do the |
.
exprA || exprB
&LT; - 这意味着评估 exprA
键,只有当这是假
然后评估 exprB
并执行 ||
。
exprA || exprB
<-- this means evaluate exprA
and only if this is false
then evaluate exprB
and do the ||
.
这篇关于&之间的差异放大器;和&功放;&安培;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!