Java Compiler and Java Virtual Machine
⚙️ Java Compiler and Java Virtual Machine (JVM)
Java uses a two-step process to run programs:
Step 1: Java Compiler (javac
)
The Java compiler is a program that:
-
Converts Java source code (
.java
file) into bytecode (.class
file). -
Bytecode is an intermediate, platform-independent code.
📌 Example:
When you write:
Save it as HelloWorld.java
.
Now compile it:
This creates a file called HelloWorld.class
— which contains bytecode.
Step 2: Java Virtual Machine (JVM)
The JVM is a virtual engine that:
-
Reads and executes the bytecode (
.class
file). -
Converts bytecode to machine-specific code using Just-In-Time (JIT) compilation.
-
Ensures that the same
.class
file can run on any device with a JVM (Windows, Mac, Linux, etc.).
📌 To run the program:
The JVM runs the bytecode and produces the output:
🎯 Why is this important?
Component | Role | Output |
---|---|---|
javac | Java Compiler | .class bytecode file |
java (JVM) | Executes the bytecode on your machine | Program output on console |
Summary
-
Java Compiler (
javac
) ➜ Converts.java
to.class
(bytecode). -
JVM (
java
) ➜ Runs the bytecode, making Java platform-independent.
Comments
Post a Comment