2

The below code complies to output b1=false.

int x=1;
boolean b1 = ++x >= 1 && x++ == 1;
System.out.println("b1="+b1);

But according to the Java Operator Precedence Table it must output b1=true.

Can any one please explain me step by step what happens?

The order of operators used for this question according to the Java Operator Precedence Table are,

1> postfix - x++

2> unary - ++x

3> relational - >=

4> equality - ==

5> logical AND - &&

The method I used,

1> ++x >= 1 && x++ == 1

2> ++x >= 1 && 1 == 1 now x=2

3> 3 >= 1 && 1 == 1 now x=3

4> true && 1 == 1

5> true && true

6> true

So therefore it should output b1=true

Where have I gone wrong?

What I'm asking is simple, We consider operator precedence over evaluation order for the expression, int x=1+2*3; BUT why use evaluation order over operator precedence for ++x >= 1 && x++ == 1; ???

  • 1
    *Operator precedence* and *order of evaluation* are two different things. The left side of `&&` is evaluated first. (In general, the left side of things is evaluated first.) – Ry- Oct 26 '16 at 08:27

1 Answers1

3

You shouldn't confuse operator precedence and evaluation order. Expressions are evaluated from left to right, regardless of operator precedence.

Step 1: Operator precedence is applied to your expression by grouping sub-expressions:

boolean b1 = ++x >= 1 && x++ == 1;
//becomes
boolean b1 = ( ((++x) >= 1) && ((x++) == 1) );

Step 2: evaluation of the sub-expressions is then done from left to right, regardless of operator precedence: ++x is therefore evaluated before x++.

Note however that x++ is only evaluated if the left part of the condition is true, otherwise the && returns false without evaluating the second condition.

In other words, your code is equivalent to:

int x = 1;
int temp1 = ++x; //temp1 = 2
boolean b1 = false;
if (temp1 >= 1) {
  int temp2 = x++; //temp2 = 2
  if (temp2 == 1) {
    b1 = true;
  }
}

Reference: you can read more about expression evaluation in JLS #15.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • That code wasn't the equivalent - that was for `&`, not `&&`. Edited; feel free to roll back. – Andy Turner Oct 26 '16 at 08:30
  • Indeed good point thanks. – assylias Oct 26 '16 at 08:31
  • Okay thanks, but could you tell me another example where expressions are evaluated from left to right and then operator precedence applied? (without using logical operators) –  Oct 26 '16 at 08:32
  • Say: `x = 1; y = x++ + ++x;` - in that case y will be 1 + 3 = 4. – assylias Oct 26 '16 at 08:39
  • anyway x++ (postfix) has an higher precedence than ++x (unary operator or prefix) –  Oct 26 '16 at 08:42
  • @Otty he fact that x++ has precedence over ++x is probably irrelevant because I don't think you can build an expression that has both prefix and postfix operators applied to the same variable (`++x++` is illegal - but if it were not I suppose it would be evaluated as `++(x++)`)... – assylias Oct 26 '16 at 08:47
  • @assylias, I don't get the answer for my question on your marked as dulicate question, :3 What I'm asking is simple, We consider operator precedence over evaluation order for the expression, `int x=1+2*3;` **BUT** why use evaluation order over operator precedence for `++x >= 1 && x++ == 1;` **???** –  Oct 26 '16 at 09:03
  • @Otty I think the accepted answer on the duplicate contains information to answer your question. In any case I have edited my answer to make it clearer - let me know if it makes more sense now. – assylias Oct 26 '16 at 09:16