Skip to content
LumenaryAcademy
Foundations 50 min

First steps: the JDK, the JVM and a first program

Where Java came from, what happens between a source file and a running program, and how to run your first one.

Code tested · 3AI-draftedAwaiting review by a computer scientist
MotivationH

Why Java, and why now

Somewhere right now a payment is clearing in a bank in Frankfurt, a parcel is being routed in São Paulo, and a search index is being rebuilt in Bengaluru, and there is a good chance that Java code is doing the work. Java is one of the most widely deployed programming languages ever made. It runs the back ends of banks, airlines, insurers and online shops; it is the language of much of the big-data world (Apache Kafka, Hadoop, Cassandra and Elasticsearch are written in Java or run on its virtual machine); it was the original language of Android; and it is what millions of people first met when they modded Minecraft.

That popularity is not an accident, and it is not only history. Java has three properties that large organisations value more than almost anything else:

  • It keeps its promises. Code compiled in the early 2000s still runs on today's Java, usually without changes. Few languages take backward compatibility as seriously.
  • It is fast enough for nearly everything. A Java program starts as portable bytecode and is compiled to machine code while it runs, guided by how it actually behaves. Long-running servers often reach speeds close to hand-written C++.
  • It is readable by strangers. Java's design favours code that a new team member can understand without surprises, even when that makes it a little longer.

Java also had a reputation for ceremony: long class declarations, getters and setters, pages of boilerplate. Much of that reputation is out of date. Since 2017 the language has changed faster than at any time in its history: var for local variables, records, switch expressions, pattern matching, text blocks, virtual threads and, in JDK 25, programs that need no class declaration at all. This course teaches that modern Java, while being honest about the older code you will meet at work.

History & global context

From Oak to Java 25

In 1991 a small team at Sun Microsystems, led by James Gosling, started the "Green Project" to write software for consumer devices such as set-top boxes. Those devices used many different processors, so the team wanted a language whose programs did not depend on any one of them. Gosling's language was first called Oak; it was renamed Java, and Sun announced it publicly in 1995 with the slogan "write once, run anywhere". The first official release, JDK 1.0, followed in January 1996.

Java arrived just as the web did, and spread quickly: into browsers at first (applets, now long gone), then into servers, where it found its true home. Some milestones worth knowing:

YearReleaseWhat it brought
1996JDK 1.0The language, the JVM and the first libraries
2004Java 5Generics, enums, annotations, the enhanced for loop
2006–2007OpenJDKSun released Java's source code under an open-source licence
2010—Oracle acquired Sun, and with it the stewardship of Java
2014Java 8Lambdas and streams: the biggest change to everyday code since generics
2017Java 9The module system and jshell
2018Java 10var, and a new feature release every six months, each March and September
2021Java 17Sealed classes; the first long-term-support release with records
2023Java 21Virtual threads, record patterns, pattern matching for switch (long-term support)
2025Java 25Compact source files, scoped values, flexible constructor bodies (long-term support)

Two ideas from this history shape how Java is made today. First, the six-month cadence: every March and September a new feature release ships, and every two years one of them is designated a long-term support (LTS) release that vendors keep patching for years. Most companies move from LTS to LTS (8, 11, 17, 21, 25), so you will meet all of these in real code. Second, features are proposed and tracked in public as numbered JEPs (JDK Enhancement Proposals) on openjdk.org. Big features often appear first as previews, which you must switch on with a flag, and become permanent a release or two later once developers have tried them. When this course says "JEP 512", you can read the proposal yourself.

This course is written for JDK 25, the LTS release of September 2025, and every program in it was run on JDK 25. Newer non-LTS releases exist; they add features without breaking what you learn here.

IntuitionH

Source, bytecode, virtual machine

Most languages you might know take one of two routes. C and Rust compile your source code into machine code for one particular processor and operating system. Python reads your source code and interprets it each time you run it. Java takes a third route with two steps.

Main.javasource codejavacMain.classbytecodethe JVMclass loader and verifierinterpreter, then JIT compilergarbage collectormachine code for this computeronce, on any machineevery time the program runs
The two steps of Java: javac compiles source code to bytecode once; the JVM loads the bytecode and turns it into machine code for the computer it is running on.

First, the compiler javac translates your .java source file into a .class file of bytecode: compact instructions for an imaginary computer that does not depend on your processor. Second, the Java Virtual Machine (JVM) loads that bytecode and runs it. The JVM starts by interpreting the bytecode, watches which methods run most often, and compiles those "hot" methods to real machine code just in time (the JIT compiler). It also manages memory for you, reclaiming objects that nothing refers to any more (the garbage collector).

