-2
public class Increment {

    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int c = --a + a++ + ++a * ++b;

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

I cant understand why c == -1, please explain.

Alex K
  • 22,315
  • 19
  • 108
  • 236
  • 1
    There are a lot of examples on SO explaining the increment and decrement operations in different scenarios. Please look at them instead. Adding another complex scenario question is unlikely to help future readers. – Keppil Jun 06 '16 at 13:29
  • 2
    Please don't do weird things with operators. – Maroun Jun 06 '16 at 13:30

1 Answers1

2

You evaluate the unary operators from left to right. Then the multiplication is evaluated before the additions.

int c=--a   +  a++   +  ++a   *  ++b;
       -1   +   -1   +   (1   *   1)    =  -2 + (1 * 1) = -1
      a==-1    a==0     a==1    b==1
Eran
  • 387,369
  • 54
  • 702
  • 768