Java Process for creating objects

Source: Internet
Author: User

Java Process for creating objects

Java is an object-oriented programming language that has objects created every moment of the Java program running. At the language level, creating objects is usually just a new keyword, and what is the process of creating objects in a virtual machine?

First, testing whether the class is loaded

virtual opportunity to a new directive, first checks whether the parameter of this directive can be positioned in a constant pool to a symbolic reference of a class, and checks whether the class represented by the symbol reference has been loaded, parsed, and initialized. If not , you must first perform the appropriate class loading process.

Ii. allocating memory for new objects

After the class load check passes, the virtual machine then allocates memory for the new object. The size of the memory required for an object is fully determined after the class is loaded.

Assuming that the memory in the Java heap is absolutely regular, that all used memory is on one side, that the free memory is on the other side, and that a pointer is placed in the middle of the point indicator, then allocating memory simply moves that pointer over the free space to a distance equal to the size of the object, which is called a "pointer collision".

If the memory in the Java heap is not regular, the memory used and the idle memory are interleaved, there is no way to simply do a pointer collision, the virtual machine must maintain a list of which memory blocks are available, at the time of allocation from the list to find a large enough space to partition the object instance, and update the records on the list, which is called the "Idle list."

The choice of which allocation method is determined by whether the Java heap is structured or not, and whether the Java heap is structured or not, is determined by whether the garbage collector in use has a compression collation function.

Third, initialize the value of 0

After the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to 0 values (excluding the object header), which ensures that the instance fields of the object can be used directly in Java code without assigning the initial values, and the program can access the 0 values corresponding to the data types of those fields.

Iv. making the necessary settings

Next, the virtual machines make the necessary settings for the object, such as which instance of the class it is, how to find the metadata information of the class, the hash code of the object, the age of the object's GC, and so on. This information is stored in the object's header.

V. Execution of the Init method

after the work is done, a new object has been created from the virtual machine's perspective, but from the Java thread Order of view, object creation has just begun,<init> method has not been executed, all fields are also zero. So in general, after executing the new command, the <init> method is followed, which initializes the object to the programmer's will, so that a truly usable object is fully generated.

To summarize the above, the process of creating an object is:

Detects whether a class is loaded without loading first load → allocate memory for new objects → Initialize the allocated memory space to 0 values → make necessary settings for the object → execute <init> method Initialize the object

It is not easy to create an object that is finished.

This post is Bo Master of "in-depth understanding of Java Virtual Machine" book excerpt, for Bo Master original.

Java Process for creating objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.