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

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

问题描述

我在Java中有很长的比较,我想知道它们中的一个或多个是否真实。比较字符串很长且难以阅读,因此我将其分解为可读性,并自动使用快捷操作符 | = 而不是 negativeValue = negativeValue || boolean

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 为真默认< something>价值是负面的。这有效吗?它会做我期望的吗?我在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.

换句话说,对于布尔值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

          • (because no matter what the right operand evaluates to, the entire expression is 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天全站免登陆