Java – operator priority issues cause “error: unexpected type”

Due to many operator priority problems recently, I began to use 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 in learning how Java handles the fact that postfix has a higher operator priority than prefixes It seems that the above statement will lead to contradiction. I guess it is handled by this error

My question is twofold:

>Why this mistake? What exactly does that mean? > Why does postfix take precedence over prefixes? I'm sure there's a good reason for this, but I can't think of one Maybe it will fix this undefined behavior, but will it cause more problems in some way?

Solution

The reason for the error is that x produces a value, and you cannot apply the decrement operator to a value, only to a variable For example, if x = 41, then x is calculated as 41 instead of the variable x, – (41) is meaningless

As for why postfix takes precedence over prefix, I guess it is to avoid ambiguity with other operators when parsing For example, the compiler can report a syntax error in X - x instead of parsing it as X – (– x)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>