简体   繁体   English

+++ x需要意外类型:找到变量:value

[英]+++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有效,+ + +不是吗?

+++y is interpreted as the ++ operator followed by +y . +++y被解释为++运算符,后跟+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-y一样有效,但++运算符期望变量操作(它不能递增值), +y被认为是一个值(执行了加法运算)。

+-+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. +-+y0 + (0 - (0 + y)) ,并且它没有增量或减量运算符,因此即使操作将整个表达式转换为值(而不是变量引用),它也具有没有效果。

In Java, the characters +++ mean ++ , followed by + , which are two different operators. 在Java中,字符+++表示++ ,后跟+ ,它们是两个不同的运算符。 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 -- . 您可以使用运算符+-~构建任意链,只要它们不包含++--

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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