EXECUTION FLOW OF MAIN()
METHOD INSIDE JVM
in JAVA Saurabh Uniyal
MCA VI
HNBGU Srinagar
Garhwal
27-May-2016
CONTENTS
 Why main(-) method
 Some main() method FAQ’s
 User defined or Predefined ?
 In which class main(-) method is define by sun ?
 Why main() method has String parameter array ?
 How JVM execute a java class ?
WHY MAIN(-) METHOD
 main(-) is a initial point
of a class Execution
JVM
Internal code
Example.java
Class Example {
public static void main(String[]
args)
{
System.out.println(“main
Block”);
}
Output :
main Block
If we create some
other method in a
class
static void m1()
{
System.out.println(“method 1
body”);
}
static void m2()
{
System.out.println(“method 2
body ”);
}
Then JVM only
execute main(-)
method’s logic
WHY MAIN(-) METHOD(CONT’D)
JVM
Internal code
Example.java
Class Example {
public static void main(String[]
args)
{
System.out.println(“main Block”);
}
Output :
main Block
method 2 body
static void m1()
{
System.out.println(“method 1
body”);
}
If we want to execute
those method’s logics
then we must call these
methods from main(-)
method
m2();
static void m2()
{
System.out.println(“method 2
body ”);
}
 so that we can say main method is a mediator between JVM and Java
Programmer.
JVM
class
main()
FAQ’S ABOUT METHOD(-)
QUES: IS MAIN METHOD IS USER DEFINED OR PREDEFINED
Ans:
main(-) method is User defined method with Predefined Syntax
Public static void main(String[] args)
{
}
given by : sun Public static void main(String[] args)
{
…………….
…………….
}
Complete logic is Programmer’s logic
QUES: IN WHICH CLASS MAIN(-) METHOD IS
DEFINE BY SUN ?
Ans:
main(-) is not physically defined in any class.
It’s syntax is logically specified by sun.
WHY MAIN() METHOD HAS STRING PARAMETER ARRAY ?
For reading runtime values from keyboard()
C:Windowssystem32cmd.exeC:_
Class Example {
public static void main(String[ ] args)
{
String name=arg[0];
String addr =arg[1];
System.out.println(“name: ”+name);
System.out.println(“addr : ”+addr);
}
}
Compile
Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.
C:Userssaurav>C:Userssaurav> javac Example.java
X[]-
C:Userssaurav>
Compiler
generate
Example.class
file in same
directoryC:Userssaurav> java Example sherlock NYsherlock NY
name :sherlock
addr : NY
HOW JVM EXECUTE THIS “EXAMPLE1” JAVA CLASS
Class Example1 {
static int x=10;
static int y=20;
int a=30;
int b=40;
public static void main(String[ ] args)
{
int p=50;
int q=60;
Eample1 e1=Example();
}
}
Example1.java
J V M
Virtual
Machine
Java
JVM System based
virtual machine
 VirtualBox
 VMWare
Process based
virtual machine
 JVM
 PVM
Runtime instance
C:> java Example1
Method Area
JVM
Heap Area JAVA Stack Area
Programm Counter
Register Area
Native method Area
Class
data
Class
data
Class
data
Class
data Class
data
Class
data
Class
data
object
object
object
object
object
object
object
object
object
Class
data
Class
data
Class
data
Class
data
Class
data
Class
data
Class
data
Class
data
Class
data
object
object
object
object
object
Runtime
area
One
program
counter
per
Thread
Responsible
For native
method
execution
C:> java Example1
Method Area
JVM
Heap Area JAVA Stack Area
Programm Counter
Register Area
Native method Area
Class Loader
Jdk/jre/lib/ex/*.jar
Class Loader
Application Class Loader
Extension Class Loader
Bootstrap Class Loader
Searching in Jdk/jre/lib/rt.jar
If found
load
class to
method
area
Searching in
If found
load
class to
method
area
If not found
If not found
Class path systemSearching in
If found
load
class to
method
area
If not found
returns
ClassNotFoundException
Using “Parent Delegation Algorithm”
Method Area Heap Area JAVA Stack Area
Programm Counter
Register Area
Native Area
Class Loader
If found, then load this class in method area by
creating java.lang.Class Object
x 0
y 0
a
b
main(s[]){ }
x = 10
y = 20
10
20p s v main(S args[])
args1010
p50
q60
length 0
1010
e12020
2020
a 30
b 40
Static variable
Linking
&
Execute
native
methods
uisng JNI
REFERENCES
“Inside the Java Virtual Machine”, Bill Venners
“JVM Architecture with SCJP”, Harikrishna sir
www.nataraj.in
THANK YOU
sauravuniyal@gmail.com

Execution flow of main() method inside jvm

  • 1.
    EXECUTION FLOW OFMAIN() METHOD INSIDE JVM in JAVA Saurabh Uniyal MCA VI HNBGU Srinagar Garhwal 27-May-2016
  • 2.
    CONTENTS  Why main(-)method  Some main() method FAQ’s  User defined or Predefined ?  In which class main(-) method is define by sun ?  Why main() method has String parameter array ?  How JVM execute a java class ?
  • 3.
    WHY MAIN(-) METHOD main(-) is a initial point of a class Execution JVM Internal code Example.java Class Example { public static void main(String[] args) { System.out.println(“main Block”); } Output : main Block If we create some other method in a class static void m1() { System.out.println(“method 1 body”); } static void m2() { System.out.println(“method 2 body ”); } Then JVM only execute main(-) method’s logic
  • 4.
    WHY MAIN(-) METHOD(CONT’D) JVM Internalcode Example.java Class Example { public static void main(String[] args) { System.out.println(“main Block”); } Output : main Block method 2 body static void m1() { System.out.println(“method 1 body”); } If we want to execute those method’s logics then we must call these methods from main(-) method m2(); static void m2() { System.out.println(“method 2 body ”); }
  • 5.
     so thatwe can say main method is a mediator between JVM and Java Programmer. JVM class main()
  • 6.
  • 7.
    QUES: IS MAINMETHOD IS USER DEFINED OR PREDEFINED Ans: main(-) method is User defined method with Predefined Syntax Public static void main(String[] args) { } given by : sun Public static void main(String[] args) { ……………. ……………. } Complete logic is Programmer’s logic
  • 8.
    QUES: IN WHICHCLASS MAIN(-) METHOD IS DEFINE BY SUN ? Ans: main(-) is not physically defined in any class. It’s syntax is logically specified by sun.
  • 9.
    WHY MAIN() METHODHAS STRING PARAMETER ARRAY ? For reading runtime values from keyboard() C:Windowssystem32cmd.exeC:_ Class Example { public static void main(String[ ] args) { String name=arg[0]; String addr =arg[1]; System.out.println(“name: ”+name); System.out.println(“addr : ”+addr); } } Compile Microsoft Windows [Version 10.0.10240] (c) 2015 Microsoft Corporation. All rights reserved. C:Userssaurav>C:Userssaurav> javac Example.java X[]- C:Userssaurav> Compiler generate Example.class file in same directoryC:Userssaurav> java Example sherlock NYsherlock NY name :sherlock addr : NY
  • 10.
    HOW JVM EXECUTETHIS “EXAMPLE1” JAVA CLASS Class Example1 { static int x=10; static int y=20; int a=30; int b=40; public static void main(String[ ] args) { int p=50; int q=60; Eample1 e1=Example(); } } Example1.java
  • 11.
    J V M Virtual Machine Java JVMSystem based virtual machine  VirtualBox  VMWare Process based virtual machine  JVM  PVM Runtime instance
  • 12.
    C:> java Example1 MethodArea JVM Heap Area JAVA Stack Area Programm Counter Register Area Native method Area Class data Class data Class data Class data Class data Class data Class data object object object object object object object object object Class data Class data Class data Class data Class data Class data Class data Class data Class data object object object object object Runtime area One program counter per Thread Responsible For native method execution
  • 13.
    C:> java Example1 MethodArea JVM Heap Area JAVA Stack Area Programm Counter Register Area Native method Area Class Loader
  • 14.
    Jdk/jre/lib/ex/*.jar Class Loader Application ClassLoader Extension Class Loader Bootstrap Class Loader Searching in Jdk/jre/lib/rt.jar If found load class to method area Searching in If found load class to method area If not found If not found Class path systemSearching in If found load class to method area If not found returns ClassNotFoundException Using “Parent Delegation Algorithm”
  • 15.
    Method Area HeapArea JAVA Stack Area Programm Counter Register Area Native Area Class Loader If found, then load this class in method area by creating java.lang.Class Object x 0 y 0 a b main(s[]){ } x = 10 y = 20 10 20p s v main(S args[]) args1010 p50 q60 length 0 1010 e12020 2020 a 30 b 40 Static variable Linking & Execute native methods uisng JNI
  • 16.
    REFERENCES “Inside the JavaVirtual Machine”, Bill Venners “JVM Architecture with SCJP”, Harikrishna sir www.nataraj.in
  • 17.