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:


public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

Save it as HelloWorld.java.

Now compile it:


javac HelloWorld.java

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:


java HelloWorld

The JVM runs the bytecode and produces the output:


Hello, World!

🎯 Why is this important?

ComponentRoleOutput
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

Popular posts from this blog

Object Oriented Programming PBCST 304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Object Oriented Programming PBCST 304 Course Details and Syllabus

Type casting in Java