Analogy, not a formal construction

Think of bytecode as sheet music. A composer writes the score once; a pianist in Accra and a pianist in Seoul can each play it on their own instrument. The JVM is the musician: it reads the score and plays it on the hardware it has, and like a good musician it plays a passage more fluently the more often it repeats it.

The bytecode is the reason for "write once, run anywhere": the same Main.class runs on Windows, macOS and Linux, on Intel and ARM processors, wherever there is a JVM. And the JIT is the reason Java is fast: the compiler that produces the final machine code can see how your program actually behaves, and optimise for that.

DefinitionsH

The words you will hear

Definition— JVM (Java Virtual Machine)

The program that loads, verifies and runs bytecode, and manages memory. The JVM is specified separately from the language, which is why other languages (Kotlin, Scala, Clojure, Groovy) also compile to bytecode and run on it. The main JVM in use today is HotSpot, part of OpenJDK.

Definition— JDK (Java Development Kit)

Everything you need to write and run Java: the JVM, the standard library (the "class library"), and tools such as java, javac, jshell, javap and jar. Since Java 11 there is no separate "JRE" to download for running programs: you install a JDK.

Definition— Bytecode and class files

Bytecode is the JVM's instruction set. javac writes it into .class files, one per class. Every class file begins with the four bytes CA FE BA BE and records which Java version it needs.

Definition— OpenJDK and distributions

OpenJDK is the open-source project where Java is developed. Several organisations build and ship JDKs from its source code, among them Oracle, the Eclipse Adoptium project (Temurin), Amazon (Corretto), Microsoft, Azul (Zulu) and Red Hat. They differ in support terms and packaging, not in the language.

ComputationC

Installing a JDK

You need a JDK, version 25 or newer, to follow this course. There are many good routes; pick one.

  • A ready-made build. Eclipse Temurin offers free installers for Windows, macOS and Linux. Oracle publishes OpenJDK builds at jdk.java.net. Both are fine for learning and for production.
  • Your system's package manager. On Linux, most distributions package OpenJDK (apt, dnf and friends); on macOS, Homebrew does; on Windows, winget does.
  • A version manager. SDKMAN! installs and switches between many JDKs on macOS and Linux. Professionals often need two or three versions side by side, and a version manager makes that painless.
  • No install at all. For trying snippets, the Java Playground runs code in your browser.

Then open a terminal and check:

bash
java --version
javac --version

The machine this course was tested on printed this for the first command:

text
openjdk 25.0.5.1 2026-11-17 LTS
OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS)
OpenJDK 64-Bit Server VM Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS, mixed mode, sharing)

Read it from left to right: the feature release (25); the interim, update and patch numbers (0.5.1: this is the fifth quarterly security-and-bug-fix update, plus one small patch); the release date; LTS; and which vendor built it. If java prints an older number, another JDK comes first on your PATH; fix that before going on, because the newest features in this course will not compile on an old JDK.

You will also want an editor that understands Java. IntelliJ IDEA (its Community Edition is free), Eclipse, NetBeans and Visual Studio Code with Java extensions are all used professionally. For the first few lessons a plain editor and a terminal are enough, and seeing the tools without an IDE in between teaches you what the IDE does for you later.

Worked examplesH

A first program, the classic way

Save this as Main.java:

java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello from Java!");
        int firstYear = 1995;
        System.out.println("Java first appeared in " + firstYear + ".");
        System.out.println("This JVM runs Java " + Runtime.version().feature() + ".");
        System.out.println("Arguments received: " + args.length);
    }
}

Run it with java Main.java. It prints:

text
Hello from Java!
Java first appeared in 1995.
This JVM runs Java 25.
Arguments received: 0

Every word of the declaration line means something, and you will understand all of it by the end of the Foundations level. For now:

  • public class Main declares a class named Main. In Java all code lives inside classes. A public class must be in a file with the same name, so Main goes in Main.java.
  • public static void main(String[] args) is the entry point: the method the JVM calls to start your program. public lets the launcher see it, static means it belongs to the class rather than to an object, void means it returns nothing, and String[] args is an array holding any command-line arguments. Run java Main.java alpha beta and the last line says Arguments received: 2.
  • System.out.println(...) prints a line. System is a class in the standard library, out is its standard-output stream, and println prints its argument followed by a newline.
  • int firstYear = 1995; declares a variable of type int (a 32-bit integer). Java is statically typed: every variable has a type the compiler knows, and it will refuse to compile code that mixes types incorrectly.
  • + between a string and anything else joins them into a longer string.
  • Statements end with semicolons, and braces group statements into blocks. Indentation is for human readers only, but every professional codebase indents consistently, usually by four spaces.

