我有以下代码:
public class Tests {
public static void main(String[] args) throws Exception {
int x = 0;
while(x<3) {
x = x++;
System.out.println(x);
}
}
}
我们知道他应该只写x++或x=x+1,但在x=x+ +时,它应该先把x归为自己,然后再加1。为什么x的值是0 ?
- - -更新
下面是字节码:
public class Tests extends java.lang.Object{
public Tests();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.lang.Exception;
Code:
0: iconst_0
1: istore_1
2: iload_1
3: iconst_3
4: if_icmpge 22
7: iload_1
8: iinc 1, 1
11: istore_1
12: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
15: iload_1
16: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
19: goto 2
22: return
}
我会仔细阅读说明,试着理解……
我想知道Java规范中是否有精确定义此行为的内容。(这句话的明显意思是我懒得去检查。)
注意Tom的字节码,关键行是7、8和11。第7行将x加载到计算堆栈中。第8行增加x。第11行将堆栈的值存储回x。在正常情况下,如果你不将值赋值回自身,我不认为有任何理由不能加载,存储,然后增加。你会得到同样的结果。
比如,假设你有一个更正常的情况,你写这样的东西:
z = (x + +) + (y + +);
它是否说(跳过技术细节的伪代码)
load x
increment x
add y
increment y
store x+y to z
or
load x
add y
store x+y to z
increment x
increment y
应该无关紧要。我认为,任何一种实现都是有效的。
我会非常谨慎地编写依赖于这种行为的代码。对我来说,它看起来非常依赖于实现。只有当您做了一些疯狂的事情(比如这里的例子),或者您有两个线程正在运行,并且依赖于表达式中的求值顺序时,才会产生影响。
从http://download.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
The increment/decrement operators can
be applied before (prefix) or after
(postfix) the operand. The code
result++; and ++result; will both end
in result being incremented by one.
The only difference is that the prefix
version (++result) evaluates to the
incremented value, whereas the
postfix version (result++) evaluates
to the original value. If you are
just performing a simple
increment/decrement, it doesn't really
matter which version you choose. But
if you use this operator in part of a
larger expression, the one that you
choose may make a significant
difference.
为了说明这一点,请尝试以下方法:
int x = 0;
int y = 0;
y = x++;
System.out.println(x);
System.out.println(y);
它会输出1和0。