3

I have an expression that I'm confused with the results how it's calculated

int a = 7;
boolean res = a++ == 7 || a++ == 9;
System.out.println("a = " + a);
System.out.println("res = " + res);

This give me as results :

a = 8
res = true

I didn't understand why a get the value 8 I expect a = 9 as a results can some one explain me how it's calculated ?

e2rabi
  • 4,728
  • 9
  • 42
  • 69
  • You have no idea how tempted I was to close this question as a duplicate of one of the questions that asks the difference between `a++` and `++a`. – Powerlord Dec 23 '18 at 20:49
  • @Powerlord it sure is a duplicate, but it's always so **interesting** to see new educative sample code like this line: `boolean res = a++ == 7 || a++ == 9;` that can be used as an exercise for students to get in touch with concepts like operator precedence. – forpas Dec 23 '18 at 20:55
  • Every answer is saying that the post-increment operator increments the variable after the condition is evaluated, which isn't true. The increment happens immediately, but evaluates to the original value. – fgb Dec 23 '18 at 22:01

4 Answers4

3

Lazy evaluation is used here. In alternative only one operand (argument) need to be true to make alternative result true. Left argument is true in this case (because you use postincrementation, a will be incremented after check), so there is no need to check left side of alternative.

It means second incrementation won't be executed.

Pochmurnik
  • 780
  • 6
  • 18
  • 35
  • 2
    It's not "lazy", it's "conditional". The `&&` operator is called the [Conditional-And Operator](https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.23), as opposed to the `&` operator, which is called a [Boolean Logical Operator](https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.22.2) and always evaluates the right side too. – Andreas Dec 23 '18 at 21:25
  • 'Lazy evaluation', not 'Lazy Operator' https://en.wikipedia.org/wiki/Lazy_evaluation – Pochmurnik Dec 23 '18 at 21:32
  • 1
    I would amend "no need." It implies that the JVM doesn't need to, but could if it really wanted -- in other words that this is an optimization. In fact, the JVM is not _allowed_ to evaluate the right-hand expressions if the left-hand one evaluates to true. – yshavit Dec 23 '18 at 23:09
3

The interesting part is this:

boolean res = a++ == 7 || a++ == 9;

The evaluation starts here:

a++ == 7

and it actually is translated to: a == 7 and then a = a + 1
so a == 7 evaluates to true and then a becomes equal to 8.
Since the 1st part of the boolean expression is true,
because of Short Circuit Evaluation for || (the OR logical operator),
the 2nd part a++ == 9 is not evaluated/executed.
This means that a is not incremented again.
If you want to test your understanding of this concept, make this change:

boolean res = ++a == 7 || a++ == 9;

and predict the result before you run the code.

forpas
  • 160,666
  • 10
  • 38
  • 76
1

the ++ operator actually changes the variable value after the statement is executed, when written after the variable (a++ instead of ++a). So res becomes true because a == 7, and after this a's value is changed to 8

Ollebrush
  • 36
  • 3
1

Lets analyse what happen step by step :

int a = 7;
boolean res = 
               a++ == 7               // a in this step is equal to 7 not 8
                         || a++ == 9; // even in this step, when the statement is end
                                      // a it will incremented to 8

so the first statement a++ == 7 is true, for that you get res => true and a => 8

for more details read this Difference between i++ and ++i in a loop?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140