Schematic creation of Java objects

Source: Internet
Author: User
Tags java reference

Http://www.cnblogs.com/chenyangyao/p/5296807.html

The process of creating objects is usually started from the new directive (I'm talking about the JVM) (see Figure 1), where the JVM first parses the symbolic reference, and if the corresponding symbol reference is not found, then the class is not loaded. So the JVM does the class loading process (see my other blog post for a specific loading process). After the symbol reference is resolved, the JVM allocates memory to the object in the heap, and the Java object implemented by the hotspot virtual machine consists of three parts: the object header, the instance field, and the Align Fill field (see Figure 2 for details), which should be noted that Instance fields include self-defined and inherited from the parent class (even if the instance field of the parent class is overridden by the quilt class or is decorated by private, it is allocated memory as well). It is totally wrong to believe that many people are always looking at inheritance as a simple "copy" when they first touch the object-oriented language. Inheritance in Java is just a logical relationship between classes (specifically how to save the logical relationship of records, the design of knowledge of the class file format, see my other blog post), only the instance field when the object was created can be simply "copied".

After allocating heap memory to an object, the JVM initializes the memory (in addition to the object header area) by 0 values, which explains why the attribute fields of Java can be used without displaying initialization, and the local variables of the method must be initialized before they can be accessed. Finally, the JVM invokes the object's constructor, and of course the invocation order is traced back to the class object.

At this point, an object is created, and a reference is typically directed to that object. In Java, there are two types of data, one is a basic type such as int, double, and the other is a reference type, such as a class, an interface, an inner class, an enumeration class, a reference to an array type, and so on. There are generally two ways to implement a reference, see Figure 3. Here is a digression, often employing the reference in C + + and Java reference for comparison, in fact, they are just "name" like, the essence of nothing to do with, C + + refers to the existing variable is just an alias (reference variable is only a symbolic reference, the compiler does not assign new memory to the reference), the reference variables in Java are really positive variables, with their own memory space, but different reference variables can "point" to the same object . So, if you want to compare the way C + + and Java reference objects, the pointers in C + + are the same as those in Java, after all, the reference in Java is actually the encapsulation of pointers.

Note: This article is the original blog post, reproduced please indicate the source.

Figure 1 Creating a process for an object

Figure 2 Composition of the object

Figure 3 Two ways to implement object references

Schematic creation of Java 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.