Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is all about performance. When by figuring out the compile-time type (aka: static type) the JVM can compute the index of the invoked method in the virtual function table of the runtime type (aka: dynamic type). Using this index step 3 simply becomes an access into an array which can be accomplished in constant time. No looping is needed.</p> <p>Example: </p> <pre><code>class A { void foo() { } void bar() { } } class B extends A { void foo() { } // Overrides A.foo() } </code></pre> <p>By default, <code>A</code> extends <code>Object</code> which defines these methods (final methods omitted as they are invoked via <code>invokespecial</code>):</p> <pre><code>class Object { public int hashCode() { ... } public boolean equals(Object o) { ... } public String toString() { ... } protected void finalize() { ... } protected Object clone() { ... } } </code></pre> <p>Now, consider this invocation:</p> <pre><code>A x = ...; x.foo(); </code></pre> <p>By figuring out that x's static type is <code>A</code> the JVM can also figure out the list of methods that are available at this call site: <code>hashCode</code>, <code>equals</code>, <code>toString</code>, <code>finalize</code>, <code>clone</code>, <code>foo</code>, <code>bar</code>. In this list, <code>foo</code> is the 6th entry (<code>hashCode</code> is 1st, <code>equals</code> is 2nd, etc.). This calculation of the index is performed once - when the JVM loads the classfile.</p> <p>After that, whenever the JVM processes <code>x.foo()</code> is just needs to access the 6th entry in the list of methods that x offers, equivalent to <code>x.getClass().getMethods[5]</code>, (which points at <code>A.foo()</code> if x's dynamic type is <code>A</code>) and invoke that method. No need to exhaustively search this array of methods.</p> <p>Note that the method's index, remains the same regardless of the dynamic type of x. That is: even if x points to an instance of B, the 6th methods is still <code>foo</code> (although this time it will point at <code>B.foo()</code>).</p> <p><strong>Update</strong></p> <p>[In light of your update]: You're right. In order to perform a virtual method dispatch all the JVM needs is the name+signature of the method (or the offset within the vtable). However, the JVM does not execute things blindly. It first checks that the cassfiles loaded into it are correct in a process called <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#88597" rel="nofollow noreferrer">verification</a> (see also <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#40222" rel="nofollow noreferrer">here</a>).</p> <p>Verification expresses one of the design principles of the JVM: <em>It does not rely on the compiler to produce correct code</em>. It checks the code itself before it allows it to be executed. In particular, the verifier checks that every invoked virtual method is actually defined by the static type of the receiver object. Obviously, the static type of the receiver is needed to perform such a check.</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