1 官网
1.1 寻找JDK文档过程
www.oracle.com -> 右下角Product Documentation -> 往下拉选择Java -> Java SE documentation
-> Previous releases -> JDK 8 -> 此时定位到:https://docs.oracle.com/javas...
1.2 The relation of JDK/JRE/JVM
Reference -> Developer Guides -> 定位到:https://docs.oracle.com/javas...
Oracle has two products that implement Java Platform Standard Edition (Java SE) 8: Java SE Development Kit (JDK) 8 and Java SE Runtime Environment (JRE) 8. JDK 8 is a superset of JRE 8, and contains everything that is in JRE 8, plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE 8 provides the libraries, the Java Virtual Machine (JVM), and other components to run applets and applications written in the Java programming language. Note that the JRE includes components not required by the Java SE specification, including both standard and non-standard Java components.
2 源码到类文件
2.1 源码
1 java 2 class Person{ 3 private String name; 4 private int age; 5 private static String address; 6 private final static String hobby="Programming"; 7 public void say(){ 8 System.out.println("person say..."); 9 } 10 public int calc(int op1,int op2){ 11 return op1+op2; 12 } 13 }
编译: javac Person.java ---> Person.class
2.2 编译过程
Person.java -> 词法分析器 -> tokens流 -> 语法分析器 -> 语法树/抽象语法树 -> 语义分析器-> 注解抽象语法树 -> 字节码生成器 -> Person.class文件
2.3 类文件(Class文件)
官网TheclassFileFormat:
https://docs.oracle.com/javas...
cafe babe 0000 0034 0027 0a00 0600 1809 0019 001a 0800 1b0a 001c 001d 0700 1e07 001f 0100 046e 616d 6501 0012 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 0100 0361 6765 0100 0149 0100 0761 6464 7265
......
magic(魔数):
The magic
item supplies the magic number identifying the class
file format; it has the value 0xCAFEBABE
.
cafe babe
minor_version, major_version
0000 0034 对应10进制的52,代表JDK 8中的一个版本
constant_pool_count
0027 对应十进制27,代表常量池中27个常量
1 ClassFile { 2 u4 magic; 3 u2 minor_version; 4 u2 major_version; 5 u2 constant_pool_count; 6 cp_info constant_pool[constant_pool_count-1]; 7 u2 access_flags; 8 u2 this_class; 9 u2 super_class; 10 u2 interfaces_count; 11 u2 interfaces[interfaces_count]; 12 u2 fields_count; 13 field_info fields[fields_count]; 14 u2 methods_count; 15 method_info methods[methods_count]; 16 u2 attributes_count; 17 attribute_info attributes[attributes_count]; 18 }
.class字节码文件
- 魔数与class文件版本
- 常量池
- 访问标志
- 类索引、父类索引、接口索引
- 字段表集合
- 方法表集合
- 属性表集合
2.4 javap文件分解器
javap -c Person.class > Person.txt
查看字节码信息:结构信息/元数据/方法信息
Compiled from "Person.java"
1 class Person { 2 Person(); 3 Code: 4 0: aload_0 5 1: invokespecial #1 // Method java/lang/Object."<init>":()V 6 4: return
public void say(); Code: 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3 // String person say... 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return
public int calc(int, int); Code: 0: iload_1 1: iload_2 2: iadd 3: ireturn }
3 类文件到虚拟机(类加载机制)
类加载机制
虚拟机把Class文件加载到内存
并对数据进行校验,转换解析和初始化
形成可以虚拟机直接使用的Java类型,即java.lang.Class
3.1 装载(Load)
查找和导入class文件
(1)通过一个类的全限定名获取定义此类的二进制字节流
(2)将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构
(3)在Java堆中生成一个代表这个类的java.lang.Class对象,作为对方