简体   繁体   中英

+++x unexpected type required: variable found: value

This may be the silly question but i have no idea why it is so.I have written following code snippet.

public class Test {
public static void main(String... str)
{
    int y = 9;
    int z = +++y; //unexpected type required:variable found:value
    int w = +-+y; // Not Error
}}

Why +-+y works and +++y Not ?

+++y is interpreted as the ++ operator followed by +y .

+y is as valid as -y is, but the ++ operator expects a variable to operate on (it cannot increment a value), and +y is considered a value (an addition operation was performed).

+-+y as 0 + (0 - (0 + y)) , and it has no increment or decrement operators with in it, so even though the operation transform the whole expression into a value (instead of a variable reference) it has no effect.

In Java, the characters +++ mean ++ , followed by + , which are two different operators. On the other hand, there is no operator +- , so the characters +-+ mean + , then - , then + .

If you want to play with these operators, there's also ~ , which is a binary not . You can build arbitrary chains with the operators + , - and ~ , as long as they don't contain ++ or -- .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM