b = b ++和b ++之间的差异 [英] Difference between b=b++ and b++

查看:163
本文介绍了b = b ++和b ++之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在接受采访时被问到以下问题。

I was asked in an interview the below question.

int b = 0;
b = b++;
b = b++;
b = b++;
b = b++;

每行执行后b的值是多少?
每行的输出为0。

what will be the value of b after every line execution ? The output is coming as 0 for every line.

为什么输出不是0,1,2,3?

Why is the output not coming as 0,1,2,3 ?

推荐答案

在Java中,表达式

b = b++

相当于

int tmp = b;
b = b + 1;
b = tmp;

因此结果。

(In一些其他语言,完全相同的表达式具有未指定的行为。请参阅未定义的行为和序列点 。)

(In some other languages, the exact same expression has unspecified behaviour. See Undefined behavior and sequence points.)

这篇关于b = b ++和b ++之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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