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
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() { }
}
results in the tables
BaseClass 1. BaseClass/method1() 2. BaseClass/method2() 3. BaseClass/method3() NextClass 1. BaseClass/method1() 2. NextClass/method2() 3. BaseClass/method3() 4. NextClass/method4()
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.
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.
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()
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.
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.
Comparing both instructions in the JVM Spec, the very first difference is that invokevirtual
checks the accessibility of the method during the lookup, while invokeinterface
doesn't.