Java Bytecode字节码入门

int 赋值 1

 int  s = 1;

对应的字节码是:

  0: iconst_1
  1: istore_1

int 赋值 2

 int  s = 123;

对应的字节码是:

  0: bipush        123
  2: istore_1

 int x = 123;
 int y = 456;

对应的字节码是:

 	0: bipush        123
    2: istore_1
    3: sipush        456
    6: istore_2
    7: return

数组

int[] a = {1,2};

对应的字节码是:

   		0: iconst_2
       1: newarray       int
       3: dup
       4: iconst_0
       5: iconst_1
       6: iastore
       7: dup
       8: iconst_1
       9: iconst_2
      10: iastore
      11: astore_1

 int[] a = { 1, 2 };
        final int size = a.length;
        for (int i = 0; i < size; i++) {
            int  temp = a[i];
        }

对应的字节码是:

   		 0: iconst_2
       1: newarray       int
       3: dup
       4: iconst_0
       5: iconst_1
       6: iastore
       7: dup
       8: iconst_1
       9: iconst_2
      10: iastore
      11: astore_1
      12: aload_1
      13: arraylength
      14: istore_2
      15: iconst_0
      16: istore_3
      17: goto          28
      20: aload_1
      21: iload_3
      22: iaload
      23: istore        4
      25: iinc          3, 1
      28: iload_3
      29: iload_2
      30: if_icmplt     20
      33: return

 		int[] a = { 1, 2 };
        for (int  temp: a) {
        }

对应的字节码是:

   		 0: iconst_2
       1: newarray       int
       3: dup
       4: iconst_0
       5: iconst_1
       6: iastore
       7: dup
       8: iconst_1
       9: iconst_2
      10: iastore
      11: astore_1
      12: aload_1
      13: dup
      14: astore        5
      16: arraylength
      17: istore        4
      19: iconst_0
      20: istore_3
      21: goto          32
      24: aload         5
      26: iload_3
      27: iaload
      28: istore_2
      29: iinc          3, 1
      32: iload_3
      33: iload         4
      35: if_icmplt     24
      38: return

Switch 1

 		public void test(int num) {
        switch (num) {
            case 0:
                break;
            case 1:
                break;
            default:
                break;

        }
    }

对应的字节码是:

   		  0: iload_1
       1: tableswitch   { // 0 to 1

                     0: 24

                     1: 27
               default: 30
          }
      24: goto          30
      27: goto          30
      30: return

Switch 2

 		public void test(int num,int num2) {
        switch (num2) {
            case 0:
                break;
            case 1:
                break;
            default:
                break;

        }
    }

对应的字节码是:

   		   0: iload_2
       1: tableswitch   { // 0 to 1

                     0: 24

                     1: 27
               default: 30
          }
      24: goto          30
      27: goto          30
      30: return

static + switch

 		 public static void test(int num) {
        switch (num) {
            case 0:
                break;
            case 1:
                break;
            default:
                break;

        }
    }

对应的字节码是:

   	   0: iload_0
       1: tableswitch   { // 0 to 1

                     0: 24

                     1: 27
               default: 30
          }
      24: goto          30
      27: goto          30
      30: return

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值