Skip to main content

Java Virtual Machine(JVM)


Java Virtual Machine(JVM)

What is the meaning of Virtual?

Virtual means Not Reality. it doesn’t physically exist.

For example
A virtual classroom is a video conferencing tool that allows teachers and students to interact with one another and the learning content. Virtual classrooms differ from traditional video conferencing solutions in that they include additional capabilities that are necessary in a learning environment.

What is the meaning of Machine?

Machine means help for your work. then different machines help with different things.

Example -Car is a machine.

Then what is the meaning of Virtual Machine?

There is a machine but it's not real. it’s virtual.

There are two types of Virtual Machine

System-Based Virtual Machine(SVM)

  • A single physical machine that allows users to work in multiple virtual environments or instances. These environments are entirely independent of one another.
Example -Xen , Hyperviser

Application-Based Virtual Machine(AVM)

  • Application Based Virtual Machine does not involve any hardware device, but it needs some software or applications to create a virtual machine. we can also be called process Based Virtual Machine
Example- Java Virtual Machine(JVM) , Common Language Runtime(CLR),Parrot Virtual Machine (PVM)

Let’s try to understand what is the meaning of Java Virtual Machine?

Java Virtual Machine is Completely Specification. it says how should be done. Java Virtual Machine (JVM) is platform-independent Because every platform such as Windows, Mac OS and Linux has its own JVM. when you download Java Runtime Environment(JRE), Java Virtual Machine(JVM) comes to the JRE that will deploy all the necessary code to create a Java Virtual Machine(JVM). JRE is tightly platform-dependent.

When you are executing a java program then it will create a JVM instance in the computer. When your program will exit then the JVM instance also will die. then JVM instances exist in a computer until a program runs.

If you are not executing any program on your computer at that particular time frame, we don’t have a JVM instance on the Computer, only JDK and JRE are there.

Let’s see how to JVM instance to create

Now think you want to compile Hello world application. then you type javac HelloWorld.java. then it will create a java class file. when we want to execute the program you type terminal java HelloWorld and press enter, At the moment that type java keyword in the execution the JVM instance will create. the java instance is called from the Operating System by the java filename command.

When a JVM instance is called, it creates a non-daemon thread. I will discuss daemon-thread and non-daemon thread in this article. just keep in mind that. then your class java main method ‘public static void main’ is executed. keep in mind, that your class must have the main method and it should be ‘public’ and ‘void’.

The JVM start a non-demon thread also create. This demon thread is capable to create other non-demon threads.

There are two ways to destroy the JVM instance.
1. There’s no non-daemon thread that exists which means all the non-daemon threads are destroyed.
2. An application can do suicide, which means it can call the System exit method.

This is the life cycle for Java Virtual Machine.

Look at the JDK JRE And JVM Architecture

Let’s see what are the component inside the Java Virtual Machine(JVM)

There are three components in Java Virtual Machine.

  1. Class Loader
  2. Memory Area
  3. Execution Engine

JVM Architecture

Let’s see How Memory Area works

Method Area

When you load the class it will load all the class information into this method area.

Heap Area

when you load this class all objects will come to the Heap Area

Each and JVM have only method area and one Heap Area

Stack

The stack keeps the method information such as example-Local variable

PC Registry

The PC register will hold the information about the next execution. PC register will not hold the information about the non-native method.

Native Methods

A native method gives facilities to hold the native methods and other stuff.

Stack and PC register will hold the information per thread.

Let’s See about Class Loader

ClassLoader is the main responsibility of taking the class and load into the Memory Area.

ClassLoader has 3 phases

  1. Loading
  2. Linking
  3. Initialization.
✔ Loading

Loading is mainly responsible for that take class files and putting in into the memory. when did do that it’s doing several things,

- Fully qualified class name

-It reads about whether this is a class, an interface or an enum.

It reads about immediate parent information.

it reads about variable information.

Then it loads the class file to the memory area. JVM does a special thing, JVM creates an object from the class type.

✔ Linking

Linking is divided into three Steps

  1. Verification
  2. Preparation
  3. Resolution

Verification

What is the Verification means?

Java has a bytecode verifier in JVM. Where if there exactly check class file safety execute or not. Then JVM makes sure to execute safety execute. When you try to load the class, the bytecode verifier exactly checks whether the class is safe to execute or not. It means the bytecode verifier verifies, whether it is come from a valid compiler, whether it has a correct structure and whether this class file has correct formatting. If any of these areas are not satisfied and JVM throws a Runtime Exception called Verifier Exception. Everything is done then the verification process is completed. after then its moves to the preparation process.

Preparation

What is the meaning of Preparation?

If you use any instance variables or static variables in your class, this preparation part will assign a default value for that. Keep in mind These are the default value Not initialised values.

Think your class has a static int variable called age assign the value of 12. So in preparation, this will assign as 0 because the default value of int is 0.

Resolution

Resolution: In this phase, it will replace the symbolic link with the direct link.

