How Java Works
How Java Works
When we write a Java program, we typically use tools like JShell to test our code. However,
running a complete program requires understanding the role of the Java Virtual Machine
(JVM) and the Java Runtime Environment (JRE). Let's delve into how JVM operates and the
control flow of a Java program.
The Role of JVM
The JVM is an integral part of the Java ecosystem, allowing Java code to be executed on any
platform. Each operating system (OS) has a specific JVM designed for it, making Java
platform-independent at the source code level but platform-dependent at the binary level.
Diagram
Layers
● The main method has a specific signature: public static void main (String[] args) {}.
Understanding JRE
The JRE is essential for running Java programs. It includes the JVM and a set of libraries and
other files that the JVM uses at runtime. Think of JRE as a kitchen:
● Kitchen Analogy:
o The kitchen provides the environment to cook a dish.
o Utensils and Ingredients are like the libraries and resources JRE provides.
Example:
● Suppose you want to bake a cake. The JRE is like the entire kitchen setup needed for
baking, including the oven, mixing bowls, and ingredients. The JVM is the oven that
actually bakes the cake using the ingredients (bytecode) provided.
Visualizing Java Architecture
Imagine the Java development environment as nested boxes:
● Outer Box (JDK): The Java Development Kit includes everything for development.