Unlike many programming languages that are compiled directly into machine code, Java follows a two-step execution process. First, the source code is converted into bytecode, and then the bytecode is executed by the Java Virtual Machine (JVM). This unique architecture makes Java secure, portable, and reliable.
To understand how Java works internally, it is important to understand three core components: JVM (Java Virtual Machine), JRE (Java Runtime Environment), and JDK (Java Development Kit).
Table of Contents
How Java Works?
When a Java program is written and executed, it passes through several stages before producing the final output.Step 1: Writing the Source Code
A programmer writes Java code and saves it in a file with the .java extension.
// Java program to show how Java works
public class HelloJava
{
public static void main(String[] args)
{
System.out.println("Welcome to Java");
}
}
Step 2: CompilationThe Java compiler (javac) converts the source code into bytecode.
Command:
Output:javac HelloJava.java
Step 3: Bytecode GenerationHelloJava.class
The generated .class file contains bytecode. Bytecode is platform-independent and can run on any system that has a JVM installed.
Step 4: Class Loading
The JVM loads the required class files into memory using the Class Loader subsystem.
Step 5: Bytecode Verification
Before execution, the JVM verifies the bytecode to ensure that it follows Java security rules and does not contain harmful instructions.
Step 6: Execution
The Execution Engine converts bytecode into machine code that the operating system can understand and execute.
Step 7: Output Generation
The machine code runs on the operating system and displays the output.
What is JVM?
JVM (Java Virtual Machine) is a virtual machine that executes Java bytecode and converts it into machine code. It acts as a bridge between Java programs and the operating system.The JVM is responsible for loading, verifying, executing, and managing Java applications during runtime.
Features of JVM
- Platform Independence: The JVM enables Java programs to run on different operating systems without changing the source code. Since each operating system has its own JVM implementation, the same bytecode can be executed anywhere. This is the foundation of Java’s “Write Once, Run Anywhere” concept.
- Automatic Memory Management: The JVM automatically allocates memory to objects and removes unused memory when it is no longer needed. Developers do not have to manually manage memory allocation and deallocation. This reduces programming errors and improves application stability.
- Security: The JVM performs bytecode verification before execution. It checks whether the code follows Java security standards and prevents unauthorized operations. This security layer helps protect systems from malicious code.
- Garbage Collection: The JVM automatically identifies and removes unused objects from memory through Garbage Collection. This process frees memory resources and improves application performance. Developers do not need to manually delete objects.
- Performance Optimization: Modern JVMs use Just-In-Time (JIT) compilation to improve execution speed. Frequently executed bytecode is converted into machine code and reused later. This significantly enhances application performance.
The JVM consists of several important components that work together to execute Java programs.
1. Class Loader
The Class Loader is responsible for loading class files into memory when they are needed. It dynamically loads classes at runtime and ensures that all required classes are available for execution. The class leader is responsible for:
- Loading class files into memory.
- Linking classes with the JVM.
- Initializing classes before execution.
The Method Area stores class-level information such as class names, method definitions, static variables, and runtime constants. This area is shared among all threads within the JVM. The information stored in the Method Area includes:
- Class metadata
- Method information
- Static variables
- Runtime constant pool
Heap Memory is the runtime area where objects are created and stored. All objects created using the new keyword are stored in the heap. The Heap Memory is:
- Shared among all threads.
- Managed by Garbage Collection.
- Stores all objects and arrays.
In this example, the object is stored in Heap Memory.Student s = new Student();
4. Stack Memory
Stack Memory stores local variables, method calls, and temporary data. Each thread has its own stack memory. The Stack Memory has:
- Each thread has its own stack.
- Stores local variables and method calls.
- Memory is released automatically after method execution.
The variable age is stored in the stack.int age = 15;
5. Program Counter Register
The Program Counter (PC Register) stores the address of the currently executing instruction. Each thread has its own PC Register, allowing multiple threads to execute independently.
6. Native Method Stack
The Native Method Stack stores native methods written in languages such as C and C++. It helps Java interact with system-level libraries and operating system functions.
7. Execution Engine
The Execution Engine executes bytecode loaded by the JVM.
- Interpreter: The Interpreter reads and executes bytecode line by line. Although simple, repeated execution of the same code can be slower.
- JIT Compiler: The Just-In-Time Compiler converts frequently executed bytecode into machine code. This improves execution speed and application performance.
- Garbage Collector: The Garbage Collector removes unused objects from memory and reclaims resources automatically.
What is JRE?
JRE (Java Runtime Environment) is a software package that provides everything required to run Java applications. It contains the JVM along with libraries and supporting files needed during execution.The JRE is designed primarily for users who want to run Java applications but do not need development tools.
Components of JRE
1. JVM
The JVM is the core component of the JRE. It executes Java bytecode and manages runtime operations.
2. Core Java Libraries
The JRE includes thousands of predefined classes and packages that support various programming tasks. These libraries simplify application development and execution.
Examples of Library Functions
- File handling
- Input and output operations
- Networking
- Database connectivity
- Collections framework
The JRE includes configuration files and resources required for the smooth execution of Java applications. These files support the JVM during runtime.
Features of JRE
- Provides a Runtime Environment: The JRE creates the environment required to execute Java applications. It supplies all necessary runtime resources and services.
- Includes Standard Libraries: The JRE provides access to Java’s extensive library collection. Developers can use these libraries without writing functionality from scratch.
- Supports Cross-Platform Execution: Different operating systems have different JRE implementations. However, all JREs can execute the same Java bytecode.
- Simplifies Application Deployment: Users only need the JRE to run Java programs. They do not need development tools such as compilers and debuggers.
- Contains JVM: The JVM is included within the JRE. This allows Java programs to execute without requiring a separate JVM installation.
What is JDK?
JDK (Java Development Kit) is a complete software package used for developing, compiling, debugging, and running Java applications.The JDK contains both the JRE and additional development tools required by programmers.
Components of JDK
1. Java Compiler (javac)
The Java compiler converts Java source code into bytecode.
Example:
The compiler checks syntax errors and generates a .class file.javac Program.java
2. Java Launcher (java)
The Java Launcher starts the JVM and executes Java applications.
Example:
3. Java Debugger (jdb)java Program
The Java Debugger helps developers identify and fix errors in programs. It allows step-by-step execution and variable inspection.
4. Javadoc Tool
The Javadoc tool automatically generates documentation from source code comments. This helps create professional API documentation.
5. Jar Tool
The Jar Tool packages multiple files into a single JAR (Java Archive) file. This simplifies application distribution and deployment.
6. Additional Development Tools
The JDK contains several tools for testing, monitoring, packaging, and performance analysis. These tools help developers build efficient applications.
Features of JDK
- Complete Development Environment: The JDK provides all tools needed for writing, compiling, debugging, and running Java programs.
- Includes JRE: The JDK includes the JRE, which means developers can both create and execute Java applications using a single package.
- Supports Debugging: Built-in debugging tools help developers identify and resolve program errors quickly.
- Documentation Generation: The Javadoc tool allows developers to create detailed documentation directly from source code.
- Application Packaging: The JDK enables developers to package applications into JAR files for easier deployment and sharing.
Relationship Between JVM, JRE, and JDK
- JVM is a Part of JRE: The Java Virtual Machine (JVM) is the component responsible for executing Java bytecode. It loads class files, manages memory, performs garbage collection, and converts bytecode into machine code. However, the JVM alone cannot run Java applications because it does not include the necessary libraries and supporting files.
- JRE Contains JVM: The Java Runtime Environment (JRE) includes the JVM along with all the libraries and runtime files required to execute Java applications. The JRE provides a complete environment for running Java programs. Users who only want to run Java applications typically need the JRE.
- JDK Contains JRE: The Java Development Kit (JDK) includes the JRE and additional development tools such as the compiler, debugger, and documentation generator. The JDK is used by programmers to write, compile, test, debug, and run Java applications.
A Java program cannot run without the JVM. A Java application needs the JRE because it contains the JVM and required libraries. A Java developer needs the JDK because it contains both the JRE and development tools.Think of building and driving a car:
- JVM is the engine that actually runs the car.
- JRE is the complete car that allows you to drive.
- JDK is the factory that contains all the tools needed to build and test the car.
Difference Between JVM, JRE, and JDK
| Basis of Comparison | JVM | JRE | JDK |
|---|---|---|---|
| Definition | JVM is a virtual machine that executes Java bytecode and converts it into machine code. | JRE is a runtime environment that provides everything needed to run Java applications. | JDK is a complete software development kit that provides tools for developing and running Java applications. |
| Primary Purpose | The primary purpose of JVM is to execute Java programs. | The primary purpose of JRE is to provide an environment for running Java programs. | The primary purpose of JDK is to help developers create, compile, test, and run Java programs. |
| Main Components | JVM mainly consists of components such as the Class Loader, Memory Areas, Execution Engine, and Garbage Collector. | JRE consists of the JVM, Java libraries, and supporting runtime files. | JDK consists of the JRE along with development tools such as the compiler, debugger, and Javadoc. |
| Compiler Availability | JVM does not contain a Java compiler. | JRE does not contain a Java compiler. | JDK contains the Java compiler (javac) used to convert source code into bytecode. |
| Development Support | JVM alone cannot be used for Java development. | JRE alone cannot be used to create or compile Java programs. | JDK provides all the tools required for Java application development. |
| Execution Support | JVM is responsible for executing Java bytecode. | JRE provides the complete environment required for program execution. | JDK allows developers to both create and execute Java applications. |
| Intended Users | JVM works behind the scenes for both developers and end users. | JRE is mainly used by users who only need to run Java applications. | JDK is mainly used by programmers and software developers. |
| Size | JVM is the smallest component among the three. | JRE is larger than JVM because it includes additional libraries and runtime files. | JDK is the largest package because it contains both the JRE and development tools. |
| Dependency | JVM cannot function independently because it requires runtime support. | JRE depends on JVM for executing Java programs. | JDK depends on JRE and JVM for compiling and running applications. |
| Example Use | JVM is used when a compiled Java application is executed. | JRE is used when a user runs a Java application on a computer. | JDK is used when a developer writes, compiles, debugs, and runs Java programs. |
Advantages of Java Architecture
- Platform Independence: Java programs can run on different operating systems without changing the code.
- Enhanced Security: The JVM provides multiple security mechanisms that help prevent unauthorized access.
- Automatic Memory Management: Garbage Collection automatically frees unused memory.
- Better Performance: JIT compilation improves execution speed.
- Portability: Java applications can easily be moved between systems.
Limitations of Java Architecture
- Higher Memory Usage: The JVM requires additional memory compared to some native applications.
- Startup Delay: The JVM must initialize before program execution begins.
- Additional Runtime Layer: Java programs require JVM execution, adding an extra layer between code and hardware.
- Platform-Specific JVM Requirement: Each system must have a compatible JVM installed.
- Slower Than Native Languages: In certain situations, Java may perform slower than languages such as C and C++.
Conclusion
Java uses a unique architecture involving JVM, JRE, and JDK to achieve platform independence and portability. The source code is first compiled into bytecode and then executed by the JVM. The JRE provides the runtime environment, while the JDK supplies the tools needed for development. Understanding how these components work together is essential for every Java programmer because they form the foundation of Java application development and execution.Frequently Asked Questions
1. What is the role of JVM in Java?2. What is the difference between JRE and JDK?The Java Virtual Machine (JVM) is responsible for executing Java bytecode. It converts bytecode into machine code, manages memory, performs garbage collection, and provides a runtime environment for Java applications.
3. Why is Java called a platform-independent language?The JRE (Java Runtime Environment) is used to run Java applications and contains the JVM along with required libraries. The JDK (Java Development Kit) contains the JRE and additional tools such as the compiler, debugger, and documentation generator that are needed for Java development.
4. Can I run a Java program without installing JDK?Java is called platform-independent because its source code is compiled into bytecode instead of machine code. This bytecode can run on any operating system that has a compatible JVM installed, allowing the same program to work across different platforms.
5. How are JVM, JRE, and JDK related to each other?Yes, you can run a Java program without installing the JDK if a JRE is available on the system. However, if you want to write, compile, or debug Java programs, you must install the JDK.
JVM is a component that executes Java bytecode. JRE contains the JVM along with libraries and runtime files required to run Java applications. JDK contains the JRE and additional development tools used to create, compile, test, and run Java programs.
0 Comments