int i=0;
i=i++;
结果i是多少?
这是一个经常被提及的问题,答案一直五花八门。
具体测试一下以说明问题:
代码1:
- public class Test{
- public static void main(String[] args){
- int i= 0 ;
- i=i++;
- System.out.println(i);
- }
- }
结果i依然是0.分析其反编译后的代码:
- public static void main(java.lang.String[]);
- Code:
- 0 : iconst_0 //0放到栈顶
- 1 : istore_1 //把栈顶的值保存到局部变量1,也就是i中
- 2 : iload_1 //把i的值放到栈顶,也就是说此时栈顶的值是0
- 3 : iinc 1 , 1 //注意这个指令,把局部变量1,也就是i,增加1,这个指令不会导致栈的变化,也就是说局部变量1,即i此时为1了。
- 6 : istore_1 //把栈顶的值(0)保存到局部变量1,也就是让i为0了,所以最后i为0
- 7 : getstatic # 2 ; //Field java/lang/System.out:Ljava/io/PrintStream;
- 10 : iload_1
- 11 : invokevirtual # 3 ; //Method java/io/PrintStream.println:(I)V
- 14 : return
值得注意到是i被修改了两次,第一次是i++;i变为1,最后一次是i=0;所以结果i是0
代码2:
- public class Test2{
- public static void main(String[] args){
- int i= 0 ;
- int j= 0 ;
- j=i++;
- System.out.println(i);
- System.out.println(j);
- }
- }
这个结果肯定都知道,i是1,j是0.同样看反编译之后的代码:
- public static void main(java.lang.String[]);
- Code:
- 0 : iconst_0
- 1 : istore_1 //i=0
- 2 : iconst_0
- 3 : istore_2 //j=0
- 4 : iload_1 //把i的值放到栈顶,也就是说此时栈顶的值是0
- 5 : iinc 1 , 1 //局部变量1加1,也就是让i++了,此时i已经是1了,上面说过,此指令不会导致栈变化
- 8 : istore_2 //把栈顶的值(注意是0)存入局部变量2,也就是j中,所以j=0
- 9 : getstatic # 2 ; //Field java/lang/System.out:Ljava/io/PrintStream;
- 12 : iload_1
- 13 : invokevirtual # 3 ; //Method java/io/PrintStream.println:(I)V
- 16 : getstatic # 2 ; //Field java/lang/System.out:Ljava/io/PrintStream;
- 19 : iload_2
- 20 : invokevirtual # 3 ; //Method java/io/PrintStream.println:(I)V
- 23 : return
很明显可以看出,java是先把i的值取出来放到栈顶,我们可以认为是引入了第三个变量int k=i;然后i++,这时候i为1了,然后让j=k;也就是0.结论,i的++运算是在对j这个变量的赋值之前完成的。
代码3:
- public class Test3{
- public static void main(String[] args){
- int i= 0 ;
- int j= 0 ;
- j=++i;
- System.out.println(i);
- System.out.println(j);
- }
- }
结果大家也都知道,i=1,j=1
看操作过程:
- public static void main(java.lang.String[]);
- Code:
- 0 : iconst_0
- 1 : istore_1 //i=0
- 2 : iconst_0
- 3 : istore_2 //j=0
- 4 : iinc 1 , 1 //局部变量i加1,这时候i变成1了 。
- 7 : iload_1 //把i的值放到栈顶,栈顶的值是1
- 8 : istore_2 //j=1
- 9 : getstatic # 2 ; //Field java/lang/System.out:Ljava/io/PrintStream;
- 12 : iload_1
- 13 : invokevirtual # 3 ; //Method java/io/PrintStream.println:(I)V
- 16 : getstatic # 2 ; //Field java/lang/System.out:Ljava/io/PrintStream;
- 19 : iload_2
- 20 : invokevirtual # 3 ; //Method java/io/PrintStream.println:(I)V
- 23 : return
对比代码2和代码3,关键的差别就是iload_1 个 iinc这两条指令的位置变了。
最后把iinc指令介绍一下:
iinc
Operation
Increment local variable by constant 给局部变量加上一个常量的值
Format
iinc index const
Forms
iinc = 132 (0x84)
Operand Stack
No change 操作数栈无变化
Description
The index is an unsigned byte that must be an index into the local variable array of the current frame (§3.6) . The const is an immediate signed byte. The local variable at index must contain anint
. The value const is first sign-extended to anint
, and then the local variable at index is incremented by that amount.
Notes
The iinc opcode can be used in conjunction with the wide instruction to access a local variable using a two-byte unsigned index and to increment it by a two-byte immediate value.
转自: http://blog.youkuaiyun.com/ZangXT/archive/2008/11/05/3229281.aspx