JVM Architecture : Working of JVM

What is Java Virtual Machine (JVM) ?

The 3 major section that the compiled program goes through when it is run are:

Class Loader Subsystem

It is mainly responsible for three activities.

  1. Loading
  2. Linking
  3. Initialization

1. Loading

package com.learn.java.sec1.jvm_architecture;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

class Student  {
    private String name;
    private 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;
    }
}


public class ClassObjectJVM {
    public static void main (String[] args) {
        Student s1 = new Student();

        // Getting hold of Class object created by JVM.
        Class c1 = s1.getClass();

        // Printing type of object using c1.
        System.out.println("Name of class: " + c1.getName());

        // getting all methods in an array
        System.out.println("\nAll the Declared Methods of the class: ");
        Method m[] = c1.getDeclaredMethods();
        for (Method method : m) {
            System.out.println(method.getName());
        }

        // getting all fields in an array
        System.out.println("\nAll the Declared Fields of the class: ");
        Field f[] = c1.getDeclaredFields();
        for (Field field : f) {
            System.out.println(field.getName());
        }

    }
}

Output:

Note:- For every loaded .class file, only one object of Class is created.
Student s2 = new Student();
// c2 will point to same object where c1 is pointing
Class c2 = s2.getClass();
System.out.println(c1==c2); // true

2. Linking

3. Initialization

public class Test { 
    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()); 
  
        // Test class is loaded by Application loader 
        System.out.println(Test.class.getClassLoader()); 
    } 
}  

Output:

null
sun.misc.Launcher$AppClassLoader@73d16e93
Notes:

JVM Memory

Components of JVM Memory

##Execution Engine

Java Native Interface (JNI).