Saturday, September 17, 2016

JVM Architecture

JVM(Java Virtual Machine):
=====================

JVM is responsible to execute java program.
After compilation successful we should get .class file. After that .class file given as input to JVM while executing the program.
The JVM mainly contains three parts, those are:
1.Class Loader Sub System
2.Memory Area
3.Execution Engine
1)Class Loader Sub System(CLS):
-----------------------------------------
Class Loader Sub System is mainly responsible for three activities. Those are:
1.Loading
2.Linking
3.Initialization
1.Loading:
-------------
In loading three types of class loaders are there:
i)  Bootstrap Class Loader
ii) Extension Class Loader
iii)Application Class Loader
i)Bootstrap Class Loader:
-------------------------------
It is responsible to load classes from bootstrap class path(noting but rt.jar). All core java classes are loaded by Bootstrap Class Loader.
ii)Extension Class Loader:
---------------------------------
It is responsible to load the classes present in ext folder(C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext).
iii)Application Class Loader:
-----------------------------------
To load classes from application level class path(Environment Variable class path).
2.Linking:
------------
In linking 3 steps are there
i)  Verify
ii) Prepare
iii)Resolve
i)Verify:
----------
After loading verification will be started, it verifies the loaded byte code is valid or not, if verification fails then we will get verify error.
ii)Prepare:
-------------
For static variables memory will be created and assigned with default values.
iii)Resolve:
--------------
All Symbolic references are replaced with original references from method area.
3.Initialization:
-------------------
Static variables initialized with original values and static blocks will be executed.
After initialization class loading successfully completed.
2)Memory Area:
--------------------
The JVM memory area contains 5 different memory's. Those are:
i)  Method Area
ii) Heap Area
iii)Stack Area
iv) PC Registers
v)  Native Method Stacks
i)Method Area:
-----------------
Class level data and static variables will be stored here.
ii)Heap Area:
----------------
All objects and it'corresponding instance variables are stored here, and Array in java also an object, these are also stored here.
iii)Stack Area:
------------------
For every thread one run time stack will be created, each and every method call performed by that will be stored in corresponding stack, including local variables also. Each entry in stack is called stack frame, and stack frame contains three parts.
a)Local Variable Array
b)Frame Data
c)Operand Stack

a)Local Variable Array:
-----------------------------
Related to that method how many local variables are there and their corresponding values will be stored in Local Variable Array.
b)Frame Data:
----------------
It contains all symbols we used in that method by default it will handle.
In case of exception if any exception occurs then the corresponding catch block information will be their inside Frame Data.
c)Operand Stack:
--------------------
If any intermediate operation is required to perform which is act as run time work space.
iv)PC Registers:
--------------------
For every thread one PC Register  will be created, it hold the address of current executing instruction, once the current executing instruction completed automatically PC Register updated to next instruction.
v)Native Method Stacks:
-----------------------------
For every thread a separate run time stack will be created to hold native method information.
3.Execution Engine:
------------------------
Execution Engine is responsible to execute your program.
Even though if we call one method multiple times interpretation must be required, instead of if it is the repeatedly required method better to generate machine code directly once, next time without interpretation we can execute. JIT is responsible to improve the performance of the system.
Some times while executing java programs execution engine may require native method libraries, Java Native Interface is responsible to provide native method information.

Friday, September 16, 2016

How to create simple java program which print "Hello World!" ?
Here I'm showing some simple steps just follow to do so.
Step1:
------
Open notepad 
Step2:
-------
Just type below java code.


public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}

}

Step3:
--------
save the program with calss name and with .java extension. In the above program class name is HelloWold,

so you should save above program with HelloWorld.java
        My Suggestion:
------------------

Open 'D' Drive --->Create Folder with JavaExamples--->Take a new .txt file--->type javacode--->save as HelloWorld.java

Step4:
after saved the file, you have to compile the program, for that you need to open command prompt. To open command prompt press windowslogo+R you will see the below prompt type cmd and press OK button. After follow the below figure commands to move the cursor to your JavaExamples directory.


Compilation:
----------------
Now you have to compile your java program for to see the below figure continuation to above command prompt figure.
Syntax:
--------
javac FileName.java
Ex:javac HelloWorld.java
After Compilation successful it won't give you any successful message instead
it simply create .class file in your directory this is your conformation to run java program.

Execute Java Program:
-----------------------------
Syntax:
---------
java FileName

Ex:java HelloWorld