6 Aug 2011

Under the Hood : Java Program Execution


Starting JVM
By invoking the main() of a class and passing argument to it. But at this stage main() still not invoked ,because JVM does not have binary representation of the class.


Loading class or interface.
JVM retrieve the binary representation of a class or interface from compiled source code and constructing the .class file format.

Loading is implemented by the ClassLoader and its subclasses.
If any error occurs during loading  , it throws instance of LinkageError or its subclasses.
Array classes do not have an external binary representation; they are created by the Java virtual machine rather than by a class loader.


Linking {Verification, Preparation, Resolution} class or interface
Linking is a process of taking binary form of class or interface and combining it to run-time state of the Java virtual machine.
 Verification ensures that the binary representation of a class or interface is structurally correct.
For example, it checks that every instruction has a valid operation code, every method is provided with a structurally correct signature etc.
If an error occurs during verification, , it throws instance of LinkageError or its subclasses.
Preparation involves creating the static fields for a class or interface and initializing such fields to the standard default values or explicit .
Resolution is the process of checking symbolic references from class other classes and interfaces, by loading the other classes and interfaces that are mentioned and checking that the references are correct.
If an error occurs during Resolution, it throws instance of IncompatibleClassChangeError or its subclasses


Initialization class or interface
Initializes the static initializer block and static fields but final  static field are not initialized at  this stage because they are complie-time constant..
Initialization occur before any instance is created or any static method is invoked.
Once a class is initialized  its main() is invoked by JVM.
Before a class or interface is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized.
the superinterfaces of an interface need not be initialized before the interface is initialized.


Instantiation of class
When new() operator is encountered a instance is created.
Memory is allocated for instance variable of class and that of its superclass as well.
If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError.


Unloading class and interface.
A class or interface may be unloaded if and only if its class loader is unreachable.
The bootstrap class loader is always reachable; as a result, system classes may never be unloaded.


JVM Exists
The Java virtual machine terminates all its activity and exits when one of two things happens: 
All the threads that are not daemon threads terminate.
Some thread invokes the exit method of class Runtime or class System, and the exit operation is permitted by the security manager. 

No comments:

Post a Comment