当前位置:网站首页>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 :
- invokestatic: Call static methods , The parsing phase determines the unique method version
- invokespecial: call <init> Method 、 Private and superclass methods , The parsing phase determines the unique method version
- invokevirtual: Call all virtual methods
- 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;边栏推荐
- Vulkan-官方示例解读-Shadows(光栅化)
- 【服务器数据恢复】同友存储raid5崩溃的数据恢复案例
- Methods to detect whether PHP websites have been broken
- 图像检测相关模型数据格式
- Matter protocol
- Sql优化
- Market prospect analysis and Research Report of Ethernet scanner in 2022
- Esp32 development -lvgl display picture
- Vulkan-官方示例解读-RayTracing
- Market prospect analysis and Research Report of modular lithium ion battery in 2022
猜你喜欢

This artifact is highly recommended. One line command will convert the web page to PDF!

Explain in detail the structure and working principle of the crystal oscillator
![[network] socket programming](/img/df/2afc300bfc2dd319247a4b75ef7e2c.png)
[network] socket programming
![[server data recovery] data recovery case of RAID5 crash of buddy storage](/img/c4/681c542310f05192bd14757965ef0d.png)
[server data recovery] data recovery case of RAID5 crash of buddy storage

Sslstrip Ultimate - location hijacking

Discussion on the development trend of remote power management unit (Intelligent PDU)

【CustomView】Glide+BitmapTransformation 图片上下边框波浪处理(WaveTransformation)

【服务器数据恢复】同友存储raid5崩溃的数据恢复案例

Embedded basic interface-i2s

MySQL stored procedure
随机推荐
Embedded basic interface -spi
Why does the hospital comprehensive security system synchronize the NTP clock of the network server?
关于重复发包的防护与绕过
Personalized use of QT log module
Code replicates CSRF attack and resolves it
Market prospect analysis and Research Report of engraving laser in 2022
【服务器数据恢复】同友存储raid5崩溃的数据恢复案例
How to invest in programming knowledge and reduce the impact of knowledge failure
Source insight 4.0 setting shortcut keys for comments and uncomments
7. list label
JVM(6):Slot变量槽、操作数栈、代码追踪、栈顶缓存技术
如何检查域名解析是否生效?
D structure as index of multidimensional array
检测php网站是否已经被攻破的方法
Red team shooting range with three-layer protection
Market prospect analysis and Research Report of hydrogen liquefier in 2022
[激光器原理与应用-2]:国内激光器重点品牌
Vulkan-官方示例解读-Shadows(光栅化)
app直播源码,平台登录页面实现和修改密码页面实现
JVM(2):内存结构、类的加载过程