Turn: The internal implementation mechanism of the Java dynamic agent (the general meaning is correct, write a line of an article)

Source: Internet
Author: User
Tags object model

go: internal Implementation mechanism of Java dynamic binding

When a Java virtual machine calls a class method, it chooses the method that is called based on the type of the object reference, which is usually known at compile time. Conversely, when a virtual machine invokes an instance method, it chooses the method that is called based on the actual type of the object (which can only be known at run time), which is dynamic binding, which is polymorphic. Dynamic binding provides a great deal of flexibility to solve real business problems and is a very graceful mechanism.

1 Java Object Model

The Java Virtual Machine specification does not specify how Java objects are represented in the heap. The internal representation of an object also affects the entire heap and the design of the garbage collector, which is determined by the virtual machine's implementation.

The basic data contained in a Java object consists of the class that it belongs to and the instance variables declared by all its superclass. As long as there is an object reference, the virtual machine must be able to quickly locate the data for the object instance. In addition, it must be able to access the corresponding class data through the object reference (stored in the type information of the method area), so there is usually a pointer to the method area in the object. When a program needs to convert an object reference to another type at run time, the virtual machine must check whether the conversion is allowed, whether the object being converted is indeed a referenced object or its superclass. When the program is performing the instanceof operation, the virtual machine also performs the same checks. So the virtual machine needs to look at the class data of the object being referenced.

Regardless of the object notation used by the virtual machine implementation, it is likely that each object has a method table because the method table accelerates the efficiency of invoking instance methods. However, the Java Virtual Machine specification does not require a method table to be used, so it is not used in all implementations.

The following is a memory representation of a Java object:

Java Object Memory model

The method data is stored in the method area of the class, which contains the bytecode binary for the specific implementation of a method. The method pointer points directly to the starting position of the method in memory and can be found by means of the method pointer.

2 Dynamic binding internal mechanism

The method table is an array of method pointers in the method area. Methods in the method table do not contain static bindings such as statics, private, and only the instance methods that require dynamic binding.

In the method table, the method from the superclass appears before the method from the subclass, and the order in which the method pointers are arranged is the same as the order in which the methods appear in the class file, except that the method covered by the method of the quilt class appears where the method first appears in the superclass.

For example, there are superclass base and subclass derive:

[Java]View PlainCopy
  1. Public class Base
  2. {
  3. Public Base ()
  4. {
  5. }
  6. public Void Test ()
  7. {
  8. SYSTEM.OUT.PRINTLN ( "int Base");
  9. }
  10. public void print ()
  11. {
  12. }
  13. }
  14. Public class Derive extends Base
  15. {
  16. Public Derive ()
  17. {
  18. }
  19. public Void Test ()
  20. {
  21. SYSTEM.OUT.PRINTLN ( "int Derive");
  22. }
  23. public void SayHello ()
  24. {
  25. }
  26. public static void Main (string[] args)
  27. {
  28. Base base = new Derive ();
  29. Base.test ();
  30. }
  31. }

The method table for base and derive in the above example is as follows:

In this example, the test () method is the same position in the method table of base and derive-position 1. In the base method table, the test () pointer is the memory address of Base's test () method, whereas in the derive method table, the location of the method table 1 places the derive test () method memory address.

When the Java Virtual machine executes base.test (), the base reference allows you to find the memory location of the actual object pointed to by base, and now the virtual machine does not know whether the actual object referenced by base is base or derive. However, based on the object memory model above, the virtual machine starts with the first pointer "special struct pointer" in the object memory and can find the actual object's type data and class instance, so that the virtual machine can know that the actual object referenced by the base is a derive pair. In order to execute test (), the virtual machine needs to find the byte Code of Test (), and the bytecode of the method is stored in the method area. The virtual machine starts with the first pointer "special struct pointer" in the object memory, the location of the search Method table 1, and the test () method of position 1 is the test () method of the derive class, which is the bytecode of the test () that the Java virtual machine will execute. Now that the virtual machine knows that the actual object being called is the derive object, the actual test () method called is the test () method of the derive class, so the Java virtual machine is able to execute correctly-the method of invoking the actual object referenced by base instead of the base reference itself.

This is an implementation of dynamic binding, according to different Java Virtual machine platform and different actual constraints, dynamic binding can have different internal implementation mechanisms.

Turn: The internal implementation mechanism of the Java dynamic agent (the general meaning is correct, write a line of an article)

Related Article

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.