This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Tuesday, 21 August 2018

How java virtual machine(JVM) works

jvm Architecture: 

In java jvm play's very important role to run our .class files. when compiler convert java code into byte code then how jvm can work's internally. 
when we save  java source file into the hard disk then  how compiler compile and how after compilea java file will be  generated .class file allocate in jvm. we will discuss in this session.

Basic Introduction of jvm working:

when we save java file in any drive ,then after compile,byte code will be generated ,which is store binary data.this binary data site all information about our class like fully class name,class name,data types,variables etc.

class loader subsystem is  responsible for load our .class file into the jvm memory area. generally various memory areas inside the jvm but our class file will be stored in the method area.

class loader system:

The class loader system is responsible for loaded our .class file into the jvm memory area.when compiler compile our java source file then class loader subsystem is only responsible for loaded our .class file into the jvm memory areas.
class loader sub system is responsible for following three activities.
1.loading: when the compiler compile's our java source file and generate .class file,then the .class file loaded  inside the jvm  memory area and class loader system is responsible for loading the .class file .
jvm store's complete information about our .class file  through the binary data available inside the method area.it store's all information like
  • fully class name
  • method names
  • data types
  • variables name
when .class file loaded into the method area then jvm creates class object inside the heap area and programmers can use this class object to get the information about class.
how class loader subsystem work's internally let me show in following diagram


Add caption



















import java.lang.reflect.*;
class Demo
{
public String show()
{
return null;
}
public int result()
{
return 50;
}
}
class Demo
{
public static void main(String[] args)throws Exception
{
int count=0;
  Class c=Class.forName("Test");
  Method[] m=c.getDeclaredMethods();
  for(Method m1:m)
  {
  count++;
  System.out.println(m1.getName());
  }
  System.out.println("total methods are"+ count);
  
}
}

output:

result
show
total methods are2



For every time only one class object will be created into the jvm memory area even though we are using class name more then time but class  class object will be created only once.

Example 2: In the  program we will get to know in jvm memory area only one class class object will be created.

import java.lang.reflect.*;
class Demo
{
public String show()
{
return null;
}
public int result()
{
return 50;
}
}
class Test
{
public static void main(String[] args)throws Exception
{
Demo d1=new Demo();
Class c1=d1.getClass();
Demo d2=new Demo();
Class c2=d2.getClass();
System.out.println(c1.hashCode());
System.out.println(c2.hashCode());
  
}
}


output:
1311053135
1311053135

In the above program we got same hash code for both class objects ,that means only one class object created  in jvm heap  area  because every object has our own hash code two object can't hold same hash code. 

2.Linking: Linking is responsible for lot's of activities in the jvm,it's check after compile, our binary data is correct or not ,our program compile valid compiler or not .If our program not compile's  valid compiler then we will get run time exception
Inside this area static variables initialize with default values.

3. Initialization: In this session static members initialized with original values and static block's will be executed in this area.It is very important part according to the static members.







































































































   












Adbox