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…
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
javapublic 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
.classfiles. - Bytecode is what makes Java platform-independent, not the source code directly.
Practice Problems
Try each question yourself first, then check the answer.
Order the steps
Put these in order: Bytecode, JVM execution, Source code, Compilation.
Show answerHide answer
What does javac create
What file does javac Program.java create, and what is inside it?
Show answerHide answer
Program.class, which contains the platform-neutral bytecode for that class.Same file, two systems
Explain why the same .class file runs on both Windows and Linux.
Show answerHide answer
Predict and locate the step
javapublic 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 answerHide answer
This text became bytecode before running!. The source became bytecode during the compilation step (javac), before the JVM ran it.