java Java布尔|=运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15411219/
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
Java boolean |= operator
提问by Pointer Null
Recently I saw a code using this:
最近我看到一个使用这个的代码:
boolean val = something();
val |= somethingElse();
Interesting part is |= (binary like) operator made on boolean primitive type.
有趣的部分是 |= (类似二进制)运算符,是在布尔基本类型上制作的。
It surprised me that |= exist for boolean, as if it was integer type, and searched Java specification for this operator, but could not find any.
令我惊讶的是 |= 存在于 boolean 中,好像它是整数类型,并在 Java 规范中搜索了此运算符,但找不到任何内容。
I'd be curious if right operand is evaluated if left value already is true.
如果左值已经为真,我会很好奇右操作数是否被评估。
Can someone point me to Java specification of this?
有人可以指出我对此的 Java 规范吗?
回答by NPE
From the JLS:
来自 JLS:
15.26.2. Compound Assignment Operators
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.15.22.2. Boolean Logical Operators
&
,^
, and|
When both operands of a
&
,^
, or|
operator are of typeboolean
orBoolean
, then the type of the bitwise operator expression isboolean
. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.For
|
, the result value is false if both operand values are false; otherwise, the result is true.
形式的复合赋值表达式
E1 op= E2
等价于E1 = (T) ((E1) op (E2))
,其中T
是 的类型E1
,只是E1
只计算一次。当 a
&
、^
或|
运算符的两个操作数都是boolean
orBoolean
类型时,按位运算符表达式的类型是boolean
。在所有情况下,操作数都会根据需要进行拆箱转换(第 5.1.8 节)。对于
|
,如果两个操作数的值都为假,则结果值为假;否则,结果为真。
This means that
这意味着
val |= somethingElse();
is strictly equivalent to
严格等价于
val = val | somethingElse();
(assuming somethingElse()
returns boolean
or Boolean
).
(假设somethingElse()
回报boolean
或Boolean
)。
I'd be curious if right operand is evaluated if left value already is true.
如果左值已经为真,我会很好奇右操作数是否被评估。
Yes, it would be evaluated, since |
does not short-circuit:
是的,它会被评估,因为|
不会短路:
15.7.2. Evaluate Operands before Operation
The Java programming language guarantees that every operand of an operator (except the conditional operators
&&
,||
, and? :
) appears to be fully evaluated before any part of the operation itself is performed.15.24. Conditional-Or Operator
||
Thus,
||
computes the same result as|
onboolean
orBoolean
operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.
Java 编程语言保证在执行操作本身的任何部分之前
&&
,运算符的每个操作数(条件运算符||
、 和除外? :
)似乎都已完全求值。因此,
||
计算与|
onboolean
或Boolean
操作数相同的结果。它的不同之处仅在于右侧操作数表达式是有条件地而不是始终进行计算的。
回答by Theodore Norvell
See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2for the definition of |
. See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2for the defintion of |=
. The definitions are just what you would think.
见http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2的定义|
。见http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2为把定义|=
。这些定义正是您所想的。
What surprises me is the lack of a ||=
operator.
令我惊讶的是缺少||=
操作员。
回答by Jean Waghetti
The bitwise logic operators will have the same effect of "normal" logic operators on booleans.
按位逻辑运算符将对布尔值具有与“普通”逻辑运算符相同的效果。
From Java Language Specification15.22:
从Java 语言规范15.22 开始:
When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.
For &, the result value is true if both operand values are true; otherwise, the result is false.
For ^, the result value is true if the operand values are different; otherwise, the result is false.
For |, the result value is false if both operand values are false; otherwise, the result is true.
当 &、^ 或 | 的两个操作数 运算符是 boolean 或 Boolean 类型,则按位运算符表达式的类型是 boolean。在所有情况下,操作数都会根据需要进行拆箱转换(第 5.1.8 节)。
对于 &,如果两个操作数的值都为真,则结果值为真;否则,结果为假。
对于 ^,如果操作数值不同,则结果值为真;否则,结果为假。
对于 |,如果两个操作数的值都为假,则结果值为假;否则,结果为真。
The only real difference is that bitwise operators cant be used to short-circuit evaluation.
唯一真正的区别是按位运算符不能用于短路评估。
For example, this code will throw NullPointerException
:
例如,此代码将抛出NullPointerException
:
Boolean b1 = new Boolean(true);
Boolean b2 = null;
if (b1 || b2) {
//no null pointer here;
}
if (b1 | b2) {
//null pointer here;
}
回答by Ostap Andrusiv
>>I'd be curious if right operand is evaluated if left value already is true.
Bitwise operators (like |
, &
, ..) evaluate both sides, before completion.
位运算符(如|
, &
, ..)在完成之前评估双方。
Logical operators (like &&
, ||
, ..) can skip the evaluation of the second part in some cases. This is called short-circuit
.
在某些情况下&&
,逻辑运算符(如, ||
, ..)可以跳过第二部分的评估。这称为short-circuit
.
回答by sivi
It is not binary it's an "OR" logic statement means
它不是二进制它是一个“或”逻辑语句意味着
val |= {something else}
is same as the Boolean expression:
与布尔表达式相同:
val == val or {something else}
which is inclusive or ( the regular or used in mathematics expressions and computer science)
包含或(数学表达式和计算机科学中的正则或使用)
In some programming languages or is noted by two || signs and in some by one | one of them is SQL and all database languages that I know of DTD JSON etc..
在某些编程语言中或被两个 || 注释 标志和一些由一| 其中之一是 SQL 和我所知道的 DTD JSON 等所有数据库语言。