Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the parent delegation mechanism in Java virtual machine

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Many novices are not very clear about how to understand the parent delegation mechanism in the Java virtual machine. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

The Java virtual machine loads class files on demand, that is, when you need to use this class, it loads its class file into memory to generate class objects. And when loading the class file of a class, the Java virtual machine uses the parent delegation mode, that is, handing the request to the parent class, which is a task delegation mode.

If you create a new package named java.lang under src in the project directory, and create a new class named String in it, will this class work properly?

The program code is as follows:

Package java.lang;public class String {static {System.out.println ("can you initialize me?") ;} public class Application {public static void main (String [] args) {java.lang.String str = new java.lang.String (); System.out.println ("hello,world");}}

Execution result:

Hello,world

Indicates that the String class customized under the java.lang package is not initialized. This is the parent delegation mechanism to protect the core API of our system from being easily destroyed.

I. principle

1. If a class loader receives a class load request, it will not load it first, but delegate the request to the loader of the parent class to execute.

2. If the parent class loader still exists, the parent class loader is further delegated upward, and the recursive request will eventually reach the top-level startup class loader.

3. If the parent class loader can complete the class loading task, it returns successfully. If the parent class loader cannot complete the loading task, the child loader will try to load it on its own. This is the parent delegation mode.

Second, function

1. Protect the program security and prevent the core API from being tampered with at will. Under the java.lang package, the main method in the developer's custom class is not allowed to be executed to prevent malicious code from damaging the program.

2. Avoid repeated loading of classes. A class will only be loaded once.

III. Sandbox security mechanism

Customize the string class, but the bootstrap class loader will be the first to load the custom String class, while the bootstrap class loader will first load the file that comes with jdk (rt. Boot. In the jar package, java\ lang\ String.class), the error message says that there is no main method because it is loaded with rt. The String class in the jar package. This ensures the protection of the core source code of java, which is the sandbox security mechanism.

IV. Supplementary content

Two necessary conditions for indicating whether two class objects are the same class in JVM

1. The full path of the class is always (package name + class name).

2. The ClassLoader that loads this class must be the same.

In other words, in JVM, even if the two class objects (class objects) originate from the same Class file and are loaded by the same virtual machine, the two class objects are not equal as long as the ClassLoader instance objects that load them are different.

Reference to the class loader

JVM must know whether a type is loaded by the boot loader or by the user class loader. If a type is loaded by a user class loader, JVM saves a reference to that class loader in the method area as part of the type information. When resolving references from one type to another, JVM needs to ensure that the class loaders of the two types are the same.

Java programs use classes in two ways: active use and passive use.

Active use can be divided into seven situations:

1. Create an instance of the class

2. Access the static variable of a class or interface, or assign a value to it

3. Reflection (for example: Class.forName ("java.lang.String)

4. Initialize a subclass of a class

5. The class marked as the boot class when the java virtual machine starts

6. Dynamic language support provided by java 7

Except for the above seven cases, other ways of using the Java class are seen as passive use of the class will not lead to class initialization

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report