【操作随记】Java查看字节码与常量池等

可以使用javap中输出附加信息的指令

javap -verbose className

使用方法:

先用javac进行编译

javac StringTest.java

然后再使用

javap -verbose StringTest

就能获得信息了

Classfile /E:/jt/jt-808-protocol/mengyan-web-test/src/main/java/com/mengyan/StringTest.class
  Last modified 2020-7-31; size 682 bytes
  MD5 checksum 96c40b7c0e562c5815f0b8f956d79075
  Compiled from "StringTest.java"
public class com.mengyan.StringTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #12.#21        // java/lang/Object."<init>":()V
   #2 = Class              #22            // java/lang/StringBuilder
   #3 = Methodref          #2.#21         // java/lang/StringBuilder."<init>":()V
   #4 = Methodref          #2.#23         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #5 = String             #24            // welcome
   #6 = Methodref          #2.#25         // java/lang/StringBuilder.toString:()Ljava/lang/String;
   #7 = Methodref          #26.#27        // java/lang/String.intern:()Ljava/lang/String;
   #8 = Methodref          #26.#28        // java/lang/String.equals:(Ljava/lang/Object;)Z
   #9 = Fieldref           #29.#30        // java/lang/System.out:Ljava/io/PrintStream;
  #10 = Methodref          #31.#32        // java/io/PrintStream.println:(Z)V
  #11 = Class              #33            // com/mengyan/StringTest
  #12 = Class              #34            // java/lang/Object
  #13 = Utf8               <init>
  #14 = Utf8               ()V
  #15 = Utf8               Code
  #16 = Utf8               LineNumberTable
  #17 = Utf8               main
  #18 = Utf8               ([Ljava/lang/String;)V
  #19 = Utf8               SourceFile
  #20 = Utf8               StringTest.java
  #21 = NameAndType        #13:#14        // "<init>":()V
  #22 = Utf8               java/lang/StringBuilder
  #23 = NameAndType        #35:#36        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #24 = Utf8               welcome
  #25 = NameAndType        #37:#38        // toString:()Ljava/lang/String;
  #26 = Class              #39            // java/lang/String
  #27 = NameAndType        #40:#38        // intern:()Ljava/lang/String;
  #28 = NameAndType        #41:#42        // equals:(Ljava/lang/Object;)Z
  #29 = Class              #43            // java/lang/System
  #30 = NameAndType        #44:#45        // out:Ljava/io/PrintStream;
  #31 = Class              #46            // java/io/PrintStream
  #32 = NameAndType        #47:#48        // println:(Z)V
  #33 = Utf8               com/mengyan/StringTest
  #34 = Utf8               java/lang/Object
  #35 = Utf8               append
  #36 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #37 = Utf8               toString
  #38 = Utf8               ()Ljava/lang/String;
  #39 = Utf8               java/lang/String
  #40 = Utf8               intern
  #41 = Utf8               equals
  #42 = Utf8               (Ljava/lang/Object;)Z
  #43 = Utf8               java/lang/System
  #44 = Utf8               out
  #45 = Utf8               Ljava/io/PrintStream;
  #46 = Utf8               java/io/PrintStream
  #47 = Utf8               println
  #48 = Utf8               (Z)V
{
  public com.mengyan.StringTest();
    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 3: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=3, args_size=1
         0: aconst_null
         1: astore_1
         2: new           #2                  // class java/lang/StringBuilder
         5: dup
         6: invokespecial #3                  // Method java/lang/StringBuilder."<init>":()V
         9: aload_1
        10: invokevirtual #4                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        13: ldc           #5                  // String welcome
        15: invokevirtual #4                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        18: invokevirtual #6                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        21: invokevirtual #7                  // Method java/lang/String.intern:()Ljava/lang/String;
        24: ldc           #5                  // String welcome
        26: invokevirtual #8                  // Method java/lang/String.equals:(Ljava/lang/Object;)Z
        29: istore_2
        30: getstatic     #9                  // Field java/lang/System.out:Ljava/io/PrintStream;
        33: iload_2
        34: invokevirtual #10                 // Method java/io/PrintStream.println:(Z)V
        37: return
      LineNumberTable:
        line 5: 0
        line 6: 2
        line 7: 30
        line 8: 37
}
SourceFile: "StringTest.java"

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页