Java Reference
In-Depth Information
invokevirtual
The PrintStream.print(boolean) method is called
using the Jasmin notation
0
invokevirtual java/io/PrintStream/print(Z)V
which specifies the full path to the method and in-
dicates that the method takes one boolean parameter
(indicated by Z) and returns no values (indicated by V
after the parentheses).
·
In the example, the
is an instance of a PrintStream
class (e.g., System.out). The 0 on TOS is Java's integer
encoding of false.
Before
After
Although the method declares that it takes only one parameter, it is instance-
specific and will consume two values from the stack. An instance of a
PrintStream class must be pushed first (shown as
·
), followed by the pa-
rameters declared by the called method. Within the PrintStream.print()
method, the
·
becomes this . The method completes but its return type is void
so it returns no result. The method has the side e
ff
ect of printing false to its
PrintStream ( this , inside the method).
Other Method Calls
Almost all methods in Java are called by invokevirtual or invokestatic.
However, important exceptions include constructor calls and calls based on
super, which are handled by the invokespecial instruction.
A constructor call is special in the following sense. An uninitialized refer-
ence to an object instance is pushed on TOS (usually by a new instruction).
The method name actually involved is
within the type of the ob-
ject pushed on TOS. The constructor consumes the reference from TOS
as its input parameter ( this ) along with any declared parameters. All
constructors are void so nothing is returned from the constructor call. A
code generator must be aware of this behavior and issue the appropriate
instructions to be able to access the instantiated object (see Exercise 10).
<
init
>
Methods called by invokevirtual are dispatched based on the actual
(runtime) type of the instance on which the method is invoked. If the
actual instance type is t , then the invoked method will be sought first in
class t and then in t 's parent in the object hierarchy, all the way up to
Object (the superest of all superclasses).
Amethod call based onsuperbegins its search for an appropriatemethod
at the current class's parent in the object hierarchy.
In other words,
 
Search WWH ::




Custom Search