Tuesday, March 24, 2009

How JVM works ?



From this week I will start write about Java language.If you are new to java language, these posts are very useful for learning java from the depth but its bit difficult to understand at your first step.For known user,its better to gain more information about java.I hope that my first topic will useful to all.

As we know,to run java program we need JVM(java virtual machine).This JVM converting your java byte code to machine understandable language .Fine ..! Let me explain first about parts of JVM and then how it works in OS.

JVM contains three parts

1. Class Loader
2. Byte code verifier
3. JIT Interpreter

Class Loader :
This class loader have two parts one is default class loader and another one is
custom class loader.

Default class loader:

Default class loader knows how to load class file from local system.It will works on regular situations, when you have java program fully compiled and waiting on your computer. Most of us running the program on this way only.

Custom class loader :

Custom class loader is, load a class file from website , other than local drive or network.
You can use custom class loader to

* Automatically verify a digital signature before excuting untrusted code
* Transparently decrypt code with a user supplied password
* Create dynamically built classes customized to the user's specific needs

The appletviewer contains the class loader that ,instead of looking in the localfilesytem for classes,access a remote server, loads
the raw byte code files via HTTP,and turns them into classes inside the JVM



Bytecode verifier :
Code may have come from the local system, or it may have travelled halfway around the planet. The bytecode verifier acts as a sort of gatekeeper: it ensures that code passed to the Java interpreter is in a fit state to be executed and can run without fear of breaking the Java interpreter. Imported code is not allowed to execute by any means until after it has passed the verifier's tests. Once the verifier is done, a number of important properties are known:

* There are no operand stack overflows or underflows
* The types of the parameters of all bytecode instructions are known to always be correct
* Object field accesses are known to be legal--private, public, or protected

While all this checking appears excruciatingly detailed, by the time the bytecode verifier has done its work, the Java interpreter can proceed, knowing that the code will run securely. Knowing these properties makes the Java interpreter much faster, because it doesn't have to check anything. There are no operand type checks and no stack overflow checks. The interpreter can thus function at full speed without compromising reliability.

JIT Interpreter:

It compiles the bytecode into platform-specific executable code that is immediately executed. Sun Microsystems suggests that it's usually faster to select the JIT compiler option, especially if the method executable is repeatedly reused.


After you've written a Java program, the source language statements are compiled by the Java compiler into bytecode and then JIT interpreter covert your byte code into machine understandable language .For ex ample, In windows JVM that convert byte code into windows understandable language the same way, linux JVM convert byte code into Linux understandable language.

Here you may ask a question that each JVM in each OS are different ?

Certainly ,Each JVM in each OS are different .

Quick View :

Soucre code compiled by java compiler and convert into bytecode.This byte code will available
as class file in Localfile system,network or remote server.Appropriate class loader will
load the class file and verify the code using with bytecode verifier.Once the
code verify by bytecode verifier,JIT interpreter will convert in to machine understandable language .Now the machine (OS) knows how do execute the program and
display the output .