Today to talk Java ClassLoader

background

Class loading mechanism as a high-frequency face questions often asked in the interview, a telephone interview a few days ago to ask, before there is to know before, but not carded own system, so that's a bit messy, spend today time to sort it, for everyone to share the same time he is a good comb, about the way to help people in need.

 

 

What is the class loading mechanism

We all know Java files are written to the file with the extension .java, we will be prepared by the compiler to compile .java file into a .class file, it simply class loading mechanism is read from the file system will be a series of class file the JVM memory resources provided for subsequent operation of the program is running.

 

 

Class loading process

We look at the class loading process in which stage, followed by an explanation of its then did what

 

Simply draw a graph, we can see from the chart, the entire class loading process has five stages, the following are explanations of each procedure done.

 

load

 

Finding such bytecode files (class file is a binary file) with the full path of a class. Static binary file storage structure runtime data structures into the method area , and a Class object to create the binary stream file, stored in the Java heap data structure an inlet region of the reference method;

 

1. Source class file: One thing to note is the class loading mechanism can not only read the class file from the file system, it can also be obtained via a network, or other jar package another program, such as JSP application.

 

2. The class loader: mentioned class loader to load the class have the order mentioned and class loaders. Java, there are about four types of loaders are: Start class loader (Bootstrap ClassLoader), extended class loader (Extension ClassLoader), the system class loader (System ClassLoader), custom class loader (Custom ClassLoader), followed by belonging to the inheritance (note that inheritance is not Java class extends inside here.

 

3. Start the class loader (Bootstrap ClassLoader): responsible for loading stored in Java_Home / jre / lib, or under -Xbootclasspath parameter specifies the path, and can be recognized by the virtual machine library (e.g. rt.jar, all the java. * classes are beginning Bootstrap ClassLoader load) to start the class loader is not directly referenced Java program.

 

4. The extension class loader (Extension ClassLoader): responsible for loading is realized by sun.misc.Launcher $ ExtClassLoader, which is responsible for loading Java_Home / jre / lib / ext directory, or specified by the system variable path java.ext.dirs All libraries (e.g. javax. * at the beginning of the class), the developer may be used as extension class loader.

 

The system class loader (System ClassLoader): responsible for loading is achieved by a sun.misc.Launcher $ AppClassLoader, which is responsible for loading the user class path (the ClassPath) designated class, the developer can directly use the class loader, If the application does not live their custom class loader, in general, this program is the default class loader.

 

6.自定义类加载器(Custom ClassLoader:自己开发的类加载器

 

7.双亲委派原则:类加载器在加载 class 文件的时候,遵从双亲委派原则,意思是加载依次由父加载器先执行加载动作,只有当父加载器没有加载到 class 文件时才由子类加载器进行加载。这种机制很好的保证了 Java API 的安全性,使得 JDK 的代码不会被篡改。

 

验证

验证的过程只要是保证 class 文件的安全性和正确性,确保加载了该 class 文件不会导致 JVM 出现任何异常,不会危害JVM 的自身安全。验证包括对文件格式的验证,元数据和字节码的验证。

 

准备

准备阶段是为类变量进行内存分配和初始化零值的过程。注意这时候分配的是类变量的内存,这些内存会在方法区中分配。此时不会分配实例变量的内存,因为实例变量是在实例化对象时一起创建在Java 堆中的。而且此时类变量是赋值为零值,即 int 类型的零值为 0,引用类型零值为 null,而不是代码中显示赋值的数值。

 

解析

解析阶段是虚拟机将常量池中的符号引用转化为直接引用的过程。在 class 文件中常量池里面存放了字面量和符号引用,符号引用包括类和接口的全限定名以及字段和方法的名称与描述符。在 JVM 动态链接的时候需要根据这些符号引用来转换为直接引用存放内存使用。

 

初始化

初始化的阶段是类加载的最后一步,这个阶段主要是执行 java 代码,进行相关初始化的动作。

 

总结

整个类加载机制是我们程序运行的开始,虽然这些动作都是 JVM 帮我们自动完成,开发人员在不需要定制类加载器的时候是不会涉及到底层细节的,但是作为一个有追求的程序员,我们还是要知道一些原理,这样不管是在面试的时候还是对自己的提升都有很大的帮助。


 

Java 极客技术公众号,是由一群热爱 Java 开发的技术人组建成立,专注分享原创、高质量的 Java 文章。如果您觉得我们的文章还不错,请帮忙赞赏、在看、转发支持,鼓励我们分享出更好的文章。

关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料。

Guess you like

Origin www.cnblogs.com/justdojava/p/11211586.html