- & # 8594; - - operator in Java

I was wondering what the -->-- operator does 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.

Thanks!

+7
java operators integer int
source share
2 answers

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

In fact, you wrote if ((x--) > (--y)) .

And, as we know from this answer , -y is a predefinition, while x-- is a post-decrement, so this is basically if (3 > 2) , which always returns true .

+26
source share

Positioning and prediction are very similar operators. Java bytecode gives a better understanding. Each of them consists of two operations. Download the variable and increase it. The only difference is in the order of these operations. If the statement in your case is composed as follows:

  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 the JVM matches an if statement, it has 3 and 2 on the stack. Lines 4 and 5 are compiled from x-- . Lines 8 and 11 of --y . x loads before increment and y after.

By the way, it is strange that javac does not optimize this static expression.

+2
source share

All Articles