invokeinterface的意义是什么? [英] What is the point of invokeinterface?

查看:360
本文介绍了invokeinterface的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这篇文章关于JVM如何调用方法的信息,我想我了解到了大部分.但是,我仍然无法理解invokeinterface的需求.

I'm reading this article about how JVM invokes methods, and I think I got most of it. However, I'm still having trouble understanding the need for invokeinterface.

按照我的理解,一个类基本上有一个方法的虚拟表,并且当使用invokevirtualinvokeinterface调用方法时,将查询该虚拟表.

The way I understand it, a class basically has a virtual table of methods and when calling a method with either invokevirtual or invokeinterface this virtual table is consulted.

那么,在接口上定义的方法和在基类上定义的方法之间有什么区别?为什么使用不同的字节码?

What is the difference, then, between a method that's defined on an interface and a method defined on a base class? Why the different bytecodes?

说明的说明看起来非常相似.

该文章似乎声称,每次调用方法时,接口的方法表都可以具有不同的偏移量".我不明白的是,为什么一个接口根本没有方法表,因为没有对象可以将接口作为其实际类型.

The article seems to claim that the method table of an interface can have "different offsets" every time a method is called. What I don't understand is why an interface would have a method table at all, since no object can have the interface as its actual type.

我想念什么?

推荐答案

每个Java类都与虚拟方法表相关联,该表包含指向类的每个方法的字节码的链接".该表继承自特定类的超类,并针对子类的新方法进行了扩展.例如,

Each Java class is associated with a virtual method table that contains "links" to the bytecode of each method of a class. That table is inherited from the superclass of a particular class and extended with regard to the new methods of a subclass. E.g.,

class BaseClass {
    public void method1() { }
    public void method2() { }
    public void method3() { }
}

class NextClass extends BaseClass {
    public void method2() { } // overridden from BaseClass
    public void method4() { }
}

表中的结果

BaseClass
1. BaseClass/method1()
2. BaseClass/method2()
3. BaseClass/method3()

NextClass
1. BaseClass/method1()
2. NextClass/method2()
3. BaseClass/method3()
4. NextClass/method4()

请注意,NextClass的虚拟方法表如何保留BaseClass表的条目顺序,并仅覆盖它覆盖的method2()的链接".

Note, how the virtual method table of NextClass retains the order of entries of the table of BaseClass and just overwrites the "link" of method2() which it overrides.

因此,通过记住BaseClass/method3()将始终是该方法将被调用的任何对象的虚拟方法表中的第三个条目,JVM的实现可以优化对invokevirtual的调用.

An implementation of the JVM can thus optimize a call to invokevirtual by remembering that BaseClass/method3() will always be the third entry in the virtual method table of any object this method will ever be invoked on.

对于invokeinterface,此优化是不可能的.例如,

With invokeinterface this optimization is not possible. E.g.,

interface MyInterface {
    void ifaceMethod();
}

class AnotherClass extends NextClass implements MyInterface {
    public void method4() { } // overridden from NextClass
    public void ifaceMethod() { }
}

class MyClass implements MyInterface {
    public void method5() { }
    public void ifaceMethod() { }
}

该类层次结构产生虚拟方法表

This class hierarchy results in the virtual method tables

AnotherClass
1. BaseClass/method1()
2. NextClass/method2()
3. BaseClass/method3()
4. AnotherClass/method4()
5. MyInterface/ifaceMethod()

MyClass
1. MyClass/method5()
2. MyInterface/ifaceMethod()

如您所见,AnotherClass在其第五个条目中包含接口的方法,而MyClass在第二个条目中包含该接口的方法.为了在虚拟方法表中实际找到正确的条目,使用invokeinterface调用方法将始终必须搜索整个表,而没有机会获得invokevirtual所做的优化风格.

As you can see, AnotherClass contains the interface's method in its fifth entry and MyClass contains it in its second entry. To actually find the correct entry in the virtual method table, a call to a method with invokeinterface will always have to search the complete table without a chance for the style of optimization that invokevirtual does.

还有其他区别,例如invokeinterface可以与实际上未实现接口的对象引用一起使用.因此,invokeinterface将必须在运行时检查表中是否存在方法,并可能引发异常.如果您想更深入地研究该主题,建议您使用 "Java接口的有效实现:Invokeinterface被认为是无害的" .

There are additional differences like the fact, that invokeinterface can be used together with object references that do not actually implement the interface. Therefore, invokeinterface will have to check at runtime whether a method exists in the table and potentially throw an exception. If you want to dive deeper into the topic, I suggest, e.g., "Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless".

这篇关于invokeinterface的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