java运算符++问题 [英] java operator ++ problem

查看:91
本文介绍了java运算符++问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么第一个代码输出是000而第二个代码是123

I wonder why first code output is 000 while the second one is 123

第一个:

int z=0;
    while(z<4)
    {
       z=z++;
       System.out.print(z);

    }

第二个:

int z=0;
int x=0;
    while(z<5)
    {
       x=z++;
       System.out.print(x);

    }

这两个代码有什么不同,为什么第一个块不增加z的值?

what is the different between these two codes , why the first block do not increase the value of the z ?

推荐答案

z = z ++ 是程序员的错误 - 它的作用是增加z然后将z设置为旧值 - 因此它用旧值覆盖z,从而撤消增量。

z=z++ is a programmer's error -- what it does is increment z and then set z to its old value -- as a result it overwrites z with its old value and hence undoes the increment.

这篇关于java运算符++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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