• Java操作符——i++ 和 ++i的区别


    问题:前置自增和后置自增的区别

    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

    字节码分析

  • 相关阅读:
    [BZOJ1584][Usaco2009 Mar]Cleaning Up 打扫卫生
    CSS浮动
    Django by example -----1总结
    C#函数重载
    linux目录的特点
    Linux调优
    linux
    对齐方式
    19-10-25-G-悲伤
    19-10-24-H
  • 原文地址:https://www.cnblogs.com/weilu2/p/java_operator_prefix_and_postfix_operators.html
Copyright © 2020-2023  润新知