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 Jvm Class loading Mechanism

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

Share

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

This article is to share with you about how to understand the Jvm class loading mechanism, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

What is jvm?

Normally, we write code in .java files, so how can we deploy it to run on the machine? By packing the jar package or the war package, and then deploying and running.

If you look at the contents of the jar package, you will know that all the .java files we wrote have been compiled into .class files.

Here is a very important step-compiling: translating the program we write into a file format that can be read by jvm.

It is worth noting that each class is compiled into a .class file, including inner classes, and so on. This means that each .class file corresponds to only one class in our code.

The life cycle of a class

The class is loaded into the memory of the jvm virtual machine, and its life cycle can be divided into: load-> verify-> prepare-> parse-> initialize-> use-> unload.

Load

When a jar package is generated, all the programs we write are compiled into a .class format that jvm can read. At this point, we need to load our compiled .class file into jvm. At this point, there will be a concept of "classloader".

The next question is, when will the classloader load a .class load with jvm? That is, under what circumstances will a class be loaded?

When a jar package runs, it specifies a main () method as the entry method. First, the class where the main () method is located is loaded into jvm, and when the code execution encounters new, it continues to load the object into jvm.

So to sum up, when you need to use this class in your code, it will be loaded into jvm.

Verification

This does not need to understand too deep, very straightforward truth, can not be any cat and dog can be loaded into the jvm, or it will be messed up. So this stage is to verify that the loaded .class file conforms to the specified rules.

What's interesting is that every .class file is romantic, because every .class file has eight hexadecimal 0×CAFEBABE, which translates to coffee babies. Romantic, huh? The first step in the verification phase is to check that the .class file begins with a coffee baby.

Prepare for

When we legally load a .class file into jvm, some preparatory work will be done.

First allocate memory space for this class, and then assign a default initial value to the class variable (the variable modified by static). But if the class variable is modified by final at the same time, it is not the initial value but the specific value.

Use the following two situations to illustrate:

Public class Student {private static int age = 18;} / / at this point memory space is allocated to the age variable and its initial value of 0 is assigned.

Public class Student {private static final int age = 18;} / age is modified by final, and the age variable is allocated memory space and assigned a value of 18.

In the parsing phase, jvm replaces the symbolic reference of the constant pool with a direct reference.

To put it simply, in the code we write, when a variable references an object, the reference is stored as a symbolic reference in the .class file. In the parsing phase, you need to resolve it to a direct reference. If there is a direct reference, the target of the reference must already exist in memory.

Initialization in the preparation phase we have allocated memory space for the class loaded into jvm and assigned the initial value to the class variable.

It is not until the initialization phase that the execution of the java program code defined in the class actually begins.

Assign the correct initial value to the static variable of the class.

Executes a static block of code for the class.

The variable assignment statements and static statements in the class are run sequentially from top to bottom, and they are initialized only when the class or interface is first actively used by the Java program. If there is a parent class, first run the variable assignment statements and static statements in the parent class in order.

We cannot use non-static variables directly in a static method. When we use a static method, we only initialize the class where the static method is located, where only static variables are assigned values and non-static variables are not assigned. Therefore, non-static variables cannot be used directly in static methods.

The above is how to understand the Jvm class loading mechanism. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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