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 Java class loading is performed

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how Java class loading is performed". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how Java class loading is performed" can help you solve the problem.

Class loader

Class loader action

Responsible for loading .class files (stored physical files) into memory

Class loader procedure

Load: get this class through the full class name to be streamed, loaded into memory, and create a Class object after loading

Link

Verify: ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine and does not endanger the security of the virtual machine itself

(whether the information in the file conforms to the virtual machine specification is there a security risk) preparation: responsible for allocating memory for class variables (variables modified by static) and setting default initialization values

(initialize static variables) parsing: replaces symbolic references in the binary data stream of a class with direct references

(if other classes are used in this class, you need to find the corresponding class at this time.)

Initialization: initialize class variables and other resources (that is, constructor initialization written by yourself) according to the programmer's subjective plan.

(static variable assignment and initialization of other resources)

Class loading classification

Bootstrap class loader: a built-in classloader for a virtual machine, usually represented as null, and without a parent null

Platform class loader: platform class loader, responsible for loading some special modules in JDK

System class loader: system class loader, which is responsible for loading the class libraries specified on the user's classpath

Class loader inheritance relationship

The parent loader of System is Platform

The parent loader of Platform is Bootstrap

Code example public class ClassLoaderDemo1 {public static void main (String [] args) {/ / get the system class loader ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader (); / / get the parent loader of the system class loader-platform class loader ClassLoader classLoader1 = systemClassLoader.getParent () / / get the parent of the platform classloader-Boot classloader ClassLoader classLoader2 = classLoader1.getParent (); System.out.println ("system classloader" + systemClassLoader); System.out.println ("platform classloader" + classLoader1); System.out.println ("Startup classloader" + classLoader2);}}

Parental delegation model

If a class loader receives a class load request, it will not load it first, but delegate the request to the parent class loader to execute it. If the parent class loader still has its parent class loader, it will further delegate upward and recursively, and the request will eventually reach the top-level startup class loader. If the parent class loader can complete the class loading task, it will return successfully. If the parent loader cannot complete this loading task, the child loader will try to load it on its own, which is the parent delegation mode. (in one sentence, the loading class is first loaded by the parent class loader. If the parent class cannot be loaded, it will reload the subclass loader in turn until the loaded class loader is found.)

Two methods in ClassLoader

Method introduction

Method name description public static ClassLoader getSystemClassLoader () get system class loader public InputStream getResourceAsStream (String name) load a resource file import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class Demo {public static void main (String [] args) throws IOException {/ / get system class loader ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader () / / use the loader to load a specified file / / parameter: the path of the file (placed in the root directory of src, where it is loaded by default) / / return value: byte stream. InputStream is = systemClassLoader.getResourceAsStream ("prop.properties"); Properties prop = new Properties (); prop.load (is); System.out.println (prop.getProperty ("name")); is.close ();}} that's all for "how Java class loading is performed". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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