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

What is the java class loader?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the java class loader". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the java class loader is.

one。 What is class loading?

Class loader is a java class that loads class files. Class loader is responsible for loading class files from file system, network or other sources. Java virtual machine loads class files (class description files) compiled by java into heap memory, verifies, transforms, parses, initializes, and finally forms java types that can be recognized by virtual machines. There are three default class loaders in jvm, namely, the root class loader, the extension class loader, and the application class loader.

two。 What does the classloader do?

The class loader processes bytecode files. In java, to load a class file into jvm, the class loader requires the following steps:

(1) load: find and import class files

(2) Link:

a. Check: check whether the loaded class file is correct: format, syntax, etc.

b. Prepare: allocate memory space to static variables

c. Parsing: converting symbolic references to direct references

(3 initialization: initialize static variables and code blocks

three。 When will dynamic loading and dynamic links be triggered?

The characteristic of java polymorphism is that it can be dynamically linked, that is, the runtime knows its specific implementation, so the parsing process can also be performed after initialization, and this process is dynamic binding.

four。 What are the initialization methods of the class?

(1) new and getStatic,putStatic,invokeStatic instructions will trigger the initialization of the class

(2) reflection call Class.forName ("com.jd.xxxx") using the method of java.long.reflect package

(3) initialize the subclass, too. Initialize the parent class first.

(4) call the static method of the class

(5) Startup class of jvm, class of main method

five。 How is the class loader loaded?

(1) find the corresponding binary byte stream through the class name

(2) transform the static storage structure represented by byte stream into the method area runtime data structure.

(3) generate an object representing this class in heap memory as an entry for method area access

six。 What did you do in the preparation phase?

(1) assign initial values to static variables, and instance variables will only be assigned to the heap at the time of object initialization.

(2) the initial value is only the default value of the variable, for example: public static final int = 123; at this time, only 0 is initialized.

7. How is the class loading of JVM loaded?

(1) depending on ClassLoader and subclasses.

(2) different ClassLoader loads different modules and has loading sequence.

(3) BootstrapClassLoader is responsible for loading all packages under jre/lib/rt.jar, or jar packages specified by-Xbootclasspath option.

(4) Extension ClassLoader is responsible for loading lib/ext/*.jar. You can also specify the directory Djava.ext.dirs.

(5) AppClassLoader loads classes under classpath or specified classes

(6) Custom class loader to implement ClassLoader

eight。 Where is the custom class loader applied?

For example, tomcat and jboss will implement ClassLoader loading according to j2ee specification. In the process of loading, they will first check whether the class has been loaded from bottom to top, and check layer by layer from Custom ClassLoader to BootStrap ClassLoader. As long as a certain classloader has been loaded, this class is considered to have been loaded, ensuring that all ClassLoader of this class are loaded only once. And the loading order is top-down, that is, the upper layer tries to load this class layer by layer.

nine。 What is parental entrustment?

1. The subclass loader will first delegate the parent class loader to load and check the loading layer by layer.

two。 So from top to bottom, the bootStrap classloader will first see if there is this class in rt.jar, then see if the extension classloader has loaded the class, and if the Application classloader does not need to load the classpath class file load directory.

Note: BootStrap ClassLoader does not belong to the class hierarchy of JVM, it does not follow the loading rules of ClassLoader and it does not have subclasses. The top-level parent class that JVM can extract is ClassLoader, and then URLClassLoader implements the abstract class, and ExtClassLoader and AppClassLoader both inherit URLClassLoader, but they are inner classes of sun.miss.Launcher, so creating a Launcher class creates ExtClassLoader, and then ExtClassLoader creates AppClassLoader as a parent loader.

ten。 How the classloader works:

1. The principle is based on three mechanisms: delegation, visibility and oneness.

two。 Delegate means that the subclass loader will delegate the parent class loader; visibility means that the subclass can see the class loaded by the parent class, but the parent class cannot see the subclass: if a class is loaded by the subclass, the parent class will throw a ClassNotFundException exception if it is loaded by the parent class; the singleness principle means that only one class is loaded once, which ensures that the subclass loader will not load the class loaded by the parent class loader again.

Note: although it is possible to override class loaders that violate the delegate and singularity mechanism, it is not advisable to do so. You should strictly follow these three mechanisms when writing your own classloader

eleven。 How to implement a class loader by yourself

a. Load the file under the custom road strength:

Train of thought: 1. Inherit the ClassLoader class, specify ClassPath road strength, rewrite the findClass file, and still use the parent class to load the class directory if it is not custom. two。 Inherit the URLClassLoader class, customize a Url, and then call findClass (url)

b. Load Class in custom format, encrypt and decrypt files accepted by network transmission.

Idea: get the network transport stream, decrypt and restore the file format. Create an instance of this class through ClassLoader's defineClass ()

twelve。 Questions and thoughts:

a. How to achieve a hot deployment?

b. What is the problem with dynamically loading classes while the program is running?

c. How do objects in JVM transition smoothly during hot deployment?

Example: if you try the user's parent loader to load the class again, an exception will be thrown to verify oneness. Others can try for themselves.

Thank you for your reading, the above is the content of "what is the java class loader", after the study of this article, I believe you have a deeper understanding of what the java class loader is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report