Java Increment/Decrement Operators - 它们的行为方式,功能是什么? [英] Java Increment / Decrement Operators - How they behave, what's the functionality?

查看:21
本文介绍了Java Increment/Decrement Operators - 它们的行为方式,功能是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It's been 3 days since I start to learn Java. I have this program and I don't understand code in main method with ++ and -- operators. I don't even know what to call them(name of these operators) Can anyone explain me what's all about.

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加1,然后使用x的这个新值

Putting the operator before the variable ++x; means, first increment x by 1, and then use this new value of 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. 

这篇关于Java Increment/Decrement Operators - 它们的行为方式,功能是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