The following figure shows the execution process of a Java program i.e. “HelloWorld.java”.
Source File
Java programs are initially written in plain text using a text editor or an Integrated Development Environment (IDE).
The source code is saved with a .java extension.
Java Compiler
The Java compiler (javac) translates the human-readable Java source code into an intermediate form called bytecode.
Bytecode is a set of instructions for the Java Virtual Machine (JVM).
Class File
A class file contains the bytecode that is generated by the compiler.
Further, this class file can be executed by any machine that supports the Java Runtime Environment (JRE).
2. Architecture of Java
There are 03 major components of Java Architecture.
Java Development Kit (JDK)
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
The below figure shows the relationship b/w JDK, JRE, and JVM.
Note: Code written in programming languages such as C and C++ is compiled into OS-specific machine code. So these kinds of programming languages are called compiled languages.
Note: Code written in programming languages like JavaScript or Python are executed directly without a compilation. These are known as interpreted languages.
What is special about Java?
Java is unique in that it combines compilation and interpretation techniques. Thus, in Java, source code is first compiled into a bytecode class file. The Interpreter or JIT compiler is then used to run this class file.
Java is both Compiler and Interpreter.
Let’s explore the working process of JDK, JRE, and JVM in brief.
2.1. Java Virtual Machine (JVM)
The JVM is a pivotal component of the Java Runtime Environment (JRE).
JVM acts as an abstract computing machine that enables computers to run Java programs as well as programs written in other languages that are also compiled into Java bytecode.
The JVM is platform-independent at the bytecode level, which means that Java programs can be executed on any device equipped with a JVM that complies with the Java Virtual Machine Specification.
First, the Java compiler compiles a Java file into a Java .class file. Then, the .class file works as an input into the JVM, which loads and executes the .class file.
The below figure shows the architecture of JVM.
Note: However, JVM doesn’t come as a separate installation and it’s not something you can download and install. JVM always comes with JDK or JRE.
Sub-Components of JVM
The major sub-components of JVM are:
1. The ClassLoader System
Function: Responsible for loading class files into the JVM. It reads the .class files (compiled Java bytecode), loads them into the runtime memory, and organizes them into a Java object.
Process: It works in three phases: loading, linking, and initialization.
Loading: finds and imports the binary data for a type.
Linking: Performs byte code verification, preparation, and (optionally) resolution.
Initialization: invokes Java code that initializes class variables to their proper starting values.
Types: There are various class loaders like Bootstrap, Extension, and Application/System class loaders.
2. Runtime Data Areas
Heap: The runtime data area where the memory is allocated to objects. It’s shared among all threads.
Method Area: Stores per-class structures such as the runtime constant pool, field and method data, and the code for methods and constructors.
Stack Area: Each thread has its stack that stores frames. A stack frame contains local variables, an operand stack, and a reference to the runtime constant pool.
Program Counter (PC) Register: Holds the address of the JVM instruction currently being executed.
Native Method Stack: Specific for non-Java (native) methods used by the Execution Engine.
3. Execution Engine
Interpreter: Reads bytecode stream and then executes the instructions step by step.
Just-In-Time (JIT) Compiler: Improves the performance of Java applications by compiling bytecode into native machine code at runtime.
Garbage Collector: Automates memory management by reclaiming memory used by objects that are no longer in use.
Runtime Libraries: Provides an interface to perform various tasks, including those related to the Java API.
4. Native Interface and Libraries
Java Native Interface (JNI): A framework that allows Java code running in a JVM to call and be called by native applications and libraries written in other languages like C, C++, and assembly.
Native Libraries: Platform-specific libraries (like graphical, network, or file system libraries) that are necessary for the execution of native methods.
Key Characteristics of JVM
Platform Independence: Java programs can run on any platform that has a compatible JVM.
Performance: Modern JVMs are highly optimized and include JIT compilers to increase the speed of execution.
Security: The JVM provides a secure execution environment through a class loader and bytecode verifier.
Robustness: JVM handles memory management and garbage collection, which helps in preventing memory leaks and other related issues.
Is JVM platform independent?
The answer is “NO”.
Because when you install JRE on a Windows OS, JRE will deploy the code necessary to create JVM instances for a Windows OS.
If you install JRE on a Linux-based PC it will only deploy the code necessary to create JVM instances for a Linux.
So JVM is platform-dependent.
2.2. Java Runtime Environment(JRE)
The JRE is an integral part of the Java Development Kit (JDK), but it can also be distributed separately.
It provides the libraries, Java Virtual Machine (JVM), and other components necessary to run applications written in the Java programming language.
JRE is the runtime portion of Java software, which is all you need to run it in your Web browser.
Key Features of JRE
1. Java Virtual Machine (JVM)
2. Core Libraries
JRE includes a set of standard class libraries that provide basic functions such as I/O, networking, GUI components, and access to system resources.
3. Deployment Technologies
Tools and technologies for deploying Java applications, including Java Web Start and Java Plug-in, are included in JRE.
4. User Interface Toolkits
Includes various UI toolkits like Abstract Window Toolkit (AWT), Swing, and JavaFX.
5. Integration Libraries
Provides libraries for database access (JDBC), remote method invocation (RMI), etc.
6. Java Plug-in
Enables applets written in the Java programming language to be run inside various browsers.
7. Web Technologies
Supports web-based applications through technologies like Java Servlets, JSP, and WebStart.
8. Cross-Platform Compatibility
JRE makes it possible for Java applications to run on different operating systems without modification.
9. Automatic Updates
JRE can be configured to update itself automatically, ensuring that the latest security patches and performance improvements are always installed.
2.3. Java Development Kit(JDK)
JDK is a comprehensive software development kit provided by Oracle Corporation for developing applications and applets using the Java programming language.
It’s a key platform component for building Java-based applications, and it’s available for various operating systems like Windows, macOS, and various versions of UNIX/Linux.
Key Features of JDK
Java Compiler (javac):
Translates Java source code into bytecode (.class files).
Bytecode can run on any platform with a compatible JVM, ensuring Java’s platform independence.
Java Virtual Machine (JVM):
A core component for executing the bytecode.
Responsible for converting bytecode into machine language and executing it.
Ensures Java’s principle of “Write Once, Run Anywhere.”
Java Runtime Environment (JRE):
Includes libraries, the Java Virtual Machine (JVM), and other components necessary to run Java applications.
It’s part of JDK but can also be distributed separately for users who only need to run Java applications.
Java API Libraries:
A vast collection of Java libraries providing a wide range of functionalities.
Includes utilities for data structures, networking, graphical interface creation, database connection, and many more.
Tools and Utilities:
Security Tools: Including key and certificate management.
Javadoc: Tool for generating API documentation from comments in source code.
Debugger (jdb): For detecting and fixing bugs in Java programs.
JAR (Java Archive): Utility for packaging multiple files into a single archive file.
Monitoring and Management Tools: Tools like jconsole, jvisualvm for monitoring JVM and Java applications.
Security Tools: Including key and certificate management.
End of the lesson….enjoy learning
Student Ratings and Reviews
There are no reviews yet. Be the first one to write one.