为什么我不能使用多个Java递增或递减运算符?

Cat*_*ysm 3 java operators

当我同时使用两个Java增量运算符时,我感到非常惊讶.
请检查以下代码..

public class Testing {

public static void main(String... str) {
    int prefix = 0, postfix = 0, both = 0;
    // Testing prefix
    System.out.println(prefix);
    System.out.println(++prefix);
    System.out.println(prefix);
    // Testing postfix
    System.out.println(postfix);
    System.out.println(postfix++);
    System.out.println(postfix);
    // mixing both prefix and postfix (I think this should be fine)
    // System.out.println(++ both ++);
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能用作++ both ++?有人能解释一下吗?谢谢..

Jon*_*eet 7

结果++x或被x++分类为,而不是变量 - 并且两个运算符仅适用于变量.

例如,从JLS的第15.14.2节:

后缀表达式后跟++运算符是后缀增量表达式.

    PostIncrementExpression:
       PostfixExpression ++
Run Code Online (Sandbox Code Playgroud)

后缀表达式的结果必须是可转换(§5.1.8)到数字类型的类型的变量,否则会发生编译时错误.

后缀增量表达式的类型是变量的类型.后缀增量表达式的结果不是变量,而是值.

(PrefixIncrementExpression在15.14.3中使用了几乎相同的语言.)