JVM bytecode
JVM bytecode is the instruction set of the Java virtual machine, the language to which Java and other JVM-compatible source code is compiled. Each instruction is represented by a single byte, hence the name bytecode, making it a compact form of data.
Due to the nature of bytecode, a JVM bytecode program is runnable on any machine with a compatible JVM, without the lengthy process of compiling from source code.
JVM bytecode is used at runtime either interpreted by a JVM or compiled to machine code via just-in-time compilation and run as a native application.
As JVM bytecode is designed for a cross-platform compatibility and security, a JVM bytecode application tends to run consistently across various hardware and software configurations.
Relation to Java
In general, a Java programmer does not need to understand JVM bytecode or even be aware of it. However, as suggested in the IBM developerWorks journal, "Understanding bytecode and what bytecode is likely to be generated by a Java compiler helps the Java programmer in the same way that knowledge of assembly helps the C or C++ programmer."Instruction set architecture
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 JVM is both a stack machine and a register machine. Each frame for a method call has an "operand stack" and an array of "local variables". 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 or one value in the operand 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, as of 2015, 202 are in use, 51 are reserved for future use, and 3 instructions are permanently reserved for JVM implementations to use. Two of these 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
- Arithmetic and logic
- Type conversion
- Object creation and manipulation
- Operand stack management
- Control transfer
- Method invocation and return
Many instructions have prefixes and/or suffixes referring to the types of operands they operate on. These are as follows:
| Prefix/suffix | Operand type |
i | integer |
l | long |
s | short |
b | byte |
c | character |
f | float |
d | double |
a | reference |
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 with the value 5 onto the stack, while dconst_1 will push a double 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. 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
Consider the following Java code:outer:
for
A Java compiler might translate the Java code above into bytecode as follows, assuming the above was put in a method:
0: iconst_2
1: istore_1
2: iload_1
3: sipush 1000
6: if_icmpge 44
9: iconst_2
10: istore_2
11: iload_2
12: iload_1
13: if_icmpge 31
16: iload_1
17: iload_2
18: irem
19: ifne 25
22: goto 38
25: iinc 2, 1
28: goto 11
31: getstatic #84; // Field java/lang/System.out:Ljava/io/PrintStream;
34: iload_1
35: invokevirtual #85; // Method java/io/PrintStream.println:V
38: iinc 1, 1
41: goto 2
44: return
Generation
The most common language targeting Java virtual machine by producing JVM bytecode is Java. Originally only one compiler existed, the javac compiler from Sun Microsystems, which compiles Java source code to JVM bytecode; but because all the specifications for JVM bytecode are now available, other parties have supplied compilers that produce JVM bytecode. Examples of other compilers include:- Eclipse compiler for Java
- Jikes, compiles from Java to JVM bytecode
- Espresso, compiles from Java to JVM bytecode
- GNU Compiler for Java, compiles from Java to JVM bytecode; it can also compile to native machine code and was part of the GNU Compiler Collection up until version 6.
- Jasmin, takes text descriptions for Java classes, written in a simple assembly-like syntax using Java virtual machine instruction set and generates a Java class file
- Jamaica, a macro assembly language for the Java virtual machine. Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions.
- Krakatau Bytecode Tools, currently contains three tools: a decompiler and disassembler for Java classfiles and an assembler to create classfiles.
- Lilac, an assembler and disassembler for the Java virtual machine.
- ColdFusion
- JRuby and Jython, two scripting languages based on Ruby and Python
- Apache Groovy, optionally typed and dynamic general-purpose language, with static-typing and static compilation capabilities
- Scala, a type-safe general-purpose programming language supporting object-oriented and functional programming
- JGNAT and AppletMagic, compile from the language Ada to JVM bytecode
- C to Java byte-code compilers
- Clojure, a functional, immutable, general-purpose programming language in the Lisp family with a strong emphasis on concurrency
- Kawa, an implementation of the Scheme programming language, also a dialect of Lisp.
- MIDletPascal
- JavaFX Script code is compiled to JVM bytecode
- Kotlin, a statically-typed general-purpose programming language with type inference
- Object Pascal source code is compiled to JVM bytecode using the Free Pascal 3.0+ compiler.
Execution
Support for dynamic languages
The Java virtual machine provides some support for dynamically typed languages. Most of the extant JVM instruction set is statically typed - in the sense that method calls have their signatures type-checked at compile time, without a mechanism to defer this decision to run time, or to choose the method dispatch by an alternative approach.JSR 292 added a new
invokedynamic instruction at the JVM level, to allow method invocation relying on dynamic type checking. The Da Vinci Machine is a prototype virtual machine implementation that hosts JVM extensions aimed at supporting dynamic languages. All JVMs supporting JSE 7 also include the invokedynamic opcode.