为什么给我0? [英] Why this giving me 0?

查看:56
本文介绍了为什么给我0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中

package test;

public class test {
    public static void main(String args[])
    {
        //System.out.println(2+3*4);
        int temp=0;
        temp+=temp++;
        System.out.println(temp);
    }

}

我不明白为什么它会给我0,请问有什么机构可以解释它吗?

I do not understand why its giving me 0.Can any body please explain it?

推荐答案

后递增++运算符在表达式后递增值.

Post increment ++ operator increment the value after the expression.

在代码中

temp + = temp ++; ,因此表达式将被评估为

temp += temp++; so the expression will be evaluated as

temp = temp + temp ++;

temp = 0 + 0 (value will not increment here as you are using post increment)

temp = 0

让我们再举一个例子

temp = temp ++ + temp; ,因此它将被评估为

temp = 0 + 1 (value is incremented here mean after temp++ expression)

然后打印将显示 1 的值.

这篇关于为什么给我0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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