74

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.

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 description of the instructions also looks very similar.

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.

What am I missing?

4

2 Answers 2

99

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.

4
  • 7
    "always have to search the complete table without a chance for the style of optimization that invokevirtual does" -- It should be noted that it doesn't always have to search the table, since it can do a different style of optimization. See the linked "considered harmless" paper for more details. Jan 3, 2013 at 8:50
  • So why need invokeinterface just use invokevirtual ?
    – shaoyihe
    Nov 3, 2018 at 8:10
  • 1
    @shaoyihe Based on janko's answer, I think it's for performance while invokeinterface cannot be optimised as invokevirtual. Reasons already in the answer (many interfaces can be implemented and the reference table is not fixed).
    – Hearen
    Mar 27, 2019 at 6:08
  • 1
    @Hearen the performance difference, if any, comes from the fact that the target type is an interface. This doesn’t explain why two different invocation instructions are needed. The JVM would notice whether the target type is an interface anyway. In fact, the specification requires the JVM to verify this beforehand. In real life, JVMs don’t work the way described in this answer (anymore). Invocations of interface methods are resolved optimistically the same way as non-interface methods. Only when the receiver type changes for an invocation instruction, a more expensive dispatch is needed.
    – Holger
    May 23, 2022 at 13:29
2

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.

3
  • Interesting, but could that be all there is to it?
    – itsadok
    Oct 1, 2009 at 17:16
  • There is more difference that this (as explained elsewhere, the method lookup process is different), but this difference comes from the fact that all methods in an interface (at least prior to Java 8) are public. Therefore a visibility check on access to methods via an interface is not needed, since they must be public which means all code can access them. Jul 28, 2016 at 22:09
  • I doubt it invokespecial will invoke the private then what the checking accessibility comes in?
    – Hearen
    Mar 27, 2019 at 6:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.