Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Dynamic dispatch of JVM method invocation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

1. Dynamic dispatch

One embodiment is override. In the following code, the result is obvious.

1 public class App {2 3 public static void main (String [] args) {4 Super object = new Sub (); 5 object.f (); 6} 7} 8 9 class Super {10 public void f () {11 System.out.println ("super: F ()"); 12} 13 14 public void f (int I) {15 System.out.println ("super: f (int)") 16} 17} 18 19 class Sub extends Super {20 21 @ Override22 public void f () {23 System.out.println ("sub: F ()"); 24} 25 26 @ Override27 public void f (int I) {28 System.out.println ("sub: F (int)") 29} 30 31 public void f (char c) {32 System.out.println ("sub: F (char)"); 33} 34}

The final output sub: F ()

So how do virtual machines do dynamic dispatch?

Different virtual machines have different implementations, the most commonly used is the use of virtual method tables (Virtual Method Table)

two。 Virtual method table

For the Super and Sub classes, the virtual method table is roughly as follows: (soul painter)

What does the soul painting above mean?

The virtual method table stores the actual entry address of each method. If a method is not overridden in the subclass, the address entry in the virtual method table of the subclass is the same as that of the method with the same signature of the parent class, pointing to the implementation entry of the parent class. If this method is overridden in the subclass, the address in the subclass method table will be replaced with the entry address of the version implemented to the subclass.

The main information drawn from the above picture is:

a. Most of the methods in the figure above, the subclasses Super and Sub are not overridden, so they all point to the type data of the parent class Object. The f () and f (int) methods are both implemented by the parent subclass, so they point to different implementation addresses. F (char) defines the implementation only in the subclass and naturally points to the type data of the subclass.

b. For the convenience of program implementation, the method with the same signature should have the same index number in the virtual method table of the parent class and the subclass, so that when the type is changed, only the method table needs to be changed. the desired entry address can be converted according to the index from different virtual method tables.

3. Case analysis

Analyze with the code at the beginning of this article. View the instructions of the main method through the javap command.

The detailed calling procedure of the invokevirtual instruction is as follows:

1) # 19 in the instruction refers to the index entry of the 19th constant table in the constant pool of class App. This constant table (CONSTATN_Methodref_info) records a symbolic reference to method f () information. JVM first finds the fully qualified name com.khlin.Super of the class calling method f () based on this symbolic reference, because the variable object is declared as a Super type.

2) look for method f () in the method table of type Super, and if found, record the index item of method f () in the method table (I don't know the specific value, it's called index here) to the 19th constant table in the constant pool of class App (constant pool parsing). Therefore, if there is no f () in the method table of type Super, the compilation failure will be reported even if the method table of type Sub has this method.

3) before invoking the invokevirtual instruction, there is an aload_1 instruction that pushes a reference to the Sub object in the heap that starts creation into the Operand stack. Then the invokevirtual instruction first finds the Sub object in the heap based on the reference to the Sub object, and then further finds the method table of the type to which the Sub object belongs.

4) at this time, through the index looked up by 2), you can locate the f () method in the Sub type method table, and then find the memory space where the method bytecode is located through the direct address. This is why the method index numbers of the parent and child classes with the same signature are the same.

4. Comprehensive consideration: an example of possible mistakes

Change the main method in the code at the beginning of this article slightly to call other methods.

1 public static void main (String [] args) {2 Super object = new Sub (); 3 char c = 4 object.f (c); 5}

The result will output sub: F (int)

It is clear that there is exactly the same type of f (char) method in the Sub method, but it calls f (int).

I believe that through the previous study, we can already understand the reason.

When object.f (c) is called, the virtual machine first goes to the method table of the Super class to find the most appropriate method.

There is no f (char) method that happens to be char in the Super class. According to the previous study of static dispatch and automatic conversion of parameter types, you can see that the compiler uses the most appropriate method f (int) besides f (char). After getting the index, the f (int) method is found in the Sub method table of the actual object, and the final execution is the f (int) method of the Sub class.

The bytecode instruction of this method proves the above argument.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report