Everything you need to know about Java fundamentals(JVM, JDK, JRE)

Kavindaperera
12 min readFeb 17, 2022

--

What exactly is Java?

Java is a high-level programming language that supports class-based, object-oriented programing that ensures developers with few implementation dependencies by using concrete classes or abstract classes.

What is Java’s history?

The following includes some significant key points in Java’s history:

  • Java initially was designed for interactive television, but the language was too powerful compared to the digital cable industry at the time. As a result, it was a failure.
  • James Gosling, also known as the father of Java, developed Java in 1995.
    The project was initiated by java team members, also known as the green team, to develop a new language for digital devices.
  • In the beginning, Java was named GREEN TALK by James Gosling, and the file extension was .gt.
  • Later on, the “GREEN TALK” was renamed Oak. (due to the fact Oak was considered a strength and chosen as a national tree for many countries)
    0ak was once again renamed Java around 1995(Due to trademark by Oak Technologies).
  • James Gosling and his team members (Green Team) Came across multiple names like Silk, Jolt, DNA, but the majority preferred the name java due to the uniqueness of the word.
  • James Gosling Came across with the name “Java” when he had a coffee in a shop called Java.
  • Later, in 2009, Oracle Corporation acquired Sun Microsystems and took ownership of three critical Sun software assets: Java, MySQL, and Solaris.
  • The first JDK1.0 was released later in 1996, on January 23rd; from that point onwards new version of JDK was released with additional features and debugs.
(History of Java — Docukits, 2022)

What java is used for?

  • It is used to develop Android Apps.
  • It is used to create Enterprise Software.
  • It is used to create web applications.
  • It is used for scientific computing applications.
  • It is also used for server-side technologies like Apache.
  • It is used for hardware programming devices and many more.

What exactly is Virtual Machine?

Virtual means not in reality (Something that does not exist) — Ex Classroom, Virtual shops, etc.

The machine is something like a device used to perform a specific task. As a result, a virtual machine is used to perform a particular job but does not exist in the real world.

The virtual machine(VM) is similar to any physical computer like a smartphone or laptop. VM is often considered a virtual computer that exists only as a bunch of code.

How Does Virtual Machine Work?

The virtual machine is constructed through virtualization technology. VM uses software or a bunch of code to simulate the VM hardware allowing multiple instances of VM to be run on a single machine.

The process is managed by a specific software known as a hypervisor. This software is responsible for managing and provisioning resources similar to memory or storage. Additionally, this software is also responsible for scheduling operations for VM to avoid overrun.

VM instances, aka guests, will be run on physical devices known as the primary host.

A java virtual machine can be one of two sorts.

  • SVM( System based virtual machine)
  • AVM (application-based virtual machine)

SVM(System based virtual machine)

This type of virtual machine includes hardware and create an environment to work for multiple users/ instances ex: Hypervisor, Xen. SVM includes one more hardware, but it establishes numerous environments to work. SVM is entirely environment independent.

SVM is designed as a substitution for physical computers. They run on the host machine (physical machine) and use its hardware resources.

AVM(Application-based virtual machine)

AVM is a single process to operate as an application on the host machine without hardware components. They are also known as a process-based virtual machines. AVM doesn’t include any hardware device. You may have an application platform to run other programs ex JVM, CLR, PVM.

Key Points

  • No hardware involved
  • Create an environment to run the application (get some inputs /languages and convert into different output/language); since it is an application, it is also known as the programming process.

WHAT IS JVM(JAVA VIRTUAL MACHINE)?

JVM is a complete specification that specifies how work should be carried out. JVM comes alongside when you install JRE (Java Runtime Environment). When you install JRE, it will install the codes that can create JVM.

Ex: If you install JRE in windows, it will create all the codes necessary to create a JVM on a windows machine
Even if java is independent, the JRE is tightly packed from dependent (When you install JRE, it will deploy all the codes to create JVM)

When you execute a java program, it will create a JVM instance. JVM will take care of converting the byte code to machine code. (Languages the operating system understands)

If you are not executing any program on the computer in that time frame, we don’t have any JVM instances (only JDK and JRE). Once you start the java program, it will create a JVM instance on that computer. The moment the computer exists, the JVM instance will also die.

JVM will only exist on the computer until we run the program. Executing five different programs at the same time will result in 5 different JVM instances. (Program doesn’t share JVM) when all the programs exist, the relevant JVM also exists.

Ex: Java hello.java
Java- is responsible for initiating a JVM instance by asking the operating system

When a JVM instance is created, it creates a non-demon thread. The relevant JVM will take public static void main (String [] args) to execute. From there onwards, you drive your program through this method.

