什么是JVM
流程图(代表一次编译 到处运行)
Java虚拟机与物理机对比
Class文件类比输入设备
CPU指令集类比输出设备
JVM类比存储器、控制器、运算器等
JVM products
最常用的目前是HotSpot,可以通过java -version命令查看
Oracle:HotSpot、JRockit
IBM:J9 VM
Ali:TaobaoVM
Zual:Zing
JDK JRE JVM
结合JDK看JVM
(1)能够把Class文件翻译成不同平台的CPU指令集
(2)也是Write Once Run Anywhere的保证
HotSpot JVM Architecture
官网:https://www.oracle.com/technetwork/tutorials/tutorials-1876574.html
Class file
Java Source
Java源码文件、Kotlin源码文件、Groovy源码文件等
public class User {
private Integer age;
private String name = "snail";
private Double salary = 100.0;
private static String address;
public void say() {
System.out.println("snail Say...");
}
public static Integer calc(Integer op1, Integer op2) {
op1 = 3;
Integer result = op1 + op2;
return result;
}
public static void main(String[] args) {
System.out.println(calc(1, 2));
}
}
Early compile
javac User.java 编译成User.class
Class format
https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html
-
查看字节码文件
使用jdk自带的 javap 指令
javap -v User.class > User.txt
-
查看虚拟机解释执行的二进制字节码
可以使用Notepad++ 安装一个HEX一Editor插件,就可以看到如下
class二进制字节码对照表
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];
}
Analyse
(1)cafebabe
The magic item supplies the magic number identifying the class file format
(2)0000 + 0034: minor_version + major_version
16进制的34等于10进制的52,表示JDK的版本为8
(3)0043: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
16进制的43等于10进制的67,表示常量池中常量的数量是66
(4)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.
字面量:文本字符串,final修饰的常量等
符号引用:类和接口的全限定名、字段名称和描述符、方法名称和描述符
(5)The constant pool
官网:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4
cp_info {
u1 tag;
u1 info[];
}
Table 4.4-A. Constant pool tags
Constant Type | Value |
---|---|
CONSTANT_Class |
7 |
CONSTANT_Fieldref |
9 |
CONSTANT_Methodref |
10 |
CONSTANT_InterfaceMethodref |
11 |
CONSTANT_String |
8 |
CONSTANT_Integer |
3 |
CONSTANT_Float |
4 |
CONSTANT_Long |
5 |
CONSTANT_Double |
6 |
CONSTANT_NameAndType |
12 |
CONSTANT_Utf8 |
1 |
CONSTANT_MethodHandle |
15 |
CONSTANT_MethodType |
16 |
CONSTANT_InvokeDynamic |
18 |
(6)First constant
由0a可以知道第一个常量的类型对应的10进制为10,所以这表示一个方法引用,查找方法引用对应的
structure
CONSTANT_Methodref_info {
u1 tag;
u2 class_index; //代表的是class_index,表示该方法所属的类在常量池中的索引
u2 name_and_type_index; //代表的是name_and_type_index,表示该方法的名称和类型的索引
}
经过分析可以得出第一个常量表示的形式为
#1 = Methodref #16.#35 // java/lang/Object."<init>":()V
(7)Second constant
由08可以知道第一个常量的类型对应的10进制为8,所以这表示一个String引用,查找方法引用对应的
structure
CONSTANT_String_info {
u1 tag;
u2 string_index; //代表的是string_index
}
经过分析可以得出第1、2个常量表示的形式为
#1 = Methodref #16.#35 // java/lang/Object."<init>":()V
#2 = String #36 // snail
反汇编
JDK自带的命令 javap
JVM相对class文件来说可以理解为是操作系统;class文件相对JVM来说可以理解为是汇编语言或者机 器语言。
类加载机制
标签:count,constant,JVM,--,u2,File,CONSTANT,pool From: https://www.cnblogs.com/snail-gao/p/16923841.html官网:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html