什么(在规范中)保证“非短路逻辑运算符实际上不会短路”?

发布于 2025-01-04 23:32:06 字数 1082 浏览 0 评论 0原文

这直接受到这个问题的启发。
有许多参考文献/声明表明,当应用于布尔值时,按位运算符不会短路。换句话说,boolean a = f() & g(),其中 f()g() 都返回布尔值,两者始终都会被评估。
然而, JLS 只说:

15.22.2 布尔逻辑运算符 &、^ 和 |
当 a &、^ 或 | 的两个操作数时运算符的类型为 boolean 或 Boolean,则类型为 按位运算符表达式是布尔值。在所有情况下,操作数 如有必要,需进行拆箱转换(第 5.1.8 节)。

对于 &,如果两个操作数都为 true,则结果值为 true; 否则,结果为 false。

对于^来说,如果操作数的值不同,结果值为true; 否则,结果为 false。

对于|,如果两个操作数的值都为假,则结果值为假; 否则,结果为 true。

这如何保证两个操作数都被实际评估?除了xor之外,如果一个参数(并且可能是第一个被求值的第二/右参数)违反条件,您仍然可以中断并返回结果。
例如。 a& b 只需将 b 计算为 false 即可将表达式计算为 false。

请注意:我不是在问它是否是这样实现的(不会短路)——它当然是这样。

我问:

会用短路违反语言来实现它 标准?

This is directly inspired by this question.
There are numerous references/statements that bitwise operators, when applied to booleans, will not short circuit. So in other words boolean a = f() & g(), where f() and g() both return boolean, both always will be evaluated.
However, JLS says only:

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

How this warrants that both operands are actually evaluated? Apart from xor, you are still able to break and return result if one of arguments (and it may be second/right being first to be evaluated) violates condition.
Eg. a & b would need only to evaluate b to be false to evaluate the expression to false.

Please note: I'm not asking if it is implemented this way (does not short circuit) -it certainly is.

I'm asking:

Would implementing it with short circuit violate language
standard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2025-01-11 23:32:06

请参阅 JLS 15.7.2 之前计算操作数操作

Java 编程语言还保证运算符的每个操作数(条件运算符 &&、|| 和 ?: 除外)在执行操作本身的任何部分之前似乎都经过完全评估。

因此,如果您有运算符 &,则在计算最终结果之前需要对两个操作数进行求值。

此外,该部分之前的部分明确要求需要首先计算任何二元运算符的左操作数。

See JLS 15.7.2 Evaluate Operands before Operation

The Java programming language also 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.

So if you have the operator &, both operands need to be evaluated before the final result is computed.

Additionally, the section before that one explicitly requests that the left operand of any binary operator needs to be evaluated first.

梦途 2025-01-11 23:32:06

JLS 明确指出,快捷方式是针对条件或和条件与执行的。它根据按位或/与运算符解释了条件或/与的行为。因此,它强调快捷方式是按位运算符行为的变体。

所以,我想说使用快捷方式会违反标准。这肯定会违背开发商的预期。

15.24 条件或运算符 ||

&&运算符就像 & (第 15.22.2 节),但仅当其左侧操作数的值为 true 时才计算其右侧操作数。

The JLS explicitly states that shortcutting is performed for conditional-or and the conditional-and. It explains the behavior of the conditional-or/and in terms of the bitwise-or/and operators. So, it is emphasizing that shortcutting is a variation in behavior from the bitwise operators.

So, I would say using shortcutting would violate the standard. It would definitely violate developers' expectations.

15.24 Conditional-Or Operator ||

The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文