"Java Virtual Machine Learning" How does the JVM virtual machine execute the method

1. Overloading and overriding methods

overload

Overloading: In the same class, the method name is the same, the formal parameter list is not used, and it has nothing to do with the return value

For the execution of overloaded methods, the JVM follows the following three rules:

  1. Select overloaded methods without considering auto-boxing, auto-unboxing of basic types, and variable-length parameters;

  2. If no suitable method is found in the first phase, then select an overloaded method that allows automatic boxing and unboxing but does not allow variable-length parameters;

  3. If no suitable method is found in phase 2, an overloaded method is chosen that allows autoboxing and variadic arguments.

package jike.test;

public class JVMInvokeMethodDemo {


    class Test{

        //方法1 基本类型自动装拆箱  以及 可变参
        public void method(Integer arg1,String... arg2){

            System.out.println("方法一执行");

        }

        //方法2 基本类型自动装拆箱
        public void method(Integer arg1,String arg2){

            System.out.println("方法二执行");

        }

        //方法3 基本类型自动装拆箱
        public void method(int arg1,String arg2){

            System.out.println("方法三执行");

        }

    }

    public static void main(String[] args) {

        JVMInvokeMethodDemo jvmInvokeMethodDemo = new JVMInvokeMethodDemo();

        Test test = jvmInvokeMethodDemo.new Test();

        test.method(1,"执行");

    }


}

idea64_Hr1V2luUeD

As shown in the above code, method three will be executed first; when method three is blocked, method two for automatic boxing and unboxing will be found according to rule two; only when method two is also blocked, JVM will perform automatic boxing and unboxing And method one of variable parameters.

rewrite

Rewriting: The subclass overrides the method of the parent class, following the same principle of two big ones and one small two. That is, the return value type and permission modifier are greater than or equal to the parent class, the exception thrown is less than or equal to the parent class, and the formal parameter list and method name are the same as the parent class

For rewriting methods, the JVM will choose the actual target method according to the dynamic type of the caller

2. How the JVM identifies methods

JVM distinguishes methods mainly through class name, method name, and method descriptor (composed of formal parameter list + return value)

But for JAVA, the return value cannot be used to distinguish the method, as shown below, IDEA will give an error message, but for the JVM,

There is no problem. Because for the JVM, the bytecode of the calling method contains the return value information, and the JVM can accurately locate the target method;

image-20230507173344885

Why can't JAVA distinguish based on the return value? Because the return value of a method can choose whether to receive it. As shown below, the JVM cannot locate the specific method without clearly indicating the type of the return value.

image-20230507173835727

3. Static binding and dynamic binding

Overloading is compile-time polymorphism based on JAVA polymorphism, while rewriting is run-time polymorphism. therefore:

Overloading is regarded as static binding, and the target method has been determined at compile time; while rewriting is dynamic binding. But there is a special situation: the subclass overrides the overloaded method of the parent class, then the JVM cannot be statically bound, and still needs to identify the target method at runtime according to the dynamic type of the caller.

4. Bytecode commands for calling methods

  1. invokestatic: Used to call static methods.
  2. invokespecial: Used to call private instance methods, constructors, and use the super keyword to call instance methods or constructors of parent classes, and default methods of implemented interfaces.
  3. invokevirtual: Used to call non-private instance methods.
  4. invokeinterface: used to call the interface method.
  5. invokedynamic: Used to invoke dynamic methods.

5. How to find specific references for symbolic references

For non-interface symbolic references, assuming that the class pointed to by the symbolic reference is C, the Java virtual machine searches for it according to the following steps.

  1. Find a method in C that matches its name and descriptor.
  2. If not found, continue searching in the parent class of C until the Object class.
  3. If not found, search in the interfaces directly or indirectly implemented by C, and the target method obtained in this step must be non-private and non-static. And, if the target method is in an indirectly implemented interface, it must be satisfied that there is no other eligible target method between C and the interface. If there are multiple target methods that meet the criteria, one of them is returned arbitrarily.

As for the interface symbol reference, assuming that the interface pointed to by the symbol reference is I, the Java virtual machine searches according to the following steps.

  1. Find a method in I that matches the name and descriptor. If not found, the public instance methods in the Object class are searched.
  2. If not found, the I superinterface is searched. The requirements of the search results in this step are consistent with the requirements of step 3 for non-interface symbol references.

おすすめ

転載: blog.csdn.net/JAVAlife2021/article/details/130547314