Java virtual machine knowledge - Method Invocation

Parse call

  Method calls the target method is a constant pool of symbolic references in the resolution phase of class loading, will be part of symbolic references into direct reference in the Class file, this resolution provided that: there is method before the program actually run a call may be determined version, and the version of this method is invoked at run immutable (compile understood, runner immutable). Call these methods is called the resolution.

  • invokestatic: call the static method.
  • invokespecial: call an instance initialization methods, private methods and the parent class method.
  • invokevirtual: instance method invocation object.
  • invokeinterface: call interface methods.
  • invokedynamic: call the method to call the object is bound invokedynamic point instruction as a target.

  The first four instructions, dispatch logic cured inside the Java virtual machine, and the guidance method of invokedynamic dispatch logic set by the user is determined.

  Methods invokestatic and invokespecial called non-virtual method call, other (except the final method) for the virtual method. Although invokevirtual because the final call, but it can not be modified to cover, so it is also a non-virtual methods.

Dispatch calls

  Resolution call must be a static process, it is completely determined at compile time, not to run again delayed the completion. The dispatch call may be static or dynamic may be divided into single and multiple dispatch dispatch pairwise combinations constitute static single assignment, multiple dispatch static, dynamic single assignment, dynamic multiple dispatch.

1, static assignment (multiple dispatch)

  Rely on static type positioning method dispatch perform a version called static assignment. A typical application is overloaded . Static assignment occurs at compile time, not a virtual machine assigned action performed. Compile able to determine 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);
    }
}

Export

hello,guy!
hello,guy!

2, the dynamic assignment (single dispatch)

  Run-time type called dynamic dispatch method of determining the actual execution version of the assignment. A typical application is the rewrite . The first step is to determine the actual invokevirtual instruction recipient type at runtime, the class method invokevirtual symbol the constant pool directly referenced to resolve 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 dispatch dispatch

  Receiver parameters and methods methods methods were collectively referred to as the amount. Single-dispatch is a selection of variables in a target method, dispatch is selected plurality of target variables in more than one method.

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());
    }
}

Export

father choose _360
son choose QQ

4, dynamic assignment of virtual machines to achieve

  The most common "stable optimization" is to establish a means for performance reasons a class vtable method area (Vritual Method Table, vtable, corresponding thereto, the interface -Interface Method Table, itable), using the virtual method table index instead of metadata lookup to improve performance.

  Vtable entry address of each store with the actual process. If a method is not overridden in a subclass, the subclass of that virtual method table entry address of the entry address and the same method of the parent class is the same, to achieve the inlet point of the parent class. If the child class overrides the method of this subclass method will replace the address in the table entry address to point to the implementation version subclass.

  For convenience of program implemented with the method the same signature, the parent class vtable subclass should have the same index number, so that when the type of conversion is only necessary to change the method of table lookup, will be different the virtual method table by an index entry address conversion required.

  After general method table in the connection initialization phase of class loading, prepared the initial value of the variable type, the virtual method table of the class is also initialized.

  In addition to the virtual machine to use the table, it will also use inline caching and inner guard type inheritance relationship analysis technique with two non-stable radical optimization methods based on the back of higher performance, refer to the late (run-time) optimization.

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159648.htm