It acts as a run-time engine to run Java applications.
It is the one that actually calls the main method present in a java code.
JVM is a part of JRE(Java Runtime Environment).
Java applications are called WORA (Write Once Run Anywhere).
We can develop Java code on one system and run on any other Java enabled system without any adjustment. This is all possible because of JVM.
When we compile a .java file, .class files(contains byte-code) with the same class names present in .java file are generated by the Java compiler. This .class file goes into various steps when we run it. These steps together describe the whole JVM.
The 3 major section that the compiled program goes through when it is run are:
Class Loader Subsystem
JVM Memory
Execution Engine
Class Loader Subsystem
It is mainly responsible for three activities.
Loading
Linking
Initialization
1. Loading
The Class loader reads the .class file, generate the corresponding binary data and save it in method area.
For each .class file, JVM stores following information in method area.
Fully qualified name of the loaded class and its immediate parent class.
Whether .class file is related to Class or Interface or Enum.
Modifier, Variables and Method information etc.
After loading .class file, JVM creates an object of type Class to represent this file in the heap memory.
Please note that this object is of type Class predefined in java.lang package.
This Class object can be used by the programmer for getting class level information like name of class, parent name, methods and variable information etc.
To get this object reference we can use getClass() method of Object class discussed in other important concepts section.
packagecom.learn.java.sec1.jvm_architecture;importjava.lang.reflect.Field;importjava.lang.reflect.Method;classStudent{privateStringname;privateintroll_No;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetRoll_no(){returnroll_No;}publicvoidsetRoll_no(introll_no){this.roll_No=roll_no;}}publicclassClassObjectJVM{publicstaticvoidmain(String[]args){Students1=newStudent();// Getting hold of Class object created by JVM.Classc1=s1.getClass();// Printing type of object using c1.System.out.println("Name of class: "+c1.getName());// getting all methods in an arraySystem.out.println("\nAll the Declared Methods of the class: ");Methodm[]=c1.getDeclaredMethods();for(Methodmethod:m){System.out.println(method.getName());}// getting all fields in an arraySystem.out.println("\nAll the Declared Fields of the class: ");Fieldf[]=c1.getDeclaredFields();for(Fieldfield:f){System.out.println(field.getName());}}}
Output:
Note:- For every loaded .class file, only one object of Class is created.
Students2=newStudent();// c2 will point to same object where c1 is pointingClassc2=s2.getClass();System.out.println(c1==c2);// true
2. Linking
It Performs verification, preparation and (optionally) resolution.
It loads the classes present in the extensions directories JAVA_HOME/jre/lib/ext(Extension path) or any other directory specified by the java.ext.dirs system property.
It is implemented in java by the sun.misc.Launcher$ExtClassLoader class.
It is responsible to load classes from application class path.
It internally uses Environment Variable which mapped to java.class.path.
It is also implemented in Java by the sun.misc.Launcher$AppClassLoader class.
publicclassTest{publicstaticvoidmain(String[]args){// String class is loaded by bootstrap loader, and // bootstrap loader is not Java object, hence null System.out.println(String.class.getClassLoader());// Test class is loaded by Application loader System.out.println(Test.class.getClassLoader());}}
Output:
null
sun.misc.Launcher$AppClassLoader@73d16e93
Notes:
JVM follow Delegation-Hierarchy principle to load classes.
System class loader delegate load request to extension class loader and extension class loader delegate request to boot-strap class loader.
If class found in boot-strap path, class is loaded otherwise request again transfers to extension class loader and then to system class loader.
At last if system class loader fails to load class, then we get run-time exception java.lang.ClassNotFoundException.
In method area, all class level information like class name, immediate parent class name, methods and variables information etc. are stored, including static variables.
There is only one method area per JVM, and it is a shared resource.
It compiles the entire bytecode and changes it to native code.
So whenever interpreter see repeated method calls, JIT provide direct native code for that part so re-interpretation is not required, thus efficiency is improved.