8

I was wondering, what does the -->-- operator do in Java?

For example, if I have the following code:

int x = 3;
int y = 3;
if (x -->-- y) {
    return true;
}

This always returns true.

Thank you!

gparyani
  • 1,958
  • 3
  • 26
  • 39
MarkusWillson
  • 411
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator – Sotirios Delimanolis Nov 30 '14 at 04:12
  • 1
    Related to, but not a duplicate of the question http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator because that is a C++ program. – Raedwald Nov 30 '14 at 13:11
  • 1
    I think OP may want to edit the question to acknowledge the pseudo-dupe, and reword accordingly to differences between Java and C++. If there are no differences, then the question may be low quality. No offense. – New Alexandria Dec 01 '14 at 05:18
  • @toy just read the notice. – nicael Dec 09 '14 at 09:16

2 Answers2

26

In Java, -->-- is not actually an operator.

What you wrote is actually if ((x--) > (--y)).

And, as we know from this answer, the --y is predecrement, while the x-- is postdecrement, so therefore, this is basically if (3 > 2), which always returns true.

Community
  • 1
  • 1
416E64726577
  • 2,214
  • 2
  • 23
  • 47
2

Positcrement and preicrement are very similar operators. Java's bytecode gives a better understanding. Each of them consists of two operations. Load variable and increment it. The only difference is in the order of this operations. If statement from your case is compiled this way:

 4: iload_1               //load x
 5: iinc          1, -1   //decrement x
 8: iinc          2, -1   //decrement y
11: iload_2               //load y
12: if_icmple     23      //check two values on the stack, if true go to 23rd instruction

When JVM comes up to an if statement it has 3 and 2 on the stack. Line 4 and 5 are compiled from x--. Lines 8 and 11 from --y. x is loaded before increment and y after.

BTW, it's strange that javac does not optimize out this static expression.

Mikhail
  • 4,175
  • 15
  • 31