i++与++i的误解

javap -c xx.class

{
i=0
i=i++
}
0: bipush 0
2: istore_1 stack ->var1
3: iload_1 var1->stack
4: iinc 1, 1 //var1 =var1+1
7: istore_1 stack ->var1 //var1 被覆盖 值不变
8: return
{
i=0
i=++i
}
0: bipush 0
2: istore_1
3: iinc 1, 1 var1=var1+1
6: iload_1 var1->stack
7: istore_1 stack->var1
8: return

{
int i = 0; //var1
int j=0; //var2
j=i++;
}
0: iconst_0 i->stack
1: istore_1 stack->var1
2: iconst_0 j->stack
3: istore_2 stack->var2
4: iload_1 var1->statck
5: iinc 1, 1 var1=var1+1
8: istore_2 stack(var1 value)->var2
结果 i :1 j:0

结论:++在前后入stack ,++在后,先入stack ,如果只一个变量 先入栈 出栈时被覆盖

 

你可能感兴趣的:(i++与++i的误解)