【发布时间】:2013-12-16 14:54:18
【问题描述】:
有人能解释一下 Java 中的后增量是如何工作的吗?
public static void main(String[] args) {
int a = 10;
/*
* 1. "a" is assigned to "b"
* 2. Then a gets incremented by 1
*/
int b = a++;
System.out.println(b); //prints 10
System.out.println(a); //prints 11
int x = 10;
/*
* 1. "x" is assigned to "x"
* 2. Then "x" is not getting incremented by 1
*/
x = x++;
System.out.println(x); //prints 10
}
所以当我们在两边都有相同的变量时,结果是不同的。请解释...
【问题讨论】:
-
在其他语言中的工作方式完全相同
-
This page可以。
-
x正在递增,但是在递增后它被设置回旧值。
标签: java