Java增量/减量运算符 – 它们的行为方式,function是什么?

我开始学习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); } } 

这些被称为前后增量/减量运算符

 x++; 

x = x + 1;

 x--; 

x = x - 1;

将运算符放在变量++x;之前++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. 

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

在极少数情况下,您可能会担心递增/递减的优先级和表达式返回的值:编写++x表示“先递增,然后返回”,而x++表示“先返回,然后递增”。 在这里,我们可以区分前后增量/减量运算符。