Java Virtual Machine Knowledge Point [Method Call]

Resolve Call

_The target method of a method call is a symbol reference in a constant pool in a Class file. During the parsing phase of class loading, part of the symbol reference is converted to a direct reference on the premise that the method has a deterministic calling version before the program actually runs and that the method is calledVersions are not mutable at runtime (runtime is known to be mutable at compile time).Calls to such methods are called parsing.

The Java virtual machine has five byte code instructions for method calls:

  • invokestatic: Calls a static method.
  • invokespecial: Invokes instance initialization, private, and parent methods.
  • invokevirtual: Calls the instance method of the object.
  • invokeinterface: Calls the interface method.
  • Invokedynamic: Calls a method that targets the invoke point object bound to the invokedynamic directive.

The first four instructions solidify the dispatch logic inside the Java virtual machine, while invokedynamic's dispatch logic is determined by the user-set boot method.

_invokestatic and invokespecial invoked methods are called non-virtual methods, other than the final method.Because final is invokevirtual, but it cannot override modifications, it is also a non-virtual method.

Assign Calls

_Resolve calls must be static procedures that are fully determined during compilation and will not be delayed until the runtime.Assignment calls can be static or dynamic, which can be divided into single and multiple assignments. The two groups together form static single assignment, static multiple assignment, dynamic single assignment and dynamic multiple assignment.

1. Static Assignment (Multiple Assignments)

The assignment that relies on static types to locate the execution version of a method is called static assignment.Typical applications are overloads.Static dispatch occurs during the compilation phase and dispatch actions are not performed by virtual machines.Compilation time determines a more appropriate version.

public class StaticDispatch {
    static abstract class Human{
    }
    static class Man extends Human{
    }
    static class Woman extends Human{
    }
    public static void sayHello(Human guy){
        System.out.println("hello,guy!");
    }
    public static void sayHello(Man guy){
        System.out.println("hello,gentlemen!");
    }
    public static void sayHello(Woman guy){
        System.out.println("hello,lady!");
    }
    public static void main(String[] args) {
        Human man=new Man();
        Human woman=new Woman();
        sayHello(man);
        sayHello(woman);
    }
}

output

hello,guy!
hello,guy!

2. Dynamic assignment (single assignment)

_The assignment of method execution versions determined by runtime based on actual type is called dynamic assignment.A typical application is rewriting.The first step in the invokevirtual directive is to determine the actual type of recipient at run time, and invokevirtual resolves class method symbol references in the constant pool to direct references.

public class DynamicDispatch {
    static abstract class Human{
        protected abstract void sayHello();
    }
    static class Man extends Human{ 
        @Override
        protected void sayHello() { 
            System.out.println("man say hello!");
        }
    }
    static class Woman extends Human{ 
        @Override
        protected void sayHello() { 
            System.out.println("woman say hello!");
        }
    } 
    public static void main(String[] args) {
        
        Human man=new Man();
        Human woman=new Woman();
        man.sayHello();
        woman.sayHello();
        man=new Woman();
        man.sayHello(); 
    }
}

Output:

man say hello!
woman say hello!
woman say hello!

3. Single and Multiple Assignments

_The receiver of the method and the parameters of the method are collectively referred to as the method's parcels.Single assignment selects the target method based on one parcel, while multiple assignments select the target method based on more than one parcel.

public class Dispatcher {
    static class QQ {}
    static class _360 {}

    public static class Father {
        public void hardChoice(QQ arg) {
            System.out.println("father choose QQ");
        }

        public void hardChoice(_360 arg) {
            System.out.println("father choose _360");
        }
    }

    public static class Son extends Father {
        @Override
        public void hardChoice(QQ arg) {
            System.out.println("son choose QQ");
        }

        @Override
        public void hardChoice(_360 arg) {
            System.out.println("son choose 360");
        }
    }

    public static void main(String[] args) {
        Father father = new Father();
        Father son = new Son();
        father.hardChoice(new _360());
        son.hardChoice(new QQ());
    }
}

output

father choose _360
son choose QQ

4. Realization of Dynamic Assignment of Virtual Machines

_For performance reasons, the most commonly used method for stability optimization is to create a virtual method table (Vritual Method Table, vtable) in the method area for the class, corresponding to -Interface Method Table, itable in the interface, and use the virtual method table index instead of metadata lookup to improve performance.

The virtual method table stores the actual entry addresses for each method.If a method is not overridden in a subclass, the address entries in the virtual method table of the subclass are identical to those of the same method of the parent class, all pointing to the implementation entries of the parent class.If this method is overridden in a subclass, the address in the subclass method table will be replaced with the entry address that points to the subclass implementation version.

_For the convenience of program implementation, methods with the same signature should all have the same index ordinal number in the virtual method tables of the parent and child classes, so that when the type is converted, the desired entry address can be converted from the different virtual method tables by index only by changing the method table of the lookup.

_Method tables are usually initialized during the join phase of class loading. After the initial value of a class's variable is prepared, the virtual opportunity initializes the class's method table as well.

_In addition to using method tables, virtual machines use two unstable, aggressive optimization methods, inline caching and daemon inline based on type inheritance relationship analysis, to get back to higher performance, referring to late (runtime) optimization.

Reference: Deep Understanding of Java Virtual Machines (2nd Edition), Java Virtual Machine Specification (Java SE 8 th Edition)

Keywords: PHP Java

Added by jobe1 on Wed, 24 Jul 2019 21:53:43 +0300