java boolean 与运算_Java boolean | =运算符

最近我看到一个代码使用这个:

boolean val = something();

val |= somethingElse();

有趣的部分是| =(二进制)运算符在布尔基元类型上制作。

令我感到惊讶的是,布尔值存在| =存在,好像它是整数类型,并搜索此运算符的Java规范,但找不到任何。

如果左值已经为真,我会好奇是否评估右操作数。

有人能指出我的Java规范吗?

它只是val = val |somethingElse(); // somethingElse()期望返回布尔值。 这是一个标准的OR

@SudhanshuUmalkar OP知道,他要求在文件中说明。

答案就在这里:dudes:stackoverflow.com/q/2486472/544983

我以为只有|| 对于布尔表达式,所以我希望运算符像|| =。 制作二进制文件是否有意义 在布尔值?

没有快捷方式评估,但没有明确提及。 只在|| 明确提到了快捷逻辑。

@Sudhanshu它不是标准OR,它是一个按位OR运算符(而不是布尔运算符)。 它也是一个积极的运算符,而不是短路运算符(例如布尔运算符)。

来自JLS:

15.26.2. Compound Assignment Operators

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

15.22.2. Boolean Logical Operators &, ^, and |

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 false if both operand values are false; otherwise, the result is true.

这意味着

val |= somethingElse();

严格等同于

val = val | somethingElse();

(假设somethingElse()返回boolean或boolean)。

I'd be curious if right operand is evaluated if left value already is true.

是的,它会被评估,因为|不会短路:

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 | on boolean or Boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.

有关|的定义,请参见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。这些定义正是您的想法。

令我惊讶的是缺少||=运算符。

链接不再工作:(

为了它的价值,我已经更新了链接。

>>I'd be curious if right operand is evaluated if left value already is true.

在完成之前,按位运算符(如|,&,..)会评估双方。

在某些情况下,逻辑运算符(如&&,||,..)可以跳过第二部分的评估。这称为short-circuit。

按位逻辑运算符对布尔值的"正常"逻辑运算符具有相同的效果。

从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.

唯一真正的区别是按位运算符不能用于短路评估。

例如,此代码将抛出NullPointerException:

Boolean b1 = new Boolean(true);

Boolean b2 = null;

if (b1 || b2) {

//no null pointer here;

}

if (b1 | b2) {

//null pointer here;

}

它不是二进制的,它是"OR"逻辑语句的意思

val |= {something else}

与布尔表达式相同:

val == val or {something else}

包容性的(或常用的或用于数学表达式和计算机科学)

在某些编程语言中或由两个||标注标志和一些一个|其中一个是SQL和我所知道的DTD JSON等所有数据库语言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值