前言:

知道class文件结构后,对后面在学习虚拟机栈的时候是有帮助的,前面先不说的那么细,大概了解下,后面再详细讲解虚拟机栈的工作原理,理解常量池和方法区信息即可,这是最重要的一部分。

 

源码HelloWorld.Java

public class HelloWorld extends Parant{
    private String name;
    private Integer age;
    public static void main(String [] args){
        System.out.println("Hello World!");
    };
}
class Parant{

}

javap 反解析,并没有给我们返回全部的class文件信息

Classfile /E:/workspace/cjl-base/target/classes/HelloWorld.class
  Last modified 2020-10-13; size 596 bytes
  MD5 checksum 4d5cfe0f2e3b9fcd811de2d8f990f050
  Compiled from "HelloWorld.java"
public class HelloWorld extends Parant
  minor version: 0    --次版本号
  major version: 52   --主版本号  52表示jdk1.8
  flags: ACC_PUBLIC, ACC_SUPER            --访问标识
Constant pool:                            --常量池,这里是重点内容
   #1 = Methodref          #6.#24         // Parant."<init>":()V  --这里的注释是javap帮我们生成,意思是最终值
   #2 = Fieldref           #25.#26        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #27            // Hello World!
   #4 = Methodref          #28.#29        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #30            // HelloWorld
   #6 = Class              #31            // Parant
   #7 = Utf8               name
   #8 = Utf8               Ljava/lang/String;
   #9 = Utf8               age
  #10 = Utf8               Ljava/lang/Integer;
  #11 = Utf8               <init>
  #12 = Utf8               ()V
  #13 = Utf8               Code
  #14 = Utf8               LineNumberTable
  #15 = Utf8               LocalVariableTable
  #16 = Utf8               this
  #17 = Utf8               LHelloWorld;
  #18 = Utf8               main
  #19 = Utf8               ([Ljava/lang/String;)V
  #20 = Utf8               args
  #21 = Utf8               [Ljava/lang/String;
  #22 = Utf8               SourceFile
  #23 = Utf8               HelloWorld.java
  #24 = NameAndType        #11:#12        // "<init>":()V
  #25 = Class              #32            // java/lang/System
  #26 = NameAndType        #33:#34        // out:Ljava/io/PrintStream;
  #27 = Utf8               Hello World!
  #28 = Class              #35            // java/io/PrintStream
  #29 = NameAndType        #36:#37        // println:(Ljava/lang/String;)V
  #30 = Utf8               HelloWorld
  #31 = Utf8               Parant
  #32 = Utf8               java/lang/System
  #33 = Utf8               out
  #34 = Utf8               Ljava/io/PrintStream;
  #35 = Utf8               java/io/PrintStream
  #36 = Utf8               println
  #37 = Utf8               (Ljava/lang/String;)V
{
  public HelloWorld();
    descriptor: ()V    --方法信息
    flags: ACC_PUBLIC  --访问权限
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method Parant."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LHelloWorld;

  public static void main(java.lang.String[]);   --main方法是入口,在虚拟机栈底部,由于main是静态方法,所以对象并不需要实例化,可以直接从方法区中拿到虚拟机栈
    descriptor: ([Ljava/lang/String;)V  --方法信息
    flags: ACC_PUBLIC, ACC_STATIC       --访问权限
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello World!
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 5: 0
        line 6: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  args   [Ljava/lang/String;
}

常量池很好理解

#1     =  Methodref          #6.#24         // Parant."<init>":()V  

Methodref(常量类型)  #1 = #6.#24  = Parant."<init>":()V  

方法体

 

Rick  74浏览  0评论  2020-10-13

上一篇