Three ways to run it

java Main.java uses the source launcher (JDK 11 and later): it compiles the file in memory and runs it at once, leaving nothing on disk. That is perfect for small programs and scripts, and it is how every program in this course is checked. Since JDK 22 the launcher can also run a program spread over several source files in the same directory.

For anything larger you compile first and run the class files:

bash
javac -d out Main.java     # compile: writes out/Main.class
java -cp out Main          # run the class Main, looking for classes in out/

Here -d out says where to put class files and -cp out sets the class path, the list of places the JVM searches for classes. Real projects leave this to a build tool (Maven or Gradle, in the last lesson), but it helps to have done it once by hand.

The third way is jshell, Java's interactive shell (a REPL), for trying out one expression at a time:

text
|  Welcome to JShell -- Version 25.0.5.1
|  For an introduction type: /help intro

jshell> int x = 6 * 7;
x ==> 42

jshell> x + 1
$2 ==> 43

jshell> "Ada Lovelace".length()
$3 ==> 12

jshell> Math.sqrt(2)
$4 ==> 1.4142135623730951

Type /exit to leave. Results you did not name get names like $2 so you can use them again.

Worked examplesH

Compact source files (JDK 25)

The classic form asks a beginner to type public, class, static and String[] before writing a single line of their own. JDK 25 makes all of that optional for small programs (JEP 512). A compact source file may contain methods and fields with no class around them, main may be an ordinary method with no parameters, and the new class IO offers simple console printing:

java
void main() {
    String[] cities = {"Lagos", "Lima", "Hanoi", "Oslo"};
    for (String city : cities) {
        IO.println("Hello, " + city + "!");
    }
    IO.println(cities.length + " greetings sent.");
}
text
Hello, Lagos!
Hello, Lima!
Hello, Hanoi!
Hello, Oslo!
4 greetings sent.

Behind the scenes the compiler wraps the file in a class it names after the file, so nothing about Java's model has changed: there is still a class, and main is still a method. A compact source file also imports the whole java.base module automatically, so classes such as List and Map work without import lines. As the program grows you can add a class declaration around it without rewriting anything.

This course uses both forms. Compact files keep short examples short; the classic form is what you will see in nearly every existing codebase, and it is still what you write when a program has several classes that other code refers to by name.

A slightly larger program

Methods let you name a computation and reuse it. Here toFahrenheit is a static method that takes a double (a floating-point number) and returns one, and printf formats numbers into neat columns:

java
public class Main {
    static double toFahrenheit(double celsius) {
        return celsius * 9 / 5 + 32;
    }

    public static void main(String[] args) {
        double[] readings = {-10, 0, 21.5, 37, 100};
        for (double c : readings) {
            System.out.printf("%6.1f C = %6.1f F%n", c, toFahrenheit(c));
        }
        System.out.println(toFahrenheit(37));
    }
}
text
 -10.0 C =   14.0 F
   0.0 C =   32.0 F
  21.5 C =   70.7 F
  37.0 C =   98.6 F
 100.0 C =  212.0 F
98.6

In the format string, %6.1f means "a floating-point number, six characters wide, one digit after the point", and %n is a newline appropriate to the operating system. You will see much more of methods in Methods, classes and objects.

ExplanationH

Looking inside a class file

It is worth seeing bytecode once, so that "the JVM runs bytecode" is a fact you have observed rather than a phrase. Take this program:

java
public class Main {
    static int square(int x) {
        return x * x;
    }

    public static void main(String[] args) {
        System.out.println(square(12));
    }
}

Compile it with javac -d out Main.java, then ask the disassembler javap to show the bytecode with javap -c -cp out Main. Part of what it printed:

text
  static int square(int);
    Code:
         0: iload_0
         1: iload_0
         2: imul
         3: ireturn

The JVM is a stack machine. iload_0 pushes the first local variable (the parameter x, an int) onto an operand stack; it is pushed twice; imul pops two ints and pushes their product; ireturn returns the int on top of the stack. The i prefix marks integer instructions; there are matching families for long, float, double and object references. The first bytes of the file, shown by a hex dump, are cafe babe 0000 0045: the famous magic number, then the class-file version, where 0x45 (69) means "needs Java 25 or newer". An older JVM refuses to load it, with an UnsupportedClassVersionError.

