Java &= 运算符是应用 & 还是 && ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3907082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does the Java &= operator apply & or &&?
提问by pakore
Assuming
假设
boolean a = false;
I was wondering if doing:
我想知道是否这样做:
a &= b;
is equivalent to
相当于
a = a && b; //logical AND, a is false hence b is not evaluated.
or on the other hand it means
或者另一方面这意味着
a = a & b; //Bitwise AND. Both a and b are evaluated.
采纳答案by Stephen C
From the Java Language Specification - 15.26.2 Compound Assignment Operators.
来自 Java 语言规范 - 15.26.2 复合赋值运算符。
A compound assignment expression of the form
E1 op= E2
is equivalent toE1 = (T)((E1) op (E2))
, whereT
is the type ofE1
, except thatE1
is evaluated only once.
形式的复合赋值表达式
E1 op= E2
等价于E1 = (T)((E1) op (E2))
,其中T
是 的类型E1
,只是E1
只计算一次。
So a &= b;
is equivalent to a = a & b;
.
所以a &= b;
相当于a = a & b;
.
(In some usages, the type-casting makes a difference to the result, but in this one b
has to be boolean
and the type-cast does nothing.)
(在某些用法中,类型转换对结果有影响,但在这种b
情况下必须如此boolean
,类型转换什么也不做。)
And, for the record, a &&= b;
is not valid Java. There is no &&=
operator.
而且,为了记录,a &&= b;
Java 是无效的。没有&&=
运营商。
In practice, there is little semantic difference between a = a & b;
and a = a && b;
. (If b
is a variable or a constant, the result is going to be the same for both versions. There is only a semantic difference when b
is a subexpression that has side-effects. In the &
case, the side-effect always occurs. In the &&
case it occurs depending on the value of a
.)
实际上,a = a & b;
和之间的语义差异很小a = a && b;
。(如果b
是变量或常量,则两个版本的结果将相同。只有当b
是具有副作用的子表达式时的语义差异。在这种&
情况下,副作用总是发生。在&&
如果它发生取决于a
.)
On the performance side, the trade-off is between the cost of evaluating b
, and the cost of a test and branch of the value of a
, and the potential saving of avoiding an unnecessary assignment to a
. The analysis is not straight-forward, but unless the cost of calculating b
is non-trivial, the performance difference between the two versions is too small to be worth considering.
在性能方面,权衡是在评估b
成本、测试和分支的成本以及a
避免对 进行不必要分配的潜在节省之间进行权衡a
。分析并不直截了当,但除非计算成本b
非同寻常,否则两个版本之间的性能差异太小,不值得考虑。
回答by Philippe Leybaert
It's the last one:
这是最后一个:
a = a & b;
回答by stew
see 15.22.2 of the JLS. For boolean operands, the &
operator is boolean, not bitwise. The only difference between &&
and &
for boolean operands is that for &&
it is short circuited (meaning that the second operand isn't evaluated if the first operand evaluates to false).
参见JLS 的 15.22.2。对于布尔操作数,&
运算符是布尔值,而不是按位。布尔操作数&&
和&
布尔操作数的唯一区别在于&&
它是短路的(这意味着如果第一个操作数的计算结果为假,则不计算第二个操作数)。
So in your case, if b
is a primitive, a = a && b
, a = a & b
, and a &= b
all do the same thing.
所以在你的情况下, ifb
是一个原始的,a = a && b
, a = a & b
,并且a &= b
都做同样的事情。
回答by Daniel Testa
i came across a similar situation using booleans where I wanted to avoid calling b() if a was already false.
我在使用布尔值时遇到了类似的情况,如果 a 已经是假的,我想避免调用 b()。
This worked for me:
这对我有用:
a &= a && b()
回答by user7291698
Here's a simple way to test it:
这是一个简单的测试方法:
public class OperatorTest {
public static void main(String[] args) {
boolean a = false;
a &= b();
}
private static boolean b() {
System.out.println("b() was called");
return true;
}
}
The output is b() was called
, therefore the right-hand operand is evaluated.
输出为b() was called
,因此评估右侧操作数。
So, as already mentioned by others, a &= b
is the same as a = a & b
.
所以,正如其他人已经提到的, a &= b
与a = a & b
.