From CHAPTER 3 of the JVM Spec
Instruction mnemonics shown above with trailing letters between angle brackets (for instance, iload_<n>) denote families of instructions (with members iload_0, iload_1, iload_2, and iload_3 in the case of iload_<n>). Such families of instructions are specializations of an additional generic instruction (iload) that takes one operand. For the specialized instructions, the operand is implicit and does not need to be stored or fetched. The semantics are otherwise the same (iload_0 means the same thing as iload with the operand 0). The letter between the angle brackets specifies the type of the implicit operand for that family of instructions: for <n>, a nonnegative integer; for <i>, an int
; for <l>, a long
; for <f>, a float
; and for <d>, a double
. Forms for type int
are used in many cases to perform operations on values of type byte
, char
, and short
(§3.11.1).
This notation for instruction families is used throughout The JavaTM Virtual Machine Specification.
byte
, short
, and char
types
(§3.11.1), or for values of the boolean
type; those operations are handled by
instructions operating on type int
. Integer and floating-point instructions also differ
in their behavior on overflow and divide-by-zero. The arithmetic instructions are as
follows:
The Java virtual machine does not indicate overflow during operations on integer data types. The only integer operations that can throw an exception are the integer divide instructions (idiv and ldiv) and the integer remainder instructions (irem and lrem), which throw an ArithmeticException
if the divisor is zero.
Java virtual machine operations on floating-point numbers behave as specified in IEEE 754. In particular, the Java virtual machine requires full support of IEEE 754 denormalized floating-point numbers and gradual underflow, which make it easier to prove desirable properties of particular numerical algorithms.
The Java virtual machine requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision. Inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, the one having a least significant bit of zero is chosen. This is the IEEE 754 standard's default rounding mode, known as round to nearest mode.
The Java virtual machine uses the IEEE 754 round towards zero mode when converting a floating-point value to an integer. This results in the number being truncated; any bits of the significand that represent the fractional part of the operand value are discarded. Round towards zero mode chooses as its result the type's value closest to, but no greater in magnitude than, the infinitely precise result.
The Java virtual machine's floating-point operators do not throw runtime exceptions (not to be confused with IEEE 754 floating-point exceptions). An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result.
Comparisons on values of type long
(lcmp) perform a signed comparison. Comparisons on values of floating-point types (dcmpg, dcmpl, fcmpg, fcmpl) are performed using IEEE 754 nonsignaling comparisons.
The Java virtual machine directly supports the following widening numeric conversions:
int
to long
, float
, or double
long
to float
or double
float
to double
int
value to a double
. Widening numeric conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from int
to long
and int
to double
do not lose any information at all; the numeric value is preserved exactly. Conversions widening from float
to double
that are FP-strict (§3.8.2) also preserve the numeric value exactly; however, such conversions that are not FP-strict may lose information about the overall magnitude of the converted value.
Conversion of an int
or a long
value to float
, or of a long
value to double
, may lose precision, that is, may lose some of the least significant bits of the value; the resulting floating-point value is a correctly rounded version of the integer value, using IEEE 754 round to nearest mode.
A widening numeric conversion of an int
to a long
simply sign-extends the two's-complement representation of the int
value to fill the wider format. A widening numeric conversion of a char
to an integral type zero-extends the representation of the char
value to fill the wider format.
Despite the fact that loss of precision may occur, widening numeric conversions never cause the Java virtual machine to throw a runtime exception (not to be confused with an IEEE 754 floating-point exception).
Note that widening numeric conversions do not exist from integral types byte
, char
, and short
to type int
. As noted in §3.11.1, values of type byte
, char
, and short
are internally widened to type int
, making these conversions implicit.
The Java virtual machine also directly supports the following narrowing numeric conversions:
int
to byte
, short
, or char
long
to int
float
to int
or long
double
to int
, long
, or float
A narrowing numeric conversion of an int
or long
to an integral type T simply discards all but the N lowest-order bits, where N is the number of bits used to represent type T. This may cause the resulting value not to have the same sign as the input value.
In a narrowing numeric conversion of a floating-point value to an integral type T, where T is either int
or long
, the floating-point value is converted as follows:
int
or long
0
.
long
and this integer value can be represented as a long
, then the result is the long
value V.
int
and this integer value can be represented as an int
, then the result is the int
value V.
int
or long
.
int
or long
.
double
to float
behaves in accordance with IEEE 754. The result is correctly rounded using IEEE 754 round to nearest mode. A value too small to be represented as a float
is converted to a positive or negative zero of type float
; a value too large to be represented as a float
is converted to a positive or negative infinity. A double
NaN is always converted to a float
NaN.Despite the fact that overflow, underflow, or loss of precision may occur, narrowing conversions among numeric types never cause the Java virtual machine to throw a runtime exception (not to be confused with an IEEE 754 floating-point exception).
static
fields, known as class variables) and fields of class instances (non-static
fields, known as instance variables): getfield, putfield, getstatic, putstatic.
int
and reference
types. It also has distinct conditional branch instructions that test for the null reference and thus is not required to specify a concrete value for null
(§3.4).
Conditional branches on comparisons between data of types boolean
, byte
, char
, and short
are performed using int
comparison instructions (§3.11.1). A conditional branch on a comparison between data of types long
, float
, or double
is initiated using an instruction that compares the data and produces an int
result of the comparison (§3.11.3). A subsequent int
comparison instruction tests this result and effects the conditional branch. Because of its emphasis on int
comparisons, the Java virtual machine provides a rich complement of conditional branch instructions for type int
.
All int
conditional control transfer instructions perform signed comparisons.
private
method, or a superclass method.
static
) method in a named class.
boolean
, byte
, char
, short
, or int
), lreturn, freturn
, dreturn, and areturn. In addition, the return instruction is used to return from
methods declared to be void
, instance initialization methods, and class or interface
initialization methods.
finally
finally
keyword uses the jsr, jsr_w, and ret instructions.
See Section 4.9.6, "Exceptions and finally," and Section 7.13, "Compiling
finally."
Method-level synchronization is handled as part of method invocation and return (see Section 3.11.8, "Method Invocation and Return Instructions").
Synchronization of sequences of instructions is typically used to encode the synchronized blocks of the Java programming language. The Java virtual machine supplies the monitorenter and monitorexit instructions to support such constructs.
Proper implementation of synchronized blocks requires cooperation from a compiler targeting the Java virtual machine. The compiler must ensure that at any method invocation completion a monitorexit instruction will have been executed for each monitorenter instruction executed since the method invocation. This must be the case whether the method invocation completes normally (§3.6.4) or abruptly (§3.6.5).
The compiler enforces proper pairing of monitorenter and monitorexit instructions on abrupt method invocation completion by generating exception handlers (§3.10) that will match any exception and whose associated code executes the necessary monitorexit instructions (§7.14).