The bytecode comprises various instruction types, including data manipulation, control transfer, object creation and manipulation, and method invocation, all integral to Java's object-oriented programming model. The operand stack is used for passing operands to computations and for receiving the return value of a called method, while local variables serve the same purpose as
registers and are also used to pass method arguments. The maximum size of the operand stack and local variable array, computed by the compiler, is part of the attributes of each method. Each can be independently sized from 0 to 65535 values, where each value is 32 bits. and types, which are 64 bits, take up two consecutive local variables (which need not be 64-bit aligned in the local variables array) or one value in the operand stack (but are counted as two units in the depth of the stack).
Instruction set Each
bytecode is composed of one byte that represents the
opcode, along with zero or more bytes for operands. Of the 256 possible byte-long
opcodes, , 202 are in use (~79%), 51 are reserved for future use (~20%), and 3 instructions (~1%) are permanently reserved for JVM implementations to use. Two of these (impdep1 and impdep2) are to provide traps for implementation-specific software and hardware, respectively. The third is used for debuggers to implement breakpoints. Instructions fall into a number of broad groups: • Load and store (e.g. aload_0, istore) • Arithmetic and logic (e.g. ladd, fcmpl) • Type conversion (e.g. i2b, d2i) • Object creation and manipulation (new, putfield) • Operand stack management (e.g. swap, dup2) • Control transfer (e.g. ifeq, goto) • Method invocation and return (e.g. invokespecial, areturn) There are also a few instructions for a number of more specialized tasks such as exception throwing, synchronization, etc. Many instructions have
prefixes and/or suffixes referring to the types of operands they operate on. These are as follows: For example, iadd will add two integers, while dadd will add two doubles. The const, load, and store instructions may also take a suffix of the form _
n, where
n is a number from 0–3 for load and store. The maximum
n for const differs by type. The const instructions push a value of the specified type onto the stack. For example, iconst_5 will push an integer (32 bit value) with the value 5 onto the stack, while dconst_1 will push a double (64 bit floating point value) with the value 1 onto the stack. There is also an aconst_null, which pushes a reference. The
n for the load and store instructions specifies the index in the local variable array to load from or store to. The aload_0 instruction pushes the object in local variable 0 onto the stack (this is usually the
this object). istore_1 stores the integer on the top of the stack into local variable 1. For local variables beyond 3 the suffix is dropped and operands must be used. == Example ==