Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 282
wmz Avatar asked Feb 13 '12 19:02

wmz


People also ask

Which operator is a non short circuiting logical?

The | and & logical operators, known as non-short circuit operators, should not be used. Using a non-short circuit operator reduces the efficiency of the program, is potentially confusing and can even lead to the program crashing if the first operand acts as a safety check for the second.

What is does it mean if a logical operator short circuits?

In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned.

What is correct for logical AND and short circuit and?

The logical AND expression is a short-circuit operator. As each operand is converted to a boolean, if the result of one conversion is found to be false , the AND operator stops and returns the original value of that falsy operand; it does not evaluate any of the remaining operands.

Which type of operators use short-circuit evaluation?

So when Java finds the value on the left side of an || operator to be true, then Java declares the entire expression to be true. Java's && and || operators use short circuit evaluation.


2 Answers

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.

like image 93
Philipp Wendler Avatar answered Oct 05 '22 22:10

Philipp Wendler


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.

like image 31
Devon_C_Miller Avatar answered Oct 05 '22 23:10

Devon_C_Miller