JVM is an abbreviated form of Java Virtual Machine. It is a Java byte code processing engine that converts Java byte code into machine language, which is then executed by the machine (computer). Java byte code is an intermediary language between Java and the machine on which it has to be executed, and this execution can be done when JVM processes Java byte code into the machine language.
JVM in Simple Words
Let us understand this concept of Java byte code, JVM, and Machine in simple words.
When a Java program is created, a file with .java extension is built. This file may contain several attributes that include but not limited to Classes, methods, objects, and so on. This .java file is then compiled using a Java Compiler, which generates a .class file (also called Java Byte Code). JVM reads this .class file, understands its code, interprets the code into machine language, and executes the file.
Is JVM Platform Independent?
Java is platform independent because of JVM but JVM is platform dependent. The machine on which Java code has to be executed must have the JVM installed.
Depending upon the machine on which Java byte code has to be executed, JVM interprets the .class file and generates a different machine language, which is understandable to that machine only. Different machines may have different operating systems such as Windows, Mac, Unix, or Linux. Without JVM, these operating systems cannot interpret Java Byte Code because an operating system understands the machine language only. JVM converts the Java Byte Code into machine language and makes it understandable to the operating system for execution. It means, Java can run on any machine but it needs JVM installed on that machine.
Components of JVM
While interpreting and executing Java byte code, JVM uses several components such as Stack, Registers, Garbage Collector, and so on. Let us have a look at some of its components:
- Stack: It is a storehouse for method arguments and local variables. It can be manipulated with help of the registers that point to its different parts.
- Registers: There are many registers in JVM such as Vars, Frame, Program Counter, and OpTop. Vars register points to the local variables that are in use by the current method. The Frame register points to the execution environment, which is an environment for stack. The Optop points to the operand stack where instructions for byte code are executed. The Program Counter register points to the Method area where byte code is kept.
- Method Area: It is a place holder for Java byte code. This area is shared between all threads so that the synchronization can be ensured.
- Garbage Collector: It is the component of JVM where actual Java objects are stored. Please note that references of the Java objects are stored in the Stack but actual objects are in the Garbage Collector. Java uses the Garbage Collection to free the memory allocated to different objects unlike C/C++ where operators are used to free memory.