Reference Blog:
Insider of the "doubts" Java dynamic binding mechanism : http://blog.csdn.net/zero_295813128/article/details/52117737
Java Virtual Machine Architecture
: http://hxraid.iteye.com/blog/676235
--------------------------------------------------------------------------------------------------
Here is a summary of your own understanding:
The first two concepts have to understand the method area and the constant pool. These are both Java Virtual data regions at runtime, and the constant pool is the method area
Part of it.
Method area
The method area, like the Java heap, is an area of memory shared by each thread that stores the class information, constants, and static changes that have been loaded by the virtual machine.
Code, even after the compiler compiles the data.
Constant pool
Chang are part of the method area and are used to hold various literal and symbolic references generated by the compilation period. There are 11 constant tables in a constant pool.
Constant table type flag value (1 byte) description
Constant_utf8 1UTF-8 encoded Unicode string
Constant_integer3literal literals of type int
Constant_float 4Float type literals
Constant_long 5literal literals of type Long
Constant_double 6literal literals of type Double
Constant_class 7 symbolic references to a class or interface
Constant_string 8Reference to String type literals
Constant_fieldref9 symbol reference to a field
Constant_methodrefA symbolic reference to a method within a class
Constant_interfacemethodref a symbolic reference to a method in an access port
Constant_nameandtypeA partial symbolic reference to a field or method
---------------------------------------------------------------------------------------------------
For the principle of polymorphism the main research method area. In Java, polymorphism refers to a pointer to a base class or a reference to an object of a derived class, while a specific visit to a
The dynamic binding of the implementation method when asked. There are static bindings and dynamic bindings for method calls in Java, and static binding refers to the time we compile
The bytecode that will execute that method is determined, and dynamic binding is only known at run time.
Static bindings
Static methods in Java, private methods, and calls to final-decorated methods are static bindings, and for overloaded instance methods
Called, but also with static bindings. The principle of static binding is the process of a constant pool parsing, the following is a detailed explanation of its process:
If there are two classes A, B, in Class A We call a static method in class B, at compile time, the action of this call is compiled into a
A static invocation instruction that refers to the symbolic reference to the method stored in the Constant_methodref table in the constant pool, which is referenced by this symbol
You can get the full class name of the static method B,JVM load Class B, you will get the direct address of the method in class B, the address will be stored in the Class A constant pool corresponding to the
In the constant table, this is the constant pool parsing process, and when the call is initiated again, the corresponding method is called directly from the direct address.
As you can see from the above process, we have determined which bytecode code should be executed during the compile phase.
Dynamic binding
An important data structure for JVM management-the method table-needs to be understood before dynamic binding is explained. It records the current class as an array and all of its
The visible method of the superclass is the direct address of the bytecode in memory.
Dynamic binding The preceding process is similar to a static binding, if there are two classes A, a, b inherits the Class A, and the F1 () method is overridden in Class A, we use
The upward transformation method calls the F1 () method with a type reference to the B instance, and the compiler generates a bytecode instruction that goes to the constant table to find the F1 () method letter
This reference determines the full name of the type that invokes the method, that is, the full names of Class A, which is loaded into the class A byte code according to the name, to the method corresponding to Class A
The table finds the F1 () method, records its direct address into the corresponding constant table of the class that calls the F1 () method, ends the constant pool parsing, and can think about whether we can
Determines which piece of bytecode is executed when the F1 () method is called, and the answer is no, because at this point our F1 () method specifies that the method in the parent class is executing, although the reference
The parent class type, but he points to the parent class object or the subclass object is unknown (to be exact, the program does not know at this time, the programmer must know), if pointing to the parent class
That is the F1 () method in the parent class, if you point to an instance of the subclass, the subclass does not override, still executes the parent class F1 () method, and if the subclass overrides it, it should be a subclass of F1 ()
Method. At this point, the dynamic binding comes in, OK F1 () to get a class B instance in the heap reference, by reference to find the object in the heap B, according to the object to further obtain its
Method table, which finds the direct address of the F1 () method of the method table, is finalized at this point.
The implementation principle of Java polymorphism