JAVA Virtual Machine

Harshana Samarasinghe
CodeX
Published in
5 min readJun 16, 2022

Virtual is not in reality, so it is a simulation of an environment that appears to be real to the user.

Virtual machines are computer architectures that give the same functionality as a physical computer. Specialized hardware, software, or a combination of both may be used in their implementations.

A Java virtual machine is a virtual machine that allows a computer to run Java applications as well as other languages that have been compiled to Java bytecode.

In the Virtual Machines we can describe it as two parts;

1.SVM — System Based Virtual Machine

SVM contains real hardware that creates an environment that allows numerous users or instances to operate at the same time. Because there are several environments, each one is self-contained.

Ex-: VMWare, Xen.

2.AVM — Application Based Virtual Machine

There is no physical hardware in these Virtual Machines, unlike SVMs. Instead, it has a practical use. This application aids in the creation of a platform that allows other apps to operate. This platform takes some inputs and converts them into a variety of outputs or a language that everybody can comprehend. Approach Based Virtual Machine is the name given to it as a result of this process.

Ex:- JVM, PVM

This Application Based Virtual Machine includes JVM. It is a specification that explains how things should be done.

We can’t get this JVM directly from the internet since it doesn’t exist in the real world, but it comes with the JAVA Runtime Environment(JRE), which we can readily get from the internet. When we install the JRE on our computer, it installs the essential code to set up the JVM.

Platform independence is a feature of JAVA, however platform dependency is a feature of JRE.

When we launch a JAVA program, the JVM reads the class files and converts them into a language that the operating system understands.

Creating of JVM

When we start a program, it creates a JVM instance on the computer, and when we exit the application, the JVM instance dies.

When we execute two or more applications at the same time, each of them has its own Java Virtual Machine (JVM).

It creates a non-demon thread once the JVM instance is created. A public static main method exists in every Java application. This previously constructed JVM instance continues to execute the Java application using this main function. We now have a demon thread with the ability to create non-demon threads.

Ways of destroy the JVM instance

If there are no non-demon threads in the program, it signifies that we have killed all of the non-demon threads we have produced. After that, the JVM instance will expire on its own.

The system exit mechanism is invoked.

JDK — Java Development Kit

JVM is a component of the Java Runtime Environment (JRE). There are three key components in this JVM.

  • Class Loader — load
  • Memory Area — store
  • Execution Engine — execute

Memory has 5 main parts;

Method Area — When loading in a JVM, this loads the class information.

Heap Area — After loading in a JVM This is where all of the object data is collected..

Stack — It maintains the method information as well as the local variables in a thread.

PC register — If a non-native method is executed, this will save information about the next execution in a thread.

Native Method area — This allows native methods to be stored in a thread.

JVM data types

Types in Java are divided into two categories;

  • primitive types

Ex-: boolean , byte , char , short , int , long , float and double

  • reference types

Ex-:classes, which specify the types of objects and all others.

Word Size

In the JVM, a word size is 32 bits long. There are just a few primitive data types in the JVM: byte (8 bits), short (16 bits), int (32 bits), long (64 bits), float (32 bits), double (64 bits), and char (32 bits) (16 bits).

Class Loader

Class loaders are in charge of dynamically loading Java classes into the JVM at runtime. They’re also included in the JRE (Java Runtime Environment). As a result, owing to class loaders, the JVM doesn’t need to know about the underlying files or file systems in order to run Java programs.

There are 4 ways which java initialize a class

Use a new keyword — To assign a value to each variable.

Use clone method from existing object — To duplicate an existing object.

Use reflection API (getInstance() method) — After you complete the setup, the variables will be assigned an initial value.

IO.ObjectInputStream() with this class and can get an object — Assign a value for the non-transient variable from the input stream.

How java go through constructor and instantiating parent class

When this code is compiled, the JVM generates an init() function for each constructor. What happens inside the constructor determines(depends) what happens inside the init() procedure. If the constructor(child class) contains nothing, the init method has the authority to call the default constructor of the parent class(human).

This init function might include three different types of code.

  • Invoke some other constructors init method
  • Initialize instance variables.
  • has bytecodes to particular implementation

JVM Arguments/ JAVA Classpath

JVM arguments are flags that are passed to the Java Virtual Machine at the time the application is launched. On Linux or Mac machines, they can be provided through the JAVA_OPTS setting in the whd.conf file.

output — prints the content in a/Application.java

If we change the class path by putting Application.java in the “b” directory and then putting Application.java in the “a” directory, we will get the desired result.

Conclusion

If we have two identical classes on the classpath, JVM takes the first one into account. The first class in the classpath is the one that the JVM will run. When we are provided JVM Arguments, this changes; in such cases, JVM takes the final class on the class path.

--

--

Harshana Samarasinghe
CodeX
Writer for

“Truth can only be found in one place: the code.” - Associate Engineer — Java Technology at Virtusa Sri Lanka -