Help us understand the problem. What is going on with this article?

JVMのinvoke系命令の違い

関連リソース

共通していること

オペランドのオフセットでconstant poolからmethod infoを引いてきてその情報を元にディスパッチしている。

invokevirtual

インスタンスメソッドの呼び出し。

使用例

class Main {
    public static void main(String[] args) {
        System.out.println("hello");
    }
}
  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String hello
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return

invokestatic

スタティックメソッドの呼び出し。

使用例

class Main {
    public static void main(String[] args) {
        System.currentTimeMillis();
    }
}
  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
       3: pop2
       4: return

invokeinterface

インターフェースを経由した呼び出しに使われる。

使用例

import java.util.*;

class Main {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.get("hello");
    }
}
  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/util/HashMap
       3: dup
       4: invokespecial #3                  // Method java/util/HashMap."<init>":()V
       7: astore_1
       8: aload_1
       9: ldc           #4                  // String hello
      11: invokeinterface #5,  2            // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      16: pop
      17: return

invokespecial

その他のメソッド (コンストラクタ、superclass、privateメソッド) の呼び出しに使われる。

使用例

class Main {
    public static void main(String[] args) {
        new Main();
    }
}
  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class Main
       3: dup
       4: invokespecial #3                  // Method "<init>":()V
       7: pop
       8: return

invokedynamic

呼び出すメソッドを動的に検索して呼び出す。JVM上で実装された動的言語や、Lambdaなどで使われる。

使用例

class Main {
    public static void main(String[] args) {
        Runnable runner = () -> { System.out.println("hello"); };
    }
}
  public static void main(java.lang.String[]);
    Code:
       0: invokedynamic #2,  0              // InvokeDynamic #0:run:()Ljava/lang/Runnable;
       5: astore_1
       6: return
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away