当前位置:网站首页>JVM (7): dynamic link, method call, four method call instructions, distinguishing between non virtual methods and virtual methods, and the use of invokedynamic instructions

JVM (7): dynamic link, method call, four method call instructions, distinguishing between non virtual methods and virtual methods, and the use of invokedynamic instructions

2022-06-11 04:17:00 Prince you

One 、 Dynamic links

         Each stack frame contains a reference to the method of the stack frame in the runtime constant pool . The purpose of including this reference is to support the dynamic linking of the code of the current method ( Dynamic Linking). such as : invokedynamic Instructions

         stay Java When the source file is compiled into a bytecode file , All variable and method references are referred to as symbols ( symbolic Reference) Save in class In the constant pool of files . such as : Describes when a method calls another method , It is represented by a symbolic reference to a method in the constant pool , The purpose of dynamic linking is to convert these symbolic references into direct references of calling methods .

Why do you need a constant pool ?

         The function of constant pool , Just to provide some symbols and constants , It is easy to identify the instruction . It can make the bytecode file call time smaller .

Two 、 Method call

         stay JVM in , Converting a symbolic reference to a direct reference to a calling method is related to the binding mechanism of the method .

Static links :

         When a bytecode file is loaded into JVM Internal time , If the target method to be called is known at compile time , And the operation period remains unchanged . In this case, the process of converting the symbolic reference of the calling method to a direct reference is called static linking .

Dynamic links :

         If the called method cannot be determined at compile time , in other words , Only symbolic references of calling methods can be converted to direct references during program runtime , Because of the dynamic nature of this reference conversion process , So it's called dynamic linking .

         The binding mechanism of the corresponding method is : Early binding (Early Binding) And late binding ( Late Binding). Binding is a field 、 Methods or classes are replaced by direct references in symbolic references , It only happened once .

Early binding :

         If the target method is called in the early compilation period , And the operation period remains unchanged , You can bind this method to the type it belongs to , thus , Because it is clear which target method is called , Therefore, you can use static links to convert symbol references to direct references .

Late binding :

         If the called method cannot be determined at compile time , You can only bind related methods according to the actual type during the program run time , This binding is also called late binding .

         With the emergence of high-level language , Be similar to Java The same object-oriented programming languages are growing , Although there are some differences in the syntax styles of these programming languages , But they always have one thing in common with each other , That is, they all support encapsulation 、 Object oriented features such as inheritance and polymorphism , Since this kind of programming language is polymorphic , So naturally, there are two binding methods, early binding and late binding .

        Java In fact, any ordinary method in this paper has the characteristics of virtual function , They are equivalent to c++ Virtual functions in languages (C++ You need to use keywords in virtual To explicitly define ). If in Java When you don't want a method to have the characteristics of a virtual function in a program , You can use the keyword final To mark this method .

3、 ... and 、 The four method call instructions distinguish between non virtual methods and virtual methods

Non virtual method :

  • If the method determines the specific calling version at compile time , This version is immutable at run time . Such a method is called non virtual method .
  • Static methods 、 Private method 、final Method 、 Instance builder 、 Parent methods are non virtual methods .
  • Other methods are called virtual methods .

         The following method call instructions are provided in the virtual machine :

Normal call instructions :

  1. invokestatic: Call static methods , The parsing phase determines the unique method version
  2. invokespecial: call <init> Method 、 Private and superclass methods , The parsing phase determines the unique method version
  3. invokevirtual: Call all virtual methods
  4. invokeinterface: Call interface method

Call instructions dynamically :

      5. invokedynamic: Dynamically resolve the method to be called , And then execute

         The first four instructions are fixed in the virtual machine , Method cannot be called and executed by human beings , and invokedynamic The instruction supports the user to determine the method version . among invokestatic Instructions and invokespecial A method called by an instruction is called a non virtual method , The rest (final Except decorated ) It's called virtual method .

Four 、invokedynamic Use of instructions

         JVM Bytecode instruction set has been relatively stable , Until Java7 It's just added a new one invokedynamic Instructions , This is a Java In order to achieve 『 Dynamic type language 』 An improvement made with support .

         But in Java7 Direct generation is not provided in invokedynamic Method of instruction , Need help ASM This underlying bytecode tool to generate invokedynamic Instructions . until Java8 Of Lambda Occurrence of expression ,invokedynamic Generation of instructions , stay Java There is a direct way to generate .

        Java7 The essence of the dynamic language type support added in is to Java Modification of virtual machine specification , Not right. Java Modification of language rules , This one is relatively complicated , Added method call in virtual machine , The most direct beneficiary is running in Java Compiler for dynamic language of platform .

         Dynamically typed languages and statically typed languages

         The difference between a dynamically typed language and a statically typed language is whether the type is checked at compile time or at run time , Satisfying the former is a statically typed language , Instead, it's a dynamically typed language .

         To put it bluntly, it is , Static type language is to judge the type information of variable itself : Dynamic type language is to judge the type information of variable value , Variables have no type information , Only variable values have type information , This is an important feature of dynamic language .

Java: String info = "atguigu" ; llinfo = atguigu;
JS: var name = "shkstart" ; var name = 10;
Python: info = 130.5;
原网站

版权声明
本文为[Prince you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110405099346.html