In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
JVM class loading scenario example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictional computer, which is realized by simulating various computer functions on a real computer. With the introduction of the Java language virtual machine, the Java language does not need to be recompiled when it runs on different platforms. The Java language uses the Java virtual machine to shield the information related to the specific platform, so that the Java language compiler only needs to generate the object code (bytecode) running on the Java virtual machine and can run on a variety of platforms without modification.
Load the scene for everyone to analyze the class, I hope it can help you.
Class A calls the static method of class B. in addition to loading class B, class C, which is indirectly used by an uncalled method of class B, is also loaded. This interesting scenario comes from a question: why does the type used in the method try to load when it is not called?
The scene is as follows:
Public class Main {static {System.out.println ("Main static block");} public static void main (String [] args) {Helper.staticMethod ();}} public class Helper {static {System.out.println ("Helper static block");} public static void staticMethod () {System.out.println ("Helper#staticMethod") } public void test (XXXManager ab, XXXSubInterface xxxSubInterface) {ab.setXXX (xxxSubInterface);}} public interface XXX {} public interface XXXSubInterface extends XXX {} public interface XXXManager {void setXXX (XXX xxx);}
Add the parameter JVM-varbose to execute, and the output is:
[Loaded Main from file:/Users/mazhibin/project/java/loadclasstest/target/classes/] Main static block [Loaded Helper from file:/Users/mazhibin/project/java/loadclasstest/target/classes/] [Loaded XXX from file:/Users/mazhibin/project/java/loadclasstest/target/classes/] Helper static block Helper#staticMethod
The main method executes Helper.staticMethod (), while the staticMethod method only contains print statements, so in theory, it should be enough to load Helper and why it will be loaded into the XXX class. Well, even if it is acceptable to load the class, why it is XXX, instead of using XXXManager or XXXSubInterface directly. Your question is about this scene.
Before talking about the exploration process, let's first say the final conclusion: when verifying the helper class, verifying the setXXX method will verify whether the XXXSubInterface type can be assigned to the XXX type, and then load the XXX class, and then because XXX is an interface, the code thinks that the interface and the Object class are the same, and any type can be assigned to the interface type, so the verification is successful and the XXXSubInterface class is not loaded.
Then introduce the process of class loading. First of all, it is clear that "class loading" and "loading" are two concepts, and "loading" is a step of "class loading" (Class Loading). Class loading includes three steps: loading, linking and initialization, in which the link is divided into three sub-steps: verification, preparation and parsing. Loading is the process of finding a binary representation (Binary Representation) of a class or interface type based on a specific name and creating a class or interface from this binary representation. Linking is the process of merging a class or interface into the virtual machine runtime state so that the class or interface can be executed by the Java virtual machine. The initialization of a class or interface means that the initialization method of the class or interface is executed.
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.