Tuesday, February 7, 2017

JVM - Classloading in java

Hello Everyone , Hope you all are doing good , Today we will learn about class loading in java , class loading is very basic topic which most of the programmer dont know, so lets see what and type of classloaders.

--jvm Class Loading---
Three activities while class loading
-loading
-linking
-initialization


Loading--- The class loader reads the .class files and generate the correposnidn binary data.

Let see this with example

import java.lang.reflect.Method;

public class ClassLoading {

public static void main(String[] args) {
Student s1= new Student();
Class c1= s1.getClass();

System.out.println(s1.getClass().getName());
Method[] m = s1.getClass().getMethods();
for(Method m1:m)
{
System.out.println(m1.getName());
}
Student s2 = new Student();
Class c2= s2.getClass();
System.out.println(c1==c2);
}


}


class Student {

String name;
int roll_no;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRoll_no() {
return roll_no;
}
public void setRoll_no(int roll_no) {
this.roll_no = roll_no;
}

}

Output----
inv.learning.jvm.Student
getRoll_no
setRoll_no
getName
setName
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
true


NOTE: If we see the line System.out.println(c1==c2) it will give true that means Class c1 and Class c2 always point to same Class Object.
For every loaded .class file, only one object of Class is created.

--------------------------------------------------------
Linking-- Performs verification, preparation, and (optionally) resolution.
Verification : Its ensure the correctness of .class file if not generated by valid compile it will give the runtime exception.
Preparation : JVM allocates memory for class variables and initializing the memory to default values.
Resolution : It is the process of replacing symbolic references from the type with direct references. It is done by searching into method area to locate the referenced entity.

----------------------

Initialization:- In this phase all the static variable and static blocked get the assigned values in the code , it executed from top to bottom and from parent to child.
basically there are 3 type of class loader.
1) Bootstrap class loader- These class loader is essential used to load the trusted classes. Like all the native java api classes which are defined in JAVA_HOME/jre/lib.
2)Extension class loader- child of bootstrap class loader , load all the class of JAVA_HOME/jre/lib/ext .
3) Application class loader - child of extension class loader , used to load the application .class files.

package inv.learning.jvm;

public class ClassLoadingDemo {

public static void main(String[] args) {

// String class is loaded by bootstrap loader, and
        // bootstrap loader is not Java object, hence null
System.out.println(String.class.getClassLoader());

System.out.println(ClassLoadingDemo.class.getClassLoader());
}
}

Thanks for reading
-Noeik

0 comments:

Post a Comment