这可能是一个愚蠢的问题,但我不知道它为什么如此。我写了下面的代码片段。
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 }}为什么+ - + y有效并且+++ y不是?
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 。
+y与-y一样有效,但++运算符需要一个变量来操作(它不能增加一个值),而+y被认为是一个值(执行了加法操作)。
+-+y为0 + (0 - (0 + y)) ,并且它没有增加或减少运算符,因此即使该运算将整个表达式转换为值(而不是变量引用)没有效果。
+++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.
更多推荐
发布评论