You will rarely read bytecode at work. But knowing it is there explains a great deal: why Kotlin and Java code can call each other freely (both become the same bytecode), why a library compiled years ago still loads, and why the first few seconds of a Java program are slower than the rest (the JIT has not compiled the hot code yet). We come back to the JIT in Inside the JVM.

Reading a compiler error

Leave out one semicolon:

java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello")
    }
}

and java Main.java refuses to run it:

text
Main.java:3: error: ';' expected
        System.out.println("Hello")
                                   ^
1 error
error: compilation failed

Read compiler messages from the top: the file, the line number (3), what was expected, and a caret pointing at the place. Fix the first error first; one mistake often causes several messages, and the later ones may disappear once it is fixed. Notice also when the error appeared: before the program ran at all. Catching mistakes at compile time is one of the main things a static type system buys you.

Common misconceptionsH

Common misconceptions

Misconception

"Java and JavaScript are related." Only by name. JavaScript was renamed in 1995 to ride on Java's publicity. The two languages differ in typing, in their object models, in where they run and in how they are developed. Knowing one helps with the other about as much as knowing any second language does.

Misconception

"Java is interpreted, so it is slow." The JVM does start by interpreting bytecode, but it compiles frequently used methods to optimised machine code while the program runs. What is true is that Java programs take a moment to warm up, and that a JVM uses more memory at startup than a small C program. Recent JDKs attack startup time directly; see Inside the JVM.

Misconception

"I need to install the JRE to run Java programs." That was true for many years. Since Java 11 you install a JDK, and applications that ship to end users usually bundle a trimmed runtime made with the jlink tool, so the user installs nothing.

Misconception

"Compact source files are a different, simpler Java." They are the same language. The compiler supplies the class declaration you left out, and everything you learn about classes later still applies to that implicit class.

Optional Christian reflection · A promise kept

A reflection from the Christian tradition that inspires Lumenary Academy. It is not part of the mathematics and you are welcome to skip it.

Java's designers made a promise in the 1990s: code you write today will still run tomorrow. Thirty years later, they have mostly kept it, at real cost to themselves; many tempting changes were refused because they would have broken someone's program. Scripture praises the person "that sweareth to his own hurt, and changeth not" (Psalm 15:4, King James Version): someone whose word holds even when keeping it is inconvenient. Software engineering has its own version of that virtue. Others build on what we publish, and keeping faith with them is a kind of love of neighbour.

Further reading

Where to go from here

  • dev.java/learn is the Java team's own tutorial site, kept up to date with each release.
  • The Java SE 25 documentation includes the reference pages for java, javac and jshell, and the API documentation for every class in the standard library. Learn to read the API documentation early; professionals live in it.
  • The JEP index on openjdk.org lists every feature proposal by number. Reading the "Motivation" section of a JEP is the best way to understand why a feature exists.

In the next lesson, Types, operators and control flow, we look closely at Java's types, its arithmetic (including its traps) and the statements and expressions that make decisions and loops.

Exercises

Exercises

1. First steps: the JDK, the JVM and a first program

Warm-upHuman

You run javac -d out Main.java on a file that declares one class, Main. What does javac write into out/?

Choose one

2. First steps: the JDK, the JVM and a first program

CoreComputational

What does this compact source file print?

void main() {
    IO.println("1 + 2 = " + 1 + 2);
}

3. First steps: the JDK, the JVM and a first program

CoreComputational

And this one? (Compare it with the previous exercise.)

void main() {
    IO.println(1 + 2 + " is the sum");
}

4. First steps: the JDK, the JVM and a first program

Warm-upHuman

In the line public static void main(String[] args), which words are Java keywords (reserved words of the language), as opposed to names chosen by people? List them.

Write elements separated by commas. Order and repeats don’t matter — just like in a set!

5. First steps: the JDK, the JVM and a first program

CoreComputational

What number does this program print?

public class Main {
    public static void main(String[] args) {
        int total = 0;
        for (int i = 1; i <= 4; i++) {
            total = total + i * i;
        }
        System.out.println(total);
    }
}

6. First steps: the JDK, the JVM and a first program

Warm-upHuman

Java has shipped a feature release every March and September since Java 10 in March 2018, and Java 21 shipped in September 2023. Which release number shipped in September 2025?

7. First steps: the JDK, the JVM and a first program

StretchHuman

Which statements about JDK 25 compact source files are true? Choose all that apply.

Choose all that apply

Further reading and resources