Java bytecode is the instruction set of the Java virtual machine. Each bytecode
is composed by one, or in some cases two, bytes that represent the
instruction, along with zero or more bytes for passing parameters.
Of the 255 possible byte-long opcodes, 198 are currently in use, 54 are
reserved for future use, and 3 instructions are set aside as
permanently unimplemented. Note that the Java bytecode system does
not directly support floating point operations beyond 32 bits -- except
indirectly via bytecodes that enable the use of 64 bit and 80 bit intermediate
IEEE floating point maths. Relation to Java
A Java programmer does not need to be aware of or understand Java bytecode at
all. 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." 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
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 5, while "dconst_1" will push a double
1. There is also an "aconst_null", which pushes "null". The n for the "load" and
"store" instructions specifies the location in the variable table to load
from or store to. The "aload_0" instruction pushes the object in
variable 0 onto the stack. "istore_1" stores the integer on the top of the
stack into variable 1. For variables with higher numbers the suffix is
dropped and operands must be used. Example
Consider the following Java code: A Java compiler might translate the Java
code above into byte code 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
javaSystem.out:LjavaPrintStream; 34: iload_1
35: invokevirtual #85; Method javaPrintStream.println:(I)V
38: iinc 1, 1 41: goto 2
44: return Generation
The most common language targeting Java Virtual Machine by producing Java
bytecode is Java. Originally only one compiler existed, the javac compiler
from Sun Microsystems, which compiles Java source code to Java bytecode; but
because all the specifications for Java bytecode are now available, other
parties have supplied compilers that produce Java bytecode. Examples of other
compilers include: Jikes, compiles from Java to Java
bytecode Espresso, compiles from Java to Java
bytecode GCJ, the GNU Compiler for Java, compiles
from Java to Java bytecode; it is also able to compile to native machine code
and is available as part of the GNU Compiler Collection.
Some projects provide Java assemblers to enable writing Java bytecode by hand.
Assembly code may be also generated by machine, for example by a compiler
targeting a Java virtual machine. Notable Java assemblers include:
Jasmin, takes textual 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 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.
Others have developed compilers, for different programming languages, in
order to target the Java virtual machine, such as:
ColdFusion JRuby and Jython, two scripting
languages based on Ruby and Python Groovy, a scripting language based on
Java Scala, a type-safe general-purpose
programming language supporting object-oriented and functional
programming JGNAT and AppletMagic, compile from the
Ada programming language to Java 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 MIDletPascal
JavaFX Script code is also compiled to Java bytecode
Kotlin Execution
There are several machines available today, both free and commercial
products. If executing Java bytecode in a Java
virtual machine is not desirable, a developer can also compile Java source
code or Java bytecode directly to native machine code with tools such as the GNU
Compiler for Java. Some processors can execute Java bytecode natively. Such
processors are known as Java processors. Support for dynamic languages
The Java Virtual Machine provides some support for dynamically typed languages.
Most of the existing 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.
See also Java bytecode instruction listings
Java class file List of JVM languages
Java backporting tools C to Java Virtual Machine compilers
JStik Common Intermediate Language,
Microsoft's rival to Java bytecode ObjectWeb ASM
References External links
Oracle's Java Virtual Machine Specification
Programming Languages for the Java Virtual Machine
Bytecode Visualizer – bytecode viewer and debugger
AdaptJ StackTrace – bytecode level debugging with a full control of the
stack, the local variables, and the execution flow
Java Class Unpacker – plugin for Total Commander, it lets open class files as
compressed archives and see fields and methods as files. The bytecode can be
viewed as text using F3