jvm: Java Virtual Machine(Java虚拟机)
特点:Write Once Run Anywhere

java8官方文档:https://docs.oracle.com/javase/8/docs/index.html



(1)源码到类文件
(2)类文件到JVM
(3)JVM各种折腾[内部结构、执行方式、垃圾回收、本地调用等]
Person.java
public class Person {
private String name = "Jane";
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 -g:vars Person.java —> Person.class

分析编译器干了什么事:
Person.java -> 词法分析器 -> token流 -> 语法分析器 -> 语法树/抽象语法树 -> 语义分析器 -> 注解抽象语法树 -> 字节码生成器 -> Person.class文件。由此可知,其实我们的编译器做的事情就是“对等信息转换”。JAVA文件中的信息其实跟我们Class文件中的信息是一样的。
16进制
用Binary Viewer打开class文件得到如下16进制内容:
cafe babe 0000 0034 003f 0a00 0a00 2b08
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
官网 : 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
16进制 u4 :cafebabe
magic:The magic item supplies the magic number identifying the class file format
16进制 u2+u2:0000+0034,34等于10进制的52,表示JDK8
minor_version
major_version
16进制 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.
表示常量池中的数量是62
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.
常量池主要存储两方面内容:字面量(Literal)和符号引用(Symbolic References)。
字面量:文本字符串,final修饰的等。
符号引用:类和接口的全限定名、字段名称和描述符、方法名称和描述符。
反编译指令:javap -v -p Person.class
进行反编译之后,查看字节码信息和指令等信息是否有一种感觉?
JVM相对class文件来说可以理解为是操作系统;class文件相对JVM来说可以理解为是汇编语言或者机器语言。
反编译之后类文件结构如下图:


官网 :https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4
cp_info constant_pool[constant_pool_count-1] 也就是这块包括的信息
cp_info:其实就是一个表格的形式
cp_info {
u1 tag;
u1 info[];
}

上面分析到常量池中常量的数量是62,接下来我们来具体分析一下这62个常量。
用Binary Viewer打开class文件和命令行反编译内容一起分析,以下u1表示1个字节,u2表示2个字节,以下16进制字母不区分大小写。
(1)
16进制内容分析:
往下数一个u1,即0a->10:代表的是CONSTANT_Methodref,表示这是一个方法引用
往下数u2和u2
u2,即00 0a->10:代表的是class_index,表示该方法所属的类在常量池中的索引
u2,即00 2b->43:代表的是name_and_type_index,表示该方法的名称和类型的索引
反编译内容:#1 = Methodref #10,#43
(2)
16进制内容分析:
往下数u1,即08->8:表示的是CONSTANT_String,表示字符串类型
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
往下数u2
u2,即00 2c->44:代表的是string_index
反编译内容:#2 = String #44
(3)
16进制内容分析:
往下数u1,即09->9:表示的是CONSTANT_Fieldref,表示字段类型
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
往下数u2和u2
u2,即00 0d->13:代表的是class_index
u2,即00 2d->45:代表的是name_and_type_index
反编译内容:#3 = Fieldref #13.#45