In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you the process of how to analyze an object of javanew. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
This article mainly introduces the parsing of a process example of an object in java new, which is introduced in great detail through the sample code, which has a certain reference value for everyone's study or work. Friends who need it can refer to it.
When java new an object, it will first check whether the class to which the object belongs has been loaded into memory, and if not, it will first load it through the fully qualified name of the class. After the class is loaded and initialized, the object is created.
Let's assume that this class is used for the first time, so that new an object can be divided into two processes: loading and initializing the class and creating the object.
Class loading process (using this class for the first time)
Java uses the parent delegate model for class loading, so before describing the class loading process, let's take a look at how it works:
The working process of the parent delegate model is that if a class loader (ClassLoader) receives a request for class loading, it will not try to load the class first, but delegate the request to the parent class loader, as is the case with every level of class loader, so all load requests should eventually be passed to the top-level startup class loader. The child loader tries to load itself only when the parent loader reports that it cannot complete the load request (the class it needs to load is not found in its search scope).
The advantage of using parental delegation mechanism is that it can effectively ensure the global uniqueness of a class. When there are multiple classes with the same qualified name in the program, the class loader will always load only one of the classes.
1. Load
It is the responsibility of the class loader to stream the binary bytes of a class into the JVM according to the fully qualified name of the class, store it in the method area of the runtime memory area, and then convert it to a java.lang.Class object instance corresponding to the target type.
2. Verification
Format verification: verify compliance with class file specification semantic verification: check whether a type marked final contains subclasses; check whether final methods in a class are overridden by subclasses Ensure that there are no incompatible method declarations between the parent class and the subclass (for example, the method signature is the same, but the return value of the method is different) operation verification: the data in the Operand stack must be operated correctly, validation is performed on various symbolic references in the constant pool (usually performed in the parsing phase to check whether the specified type can be located to the specified type by the fully qualified name described in the symbol reference) And whether the access modifier of class member information allows access, etc.)
3. Preparation
Allocate memory space for all static variables in the class and set an initial value for them (since no object has been generated, the instance variable is not within the scope of this operation) static variables (constants) modified by final will be assigned directly
4. Analysis
Change the symbolic reference in the constant pool to a direct reference (get the pointer or offset of the class or field or method in memory so that the method can be called directly), which can be performed after initialization. Parse content that requires static binding. / / all methods and fields that will not be overridden will be statically bound
The above three stages 2, 3, and 4 are collectively called the link phase. What the link phase needs to do is to merge the class data information of the binary byte stream loaded into JVM into the run-time state of JVM.
5. Initialization (father before son)
4.1 assign values to static variables
4.2 execute static code block
Note: only jvm can call the static code block
If multithreading needs to initialize a class at the same time, only one of the threads can be allowed to initialize it, and the other threads must wait until the active thread finishes initializing the class.
Because the subclass has a dependency on the parent class, the loading order of the class is to load the parent class first and then the subclass, and the initialization is the same. However, when the parent class initializes, the value of the subclass static variable is also available, which is the default value.
Finally, the method area stores the current class information, including static variables of the class, class initialization code (assignment statements and static initialization code blocks when defining static variables), instance variable definition, instance initialization code (instance code blocks and constructors of assignment statements when defining instance variables) and instance methods, as well as references to class information of the parent class.
Second, create objects
1. Allocate the memory needed by the object in the heap area
The allocated memory includes all instance variables of this class and the parent class, but does not include any static variables
2. Assign default values to all instance variables
Copy the definition of the instance variable in the method area to the heap area, and then assign the default value
3. Execute the initialization code of the instance
The initialization order is to initialize the parent class and then initialize the subclass. during initialization, the instance code block is executed first and then the construction method.
4. If there is a c reference similar to Child c = new Child (), define the Child type reference variable c in the stack area, and then assign the address of the heap area object to it
It should be noted that each subclass object holds a reference to the parent object, which can be called internally through the super keyword, but not externally
Add:
When calling the instance method through the instance reference, first look for the actual type information of the object in the method area, and then go to the parent class type information if you can't find it.
If the inheritance is deep and the method to be called is in the upper parent class, the efficiency of the call is relatively low, because each call has to go through many lookups. At this time, most systems will use a method called virtual method table to optimize the efficiency of calls.
The so-called virtual method table is to create a table for each class when the class is loaded. This table includes all the dynamically bound methods and their addresses of the objects of this class, including the methods of the parent class, but a method has only one record. When the subclass overrides the parent class method, it only retains the subclass. When you use the object dynamic binding method, you only need to look up this table, instead of looking up each parent class one by one.
The above is the process of how to parse javanew an object shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to 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.
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.