Home > Java > JavaInterview questions > Introduce the composition of java objects

Introduce the composition of java objects

王林
Release: 2021-02-19 10:56:08
forward
3248 people have browsed it

Introduce the composition of java objects

Foreword:

The so-called objects are actually entities generated through java classes.

For example: the blueprint for building a building is a Java class. The building built according to this blueprint can be seen, and the actual existing building used is equivalent to the object obtained by using this blueprint.

Java class is actually an abstract concept, and the object is a specific concept that is concretized through the abstract concept of this java class.

In the HotSpot virtual machine, it is our common virtual machine. The object composition is divided into three areas: object header, instance data, and alignment padding.

Object header

The object header is divided into two parts: the object's own runtime data, the type pointer

The object's own runtime data, such as: HashCode, lock status Flag, lock held by the thread, biased thread ID, biased timestamp, GC generation age, etc. The length of this part of data is 32bit and 64bit respectively in 32-bit and 64-bit virtual machines (if the compression pointer is not enabled). This part is called "Mark Word".

And Mark Word is designed to have no fixed data structure to store more information in a small space. It will store different data according to different states. For example, in the 32-bit case, the storage content changes as follows

Introduce the composition of java objects

# type pointer, which is a pointer to the class metadata of this object, that is, this pointer is used to know this Which class the object is an instance of, but not all virtual machine implementations use this to find the metadata of the class.

Like the HotSpot virtual machine, we all know that a new object usually stores a reference to the object. This reference stores the address of the object instance. However, in the implementation of some virtual machines, this reference stores a The handle address of the object, which contains the object instance address and object type address.

That is, this type of virtual machine has a handle pool. The handle pool stores the instance data address and class data address of the object, so this type of virtual machine can directly find the object through the class data address in the handle pool. It is an instance of which class it belongs to.

(Learning video sharing: java video tutorial)

Introduce the composition of java objects

If the object is an array, then a place is needed to record the data length.

Instance data

Instance data is the data area where the object is actually stored, including various types of field contents.

Alignment padding

This part has no other meaning, it just serves as a placeholder, mainly because the memory management of the HotSpot virtual machine requires that the size of the object must be 8 bytes An integer multiple, and the object header is exactly an integer multiple of 8 bytes, but the instance data is not necessarily, so it needs to be aligned and filled.

Author: yes's leveling guide
Link: https://juejin.cn/post/6844903827427033096
Source: Nuggets

Related recommendations:javaInterview questions and answers

The above is the detailed content of Introduce the composition of java objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template