1. 程式人生 > 實用技巧 >JVM指令分析

JVM指令分析

程式碼:

1 public class AppGo{
2  public static void test() {
3   boolean flag = true;
4   if (flag) System.out.println("Hello, Java!");
5   if (flag == true) System.out.println("Hello, JVM!");
6  }
7 }

javac -g AppGo.java

javap -p -v AppGo.class

Classfile /code/asmtoolsTest/AppGo.class
  Last modified 
2020-11-16; size 549 bytes MD5 checksum 080f0b86a8d6f3f00b616181e3b05973 Compiled from "AppGo.java" public class AppGo minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #7.#21 // java/lang/Object."<init>":()V #2 = Fieldref #22.#23 //
java/lang/System.out:Ljava/io/PrintStream; #3 = String #24 // Hello, Java! #4 = Methodref #25.#26 // java/io/PrintStream.println:(Ljava/lang/String;)V #5 = String #27 // Hello, JVM! #6 = Class #28 // AppGo #7 = Class #29 //
java/lang/Object #8 = Utf8 <init> #9 = Utf8 ()V #10 = Utf8 Code #11 = Utf8 LineNumberTable #12 = Utf8 LocalVariableTable #13 = Utf8 this #14 = Utf8 LAppGo; #15 = Utf8 test #16 = Utf8 flag #17 = Utf8 Z #18 = Utf8 StackMapTable #19 = Utf8 SourceFile #20 = Utf8 AppGo.java #21 = NameAndType #8:#9 // "<init>":()V #22 = Class #30 // java/lang/System #23 = NameAndType #31:#32 // out:Ljava/io/PrintStream; #24 = Utf8 Hello, Java! #25 = Class #33 // java/io/PrintStream #26 = NameAndType #34:#35 // println:(Ljava/lang/String;)V #27 = Utf8 Hello, JVM! #28 = Utf8 AppGo #29 = Utf8 java/lang/Object #30 = Utf8 java/lang/System #31 = Utf8 out #32 = Utf8 Ljava/io/PrintStream; #33 = Utf8 java/io/PrintStream #34 = Utf8 println #35 = Utf8 (Ljava/lang/String;)V { public AppGo(); descriptor: ()V flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 2: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LAppGo; public static void test(); descriptor: ()V flags: ACC_PUBLIC, ACC_STATIC 區域性變量表 運算元棧 Code: stack=2, locals=1, args_size=0 0: iconst_1 // 將int型值1入運算元棧 null 1 1: istore_0 // 從運算元棧頂出棧一個int型值存入區域性變數0號位置 1 null 2: iload_0 // 從區域性變數0號位置載入一個int型值入運算元棧 1 1 3: ifeq 14 // 從運算元棧出棧1個數,跟0比較(ifeq),相等則跳過if後面的語句 1 null 6: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 9: ldc #3 // String Hello, Java! 11: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 14: iload_0 // 從區域性變數0號位置載入一個int型值入運算元棧 1 1 15: iconst_1 // 將int型值1入運算元棧 1 1, 1 16: if_icmpne 27 // 從運算元棧棧頂出2個int,如果不相等(if_icmpne),則跳過if後面的語句 1 null 19: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 22: ldc #5 // String Hello, JVM! 24: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 27: return LineNumberTable: line 4: 0 line 5: 2 line 6: 14 line 7: 27 LocalVariableTable: Start Length Slot Name Signature 2 26 0 flag Z StackMapTable: number_of_entries = 2 frame_type = 252 /* append */ offset_delta = 14 locals = [ int ] frame_type = 12 /* same */ } SourceFile: "AppGo.java"