要理解之间的区别,只是靠记忆,很难记住!
今天换个角度,从字节码,指令查看代码
先查看i++
public class ltq {
public static void main(String[] args) {
int i=8;
i=i++;
System.out.println(i);
}
}
使用IDEA 插件查看字节码文件
可以看到字节码文件对应的指令集
bipush 8
istore_1
iload_1
iinc 1 by 1
istore_1
执行流程如下:
再来看看++i
public class ltq {
public static void main(String[] args) {
int i=8;
i=++i;
System.out.println(i);
}
}
使用IDEA 插件查看字节码文件
bipush 8
istore_1
iinc 1 by 1
iload_1
istore_1
执行流程如下:
区别就在于是i++是先压栈,后增加值,在弹栈
++i是增加值,压栈,再出栈