Java i ++操作说明 [英] Java i++ operation explanation

查看:74
本文介绍了Java i ++操作说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

a + = b和a = + b之间有什么区别,还有++和++ a?

在&xdquo; x = x ++”?后x是什么

在Test1中,i将其值递增1并返回旧值并将其增量值保存在i变量中。但是在Test2中,我将其值增加1并返回其旧值,并且还会发生增量。他们是否为i中的变量分配了i的副本。 Test2中的操作步骤是什么。

In Test1 i increments its value by 1 and return old value and keep its incremental value in i variable. But in Test2 i increments its value by 1 and return its old value and the increment also occurred. Are they make a copy of i for the increment which is not assigning in the i variable. What is the operational step in Test2.

Test1

int i = 0;
System.out.print(i++);
System.out.print(i);

输出01

Test2

int i = 0;
i = i++;
System.out.println(i);

输出0

推荐答案

语句 i = i ++ 在Java中具有明确定义的行为。首先, i 的值被压入堆栈。然后,变量 i 递增。最后,弹出堆栈顶部的值并将其分配到 i 。最终结果是没有任何反应 - 智能优化器可以删除整个语句。

The statement i = i++ has well-defined behavior in Java. First, the value of i is pushed on a stack. Then, the variable i is incremented. Finally, the value on top the stack is popped off and assigned into i. The net result is that nothing happens -- a smart optimizer could remove the whole statement.

这篇关于Java i ++操作说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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