快捷方式“或分配"(|=) Java 中的运算符 [英] Shortcut "or-assignment" (|=) operator in Java

查看:23
本文介绍了快捷方式“或分配"(|=) Java 中的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在 Java 中进行一长串比较,我想知道其中一个或多个是否正确.比较的字符串又长又难读,所以为了可读性我把它分解了,自动去使用快捷操作符|=而不是negativeValue = negativeValue ||布尔值.

I have a long set of comparisons to do in Java, and I'd like to know if one or more of them come out as true. The string of comparisons was long and difficult to read, so I broke it up for readability, and automatically went to use a shortcut operator |= rather than negativeValue = negativeValue || boolean.

boolean negativeValue = false;
negativeValue |= (defaultStock < 0);
negativeValue |= (defaultWholesale < 0);
negativeValue |= (defaultRetail < 0);
negativeValue |= (defaultDelivery < 0);

我希望 negativeValue 为真,如果有任何默认;值为负.这是有效的吗?它会做我期望的事情吗?我在 Sun 的网站或 stackoverflow 上看不到它提到它,但 Eclipse 似乎没有问题,并且代码编译并运行.

I expect negativeValue to be true if any of the default<something> values are negative. Is this valid? Will it do what I expect? I couldn't see it mentioned on Sun's site or stackoverflow, but Eclipse doesn't seem to have a problem with it and the code compiles and runs.

同样,如果我想执行多个逻辑交集,我可以使用 &= 而不是 && 吗?

Similarly, if I wanted to perform several logical intersections, could I use &= instead of &&?

推荐答案

|= 是一个复合赋值运算符 (JLS 15.26.2) 用于布尔逻辑运算符 | (JLS 15.22.2);不要与条件或 || (JLS 15.24).还有&=^=分别对应布尔逻辑&^ 分别.

The |= is a compound assignment operator (JLS 15.26.2) for the boolean logical operator | (JLS 15.22.2); not to be confused with the conditional-or || (JLS 15.24). There are also &= and ^= corresponding to the compound assignment version of the boolean logical & and ^ respectively.

换句话说,对于boolean b1, b2,这两个是等价的:

In other words, for boolean b1, b2, these two are equivalent:

 b1 |= b2;
 b1 = b1 | b2;

逻辑运算符(&|)与其对应的条件运算符(&&)之间的差异||) 是前者不短路";后者做.即:

The difference between the logical operators (& and |) compared to their conditional counterparts (&& and ||) is that the former do not "shortcircuit"; the latter do. That is:

  • &| 总是计算两个操作数
  • &&|| 有条件地评估右操作数;只有当它的值可能影响二元运算的结果时,才计算正确的操作数.这意味着在以下情况下不会评估正确的操作数:
    • && 的左操作数计算结果为 false
      • (因为无论右操作数的计算结果是什么,整个表达式都是false)
      • & and | always evaluate both operands
      • && and || evaluate the right operand conditionally; the right operand is evaluated only if its value could affect the result of the binary operation. That means that the right operand is NOT evaluated when:
        • The left operand of && evaluates to false
          • (because no matter what the right operand evaluates to, the entire expression is false)
          • (因为无论右操作数的计算结果是什么,整个表达式都是true)

          所以回到你最初的问题,是的,这个结构是有效的,而 |= 并不是 =|| 的等效快捷方式.,它确实计算出你想要的.由于您使用的 |= 运算符的右侧是一个简单的整数比较运算,因此 | 不短路这一事实无关紧要.

          So going back to your original question, yes, that construct is valid, and while |= is not exactly an equivalent shortcut for = and ||, it does compute what you want. Since the right hand side of the |= operator in your usage is a simple integer comparison operation, the fact that | does not shortcircuit is insignificant.

          在某些情况下,需要或什至需要短路,但您的情况不是其中之一.

          There are cases, when shortcircuiting is desired, or even required, but your scenario is not one of them.

          遗憾的是,与其他一些语言不同,Java 没有 &&=||=.这在问题中讨论过为什么 Java 没有条件与和条件或运算符的复合赋值版本?(&&=, ||=).

          It is unfortunate that unlike some other languages, Java does not have &&= and ||=. This was discussed in the question Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=).

          这篇关于快捷方式“或分配"(|=) Java 中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