Skip to content
CampusEduX

Introduction to Java · Lesson 5 of 150

How Java Works

This describes the journey your code takes — from the moment you write it, to the moment it actually runs on a computer. Java code is first turned into a special in-between format called bytecode, and then the JVM reads that bytecode and ex…

2 min read

01What does "How Java Works" mean?

This describes the journey your code takes — from the moment you write it, to the moment it actually runs on a computer. Java code is first turned into a special in-between format called bytecode, and then the JVM reads that bytecode and executes it.

02Why is it used?

This two-step process (compile to bytecode, then run on JVM) is exactly why Java can work on different operating systems without changes. The bytecode is the same everywhere; only the JVM differs per platform.

03Real-Life Example

Imagine writing a letter in a universal script that any translator, in any country, can read aloud in the local language. You write it once (your Java code becomes bytecode), and each country's translator (the JVM on each OS) reads it aloud correctly for local listeners.

04Syntax

java
// Step 1: Write code -> MyProgram.java // Step 2: Compile -> javac MyProgram.java (creates MyProgram.class, the bytecode) // Step 3: Run -> java MyProgram (JVM executes the bytecode)

05Example Program

java
public class HowItWorks { public static void main(String[] args) { System.out.println("This text became bytecode before running!"); } }

Output:

This text became bytecode before running!

06Key Points to Remember

  • Java code goes through: Source Code → Compilation → Bytecode → JVM Execution.
  • Bytecode is stored in .class files.
  • Bytecode is what makes Java platform-independent, not the source code directly.

Practice Problems

Try each question yourself first, then check the answer.

1

Order the steps

Put these in order: Bytecode, JVM execution, Source code, Compilation.

Show answer
Source code -> Compilation -> Bytecode -> JVM execution.
2

What does javac create

What file does javac Program.java create, and what is inside it?

Show answer
Program.class, which contains the platform-neutral bytecode for that class.
3

Same file, two systems

Explain why the same .class file runs on both Windows and Linux.

Show answer
Bytecode is not tied to any OS or CPU. Each platform ships its own JVM that knows how to execute that same bytecode, so no recompilation is needed.
4

Predict and locate the step

java
public class Main { public static void main(String[] args) { System.out.println("This text became bytecode before running!"); } }

What is printed, and at which step did the text become bytecode?

Show answer
It prints This text became bytecode before running!. The source became bytecode during the compilation step (javac), before the JVM ran it.