简体   繁体   English

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

[英]What (in the specs) warrants that 'non short circuit logical operators will in fact not short circuit?

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. 所以换句话说boolean a = f() & g() ,其中f()g()都返回boolean, 两者总是会被计算。
However, JLS says only: 但是, JLS只说:

15.22.2 Boolean Logical Operators &, ^, and | 15.22.2布尔逻辑运算符&,^和|
When both operands of a &, ^, or | 当两个操作数都是&,^或|时 operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. operator的类型为boolean或Boolean,则按位运算符表达式的类型为boolean。 In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary. 在所有情况下,操作数都根据需要进行拆箱转换(第5.1.8节)。

For &, the result value is true if both operand values are true; 对于&,如果两个操作数值都为真,则结果值为true; otherwise, the result is false. 否则,结果是错误的。

For ^, the result value is true if the operand values are different; 对于^,如果操作数值不同,则结果值为true; otherwise, the result is false. 否则,结果是错误的。

For |, the result value is false if both operand values are false; 对于|,如果两个操作数值都为false,则结果值为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. 除了xor之外,如果其中一个参数(并且它可能是第二个/右首先被评估)违反条件,您仍然能够中断并返回结果。
Eg. 例如。 a & b would need only to evaluate b to be false to evaluate the expression to false. a & b只需将b评估为false即可将表达式计算为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? 短路实施会违反语言标准吗?

See JLS 15.7.2 Evaluate Operands before Operation 请参阅JLS 15.7.2在操作之前评估操作数

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. Java编程语言还保证在执行操作本身的任何部分之前,操作符的每个操作数(条件运算符&&,||和?:)除外。

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. 另外,在那之前的部分明确地要求首先评估任何二元运算符的左操作数。

The JLS explicitly states that shortcutting is performed for conditional-or and the conditional-and. JLS明确指出对条件 - 和条件 - 和。执行快捷方式。 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 || 15.24条件运算符||

The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true. &&运算符类似于&(§15.22.2),但仅当其左侧操作数的值为true时才计算其右侧操作数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM