Java Virtual Machine (JVM)

JVM stands for Java Virtual Machine. JVM is just a specification which specify about how the things are working. The word called JVM itself consists with a term called Virtual. That means it is some kind of machine that does not exists physically. That means we can not install or uninstall JVM into our machine.

We can categorize Virtual Machines (VMs) in to two Parts.

  1. System-Based Virtual Machines:

These VMs can have one or more physical hardware to provide an environment for run the application.

Ex: Hypervisor, Xen

2. Application-Based Virtual Machines:

These VMs does not have any kind of hardware to run its application. Instead, it has software application to run the applications or processes or programs. Therefore, this kind of VMs called Process-based virtual machines.

EX: JVM, CLR (Common Language Runtime), PVM (Parrot Virtual Machine)

What is JRE?

JRE stands for Java Run-time Environment. JRE is a software and itself consists with class libraries and resources which help to create JVM instances.

That means, when we install JRE in our machines, it deploys codes to create and run JVM instances by based on its operating system.

For an example, If we install JRE on your windows machine, it deploys all the codes to create JVM instances on your windows environment.

If we install JRE on your Mac environment, it deploys all the codes to create JVM instances on your Mac operating system.

Does JVM instance always reside in your computer?

Actually it is not. JVM generates whenever you execute your Java program and it destroys whenever your Java program exit from its execution.

When this JVM instance generates we tell that a non-daemon thread is generated. Also, we called this thread as “Main thread”.

  • Daemon Thread: This is a low priority thread which provide services for non-daemon thread. They are running on background by performing the tasks like Garbage collection.
  • Non-Daemon Thread: This is the main thread created my JVM. The non-daemon thread can creates another number of non-daemon threads until it specifies as daemon thread.

That means when the non-daemon thread does not exists, the daemon thread which provide services for that also does not exist.

For an example, we can say that the Faculty (In this case Faculty is a daemon thread) provides services for Students (In this case Student is non-daemon thread). But, if the students do not get any service from the Faculty, then there is no reason to keep this faculty any more.

Major Ways JVM Gets Destroy

There are 2 ways which can be used by JVM to get destroyed.

  1. When the non-daemon thread exit, the created JVM instance will be destroyed.

2. If the java program called exit();, then also created JVM will be destroyed.

How Java becomes Platform Independent

Although Java becomes platform independent, JRE is highly platform dependent. Let’s see how this happens.

When we compile our Java program, JRE asks from operation system to create a Java instance which take care of conversion of source code in to byte code.

In here, what JVM does is it reads the compiled class file of particular Java program and convert that in to the language (machine code) which can be understood by your operating system. That is how java becomes platform independent.

Hope you may get some basic understand JVM and I will discuss about what exactly happens inside the JVM by my next article in more detailed manner.

Stay Safe !!!

Associate Software Engineer at Virtusa.