---
title: "[转载] java method invoke的指令简介"
author: "JohnnyMeng"
date: "2012-02-07T13:03:51Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java-method-invoke"
draft: false
lastmod: "2012-02-07T16:01:53Z"
id: 1396452
url: "https://johnnymeng.iteye.com/blog/1396452"
---

方法调用相关的jvm子令集主要有一下四种：

 

**invokestatic** -------------------------------&gt;调用类方法（静态绑定，速度快）

 

**invokevirtual** ------------------------------&gt;调用实例方法（动态绑定）

 

**invokespecial** -----------------------------&gt;调用实例方法（静态绑定，速度快）

 

**invokeinterface** ---------------------------&gt;调用引用类型为interface的实例方法（动态绑定）

 

**invokedynamic** ---------------------------&gt;JDK 7引入的，主要是为了支持动态语言的方法调用。可参考

      [New JDK 7 Feature: Support for Dynamically Typed Languages in the Java Virtual Machine](http://java.sun.com/developer/technicalArticles/DynTypeLang/)

 

操作码   操作数 说明

invokestatic indexbyte1,indexbyte2 把args从stack中弹出，调用constant pool索引指向的实例方法

 

invokevirtual indexbyte1,indexbyte2 把objectref和args从stack中弹出，调用constant pool索引指向的实例方法

 

invokespecial indexbyte1,indexbyte2 把objectref和args从stack中弹出，调用constant pool索引指向的实例方法

 

invokeinterface indexbyte1,indexbyte2 把objectref和args从stack中弹出，调用constant pool索引指向的实例方法

 

//-----------------------------------------------举例浅析区别----------------------------------------------------------------

invokevirtual和invokespecial的区别在于：invokespecial通常根据引用的类型选择方法，而不是对象的类来选择！即它使用静态绑定而不是动态绑定。

 

使用invokespecial指令分为下面三种情况：

1\. 实例初始化方法（&lt;init&gt;()）方法

2\. 私有方法

3\. 使用super关键字调用的方法

 

 

invokespecial and Private Methods

看下面一个例子：

 

Java代码      [![收藏代码](http://boy00fly.iteye.com/images/icon_star.png)]()

01. class Superclass {
02. private void interestingMethod() {
03. System.out.println("Superclass's interesting method.");
04. }
05. void exampleMethod() {
06. interestingMethod();
07. }
08. }
09. class Subclass extends Superclass {
10. void interestingMethod() {
11. System.out.println("Subclass's interesting method.");
12. }
13. public static void main(String args\[]) {
14. Subclass me = new Subclass();
15. me.exampleMethod();
16. }
17. }

 

 Here are the bytecodes for the main() method of class Subclass：

Bytecode代码      [![收藏代码](http://boy00fly.iteye.com/images/icon_star.png)]()

1. 0:   new     #5; //class Subclass
2. 3:   dup
3. 4:   invokespecial   #6; //Method "&lt;init&gt;":()V
4. 7:   astore\_1
5. 8:   aload\_1
6. 9:   invokevirtual   #7; //Method exampleMethod:()V
7. 12:  return

 Subclass从Superclass处继承了exampleMethod()方法。当Subclass的me对象调用exampleMethod()时使用的是invokevirtual。JVM会创建一个新的栈帧并将其压入栈，然后执行exampleMethod()的bytecode。下面是exampleMethod()的bytecode：

Bytecode代码      [![收藏代码](http://boy00fly.iteye.com/images/icon_star.png)]()

1. 0:   aload\_0
2. 1:   invokespecial   #5; //Method interestingMethod:()V
3. 4:   return

exampleMethod()首先将赋给局部变量0的reference压入stack（隐含参数this被传入给所有的实例方法），然后使用invokespecial指令通过这个引用调用interestingMethod()。

尽管这里的对象时Subclass的实例，而且Subclass类中的interestingMethod()方法也是能够访问的，但是JVM最终还是调用了Superclass类中的interestingMethod()方法。

程序的正确输出为："Superclass's interesting method"，因为这里生成的bytecode中调用的interestingMethod()使用的是invokespecial指令，而调用invokespecial指令时，JVM会按照你reference type来choice调用的method。

 

//-----------------------------------------------------------------指令调用和速度PK-------------------------------------------------------

可想而知，调用接口引用方法可能要比调用类引用方法慢。因为，当JVM遇到invokevirtual指令时，它把实例方法的符号引用解析为直接引用，所以生成的直接引用很可能是方法表中的一个偏移量，而且从此往后都可以使用同样的偏移量。但对于invokeinterface指令来说，虚拟机每一次遇到invokeinterface指令，都不能不重新搜寻一遍方法表，因为虚拟机不能够假设这一次的偏移量与上一次的偏移量相同。

最快的指令莫过于invokespecial和invokestatic，因为这些指令调用的都是静态绑定的，即在编译器确定了！所以当JVM为这些指令解析符号引用时，将符号引用转换成为直接饮用，所生成的直接引用将包含一个指向实际操作码的指针。

 

以上内容参考《深入Java虚拟机》

 

文章来源：[http://boy00fly.iteye.com/blog/1158854](http://boy00fly.iteye.com/blog/1158854)