In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how the Java virtual machine loads the Java class". In the actual case operation, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The class loading in the Java virtual machine is from the class file to the class in memory, which needs to go through three steps: loading, linking and initialization.
What is the load object of the virtual machine?
As mentioned above, there are two types in Java: the basic type and the reference type, which are predefined by the virtual machine, and the generic parameters in the reference type are erased during compilation, so the loaded objects are left with classes, interfaces, and array classes.
In classes, interfaces, and array classes, array classes are generated directly by the Java virtual machine, while the other two have corresponding byte streams. Both directly generated array classes and loaded classes need to be linked and initialized by the Java virtual machine. Next, let's give a detailed description of what is being done at each step.
What is the loading process of the virtual machine? 1. Load
Is the process of finding a byte stream and creating a class accordingly. As mentioned above, array classes are generated directly by the Java virtual machine, so the loading process is aimed at the classes and interfaces that generate byte streams. How to find these byte streams requires a virtual machine with the help of a class loader.
The startup class loader is implemented by C++, and there is no corresponding Java object, so null can only be used to refer to it in Java. Prior to Java 9, the boot class loader was responsible for loading the most basic and important classes, such as those stored in the jar package in JRE's lib directory (and the classes specified by the virtual machine parameter-Xbootclasspath). In addition to launching the classloader, two other important classloaders are the extended classloader (extension class loader) and the application classloader (application class loader), both provided by the Java core class library. Therefore, except for the startup classloader, the other classloaders are subclasses of java.lang.ClassLoader, so there are corresponding Java objects.
The parent class loader of the extension class loader is the startup class loader. It is responsible for loading relatively minor but general-purpose classes, such as those stored in the jar package in JRE's lib/ext directory (and those specified by the system variable java.ext.dirs).
The parent class loader of the application class loader is the extended class loader. It is responsible for loading classes under the application path. (the application path here refers to the path specified by the virtual machine parameter-cp/-classpath, the system variable java.class.path, or the environment variable CLASSPATH. By default, the classes contained in the application are loaded by the application class loader
Java 9 introduced the module system and slightly changed the classloader 1 above. The extension class loader is renamed to platform class loader (platform class loader). Except for a few key modules in Java SE, such as java.base, which is loaded by the startup class loader, all the other modules are loaded by the platform class loader. Of course, you can also customize the class loader.
In addition to the loading function, the classloader also provides the role of a namespace, and in the Java virtual machine, the uniqueness of the class is determined by both the classloader instance and the full name of the class. Even if the same string of byte streams is loaded by different class loaders, you will get two different classes. In large-scale applications, we often use this feature to run different versions of the same class.
two。 Link
Is the process of merging the created classes into the Java virtual machine so that they can be executed. It can be divided into three stages: verification, preparation and parsing.
Verification phase: make sure that the loaded class meets the constraints of the Java virtual machine.
Preparation phase: allocate memory for the static fields of the loaded class and construct other data structures related to the class hierarchy.
Parsing phase: resolves symbolic references to actual references (the Java virtual machine specification does not require resolution to be completed during the link process. It simply states that if some bytecode uses symbol references, the symbol references need to be parsed before the bytecode is executed.
Symbol reference means that before the class file is loaded into the Java virtual machine, the class cannot know the specific address of other classes and their methods and fields, or even the addresses of their own methods and fields. Whenever these members need to be referenced, the Java compiler generates a symbolic reference. In the run-time, this symbolic reference can generally be located on the specific target without ambiguity.
3. Initialization
Initialization means assigning and executing constants
< clinit >Method, after completion, the class officially becomes executable.
Class initialization trigger condition
Initialize the main class specified by the user when the virtual machine starts
Initialize the target class of the new instruction when the new instruction is encountered to create a new instance of the target class
When an instruction to call a static method is encountered, initialize the class in which the static method is located
When an instruction to access a static field is encountered, initialize the class in which the static field is located
The initialization of the subclass triggers the initialization of the parent class
If an interface defines a default method, initializing the class of the interface directly or indirectly will trigger the initialization of the interface.
Initialize a class when you make a reflection call to a class using reflection API
Initialize the class of the method that the MethodHandle points to when the MethodHandle instance is called for the first time.
Summary
The virtual machine loads the Java class is the process of the Java virtual machine converting the byte stream into the Java class. This process can be divided into three steps: loading, linking and initialization.
Loading: refers to the process of finding a byte stream and creating a class accordingly. Loading requires the help of a class loader, which uses a parent delegation model in the Java virtual machine, that is, when a load request is received, it will first forward the request to the parent class loader.
Link: refers to the process of merging the created class into the Java virtual machine so that it can execute. The link is also divided into three stages: verification, preparation, and parsing. Among them, the parsing phase is not necessary.
Initialization: assigns values to fields marked with constant values, and executes the
< clinit >The process of the method. Class initialization is performed only once, and this feature is used to implement delayed initialization of singletons.
That's all for "how the Java virtual machine loads the Java class". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.