Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each Java class is associated with a <em>virtual method table</em> 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.,</p> <pre class="lang-java prettyprint-override"><code>class BaseClass { public void method1() { } public void method2() { } public void method3() { } } class NextClass extends BaseClass { public void method2() { } // overridden from BaseClass public void method4() { } } </code></pre> <p>results in the tables</p> <pre>BaseClass 1. BaseClass/method1() 2. BaseClass/method2() 3. BaseClass/method3() NextClass 1. BaseClass/method1() 2. NextClass/method2() 3. BaseClass/method3() 4. NextClass/method4()</pre> <p>Note, how the virtual method table of <code>NextClass</code> retains the order of entries of the table of <code>BaseClass</code> and just overwrites the "link" of <code>method2()</code> which it overrides.</p> <p>An implementation of the JVM can thus optimize a call to <code>invokevirtual</code> by remembering that <code>BaseClass/method3()</code> will always be the third entry in the virtual method table of any object this method will ever be invoked on.</p> <p>With <code>invokeinterface</code> this optimization is not possible. E.g.,</p> <pre class="lang-java prettyprint-override"><code>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() { } } </code></pre> <p>This class hierarchy results in the virtual method tables</p> <pre>AnotherClass 1. BaseClass/method1() 2. NextClass/method2() 3. BaseClass/method3() 4. AnotherClass/method4() 5. MyInterface/ifaceMethod() MyClass 1. MyClass/method5() 2. MyInterface/ifaceMethod()</pre> <p>As you can see, <code>AnotherClass</code> contains the interface's method in its fifth entry and <code>MyClass</code> contains it in its second entry. To actually find the correct entry in the virtual method table, a call to a method with <code>invokeinterface</code> will always have to search the complete table without a chance for the style of optimization that <code>invokevirtual</code> does.</p> <p>There are additional differences like the fact, that <code>invokeinterface</code> can be used together with object references that do not actually implement the interface. Therefore, <code>invokeinterface</code> 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., <a href="http://www.research.ibm.com/people/d/dgrove/papers/oopsla01.html" rel="noreferrer">"Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless"</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload