According to DELUXESURVEILLANCE, the Java Virtual Machine, or JVM for short, is the central component of the JRE, i.e. the Java Runtime Environment. It enables the platform-independent execution of programs in Java bytecode.
With high-level programming languages such as C and C ++, the compilers usually generate a directly executable program file . Such “native executables” consist of the commands of the machine language of a specific hardware architecture.
This has the advantage of a slightly faster execution and less dependency on complex runtime environments. However, the program must be recompiled for each supported hardware platform and operating system, as well as for changes to the source code .
Languages like Java , C # or Python take a different route. Under the motto “Write once, run everywhere”, Java wants to achieve maximum portability. The Java compiler “javac” translates the source text into an intermediate code that is not yet executable. A JVM is required for final execution.
How does the JVM work?
The JVM is a process-based VM – a very simple virtual machine that runs the Java bytecode. Such VMs translate the instructions of the bytecode at runtime into the machine code of the real hardware and into the system calls of the operating system. This has the advantage of high portability.
One and the same program can run without recompilation on all systems for which there is a corresponding VM. In contrast to system-based VMs such as VirtualBox, VMware or Hyper-V, they deliberately only allow the execution of a single application instead of an entire operating system.
In addition to platform independence, the JVM has other advantages in terms of speed and security. The translation at runtime can perform dynamic optimizations that are not possible with conventional ahead-of-time compilers. For example, the just-in-time compiler can completely remove code paths and loops that are not necessary with the existing input data. This increases the speed of functions as they continue to be executed.
Security is one of the fundamental design criteria of the Java VM. The virtual machine largely isolates Java programs from the operating system. Access to the resources of the underlying system can be precisely controlled. The JVM contains automatic memory management via garbage collector. Typical programming errors such as buffer overflows and crashes due to prematurely released memory are not possible in the VM.
One VM, many languages
The Java VM executes an intermediate code that has little to do with the actual programming language . This is why the JVM cannot only be used in Java programming . In principle, any language can be translated into a JVM-compatible bytecode. All you need is a corresponding compiler.
In addition to the advantage of platform independence, the use of other languages also provides better integration of different software systems. For example, the Python variant Jython runs on the JVM. It can thus use Java classes and enables seamless integration of Python and Java code. Other languages for the JVM include the LISP dialect Clojure, the functional languages Erjang and Scala as well as Kotlin , Groovy and Jruby.
Implementations of the Java VM
In addition to the official hotspot JVM from Oracle, there are several other implementations of the Java VM. They differ primarily in their implementation details, areas of application and additional functions. What they all have in common is the ability to execute Java bytecode in accordance with the “Java Virtual Machine Specification” standard. The most important JVMs include:
- HotSpot: The reference implementation from Oracle and is part of the Oracle Java platform including the open source version OpenJDK.
- GraalVM: A new JVM from Oracle. It not only understands Java bytecode, but also the bytecode of the LLVM compiler system. Hence, it can also run programs written in LLVM compatible languages such as C / C ++, Julia, Rust, and many others.
- OpenJ9: The Eclipse JVM is designed for performance and scalability. Originally developed by IBM, the Eclipse Foundation is now responsible for the project.
- JamVM: Implements a very small JVM that is used, for example, on some ARM systems.
- DalvikVM: The DalvikVM used on older Android devices is often considered a JVM. Strictly speaking, it isn’t because it uses a different bytecode.