程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 讀《深入理解Java虛擬機》有感——第一部分:Class文件的結構,

讀《深入理解Java虛擬機》有感——第一部分:Class文件的結構,

編輯:JAVA綜合教程

讀《深入理解Java虛擬機》有感——第一部分:Class文件的結構,


1.產生

         源碼(.java文件)——>編譯器(如:javac)——>字節碼(.class文件)——>虛擬機(如:HotSpot)執行

 

2.Class文件

           1)構成:

           2)例子:

                    [1] .java文件

                              桌面/test/test/Father.java、桌面/test/test/Father_interface.java、桌面/test/Son.java  ;                     

                    [2]編譯出.class文件

                               cd  桌面/test;  

                                    桌面/test>javac  Son.java;

                    [3]查看.class文件

                                    桌面/test>javap -verbose  Son;

 

Classfile /C:/Users/Administrator/Desktop/test/Son.class
  Last modified 2016-4-11; size 1005 bytes
  MD5 checksum 7d138d76267a6bb372eba05296e2c932魔數
  Compiled from "Son.java"
public class test.Son extends test.Father implements test.Father_interface索引(1類索引、2父類索引、3接口索引,指向常量池
  SourceFile: "Son.java"
  minor version: 0
  major version: 51主板本號
  flags: ACC_PUBLIC, ACC_SUPER訪問標志<當前類>
Constant pool:常量池
   #1 = Methodref          #16.#33        //  test/Father."<init>":()V   符號引用{
   #2 = Fieldref           #15.#34        //  test/Son.sId_literal:I    
   #3 = Fieldref           #35.#36        //  java/lang/System.out:Ljava/io/PrintStream; 
   #4 = Class              #37            //  java/lang/StringBuilder   
   #5 = Methodref          #4.#33         //  java/lang/StringBuilder."<init>":()V
   #6 = String             #38            //  statical_sId =
   #7 = Methodref          #4.#39         //  java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/Strin
gBuilder;
   #8 = Fieldref           #15.#40        //  test/Son.statical_sId:I
   #9 = Methodref          #4.#41         //  java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
  #10 = Methodref          #4.#42         //  java/lang/StringBuilder.toString:()Ljava/lang/String;
  #11 = Methodref          #43.#44        //  java/io/PrintStream.println:(Ljava/lang/String;)V
  #12 = String             #45            //  Son實例構造器!
  #13 = String             #46            //  hello!this is son!
  #14 = String             #47            //  Son類構造器<clinit>執行!
  #15 = Class              #48            //  test/Son 1
  #16 = Class              #49            //  test/Father 2
  #17 = Class              #50            //  test/Father_interface 3    }符號引用
  #18 = Utf8               final_sId   
  #19 = Utf8               I
  #20 = Utf8               ConstantValue
  #21 = Integer            123 字面量
  #22 = Utf8               statical_sId
  #23 = Utf8               sId_literal
  #24 = Utf8               sId_nonLiteral
  #25 = Utf8               <init>
  #26 = Utf8               ()V
  #27 = Utf8               Code
  #28 = Utf8               LineNumberTable
  #29 = Utf8               sayHello
  #30 = Utf8               <clinit>
  #31 = Utf8               SourceFile
  #32 = Utf8               Son.java       符號引用{
  #33 = NameAndType        #25:#26        //  "<init>":()V
  #34 = NameAndType        #23:#19        //  sId_literal:I
  #35 = Class              #51            //  java/lang/System
  #36 = NameAndType        #52:#53        //  out:Ljava/io/PrintStream;
  #37 = Utf8               java/lang/StringBuilder
  #38 = Utf8               statical_sId =
  #39 = NameAndType        #54:#55        //  append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #40 = NameAndType        #22:#19        //  statical_sId:I
  #41 = NameAndType        #54:#56        //  append:(I)Ljava/lang/StringBuilder;
  #42 = NameAndType        #57:#58        //  toString:()Ljava/lang/String;
  #43 = Class              #59            //  java/io/PrintStream
  #44 = NameAndType        #60:#61        //  println:(Ljava/lang/String;)V
  #45 = Utf8               Son實例構造器!   }符號引用
  #46 = Utf8               hello!this is son!
  #47 = Utf8               Son類構造器<clinit>執行!
  #48 = Utf8               test/Son
  #49 = Utf8               test/Father
  #50 = Utf8               test/Father_interface
  #51 = Utf8               java/lang/System
  #52 = Utf8               out
  #53 = Utf8               Ljava/io/PrintStream;
  #54 = Utf8               append
  #55 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #56 = Utf8               (I)Ljava/lang/StringBuilder;
  #57 = Utf8               toString
  #58 = Utf8               ()Ljava/lang/String;
  #59 = Utf8               java/io/PrintStream
  #60 = Utf8               println
  #61 = Utf8               (Ljava/lang/String;)V
{
  public test.Son();方法表——實例構造器
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method test/Father."<init>":()V
         4: aload_0
         5: bipush        123
         7: putfield      #2                  // Field sId_literal:I
        10: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
        13: new           #4                  // class java/lang/StringBuilder
        16: dup
        17: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
        20: ldc           #6                  // String statical_sId =
        22: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/
lang/StringBuilder;
        25: getstatic     #8                  // Field statical_sId:I
        28: invokevirtual #9                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilde
r;
        31: invokevirtual #10                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        34: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        37: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
        40: ldc           #12                 // String Son實例構造器!
        42: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        45: sipush        333
        48: putstatic     #8                  // Field statical_sId:I
        51: return
      LineNumberTable:
        line 15: 0
        line 7: 4
        line 16: 10
        line 17: 37
        line 18: 45
        line 19: 51

  public void sayHello();方法表——sayHello()
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #13                 // String hello!this is son!
         5: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 21: 0
        line 22: 8

  static {};方法表——類構造器(或靜態代碼塊)
    flags: ACC_STATIC
    Code:
      stack=3, locals=0, args_size=0
         0: bipush        123
         2: putstatic     #8                  // Field statical_sId:I
         5: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         8: new           #4                  // class java/lang/StringBuilder
        11: dup
        12: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
        15: ldc           #6                  // String statical_sId =
        17: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/
lang/StringBuilder;
        20: getstatic     #8                  // Field statical_sId:I
        23: invokevirtual #9                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilde
r;
        26: invokevirtual #10                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        29: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        32: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
        35: ldc           #14                 // String Son類構造器<clinit>執行!
        37: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        40: return
      LineNumberTable:
        line 6: 0
        line 11: 5
        line 12: 32
        line 13: 40
}

 

 

 

          3.總結         

魔數、版本號、常量池、訪問標志、索引、字段表集合、方法表集合

魔數 版本號 常量池 訪問標志 索引 字段表集合 方法表集合

後綴名是可以改的,

所以用它來確定真

的是.class文件

44=JDK1.0

45=jdk1.1

....

字面量:int、double等基本類型數據值

符號引用:(最終指向UTF-8字符串)

     1)記錄類

                訪問標志+全限定名              

     2)記錄方法

                簡單名稱+返回類型+參數列表

     3)記錄成員屬性

                簡單名稱+返回類型

 

 

分為:本類、繼承父類類、實現接口

指向“常量池—記錄類”

 

每張表:

     訪問標志、(名稱、描述【如:返回類型、參數等】)、附加屬性attributes(Code屬性表、Linumber屬性表、局部變量表、異常表等)

 /** 括號中表示是指向“常量池”*/

 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved