A Java virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets compiled Java binary code (called bytecode) for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions. Java was designed to allow application programs to be built that could be run on any platform without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the platform. Java is also called as WORA-Write Once Run Anywhere because of JVM's work.
Working of a Compiler and JVM in JAVA:
- The java codes being written using notepad is saved as .java file
- It is then compiled using the command javac filename.java
- If the compilation is successful .class file is created as a result of compilation. It contains the bytecodes
- The bytecodes should be converted into a machine code that a machine can understand
- The JVM thus converts the bytecode into machine code and makes the file as platform independent executable file
- The executable file can be run on any platform such as Windows, Macintosh etc..
Though Compiler and JVM helps in making the java code platform independent, both Compiler and JVM are not platform independent. It must be downloaded according to your Operating System.
Components of JVM:
JVM comprises of 5 major components. They are:
- Class Loader
- ByteCode Verifier
- Garbage Collector
- Security Manager
- Execution Engine
The class loader loads the .class file into the JVM. Class Loader is similar to Namespaces. Primarily class loader objects are created and then each class is loaded in steps. Classes loaded into 2 different objects cannot interact with each other.
The JVM verifies all bytecode before it is executed. This verification consists primarily of three types of checks:
- Branches are always to valid locations
- Data is always initialized and references are always type-safe
- Access to private or package private data and methods is rigidly controlled.
Garbage Collector is an application program that runs in JVM. Java does not allow deleting the objects tat are once created. The object that hang around unused leads to Memory Leakage and to avoid this Garbage Collector is used. GC uses Mark and Sweep methodology.
Security Manager provide certain restrictions to the applications and applets.
- For Application: No restrictions by default.
- For Applet: Applets cannot access local drive and local network drive.
It is responsible for converting bytecode into a machine code.
Tags: Java,Virtual Machine,bytecode