JVM Exists when

  • All the non-demon threads created are closed(Doesn’t exist)- the JVM instance dies.
  • The application can suicide (it can call the system to exist method). Then JVM dies.

KEY POINTS OF JVM

  • Java bytecode is the machine language for java virtual machines (JVM).
  • The JVM converts the compiled binary byte code into a specific machine language.
  • The JVM machine acts as a subpart of the java runtime environment(JRE).
  • JVM Combined with the java APIs to make a java platform. The essential; use of JVM is a hotspot
    JVM is considered an abstract machine that can work on top of existing processes.
  • Java programs are first compiled into java byte code, aka binary form, and then a unique java interpreter is interested in a specific platform.

WHAT INSIDE JVM?

The three major components inside JVM are

  • Class Loader
  • Memory Area
  • Execution Engine

How Memory Area is Divided?

Size depend on each virtual machine implementation .

Method Area / Heap Area — Load all the class information used to keep type information about the classes. (objects will be presented here, also known as instances of data)For every JVM, you can only have one method area and head area. This is a thread memory area. In other words, the details which are stored here are not thread-safe. One of the exceptions that occur due to the JVM cannot allocate an object in the heap area is the "OutOfMemoryError" exception

Stacks — Will keep the method information; otherwise, local variables those types of things. Will keep the method information; otherwise, local variables are those types of things. A separate runtime stack is created for every new thread, aka Run-time stack. For every method is called upon, all the necessary details are stored until the completion of the method, where all the details are removed from the stack.

Note — When you create a thread, it will go to the method and create a 1–1 frame. In the stack / pc registers, it will create per thread.

Pc Register: If it is not a native method, pc registers will hold the information about the next execution. In other words, it stores the address of the currently executing JVM instruction. A separate PC(program counter ) register is created for every single thread, which holds the current execution address. This memory area is relatively small and up to fixed size.

Native Method Stack —A thread particularly invokes this kind of memory, and the invoked thread is at a whole new level where security and structure and security restrictions implied by JVM are no longer in exercise. Compared to other memory areas, this memory has no fixed size or any other limitation such as in increment or decrement.

Data types in JVM ?

JVM data type

  • Primitive data types
  • Reference data types

The difference is that Primitive data types hold the value itself, whereas the reference data type holds the reference ( containing the reference to the value ).

Boolean is always represented by INT or byte by JVM. Boolean false is 0, and if it is true, it is non zero. The size of the data type is the same in every environment. The only difference is long. Long always take sixty four-bit 2’s complement as its size.

One of the primitive data types is the return address type(Developers do not get access to that ). This data type is specially dedicated to JVM.

JVM implements the final keyword by using the final block.

Word size is just a base (Unable to measure) unless we assign

some value.

The particular implementation decides the length of the word size, but it consists of 2 rules to define the length of the word size

  • The particular word should be able to hold only primitive data types.
  • Two words should be able to carry long or double values.

The word should be at least the length of 32 bit.

Java stack is used to hold the variables.

CLASS LOADER ?

class Loader has three major responsibilities

  • Loader
  • Linking
  • initialization

Class Loader: Main responsibility is taking the class and loading into the memory (We can find two class loaders in java). One is JVM(bootstrap class loader). The second one is custom-defined class loaders.