Think, We have “Employee”, “Student”, “Customer” etc like objects. But the machine level does not understand those. It will consider all those objects as Business objects. They can called also Domain-specific objects. As JVM doesn’t understand who this Employee, Student, Customer etc, Then this JVM does before it reaches the machine level, JVM replaces those symbolic links with the direct link.

Example 2: Employee employee= new Employee();

So In the resolution part, it replaces this everywhere which you used Employee with the specific memory location reserved by this newly created Employee object.

✔ Initialization

Initialization assigns real value.

There is a rule, every class must be initialized before its Active Use.

What is the active use of the class?

1️. new keyword

Employee employee= new Employee();

2️. invoke static method- verify() is a static method

Employee.verify();

3️. assign values to the static field- that means to assign the static Field in Employee Class

Employee.code = 5;

4️. if the class is an initial class that has the main method.

5️. if using a reflection API to load the class-getInstance

6️. instance subclass

All these 6 cases are considered an active class in Java

If any of the above applies to the class, it should go through the initialization process. It is not required to go through the initialization process if it is not.

How to initialize a class?

  1. Use new keyword

2. Use clone() method

3. Use reflection API (get instance)

4. Use Io.objectinputstream class

How does Java go through with constructors and instantiate parent class?

without this keyword

“init()” contains code to call the default constructor to the init method, as well as code to initialize instance variables and bytecode for the specific implementation.

If this() was not specified in the Constructor, “init()” would contain code to invoke the parent class’s default constructor init method, as well as code to initialize instance variables finally, the bytecode for the specific implementation.

So, There are 3 kinds of codes in the init methods

  • Invoke some other Constructor init() method​
  • Initialize instance variable​
  • Byte code for a particular constructor​

Java Data Types in JVM

There are 2 data types in JVM

  1. Primitive Types
  2. References Types
Primitive Types

It holds the values of data. There are 3 types in primitive types

  1. Numeric
  2. Boolean
  3. Return Address

Numeric is divided into two types. There are Floating and Integral Types.

Again Floating is divided into Float and Double.

Integral is divided into five

  • byte
  • short
  • int
  • long
  • char

Return Address: this is a specific type to implement final.

Reference Types

It holds references to objects. There are 3 types in reference

  1. Class type reference
  2. Object type reference
  3. Array type reference.
  • Class type reference: Reference to an instance of the class.
  • Object type reference: Reference to class implementation.
  • Array type reference: Reference to Array.

Look at chart

What is Word Size?

Word Size is a just Base needed for assigned value. The length of a word size is decided by a particular implementation.

There are two rules about the word size

  1. A word should be able to hold any primitive data type
  2. Two words should be able to carry long or double value

According to Word size at least should be the length of 32bits.


I hope, in this article, you can get knowledge about Java Virtual Machine(JVM). I will cover Java in upcoming articles. Stay on with me.

Comments

Popular posts from this blog

How Object Behave?

  How Object Behave? Photo by Ellen Qin on  Unsplash Hello everyone, In This article, you will learn about how the state and how to relate to one another. We know about objects and variables. I give you a small reminder class that describes what an object knows and what an object does. A class is not an object. The object is used to create a class. It tells the JVM how to make an object of that particular type. Each object made from that class can have a value for the instance variable. Bates and Sierra, 2005 if you want to remind read my article first. links are here - A Trip to Objects and Classes , Know your Variables . It will help you. you already know that every object of that type can have a different instance variable. what about the method? Can objects of that type have different method behaviour? Every instance of a class has the same methods, but depending on the value of the instance variables, the methods can behave differently. Look at this example. Do...

The basics you need to know about Java Programming Language

  JAVA- Beginners need to know The basics you need to know about Java Programming Language Hello everyone, you are new to Java, This article is for you. This article great place to start learning JAVA. Let’s start What is the JAVA? Java is a Programming Language and platform-independent. It’s friendly syntax, object-oriented and has strong memory management and portability. The history of Java is very interesting. Read the history of Java by following the Link Click here —  History of Java . Platform : Any hardware or software environment in which a program runs is known as a platform. Java code can run on a variety of platforms, such as Windows, Linux, Sun Solaris, Mac OS, and others. You can understand more about it. Let’s see H ow the way Java Works Step 1 : Create a source code file. (with the .java extension) Source code means to hold on to class definition. class represent a piece of the program. the class has one or more methods. for example, In the student clas...

you better understand Java primitives and references

  Know Your Variables you better understand Java primitives and references Hello everyone, Today you will learn about what is primitives and references. Variables The variable is container holds the value while the Java program is executed. you have used variables in two places. The first one is the instance variable other one is the local variable. Later we will use variables as an argument and as a return type. you have seen the variable declare as a primitive type. Declaring the Variable Let’s see how to declare the variable. Variable must have the type and the name. Don’t mess up. you will understand it while you reading the article till the end. Let’s see what is the Primitive Datatypes The different sizes and values that can be stored in the variable are defined by data types. Data types have two types. primitive data types — These include boolean, char, byte, short, int, long, float and double. non-primitive data types — These include Classes, Interfaces, and Arrays. Data Ty...