JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which java byte code can be executed. It is a process running in the machine virtually. The task of JVM is to provide the support to execute java application and to manage the resources that are required for the execution of the application.
When we install a java package, it contains 2 things, Java Runtime Environment (JRE), Java Development Kit (JDK). The JRE provides runtime support for Java applications. The JDK provides the Java Compiler and other development tools. The JDK includes the JRE. Both the JRE and the JDK include a Java Virtual Machine (JVM). This is the application that executes a Java program. A Java program requires a JVM to run on a particular platform. JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).
The JVM performs four main tasks:
Provides runtime environment
JVM provides definitions for the following:
Class file format
Fatal error reporting etc.
Internal Architecture of JVM Let's understand the internal architecture of JVM. It contains a classloader, memory area, execution engine, etc.
Classloader: Classloader is a subsystem of JVM that is used to load class files.
Class(Method) Area: Class (Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods.
Heap: It is the runtime data area in which objects are allocated.
Stack: Java Stack store frames. It holds local variables and partial results, and plays a part in method invocation and return. Each thread has a private JVM stack created at the same time as thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
Program Counter Register: It contains the address of the JVM instruction currently being executed.
Native Method Stack: It contains all the native methods used in the application.
Execution Engine: It contains:
A virtual processor
Interpreter: Read the bytecode stream then execute the instructions. It will be used to read the byte code, to verify that and execute that after converting it into machine code.
Just-In-Time(JIT) compiler: It is used to improve performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Compiler refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU. Because of the interpreter, the byte code will be verified line-by-line, instead of using an interpreter to read the byte code Sun has provided JIT compiler. It is the installer file by using that java software will be installed in your machine.
Questions and Answers
What is the difference between JDK and JRE? JDK is a bundle of software that you can use to develop Java-based software. JDK contains various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc. JRE is an implementation of the Java Virtual Machine which actually executes Java programs. JRE (Java Runtime Environment) It is the collection of java interpreter (JVM), native libraries, java libraries, etc. it will be used to provide some environment that will support to execute the java application. We can access the JRE in the file system.
Can the Java interpreter is used for the execution of the source code? Yes
On successful compilation, a file with the .class extension is created? Yes
Can the Java source code be created in a Notepad editor? Yes
The Java Program is enclosed in a class definition? Yes
What is a compilation unit? A compilation unit is a Java Source Code file.
Is Empty .java file a valid source file? Yes, an empty .java file is a perfectly valid source file.
Can a .java file contain more than one java class? Yes, a .java file contains more than one java class, provided at the most one of them is a public class.