The virtual machine part inside the phrase “Java Virtual Machine” often confuses many peoples with full virtualization. A Java Virtual Machine is an example of application virtualization. The Java Virtual Machine converts the compiled Java code into machine language to run it on the host computer. At that time, there was a concept named WORA (Write Once Run Anywhere). Today, we still have the idea in the form of web applications. Java went to “native application”.
With langugage such as Turbo C, the compiler produces a platform dependent code, and there is no need for a Virtual Machine as the compiled Turbo C program can be executed directly by a physical CPU. In case of Java, the compiled code is a set of instructions for a virtual CPU which should work the same on every physical machine.
In this way, the designers of the Java language made the langugage platform independent. But since the code will run on a physical platform, they had to opt to put all the platform dependent code part in the Java Virtual Machine. The syntax of Java is largely looks like C++ but the things are different at backend. C++ is quite primitive makes it the choice for building most of basic things which interacts with hardware.
The Java Virtual Machine is not a virtual machine unlike a cloud server instance. No hardware other than the processor is actually virtualized in case of Java Virtual Machine. The JVM is a virtualized CPU plus some runtime like C++ or any other object oriented language has, plus it includes the system for garbage collection and other necessities. Java uses an automatic garbage collector to manage memory. Just-in-time (JIT) compilers that compile byte-codes to machine code during runtime were introduced to avoid the overhead of interpreting bytecode into machine instructions. So the Java Virtual Machine interprets or compiles the byte code into native machine instructions.
Java bytecode runs in a Java Runtime Environment. Java Virtual Machine (JVM) is the most important element of the Java Runtime Environment. The Java Virtual Machine inside the Java Runtime Environment analyzes and executes Java byte code. The Java Runtime Environment has the Java API and the Java Virtual Machine. The role of the JVM is to read the Java application through the Class Loader and execute it along with the Java API. Java Virtual Machine (JVM) does these jobs :
- Loads the code
- Verifies the code
- Executes the code
- Provides the run-time environment
- Provides the memory area
- Register set
- Provides a garbage collection heap
- Reports the fatal errors
- Provides a class file format
The JVM distributed by Sun/Oracle and IBM is commonly used.