Java 递增/递减运算符 - 它们的行为方式,功能是什么?

Java Increment / Decrement Operators - How they behave, what's the functionality?

开始学习Java已经3天了。 我有这个程序,但我不理解 main 方法中使用 ++-- 运算符的代码。我什至不知道怎么称呼他们(这些运营商的名字) 谁能给我解释一下是怎么回事。

class Example {
    public static void main(String[] args) {
         x=0;
         x++;
         System.out.println(x);
         y=1;
         y--;
         System.out.println(y);
         z=3;
         ++z;
         System.out.println(z);
     }
}

这些称为 Pre 和 Post 递增/递减运算符

x++;

等同于x = x + 1;

x--;

等同于x = x - 1;

把运算符放在变量++x;之前,意思是先把x加1,然后用x

这个新值
int x = 0; 
int z = ++x; // produce x is 1, z is 1


    int x = 0;
    int z = x++;  // produce x is 1, but z is 0 , 
                  //z gets the value of x and then x is incremented. 

++-- 称为 incrementdecrement 运算符。 它们是 x = x+1 (x+=1) / x = x-1 (x-=1) 的快捷方式。 (假设x是一个数值变量)

在极少数情况下,您可能会担心 incrementation/decrementation 的优先级和表达式 returns 的值:写作 ++x 表示 "increment first, then return",而 x++ 表示 "return first, then increment"。这里我们可以区分pre-postincrement/decrement运算符。