The Java virtual machine or JVM is a virtual machine that runs Java byte code, the code produced by a Java compiler or compilers from other languages which produce this code.
Programs written in Java are compiled into a standardized portable binary format, which typically comes in the form of files with the .class extension. A program may consist of many classes, in which case, every class will be in a different file. The first 4 bytes in hexadecimal of each class must be CA FE BA BE. Class files may be packaged together in a .jar file, with the exact same format as a .zip file, optionally with a few extra special files added.
This binary is then executed by the JVM runtime which carries out emulation of the JVM instruction set by interpreting it or by applying a just-in-time Compiler (JIT).
The JVM has a stack based architecture. Each thread has its own program counter.
Code verification is applied to bytecode class files by the runtime at program load time. This means that only a limited amount of bytecode sequences form valid programs, e.g. a JUMP (branch) instruction can only target an instruction within the same function. Because of this, the fact that JVM is a stack architecture does not imply a speed penalty for emulation on register based architectures when using a JIT compiler: In face of the codeverified JVM architecture, it makes no difference to a JIT compiler whether it gets named imaginary registers or imaginary stack positions that need to be allocated to the target architectures registers. In fact, code verification makes JVM different to a classic stack architecture whose efficient emulation with a JIT compiler is more complicated and typically carried out by a slower interpreter.
Code verification also ensures that arbitrary bitpatterns cannot get used as an address. Memory protection is achieved without the need for an MMU. Thus, JVM is an efficient way of getting memory protection on simple silicon that has no MMU.
The JVM has instructions for the following groups of tasks
- Load and store
- Arithmetic
- Type conversion
- Object Creation and Manipulation
- Operand stack management (push / pop)
- Control transfer (branching)
- Method invocation and return
- Throwing exceptions
The aim is binary compatibility. Each particular host operating system needs its own implementation of the JVM and runtime. These JVMs interpret the byte code semantically the same way, but the actual implementation may be different. More complicated than just the emulation of bytecode is compatible and efficient implementation of the Java core API which has to be mapped to each host operating system.
The specification for the JVM is published in book form and HTML and anybody is allowed to write an implementation of it. The preface states:
- We intend that this specification should sufficiently document the Java Virtual Machine to make possible compatible clean-room implementations. Sun provides tests which verify the proper operation of implementations of the Java Virtual Machine.
Kaffe is an example of a group of developers writing such an implementation.
See also
External links
- List of languages which compile to the Java virtual machine.
- Sun.com - The Java Virtual Machine Specification
- Kaffe.org - the Kaffe project