Java Reference
In-Depth Information
There are several kinds of instructions that accommodate conditional
branches. Each such instruction contains an opcode that specifies the con-
ditional test and a branch to be taken if the test is true.
ifgt
The ifgt instruction and its cognates expect that the
TOS is a signed int value, The branch is taken if the
condition (in this case, greater-than-zero) is satisfied.
In the example, 131 is popped and compared against
0. Because 131
131
0, the branch will be taken. Had the
comparison failed, control would pass to the instruc-
tion following the ifgt instruction.
>
Before
After
The JVM contains 6 such instructions, one for every possible comparison of
an int value against 0: ifeq (
=
), ifne (
), iflt (
<
), ifle (
≤
), ifgt (
>
), and
ifge (
). A separate opcode is provisioned for each instruction.
Some programs call for comparisons of non-zero values. While the in-
structions described above are su
≥
cient for such programs (see Exercise 9), a
shorter sequence of instructions can be generatedusing the following relatively
more complex instructions.
if_icmpgt
131
431
Consider a source program that at some point has
a
=
131 and must next evaluate whether
a
>
b
.Afteraniload instruction is issued for
a
and
b
,
in that order, the stack appears as shown on the left.
431 and
b
=
In the example, the if_icmpgt instruction pops the top
two elements and performs the comparison 431
>
131.
Because this test succeeds, the branch is taken.
Before
After
There are 6 instructions in this family: if_icmpeq (
a
=
b
), if_icmpne (
a
b
),
if_icmplt (
a
<
b
), if_icmple (
a
≤
b
), if_icmpgt (
a
>
b
), and if_icmpge (
a
≥
b
).
Static Method Calls
There are several forms of method-calling instructions in the JVM. In object-
oriented languages, a method could be common to all instances of some type
t
or instance specific. In the former case, Java designates such methods as
static
.
A static method of type
t
, like a static field, is referenced by its type
t
and
does not require an instance of
t
to be called. An example of a static method
is Math.pow(double a,double b), which returns
a
b
. Such methods are called
using the invokestatic instruction.