Wednesday, 13 March 2013

Java Virtual Machine


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:

  1. The java codes being written using notepad is saved as .java file 
  2. It is then compiled using the command javac
  3. If the compilation is successful .class file is created as a result of compilation. It contains the bytecodes
  4. The bytecodes should be converted into a machine code that a machine can understand 
  5. The JVM thus converts the bytecode into machine code and makes the file as platform independent executable file
  6. 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

Class Loader
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. 

Bytecode Verifier:
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:
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:
Security Manager provide certain restrictions to the applications and applets. 

  1. For Application: No restrictions by default. 
  2. For Applet: Applets cannot access local drive and local network drive.

Execution Engine:
It is responsible for converting bytecode into a machine code. 

Tags: ,,

1 comment:

  1. JVM tutorial on Method area

    JVM tutorial on Heap area