Loading — The main job — Takes your class file and put it into memory. It requires you to do additional things. Class loader takes fully qualified class name (.package) Read all the qualified class name, instance variable information, immediate parent information, and whether it s an enum or interface. (Every class, when loaded JVM, does a particular thing. It creates an object from the class type — not — note from our class type, but it is a special class type. It reads this information and creates an object from class type. (Only one object per class would create)

This phase loads files from secondary memory into the main memory(ram) for execution. The class loader is responsible for reading the .class file, generating the corresponding binary data and saving it in the method area. The JVM stores information for each .class file in the method area, and this information is regarding about
the fully qualified name of the loaded class and its immediate parents
Modifier, method information and variables etc.
Wether .class file is related to interface or enum or class.

After the necessary loading of the .class file is completed, the JVM creates an object of type class(special class) to represent this file in the heap memory. Now the programmer is capable of receiving the class level information by using this class such as class name, parent name, method name and variable information

Linking — Divided into three parts

  • Verification
  • Preparation
  • Resolution

Verification — Cant crack it. IT has a byte code verifier in the JVM. From there, it will check whether the particular class is safe to execute or not(That’s why java is safe to execute in any environment ). JVM can make sure it is safe to execute.

How does it work? — when you try to load the Java class, there is a bytecode verifier program. It will verify whether it comes from a valid compiler, whether it has the correct structure, whether this class file has correct formatting and if any of these types are not satisfied JVM through a run time exception called verify exception

Suppose you see a verifier exception indicating that your class file is altered somewhere. It checks the compiler whether it is a valid compiler, who created this class file if the class file is in the correct format and correct structure. If everything is fine, then the verification process is completed.

Preparation: If you use an instance level variable or static variable in your class, the preparation part will assign a default value for that, but this is the default value, not the initial value. (static int a = this process will assign zero for that but not for the thing .)there are default values for the variable. If it is an object, then null. If it is Boolean false, int zero. preparation part assign default values for your variables

Business object /domain-specific object — Java allows you to use domain-specific programs. When you take from the JVM level. The JVM doesn’t understand who the customer is; before it reaches the machine level, JVM replaces these symbolic links with direct links.

Ex: Student s = new Student();

Resolution Part: JVM replaces students with a specific memory location with a newly created student object.

Initialization — It will assign actual values. It will also execute static blocks. In other words, this is responsible for assigning all the static variables with their specific values in the block of the program. This execution occurs from top to bottom in the class and from parent to child hierarchy.

JVM is always flexible for implementation. They do these phases sequentially or parallelly.

JVM enforce to the implementation that initialization must be done before each active class use (Linking, preparation) whatever the order can be done but must initialize before the active user.

1- New keyword(active use/active classes)
2. Invoke static method(verify employee())
3. Assign values for the static field (Ex. Employee. code = k)
this is also considered as active use. if the variable is the final step3 (even if you read), the class will not consider as active use — no initialization
if it is final filed(called it as content) that is working in compilation level, then no required to create an object
4. if your class is initial class — which have a main method — active use(JVM must create object)
5. Reflection method API — active user ex: (GetInstance)
6. Instance subclass

JVM has a rule before any of this active use it must create an instance from the object(it should go through initialization phase) other considered as passive use, which does initialize.

JAVA ARGUMENTS AND JAVA CLASS PATH?

Java is incapable of loading two different classes with the same class name. IF you give two types but from the same non-identical name, JVM will load one class entry. The class loader will always take a unique class name.

IF two identical class names are in the same classpath, JVM considers what comes first(the first class in the JVM classpath will be executed, but this is completely different when we give JVM arguments (parameters) to execute., IF you give arguments, JVM takes the last one.

When you compile this class, the compiler creates a new method, but that method doesn’t have any name.

It you put the instances level variables into this method, and also if there is any use of static variable, it will go to this method. If a particular class has no instance level variable, you will not have this method.

IF there is a particular class with no instance variable or static variable, what is found inside this method?
You will have no method because the compiler will create this only and only if you have one instance variable or one static variable.

Initialization Process

Four ways java can initialize its class

  • New keyword(will generate new instance)
  • Clone method(from existing class)
  • Reflection API(use methods to gain instances)
  • Io.objectINputStream() // you can call and get object methods also create new objects

They assign an initial value to the variable instance variable is different based on how they begin to create. Ex: clone method to create an object(then it will get the parent object value to assign the initial values if you use objInputstream method it will assign values from object stream for all the non-transient variables

NOTE- If you create a private constructor in the human class, you create other constructors without a default constructor. Then you would result in a compile error

Ex: Employee(int id) //constructor with one argument

When you compile this code JVM create new method called init() for each of your constructor

What is inside the init() Method depends on how the class structure is

REFERENCES

What is JVM — Java Virtual Machine. 2016. [video] Directed by K. Dinesh. Youtube: Krish Dinesh.

JVM Part 02 — Inside Java Virtual Machine. 2016. [video] Directed by K. Dinesh. Youtube: Krish Dinesh.

JVM part 03 — Data Types in JVM. 2016. [video] Directed by K. Dinesh. Youtube: Krish Dinesh.

JVM part 04 — ClassLoader. 2016. [video] Directed by K. Dinesh. Youtube: Krish Dinesh.

How Java Classloader and JVM Argument works. 2016. [video] Directed by K. Dinesh. Youtube: Krish Dinesh.

Machine, J., 2022. Java Virtual Machine | Various Components of Java Virtual Machine. [online] EDUCBA. Available at: <https://www.educba.com/java-virtual-machine/> [Accessed 10 February 2022].

2022. [online] Available at: <https://techvidvan.com/tutorials/java-virtual-machine/> [Accessed 10 February 2022].

Docukits.com. 2022. History of Java — Docukits. [online] Available at: <http://docukits.com/learning-core-java/history-of-java/> [Accessed 10 February 2022].

--

--