How Java objects are accessed and located

Source: Internet
Author: User

When Java objects are accessed, we need to manipulate the specific objects through the reference type of data in the Java Virtual machine stack. Because the reference type only specifies a reference to an object in the Java Virtual Machine specification, it does not define the way that this reference should be used to locate and access specific object instances in the Java heap, so the general way of access depends on the type of Java virtual machine. Currently, the main way of access is through the handle and the direct pointer two ways.
1. Handle Access
Using the handle access method, the Java heap will be divided into a portion of memory to be used as a handle pool, reference is stored in the object's handle address. The handle contains the address of the object instance data and the specific address information of the object type data (such as Object type, implemented interface, method, parent class, field, etc.). Let me explain briefly in one example:
Object obj = new Object ();
Object obj represents a local reference, stored in the Java stack's local table of variables, representing a reference type of data.
The new object () is stored as an instance object in the Java heap, and the Java heap also stores information about the object class (objects types, implementation interfaces, methods, and so on) that are stored in the method area by the data types that the address information executes.
2. Direct pointer access
If you use pointer access, you must consider how to place information about the access type in the layout of the Java heap object (   such as the type of object, the implemented interface, method, parent class, field, and so on, and the address of the object is stored in reference.  There are pros and cons to both types of access, and the greatest benefit of using a handle is that it stores a stable handle address in the reference, and when the object is moved (it is a very common behavior to move objects during garbage collection), simply change the address of the object instance in the handle.  Reference without modification. 
The benefit of using pointer access is fast access, which reduces the time spent on pointer positioning, because Java is an object-oriented language, access to Java objects is very frequent in development, so this kind of overhead is a lot more expensive, and conversely, it increases access speed.
This article refers to an in-depth understanding of Java Virtual Machine JVM advanced features and best practices.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How Java objects are accessed and located

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.