Java operator exaggerates confusion && and ++

&& is a short-circuit operator evaluated from left to right, so if the operand on the left side of the && operator evaluates to false, the evaluation should not continue. BUT I was expecting ++ to be evaluated before && because it is a higher priority and (by reference):

Operators with a higher priority are evaluated before operators with a relatively lower priority.

In this case, why doesn't count increase in the third line of this code?

 int mask = 2; int count = 0; if( !(mask > 1) && ++count > 1) { mask += 100; } System.out.println(mask + " " + count); 
+7
java operators operator-precedence
source share
5 answers

Edit: I missed the point. See Revision for a semi-detailed explanation of short circuit ratings.

Each operand of logical logical and logical OR operators is a separate expression and, therefore, is processed independently (taking into account the own order of operators for evaluation in accordance with the correct priority of the operator).

So, although ++ has the highest priority in ++count > 1 , this expression is never evaluated (due to a short circuit).

+4
source share

No, the way to evaluate an expression goes from left to right, taking into account the priority of the operator. So, once a && b a gets false, b no longer evaluated.

JLS ยง15.23. The conditional and operator & says:

The conditional and operator && is similar to & (ยง15.22.2), but evaluates its right operand only if the value of its left operand is true.


Below is a detailed step-by-step evaluation of how this works:

This is because when you analyze an expression as a person, you start first with the operators with the lowest priority. In this case, && has a lower priority than ++ .

 ++a < 0 && foo ^^ start 

To calculate the && result, you need to know the left operand:

 ++a < 0 && ++b ^^^^^^^ so compute this 

Now, when you want to know the result ++a < 0 , first look at the operator with the lowest priority, which is < :

 ++a < 0 ^ evaluate this 

To do this, you will need both the left and the right operand. So, calculate them:

 ++a 0 

This is the moment when ++a incremented.

So now we are down. Back to the top:

 false && foo 

And this is where the fact that && is short-circuited. The left operand is false, so the correct expression for the operand foo no longer evaluated. Which becomes:

 false 

without evaluating the correct operand.

+6
source share

You seem unhappy with the explanation of the short circuit assessment. Why is priority not important here?

Imagine this code:

 if( !(mask > 1) && Increment(count) > 1) 

A functional application (i.e. calling a function by writing parentheses) is the highest priority, but a function call will never happen. If the evaluation of the short circuit made an assumption of priority, this would not work at all.

To expand a bit - operators like ++, - etc., all map to function calls. However, the compiler must understand the order that they link. This is not the order in which they are evaluated.

So, a + b * c does not mean training b * c, then the amount. This only means add( a, multiply(b, c)) The evaluation order is different in the case of && (although not in my example), in order to simplify the programmers quick code.

+4
source share

To avoid confusion, recall that

 if (a && b) { // do something } 

semantically the same as:

 if (a) { if (b) { // do something } } 

Obviously, if a is false, the term b will never be visible. In particular, the increment operand will not be executed there.

Remembering this extension, the behavior should be clearer. Itโ€™s better to consider && as a shortcut to the expression above the nested-if. (If you have an else you will also need to add two copies of the else code, so this is not so simple.)

A more โ€œaccurateโ€ representation will be to use the condition method:

 boolean condition() { if (!a) return false; if (!b) return false; return true; } 

Again, b will never be evaluated if a already returns false.

+1
source share

if (! (mask> 1) & ++ count> 1) {mask + = 100; }

What does this line mean:! !(2>1) i.e. !(true) i.e. false

In && left side is false, that's not all for checking rights

therefore, count does not increase.

0
source share

All Articles