日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JVM成神之路(二)-- JDK,JER与JVM的关系

發(fā)布時間:2023/12/8 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JVM成神之路(二)-- JDK,JER与JVM的关系 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關(guān)于文章

JVM系列每一篇文章我都錄制了對應(yīng)的視頻,由于CSDN不能直接發(fā)送視頻資料,如果想要作者錄制的免費視頻資料,可以加QQ:3139882589。這個是作者QQ,如果有問題想跟作者咨詢,也可以加到我們的java技術(shù)交流QQ群。加作者請備注暗號 [CSDN]

初識JVM

我們從這篇文章開始就要正式開始學(xué)習(xí)JVM了,我們學(xué)習(xí)一個知識,肯定首先要去最權(quán)威的地方進行學(xué)習(xí),所以我們學(xué)習(xí)的節(jié)奏是跟著官網(wǎng)來進行學(xué)習(xí),同時我們學(xué)習(xí)的是主流的JDK8這樣的一個版本。

Java官網(wǎng):https://docs.oracle.com/javase/8/

2.1 JVM是什么?

JVM (Java Virtual Machine,Java虛擬機)

一次編寫,到處運行(Write Once Run Anywhere)

2.2 JDK JRE JVM之間的關(guān)系

我們從官網(wǎng)的Developer Guides進去,如下。

我們會發(fā)現(xiàn)這樣一段話,

總結(jié)上面這段話意思,大概的意思就是JDK包含JRE,而JRE提供了JVM以及其他組件,用來運行JAVA編寫的程序。

下面這張描述了JVM,JRE,JDK關(guān)系的圖也相當(dāng)于對上面那段話做了解釋。如下

2.3 源碼到類文件

  • 源碼Demo

    private String name="carlNB";private int age;private final double salary=100;private static String address;private final static String hobby="Programming";private static Object obj=new Object();public void say(){System.out.println("person say...");}public static int calc(int op1,int op2){op1=3;int result=op1+op2;Object obj=new Object();return result;}public static void main(String[] args){calc(1,2);} }

編譯指令:JAVAC Person.java

2.4 分析編譯器干了什么事

Person.java -> 詞法分析器 -> tokens流 -> 語法分析器 -> 語法樹/抽象語法樹 -> 語義分析器 -> 注解抽象語法樹 -> 字節(jié)碼生成器 -> Person.class文件

由上可知,其實我們的編譯器其實做的事情其實就是“對等信息轉(zhuǎn)換”。JAVA文件中的信息其實跟我們Class文件中的信息,其實是一樣的。

2.5 分析Class文件

  • 16進制
002c 0900 0d00 2d06 4059 0000 0000 0000 0900 0d00 2e09 002f 0030 0800 310a 0032 0033 0700 340a 000d 0035 0900 0d00 3607 0037 0100 046e 616d 6501 0012 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 0100 0361 6765 0100 0149 0100 0673 616c 6172 7901 0001 4401 000d 436f 6e73 7461 6e74 ......
  • The ClassFile Structure

官網(wǎng): https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html

ClassFile {u4 magic;u2 minor_version;u2 major_version;u2 constant_pool_count;cp_info constant_pool[constant_pool_count-1];u2 access_flags;u2 this_class;u2 super_class;u2 interfaces_count;u2 interfaces[interfaces_count];u2 fields_count;field_info fields[fields_count];u2 methods_count;method_info methods[methods_count];u2 attributes_count;attribute_info attributes[attributes_count]; }
  • Simple analysis
    • u4 : ca fe ba be

      magic:The magic item supplies the magic number identifying the class file format
    • u2+u2:0000+0034,34等于10進制的52,表示JDK8

      minor_version major_version
    • u2:003f=63(10進制)

      constant_pool_count: The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one.

      表示常量池中的數(shù)量是58

      cp_info constant_pool[constant_pool_count-1]

      The constant_pool is a table of structures representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte. The constant_pool table is indexed from 1 to constant_pool_count - 1.

      常量池主要存儲兩方面內(nèi)容:字面量(Literal)和符號引用(Symbolic References)

      字面量:文本字符串,final修飾等 符號引用:類和接口的全限定名、字段名稱和描述符、方法名稱和描述符

2.6 反編譯驗證

用javap指令驗證上述猜想正確性

編譯指令:javap -v -p Person.class

進行反編譯之后,查看字節(jié)碼信息和指令等信息

是否有一種感覺?

JVM相對class文件來說可以理解為是操作系統(tǒng);class文件相對JVM來說可以理解為是匯編語言或者機器語言。

2.7 Continous analysis

上面分析到常量池中常量的數(shù)量是62,接下來我們來具體分析一下這62個常量

cp_info constant_pool[constant_pool_count-1] 也就是這塊包括的信息

cp_info其實就是一個表格的形式

All constant_pool table entries have the following general format:

cp_info {u1 tag;u1 info[]; }

官網(wǎng):https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4

(1)往下數(shù)一個u1,即0a->10:代表的是CONSTANT_Methodref,表示這是一個方法引用

CONSTANT_Fieldref_info {u1 tag;u2 class_index;u2 name_and_type_index; }

往下數(shù)u2和u2

u2,即00 0a->10:代表的是class_index,表示該方法所屬的類在常量池中的索引
u2,即00 2b->43:代表的是name_and_type_index,表示該方法的名稱和類型的索引

#1 = Methodref #10,#43

(2)往下數(shù)u1,即08->8:表示的是CONSTANT_String,表示字符串類型

CONSTANT_String_info {u1 tag;u2 string_index; }

往下數(shù)u2

u2,即00 2c->44:代表的是string_index

#1 = Methodref #10,#43 #2 = String #44

(3)往下數(shù)u1,即09->9:表示的是CONSTANT_Fieldref,表示字段類型

CONSTANT_Fieldref_info {u1 tag;u2 class_index;u2 name_and_type_index; }

往下數(shù)u2和u2

u2,即00 0d->13:代表的是class_index
u2,即00 2d->45:代表的是name_and_type_index

#1 = Methodref #10.#43 #2 = String #44 #3 = Fieldref #13.#45

index;

u2 name_and_type_index;
}

往下數(shù)u2和u2u2,即00 0d->13:代表的是class_index u2,即00 2d->45:代表的是name_and_type_index #1 = Methodref #10.#43 #2 = String #44 #3 = Fieldref #13.#45

總結(jié)

以上是生活随笔為你收集整理的JVM成神之路(二)-- JDK,JER与JVM的关系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。