The JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java byte code can be executed. It allows a system to run a program. The three notions of JVM are specification, implementation and instance.
JVMs are available for many hardware and software platforms (i.e.JVM is platform dependent). We will learn more about JVM in this chapter.
A specification where the working of Java Virtual Machine is specified, but the implementation provider is allowed to choose the algorithm independently. Its implementation has been provided by Sun micro system and other companies.
An implementation is known as JRE (Java Runtime Environment) is used here.
Runtime Instance : Whenever you write a java command on the command prompt to run the java class, an instance of JVM is created.
The JVM performs following operations:
Loads code
Verifies code
Executes code
Provides runtime environment
Memory area
Class file format
Register set
Garbage-collected heap
Fatal error reporting etc
Let's understand the internal architecture of JVM. It contains classloader, memory area, execution engine etc. Study the diagram below for more clarity:
Class loader :
Class loader is a subsystem of JVM that is used to load class files.
Class (Method) Area :
Class (Method) Area stores per-class structures, such as the runtime constant pool, field and method data, the code for methods.
Heap :
It is the runtime data area in which objects are allocated.
Stack:
Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
Program Counter Register:
It contains the address of the Java virtual machine instruction currently being executed.
Native Method Stack:
It contains all the native methods used in the application.
Execution Engine:
It contains :
A virtual processor
Interpreter: Which reads byte code stream then executes the instructions
Just-In-Time (JIT) compiler: It is used to improve the performance of the program. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here, the term compiler refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
At JVM start up, JVM occupies the same memory from operating system to run the java programs. Objects in java are stored in what is called as the heap, which when full, activates the garbage collection. During the garbage collection process, only the objects which are no longer in use are cleared. This opens up more memory space.
To understand JVM memory better, you need to know that it is divided into two parts:
Stack memory
Stack memory is used for static memory allocation which stores local variables and function calls. It is a private memory and cannot be viewed by everyone.
Heap memory
Heap memory stores objects in Java and global.
Whenever you run the program, JVM allocates the memory for variables used in the program and objects created in the program. It is a public memory and can be viewed by anyone.
package com.javabykiran.MemMgmt;
public class Hello {
int a;
int b;
static int y = 9;
{
System.out.println("Hello-I.B.");
}
static {
System.out.println("Hello-S.B.");
}
Hello(int x) {
System.out.println("Hello-1 arg const");
a = x;
b = x;
}
// Hello(int x, int y) {
// System.out.println("Hello-2 arg const");
// a=x;
// b=y;
// }
Hello(int x, int y) {
System.out.println("Hello=2 arg const");
a = x;
b = y;
}
void show() {
int x = 111;
int y = 222;
System.out.println(a);
System.out.println(b);
System.out.println(this.y);
System.out.println(x);
System.out.println(y);
}
}
package com.javabykiran.MemMgmt;
public class Lab12 {
int x = 10;
static int y = 20;
{
System.out.println("Lab12=I.B.");
}
static {
System.out.println("Lab12-S.B.");
}
public static void main(String[] args) {
Hello h1 = new Hello(100);
h1.show();
Hello h2 = new Hello(100, 200);
h2.show();
} // end main
} // end class
{
System.out.println("Lab12-I.B.");
}
static{
System.out.println ("Lab12-S.B");
}
main(-) {
Hello h1=new Hello(100) h1.show();
Hello h2=new Hello(100,200) h2.show();
}
Stack of Hello class{
System.out.println ("Hello-I.B."); }
static { sop("Hello-S.B."); }
Hello(int x) {---}
Hello(int x, int y)
{----}
void show() {--}
Stack memory Heap memory Stack of Lab12 class
Default objects will be created, which will be type java.lang.Class
Stacks will be constructed with all the block members of the class
Memory will be allocated for static variables of the class
Static blocks will be executed
Memory will be allocated for the reference variables
Memory will be allocated for the instance variables
Instance blocks will be executed
The appropriate constructor will be called
Newly created object address will be assigned to reference variables.
package com.javabykiran.MemMgmt;
class Hi {
int a = 88;
static int b = 99;
Hi() {
System.out.println(" in const");
}
void m1() {
System.out.println(a);
System.out.println(b);
}
static void m2() {
// System.out.println(a); //non static variable
System.out.println(b);
}
{
System.out.println("Hello-I.B.");
System.out.println(a);
System.out.println(b);
}
static {
System.out.println("Hello-S.B.");
//System.out.println(a); // 'a' not allowed as static can not accomodate non-static
System.out.println(b); //OK
}
}
package com.javabykiran.MemMgmt;
class Lab13 {
public static void main(String[] args) {
Hi h = new Hi();
h.m1();
h.m2();
}
}
Output:
Hello-S.B.
Hello-I.B.
99
in const
88
99
99
When you invoke a method by passing primitive as parameter, it is called call by value mechanism
In case call by value happens, modifications that have happened in the method will not be reflected to the caller of the method
When you invoke a method by passing object as parameters, it is called a call by reflected mechanism
In case of call by reference, modifications that have happened in the method will be reflected to the caller of the method
class Hui {
int x=10;
}
class Hello {
void m1(int p) {
System.out.println ("m1 Begin");
System.out.println (p);
p=p+10;
System.out.println (p);
System.out.println ("m1 end')
}
void m2(Hui hui) {
System.out.println ("m2 begin");
System.out.println (hui.x);
hui.x=hui.x+10;
System.out.println (hui.x);
System.out.println ("m2 end");
}
}
class Lab14 {
public static void main (String as[]) {
Hello h=new Hello();
int a=100;
System.out.println(a);
h.m1(a);
System.out.println(a);
Hui ha=new Hui();
System.out.println(ha.x);
h.m2(ha);
System.out.println(ha.x);
}
}
Output:
100
m1 Begin
100
110
m1 end
100
10
m2 begin
10
20
m2 end
20
Ans. Yes, you can overloaded it.
Ans. JVM always calls the main method which is given in the following example.
public static void main (String as[])
class Lab15 {
public static void main (String as[]) {
System.out.println ("main()");
}
public static void main(int x) {
System.out.println ("main(int x)");
}
public static void main(String as) {
System.out.println ("main(string as)");
main();
main(10);
main("jbk");
}
}
Ans. Yes, you can.
You may call not only the main method, but any method inside the same method, which is called exclusive calling. Recursive calling must be terminated at the same point with the same condition otherwise you will get a stack overflow error.
class Hello
{
int fact(int n) {
if(n==0) {
return 1;
} else {
int x=n*fact(n-1);
return x; }
}
}
class Lab16 {
public static void main (String as[]) {
Hello h=new Hello();
int a=IntegerparseInt(as[0]);
if(a>=0) {
int f=h.fact(a);
}
else {
System.out.println ("plz enter positive number");
}
}
}
Ans. The main () invocation will depend on the class name which you are passing command line.
For Example: When you write
Java Hello- Main of Hello class will be called Java Hui- Main at Hui class will be called.
When you try to use the class, the JVM loads class into main memory.
We can use the class's code in two ways:Hello h=new Hello ();
Ans. NO → Error Modifier static is not allowed here.
Ans. NO → Hello.a will only access static variables.
Error non-static variable cannot be referenced from a static can text.