What is JDK, JRE and JVM ??
We must know the differences between JDK, JRE and JVM before proceeding further in java.
What is JVM ??
JVM (Java Virtual Machine) is a abstract machine that enables the computer to run a Java program.
A bytecode is generated from the java code when the java compiler first compiles the java code. After that, the JVM translates the bytecode into a set of instructions that the CPU can understand and directly executes it.
Components of jvm are :
- Class Loaders
- Run-Time Data areas
- Exceution engine
During runtime, Class loaders are responsible for dynamically loading java classes to the JVM (Java Virtual Machine) and they are the part of JRE (Java Runtime Environment). It is due to class loaders that in order to run java code the JVM doesn’t need to know about the underlying files or file systems. Class loaders are responsible for loading classes into memory because these Java classes aren’t loaded into memory all at once, but when required by an application.
Run-Time Data areas:
To execute a Java program different memory areas are defined by the JVM known as run-time data areas and are used during runtime. Some of these areas are created when JVM starts and destroyed when JVM exits while when a thread is created some of these areas are created and when a thread exits they are destroyed.
Following are the different runtime data areas :
Method area is storage area for compiled code. It stores structures such as field, method-data, constructors, code for methods and class names. For each and every class these structure is stored by the JVM . This memory area is shared by all the threads present in the JVM.
It is the area where memory for all the arrays and class instances are allocated by the JVM. The heap memory for objects is reclaimed by the Garbage Collector (GC).
As soon as JVM starts, it creates heap area. This area is shared by all the threads present in the JVM. For heap area the memory does not need to be together in sequence.
Stack area is the area where each frame stores nested method calls, partial results and local variables and data is stored as frames. JVM creates the stack area whenever new thread is created. This area is private for each thread.
In JVM stack, the memory does not need to be in a sequence.
The PC Register which stores the address of the currently executing instruction is a part of JVM thread. This value is not defined if the currently executing instruction is a part of the native method.
Execution engine executes the instructions using information present in the memory areas.
It has following three parts:
Once classloaders load and verify bytecode, the interpreter executes the bytecode line by line. This execution is quite slow. The disadvantage of the interpreter is that when one method is called multiple times, every time new interpretation is required.
Just-In-Time (JIT) Compiler:
It is responsible for the optimization of the Java programs. During run-time it compiles the bytecode of the often-called methods into native code.
JVM automatically monitors which methods are being executed. It schedules for compilation into machine code when a method becomes eligible for JIT compilation. A different JVM thread is used for the compilation into machine code.
It does not interrupt the execution and runs the java code faster after the compilation is completed.
Garbage Collector:Garbage Collection feature helps Java to takes care of memory management. By looking at heap memory, it identifies which objects are being used and which are not being used and then deletes the objects which are unused.
Garbage collector is a "daemon thread". System.gc() method can be used to call it explicitly. However, it won’t be executed immediately, JVM decides when to invoke GC.
Java Native Method Interface:
It acts as an interface between the native (C/C++) libraries and the Java code.
What is JRE ??
JRE (Java Runtime Environment) is a software package that provides Java class libraries, along with Java Virtual Machine (JVM), and other components to run applications written in Java programming. It is used to provide the runtime environment.
Core components of the JRE include:
- An implementation of a Java Virtual Machine (JVM)
- Classes required to run the Java programs
What is JDK ??
JDK is also known as the Java Development Kit. JDK (Java Development Kit) is a software development kit to develop applications in Java. Whenever we download JDK, JRE is also downloaded so there is no need to download it separately. In addition to JRE, JDK also contains a number of development tools like Java debugger, compilers, JavaDoc etc .
Core components of the JDK include:
- Development Tools
Some of the development tools that we need for compiling, debugging, archiving, generating Javadocs includes: