Operator precedence issue leads to "error: unexpected type"

java

Solution

The cause of the error is that `x++` produces a value, and you can't apply a decrement operator to a value, only to a variable. For example if x=41, `x++` evaluates to 41, not to the variable x, and `--(41)` is meaningless.

As to why postfix has higher precedence than prefix, my guess is that it is to avoid ambiguity with other operators while parsing. For example, the compiler can report a syntax error for `x--x` instead of parsing it as `x-(-x)`.

Problem

Since there have been so many operator precedence questions recently, I started playing with some code and came up with this: ``` int x = someNumber; int y = --x++; ``` This gives: ``` Error: unexpected type required: variable found: value ``` I tried this because I was interested to learn how java would deal with the fact that `postfix` has a higher operator precedence than `prefix`. This seems like the above statement would lead to a contradiction, which I guess is handled by this error. My question is two-fold: - Why this error? What does it mean, exactly? - Why does `postfix` have a higher precedence than `prefix`? I'm sure there's a good reason for it, but I haven't been able to come up with one. Perhaps it would fix this undefined behavior, but it would somehow give rise to more problems?

Original source