Saturday, 22 February 2014

java - explained from source code to execution

The java code undergoes multiple process before the final execution. The processes are,


  • Compilation

  • Loading

  • Linking

    • Verification

    • Preparation

    • Resolution



  • Execution



Compilation:


Compilation is performed by java compiler (javac.exe presents inside JDK/bin). It takes the source code (.java files) and creates the binary files called class files. The class files are the intermediate files which can be executed in any other Java run time machine independent of the Operating system.

Loading:


Loading is the process of finding the binary representation(class files) of the class/interface given the file name. The class loader is explained in detail here.

Linking


Once the classes are loaded into JVM linking starts and it is the process of verifying, preparing and resolving the classes and all it's references.
Verification:

This steps is to ensure that the class obeys for all semantics of the java language. For example it check the following




  •  The byte code is structurally correct

  • The destination of each go to is a byte code instruction

  • The stack will not overflow or iunderflow


Preparation

Here JVM allocates memory for all the variables and instances and prepares them for initialization. Here the variables are set with the default values. For example,  the variable 'int i=10' will be set with the value zero here instead of 10.
Resolution

This is the process of locating all the referenced classes, interfaces and variables and linking them with the direct references.

So to conclude,

  • Classes are compiled,

  • Loaded into JVM

  • verified

  • memory is allocated with the default values

  • references are linked


The final step will be initialization. Here the variables are set with the values declared.  So in the above example the value of "i" will be initialized with 10. After this steps JVM will perform the execution of the classes and gives the output.

No comments:

Post a Comment