• for循环的字节码


    源代码:

    public class Wizard {
        private int age;
        
        private void forCycle() {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
            }
        }
    }

    javap -v -p Wizard.class 得到class文件:

    Classfile /D:/Users/Administrator/workspace/TestJava/bin/com/bytecode/Wizard.class
      Last modified 2018-3-17; size 545 bytes
      MD5 checksum 529c4f32d75aa1bdce39f2b8fa128d76
      Compiled from "Wizard.java"
    public class com.bytecode.Wizard
      SourceFile: "Wizard.java"
      minor version: 0
      major version: 51
      flags: ACC_PUBLIC, ACC_SUPER
    
    Constant pool:
       #1 = Class              #2             //  com/bytecode/Wizard
       #2 = Utf8               com/bytecode/Wizard
       #3 = Class              #4             //  java/lang/Object
       #4 = Utf8               java/lang/Object
       #5 = Utf8               age
       #6 = Utf8               I
       #7 = Utf8               <init>
       #8 = Utf8               ()V
       #9 = Utf8               Code
      #10 = Methodref          #3.#11         //  java/lang/Object."<init>":()V
      #11 = NameAndType        #7:#8          //  "<init>":()V
      #12 = Utf8               LineNumberTable
      #13 = Utf8               LocalVariableTable
      #14 = Utf8               this
      #15 = Utf8               Lcom/bytecode/Wizard;
      #16 = Utf8               forCycle
      #17 = Fieldref           #18.#20        //  java/lang/System.out:Ljava/io/PrintStream;
      #18 = Class              #19            //  java/lang/System
      #19 = Utf8               java/lang/System
      #20 = NameAndType        #21:#22        //  out:Ljava/io/PrintStream;
      #21 = Utf8               out
      #22 = Utf8               Ljava/io/PrintStream;
      #23 = Methodref          #24.#26        //  java/io/PrintStream.println:(I)V
      #24 = Class              #25            //  java/io/PrintStream
      #25 = Utf8               java/io/PrintStream
      #26 = NameAndType        #27:#28        //  println:(I)V
      #27 = Utf8               println
      #28 = Utf8               (I)V
      #29 = Utf8               i
      #30 = Utf8               StackMapTable
      #31 = Utf8               SourceFile
      #32 = Utf8               Wizard.java
    {
      private int age;
        flags: ACC_PRIVATE
    
    
      public com.bytecode.Wizard();
        flags: ACC_PUBLIC
    
        Code:
          stack=1, locals=1, args_size=1
             0: aload_0       
             1: invokespecial #10                 // Method java/lang/Object."<init>":()V
             4: return        
          LineNumberTable:
            line 3: 0
          LocalVariableTable:
            Start  Length  Slot  Name   Signature
                   0       5     0  this   Lcom/bytecode/Wizard;
    
      private void forCycle();
        flags: ACC_PRIVATE
    
        Code:
          stack=2, locals=2, args_size=1
             0: iconst_0      
             1: istore_1      
             2: goto          15
             5: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
             8: iload_1       
             9: invokevirtual #23                 // Method java/io/PrintStream.println:(I)V
            12: iinc          1, 1
            15: iload_1       
            16: bipush        10
            18: if_icmplt     5
            21: return        
          LineNumberTable:
            line 7: 0
            line 8: 5
            line 7: 12
            line 10: 21
          LocalVariableTable:
            Start  Length  Slot  Name   Signature
                   0      22     0  this   Lcom/bytecode/Wizard;
                   2      19     1     i   I
          StackMapTable: number_of_entries = 2
               frame_type = 252 /* append */
                 offset_delta = 5
            locals = [ int ]
               frame_type = 9 /* same */
    
    }

    分析for循环的字节码:

    字节码 描述 操作数栈
    iconst_0 0入栈 0
    istore_1 把0赋值给局部变量i  
    goto 15 跳转到第15行  
    iload_1 局部变量i的值入栈 0
    bipush 10 10入栈 0, 10
    if_icmplt 5 比较0和10,小于则跳到第5行  
    getstatic #17 System.out入栈 Systme.out
    iload_1 局部变量i的值入栈 System.out, 0
    invokevirtual #23 调用print方法  
    iinc 1, 1 局部变量i自增1  
    iload_1 把i的值入栈 2
  • 相关阅读:
    CentOS 7 配置 ISCSI 服务器
    CentOS 7 配置 http 服务器
    CentOS 7 配置 samba服务器
    ssh的两种连接方法(包括无密码访问)
    CentOS 7 破解mariadb密码
    Selenium2+python自动化53-unittest批量执行(discover)【转载】
    Selenium2+python自动化52-unittest执行顺序【转载】
    Selenium2+python自动化51-unittest简介【转载】
    selenium3+python自动化50-环境搭建(firefox)【转载】
    Selenium2+python自动化49-判断文本(text_to_be_present_in_element)【转载】
  • 原文地址:https://www.cnblogs.com/allenwas3/p/8588583.html
Copyright © 2020-2023  润新知