AI Hints
Home›Java Tutorial›What Java Is, Where Java Is Used and How Java Runs Your Code
Java · Lesson 3 of 120

How Java Runs Your Code

Source, bytecode, JVM.

⏱ 2 min read▶ Video at 2:26🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

Java does something clever. It doesn't turn your code straight into instructions for your exact computer. It uses a middle step, called bytecode.

  • Not straight to your machine
  • A middle step
  • Called bytecode

From text to results

First, you write your code, in a file ending in dot java. The Java compiler, called javac, checks it, and turns it into bytecode. Bytecode is saved in a file ending in dot class. Then the Java Virtual Machine, or JVM, runs that bytecode. And you see the results.

  1. Write Main.java
  2. javac compiles
  3. Main.class (bytecode)
  4. JVM runs it
  5. Results

Three names to know

The JVM is the program that runs bytecode. There's a JVM for Windows, Mac, Linux, and more. That's why the same bytecode runs anywhere. The JDK, or Java Development Kit, is what you install to write Java. It includes the compiler, and the JVM.

keyvalue
JVMruns bytecode
Bytecodethe same on every computer
JDKwhat developers install
javacthe compiler, inside the JDK

Top to bottom

Inside main, Java runs one statement at a time, from top to bottom. Each line ending in a semicolon is one statement. The last one works out ten times four, plus two, before printing. Run it. The steps come out in order, then forty two.

Main.javajava
public class Main {
    public static void main(String[] args) {
        System.out.println("Step one");
        System.out.println("Step two");
        System.out.println("Step three");
        System.out.println(10 * 4 + 2);
    }
}
Output
Step one
Step two
Step three
42

Values in a sentence

Here's a small taste of what's coming. String name creates a variable, a named box that holds text. Int year holds a whole number. The plus sign joins text and numbers into one sentence. Run it. Java was released in nineteen ninety five, thirty one years ago.

Main.javajava
public class Main {
    public static void main(String[] args) {
        String name = "Java";
        int year = 1995;
        System.out.println(name + " was released in " + year);
        System.out.println("That is " + (2026 - year) + " years ago");
    }
}
Output
Java was released in 1995
That is 31 years ago

Summary

You write a dot java file, javac turns it into bytecode, and the JVM runs it, top to bottom. Next, let's install the JDK.

Key takeaways
  • Source → bytecode
  • JVM runs it anywhere
  • JDK for developers
Next up: Installing the JDK — Java on your computer