In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the meaning of class loading of JVM". In daily operation, I believe that many people have doubts about the meaning of class loading of JVM. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the meaning of class loading of JVM?" Next, please follow the editor to study!
Process
Loading condition
When actively using class
Create an instance of a class (new, reflection, cloning, deserialization)
Call the static method of the class (invokestatic)
Static fields that use classes or interfaces (getstatic, putstatic)
Use reflect reflection
Initialize the subclass, initialize the parent class first
Class of the main method
Examples
Passive references do not initialize classes
Package com.mousycoder.mycode.thinking_in_jvm;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-02 16:29 * / public class Parent {static {System.out.println ("Parent init");} public static int v = 100;} package com.mousycoder.mycode.thinking_in_jvm;import sun.jvm.hotspot.memory.ParNewGeneration / * * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-02 16:29 * / public class Child extends Parent {static {System.out.println ("Child init");}} package com.mousycoder.mycode.thinking_in_jvm / * * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-02 16:32 * / public class UseParent {public static void main (String [] args) {System.out.println (Child.v);}}
Virtual machine parameters-XX:+TraceClassLoading output
[Loaded com.mousycoder.mycode.thinking_in_jvm.Parent from file:/Users/mousycoder/My/code/mycode/target/classes/] [Loaded com.mousycoder.mycode.thinking_in_jvm.Child from file:/Users/mousycoder/My/code/mycode/target/classes/] Parent init100 [Loaded java.lang.Shutdown from / Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar] [Loaded java.lang.Shutdown$Lock from / Library/Java/JavaVirtualMachines/ Jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar]
Indicates that Child is loaded but not initialized
The final constant does not cause class initialization
Package com.mousycoder.mycode.thinking_in_jvm;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-02 16:41 * / public class FinalFieldClass {public static final String constString = "CONST"; static {System.out.println ("FinalFieldClass init");} package com.mousycoder.mycode.thinking_in_jvm;public class UseFinalField {public static void main (String [] args) {System.out.println (FinalFieldClass.constString) }}
Output
CONST
Instead of initializing the constant constString because the constant CONST is referenced, the FinalFieldClass class puts the CONST directly in the constant pool
Load
Example: load the String class
Package com.mousycoder.mycode.thinking_in_jvm;import java.lang.reflect.Method;import java.lang.reflect.Modifier;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-03 13:38 * / public class StringTest {public static void main (String [] args) throws ClassNotFoundException {Class clzStr = Class.forName ("java.lang.String"); Method [] ms = clzStr.getDeclaredMethods () For (Method m: ms) {String mod = Modifier.toString (m.getModifiers ()); System.out.println (mod + "" + m.getName () + "("); Class [] ps = m.getParameterTypes (); if (ps.length = = 0) {System.out.println (')') } for (int I = 0; I
< ps.length; i++) { char end =i==ps.length-1 ? ')':','; System.out.println(ps[i].getSimpleName() + end); } System.out.println(); } }} 输出 `` public equals ( Object) public toString ( ) public hashCode ( ) public volatile compareTo ( Object) public compareTo ( String) public indexOf ( String, int)` `` 验证 保证加载的字节码合法Prepare for
Allocate the corresponding memory space for the class and set the initial value. No java code will be executed at this stage | Type | default initial value |-- |-| int | 0 | | long | 0L | | short | (short) 0 | | char |\ u0000 | | boolean | false | | reference | null | | float | 0f | double | 0f | |
Analysis
Convert symbolic references (literal references) of classes, interfaces, fields, and methods to direct references (find the location of the table in the method)
Initialization
Executes the initialization method of the class, which is automatically generated by the compiler and is generated by the combination of assignment statements of static members of the class and blocks of static statements, where the function is thread-safe with locks and can lead to deadlocks.
At this point, the study on "what is the meaning of class loading of JVM" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.