What is the relationship between static connection, dynamic connection, dispatch, virtual method, and non-virtual method?

Write first

For those who are engaged in java development, they must have seen these concepts, but they don’t study carefully but don’t know what they are. Sometimes I feel that a few gratitudes are talking about one thing. In the cloud, I am also in this situation. For those who have studied these concepts, here is a summary of their connections.

1. What are these talking about?

1. Connect

1.1 Static connection and non-virtual method

There are these steps in the loading process of the class: loading -> verification -> preparation -> parsing -> initialization these five steps, where the purpose of the parsing step is to reference the symbol (pointing to the data that has not been loaded into the memory) Converted to direct reference (pointing to the address in the virtual machine memory). The process of converting symbolic references to direct references in the class resolution stage is called static linking. The method will have a determinable call before it actually runs. Version, and the calling version of this method is immutable at runtime. It can also be said that "the compiler knows that it is immutable at runtime" is called parsing.

  1. Which method calls will convert symbol references to direct references during the resolution phase?
    The answer is a non-virtual method, so what is a virtual method? Constructors, private methods, parent methods, static methods, and final modified methods are called virtual methods. They are controlled by invokestatic and invokespecial instructions. The invocation of non-virtual methods will convert their symbolic references into direct in the parsing stage of class loading. Reference.

  2. The relationship between static linking and non
    -virtual methods The invocation of non-virtual methods will convert symbolic references into direct references in the parsing stage of the class. This process is called static linking.

1.2 Dynamic connection and virtual methods

Corresponding to static connection, some method calls cannot be converted to direct references in the parsing stage of class loading, because the specific methods they call cannot be determined at this stage, such as common parent-oriented and interface-oriented programming Wait. This part of the method call can only convert symbol references into direct references during runtime, and this part is dynamic linking.

  1. Which methods belong to the category of dynamic connection?
    The definition of non-virtual methods has been mentioned above, so except for non-virtual methods, other methods are all virtual methods. For example, the call of interface method and the call of instantiation method are all virtual method calls. At the instruction level, they are invokevirtual, invokeinterface, Invokedynamic these instructions are applied to the call of virtual methods. One of the special cases is that the final modified method is a non-virtual method, and the invokevirtual instruction is still used.
  2. The relationship between dynamic linking and virtual methods
    Non-virtual methods convert symbolic references into direct references during the running phase of the program. This process is called dynamic linking.

2. Dispatch

Static linking and dynamic linking describe the process or action of converting symbolic references into direct references. Dispatching and connection are not a hierarchical concept, but dispatching describes the process of method version determination.

2.1 Static dispatch

Dispatching can be divided into static dispatch and dynamic dispatch. Java overloading and rewriting are the best examples of static dispatch and dynamic dispatch. Before introducing static dispatch in detail, let's look at an example of method overloading in Java.

public class TestDispath {
    
    
    static abstract class Father{
    
    

    }
    static class Son extends Father{
    
    

    }
    static class Daughter extends Father{
    
    

    }

    public void test(Father father){
    
    
        System.out.println("我是父亲");
    }
    public void test(Son son){
    
    
        System.out.println("我是儿子");
    }
    public void test(Daughter daughter){
    
    
        System.out.println("我是女儿");
    }

    public static void main(String[] args) {
    
    
        Father son = new Son();
        Father daugther = new Daughter();
        TestDispath testDispath = new TestDispath();
        testDispath.test(son);
        testDispath.test(daugther);
    }
}

The above is an example of method overloading, and the output result is as follows:

我是父亲
我是父亲

Process finished with exit code 0

So why is the method test (Father father) actually called? Let's first understand the next two concepts.

  1. Static type: Father son = new Son(); in the code above, where Father is the static type of the variable son.
  2. Actual type: Father son = new Son(); in the code above, where Son is the actual type of the variable son.

According to the above output display, we can see that the method call is determined according to the static type of the variable when overloading, then we can get the definition of static dispatch, "all depend on the static type to determine the dispatch action of the method execution version , Are called static dispatch" , method overloading in java is static dispatch, and static dispatch is done in the compiler. The runtime will not change, so static dispatch is also classified as an analytic category.

2.2 Dynamic dispatch

We already know that "the dispatch action that determines the method call based on the static type of the variable is called static dispatch", and the corresponding " dynamic dispatch of the dispatch action of the method based on the actual type ", the example of dynamic dispatch is our commonly used heavy Write, let's look at a rewritten example below.

public class TestDynamicDispath {
    
    
    static abstract class Father{
    
    
        public abstract void test();
    }
    static class Son extends Father{
    
    

        @Override
        public void test() {
    
    
            System.out.println("测试儿子");
        }
    }
    static class Daughter extends Father{
    
    

        @Override
        public void test() {
    
    
            System.out.println("测试女儿");
        }
    }

    public static void main(String[] args) {
    
    
        Father son = new Son();
        Father daughter = new Daughter();
        son.test();
        daughter.test();
        son = new Daughter();
        son.test();
    }
}

The output of the code above is as follows:

测试儿子
测试女儿
测试女儿

Process finished with exit code 0

As can be seen from the output above, which method is actually called depends entirely on the actual type of the variable.

2.3 Single dispatch, multiple dispatch

According to the "quantity" in the distribution, the distribution can be divided into single distribution and multiple distribution. So what is the quantity?
The receiver of the method and the parameters of the method are collectively referred to as the quantity. According to the quantity, the distribution can be divided into single distribution and multiple distribution. The selection of methods based on one quantity is called single distribution , and the selection of methods based on more than one quantity is called multiple distribution .

  1. Is static dispatch in java single dispatch or multiple dispatch?
    In overloading, calling the method, there are two factors that affect the method invocation, one is the method receiver (that is, the method caller), and the parameters passed in, the method caller is different or the method parameters are different will be called differently. Methods. So static dispatch in java belongs to static multiple dispatch.
  2. Is dynamic dispatch in java single dispatch or multiple dispatch?
    In rewriting, when calling a method, there is only one factor that affects the method invocation, that is, the receiver of the method, that is, the actual type of the method, so java's dynamic dispatch is a dynamic single dispatch.
    So java dispatch belongs to static multiple dispatch and dynamic single dispatch.

2. Full text summary

Static linking and dynamic linking describe the action of converting symbolic references into direct references. Virtual methods and non-virtual methods are based on when the method is converted into a direct reference to classify the method types, and the assignment describes the method selection process when the method is called. They are all connected, but they are all talking about different things. We can summarize them with this paragraph: Non-virtual methods will convert symbol references into direct references during the parsing stage of class loading. This process is called static linking. The virtual method converts symbolic references into direct references during the running phase of the program. This process is called dynamic linking. In the process of dynamic linking, the specific call of the method is the dispatching process. The dispatching is divided into static based on whether the static type is determined or the actual type is determined. Dispatching and dynamic dispatching respectively correspond to overloading and rewriting in java. From this we can also conclude that java is a static multi-dispatch and dynamic single-dispatch language (static dispatch actually occurs in the compilation stage, so there are also The static assignment is classified as analytic. If the static assignment is counted in the assignment, there will be some problems with the description in the second half of the sentence).

Guess you like

Origin blog.csdn.net/m0_46897923/article/details/114492088