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

Example Analysis of object creation and OOP-Klass Model in JVM

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you the JVM object creation and OOP-Klass model example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

In OOP-KLASS model in JVM, OOP-KLASS model is used to represent java object in JVM.

That is:

When 1.jvm loads class, it creates an instanceKlass to represent its metadata, including constant pools, fields, methods, etc., which are stored in the method area; instanceKlass is the data structure in jvm

two。 When you new an object, jvm creates an instanceOopDesc to represent the object, which is stored in the heap area and its reference is stored in the stack area; it is used to represent the instance information of the object, which looks like a pointer is actually an object hidden in the pointer; instanceOopDesc corresponds to the object instance in the java

3.HotSpot does not expose instanceKlass to Java, but creates a corresponding instanceOopDesc to represent the java.lang.Class object, calling the latter a "Java mirror" of the former, and the klass holds a reference to the oop (_ java_mirror is the instanceKlass's reference to the Class object).

4. Note that the instanceOopDesc type pointer returned by the new operation points to instanceKlass, while instanceKlass points to the instanceOopDesc; of the corresponding type Class instance. To put it simply, it is the Person instance-- > Person's instanceKlass-- > Person's Class.

InstanceOopDesc, which contains only data information, consists of three parts

1. Object header, also known as Mark Word, mainly stores object runtime record information, such as hashcode, GC generation age, lock status flag, thread ID, timestamp, etc.

two。 The metadata pointer, which points to the instanceKlass instance of the method area (through which the virtual machine groups to determine which class the object is an instance of. )

3. Instance data

4. In addition, if it is an array object, there is an extra array length

An example shows that class Model {public static int a = 1; public int b; public Model (int b) {this.b = b;}} public static void main (String [] args) {int c = 10; Model modelA = new Model (2); Model modelB = new Model (3);}

Method area: class information, class variables (static variables and constants), methods

Heap: objects, member variables

Stack: local variabl

(1) when the program is running, first load the bytecode file through the class loader, after parsing, load the method area! All kinds of information about the class, including class variables, constants and methods, are stored in the method area. For calls to the same method, different instances of the same class call the same method that exists in the method area. The life cycle of a class variable is created when the program starts to run and ends when the program ends!

(2) when an object is new in the program, the object is stored in the heap, and the variables of the object are stored in the stack, pointing to the reference in the heap! The member variables of the object are stored in the heap, and when the object is reclaimed, the member variables of the object disappear!

(3) when a method is called, JVM allocates a stack frame in the stack to store the local variables of the method. When the method call ends, the local variable disappears!

Class variable: belongs to the attribute information of the class, independent of the instance of the class, multiple instances share the same class variable, exist in the method area. Class variables are decorated with static, including static variables and constants. Static variables have default initial values, and constants must be declared and initialized at the same time.

Member variable: a variable that belongs to an instance, only related to the instance, written under the class, outside the method, not modified by static. Member variables survive as the member is created and destroyed as the member is recycled.

Local variable: declared in the method, with no default initial value, created with the method call, stored in the stack, and destroyed at the end of the method call.

The above is all the content of the article "sample Analysis of object creation and OOP-Klass Model in JVM". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report