[JAVA] class loading process

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ALone_cat/article/details/102383214

Loading the class

Loading the class is divided into three phases: loading link initialization

A load

Java virtual machine process generally used Java classes: First, developers write a Java source code (.java files) compiled Java byte code file (.class files), then the class loader reads the byte code files, and java.lang.Class converted into objects. Once you have this Class object, Java virtual machine can create objects using their real reflection method. The vast majority of Java class loader provided are inherited from the ClassLoader class, they are used to load the bytecode files from different sources.

Mentioned load class, we must say that the class loader. The system provides three types of loader, namely:

  • Bootstrap ClassLoader (also known as class loader boot loader root / boot class loader)
  • ExtClassLoader (extended class loader)
  • The AppClassLoader (application class loader)

Each type has its designated loader class loader path .

  1. Bootstrap ClassLoader main load JAVA_HOME / jre / lib in the jar package, when all the jar packages in this directory are required to run the JVM jar package. Note: The class loader itself is actually a Java class, therefore, its own class loader needs to be loaded before using other class loader, obviously there must be a top-level parent class loader (ie Bootstrap ClassLoader, that class is loaded is developed by the C language code) is the parent of another class loader. The key point is that, if a class is a class loader Bootstrap ClassLoader, then the class getClassLoader () method returns null .
  2. ExtClassLoader main load Java core extension class that JAVA_HOME jar file in the / jre / ext directory.
  3. The main load is AppClassLoader developers to write in your application class, that is, all the jar files in the CLASSPATH path.

The relationship between the three loader:
 Relationships Between Three loader
here we should be a clear principle: a class will only generate a class object, whether it was new a few times. Why do you say this way? Here we talked about parents delegation model between the class loader.
Parent delegation model
Parent delegation model of working process is as follows:,

  1. Whether the current class loader queries from their own already loaded class is already loaded such that if the original has been loaded already loaded class is returned.
  2. If not, go to delegate the parent class loader to load. Parent class loader will use the same strategy to see that they have been loaded class contains the class, returns have not commissioned the parent class to load until the delegate class loader to boot up. Because if the parent class loader is empty, on behalf of the use of the boot class loader as parent class loader to load.
  3. 如果启动类加载器加载失败,就会使用扩展类加载器来尝试加载,继续失败则会使用 AppClassLoader 来加载,继续失败就会抛出一个异常 ClassNotFoundException

使用双亲委派模型的好处

  1. 安全性,避免用户自己编写的类动态替换 Java 的一些核心类。如果不采用双亲委派模型的加载方式进行 类的加载工作,那我们就可以随时使用自定义的类来动态替代 Java 核心 API 中定义的类。例如:如果黑客将“病毒 代码”植入到自定义的 String 类当中,随后类加载器将自定义的 String 类加载到 JVM 上,那么此时就会对 JVM 产生意想不到“病毒攻击”。而双亲委派的这种加载方式就可以避免这种情况,因为 String 类已经在启动时就被引导类加载器进行了加载。
  2. 避免类的重复加载,因为 JVM 判定两个类是否是同一个类,不仅仅根据类名是否相同进行判定,还需要 判断加载该类的类加载器是否是同一个类加载器,相同的 class 文件被不同的类加载器加载得到的结果就是两个不同的类。

二、链接

当类被加载之后,系统为之生成一个对应的Class对象,接着将会进入连接阶段,连接阶段负责把类的二进制数据合并到JRE中。类连接又可分为如下3个阶段:

  1. 验证:验证阶段用于检验被加载的类是否有正确的内部结构,并和其他类协调一致。Java是相对C++语言是安全的语言,例如它有C++不具有的数组越界的检查。这本身就是对自身安全的一种保护。验证阶段是Java非常重要的一个阶段,它会直接的保证应用是否会被恶意入侵的一道重要的防线,越是严谨的验证机制越安全。验证的目的在于确保Class文件的字节流中包含信息符合当前虚拟机要求,不会危害虚拟机自身安全。其主要包括四种验证,文件格式验证,元数据验证,字节码验证,符号引用验证。
  2. Preparation : class preparation phase is responsible for static variables to allocate memory for the class, and set the default initial value.
  3. Analytical : The binary data class replaced symbolic references direct reference. Explain: symbolic references: Symbol reference is to target a set of symbols to describe quoted, literal symbols can be literally any form, as long as the conflict does not appear to be able to locate the line. Independent of layout and memory. Direct reference: is a pointer to the target, or can be positioned directly offset handle. The reference is in memory layout and relevant, and be sure to load coming.

Third, the initialization

Initialization is given to correct the initial value of a static variable of the class , the class if the statement: private static int a = 10, it performs the process: First byte code files are loaded into memory, the first verification link step, after the verification by the preparation phase, to allocate a memory, as is a static variable, so at this time is equal to a default initial value of type int 0, i.e., a = 0, then the resolution (say later), to which the initialization when a step, and only then the true value a is assigned to a 10, this time a = 10.

This article also reference heart _yan blog, blog link: https: //blog.csdn.net/m0_38075425/article/details/81627349

Guess you like

Origin blog.csdn.net/ALone_cat/article/details/102383214