Showing posts with label Class Loader Sub System. Show all posts
Showing posts with label Class Loader Sub System. Show all posts

Aug 5, 2017

Understanding JVM: Class Loader Sub System - Part 2

First part of class loader sub system talked about the Loading of class files. Next task that is performed by the class loader sub system is Linking.

Linking 

It performs 3 activities:
  • Verification
  • Preparation
  • Resolution

Verification

  • It is the process of ensuring binary representation of class file (output of Loading) is correct or not. Correct means if .class file is properly formatted or not, and it is generated by valid compiler or not.
  • Internally Byte Code Verifier, which is part of Class Loader Sub System, is responsible for this activity.
  • If verification fails, then it will throw the run-time exception, java.lang.VerifyError.

Preparation

  • In preparation phase, JVM will allocate memory and assign default values for class level static variables. For example:
    • For int --> 0
    • For double --> 0.0
    • For boolean -->false
    • For object --> NULL
  • It is important to note here that only default values are assigned in this phase, actual values (as defined in the class will be assigned in initialization phase. That means if a class variable is declared as,  private static int count = 10, 0 will be assigned in the preparation phase.

Resolution

  • All symbolic references are replaced with original direct references from method area. (Method area will be explained in coming articles). For example, consider following code:
 class Test {  
      public static void main(String[] args) {  
           String name = new String("Java Territory");  
           Student s1 = new Student();  
      }  
 }
  • In the above example, class loader sub system loads Test.class, String.class, Student.class and Object.class. The names of these classes are stored in constant pool of Test class.
  • In Resolution phase, these names are replaced with actual references from the method area of JVM.
After Linking is done, Initialization of class files are done.

Intialization
  • After Loading and Linking of class files are done, JVM will assign original values (as declared in the class file) to the class level static variables.
  • Also, static blocks are executed during this phase.
Following diagram explains the functions performed by Class Loader Sub System:
If any error comes Loading, Linking or Initialization, JVM will raise runtime exception, java.lang.LinkageError.

VerifyError is a child class of LinkageError.

This is all for Class Loader Sub System. In next article, I will talk about the Types of Class Loaders. Please post your inputs in the comments section and if you like reading our article, follow us @ +Java Territory / facebook to stay connected and get notifications for new articles.

Jul 10, 2017

Understanding JVM: Class Loader Sub System - Part 1

Now we know from the previous article that JVM is responsible for loading od the Java class file. Internally Class Loader Sub System performs following steps:
  1. Loading
  2. Linking
  3. Initialization
Loading: Loading means reading .class file data and store corresponding data in method area. For each .class file, JVM will store following information:
  • fully qualified name of class
  • fully qualified name of immediate parent
  • whether .class file represents class/interface/enum
  • methods/constructors/variables information
  • modifiers information
  • constant pool information
After .class object is loaded into memory, JVM then creates the "Class" object for the the same. This object can be used to retrieve class level information like how many methods are there in class, using reflection. 

It is important to note that for every class, only one "Class" object will be created by JVM. Consider following example:

1:  public class Exmaple {  
2:       Student s1 = new Student ();  
3:       Class c1 = s1.getClass();  
4:       Student s2 = new Student ();  
5:       Class c2 = s2.getClass();  
6:  }  

In the above example, s1.getClass() and s2.getClass() will give same object as there is only one object of Class class of Student.

Classloader subsystem first loads class file and stores in method area. Then JVM creates object of Class class.

Next section will be on the second activity performed by Class Loader Sub System, i.e., Linking.
Join us at +Java Territory or facebook to stay updated.