问题:前置自增和后置自增的区别
Code-后置自增
public class Test { public static void main(String[] args) { int a = 2; int b = a++ + a * 3; System.out.println(b); } }
Code-前置自增
public class Test2 { public static void main(String[] args) { int a = 2; int b = ++a + a * 3; System.out.println(b); } }
1.运算过程分析
1.1.后置自增
字节码
public static void main(java.lang.String[]); Code: 0: iconst_2 1: istore_1 2: iload_1 3: iinc 1, 1 6: iload_1 7: iconst_3 8: imul 9: iadd 10: istore_2 11: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 14: iload_2 15: invokevirtual #3 // Method java/io/PrintStream.println:(I)V 18: return
字节码分析
1.2.前置自增
字节码
public static void main(java.lang.String[]); Code: 0: iconst_2 1: istore_1 2: iinc 1, 1 5: iload_1 6: iload_1 7: iconst_3 8: imul 9: iadd 10: istore_2 11: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 14: iload_2 15: invokevirtual #3 // Method java/io/PrintStream.println:(I)V 18: return
字节码分析