Thuta Learning
Java
BasicProgrammingBeginner

Java Intro

JavaLesson 215 min

What you'll walk away with

  • Explain the three layers (Java language, JDK, JVM) and the job each one does
  • Know that `javac` ships only with the JDK, while the JVM runs bytecode
  • Understand that bytecode is platform-neutral but each operating system needs its own JVM
  • Predict why a version mismatch such as `UnsupportedClassVersionError` happens

Explain it simply

Java Intro in plain words

Java was released by Sun Microsystems in 1995 and is now maintained by Oracle and the OpenJDK community. It is a class-based, object-oriented language, built around classes and the objects made from them.

Its most famous promise is 'Write Once, Run Anywhere': compile your code once and it runs on any machine that has a JVM. Think of the JVM as an interpreter in the human sense. You speak one language, bytecode, and the JVM on each of Windows, macOS and Linux translates it into what that particular machine understands.

Problem → Why → Solution

The problem: `java` works but `javac` is missing

Say you download 'Java' from some website and install it. `java -version` prints a version just fine, but when you type `javac` to compile your program, the shell says the command does not exist.

Or at work, a program that runs on your laptop refuses to even start on the server. Both problems have the same root: the single word 'Java' is being used for three or four different things.

terminal
$ java -version
openjdk version "21.0.5" 2024-10-15 LTS
$ javac Main.java
bash: javac: command not found      # a runtime is installed, but not a JDK

Why this hurts

Running a program needs only a JVM, but writing and compiling one needs the full JDK. If only a runtime is installed, you get `java` without `javac`. Once you can name each layer, a single error message is often enough to tell which layer is at fault.

Core concept: three layers called language, JDK and JVM

When people say 'Java' they mean one of three layers. The first is the Java language itself: its keywords, grammar and type rules, defined in a document called the Java Language Specification (JLS). It decides what you are allowed to write in a `.java` file.

The second is the JDK (Java Development Kit), the developer's toolbox. It contains the compiler `javac`, the `java` launcher that runs programs, `jshell` for quick experiments, `jar` for packaging, `javadoc` for documentation, and a standard library of thousands of classes such as `String` and `ArrayList`. A JDK also includes a JVM.

The third is the JVM (Java Virtual Machine). It loads the bytecode in `.class` files, verifies it and runs it. A JIT compiler turns frequently used code into native code, and a garbage collector reclaims memory that is no longer in use.

LayerWhat it isWho needs it
Java languageSyntax and type rules (the JLS)Anyone writing code
JDKjavac, java, jshell, jar + standard library + JVMDevelopers and build servers
JVMThe engine that runs bytecodeEvery machine that runs the program

A JRE (Java Runtime Environment) is a package with the JVM and the standard library but no compiler. Oracle stopped shipping a separate JRE starting with JDK 11, but some vendors still offer one, and that is usually where the 'Java is installed but `javac` is missing' problem comes from.

Why use bytecode as an in-between step instead of producing native machine code directly? Native code works for one CPU and operating system only, so you would compile separately for each platform. Bytecode is written for the JVM, so one `.class` file can be shipped anywhere a JVM exists. The trade-off is that the JVM has to start up before your program does, but the JIT compiler optimizes hot code while it runs, so long-running servers end up performing close to native code.

Picture the three layers
  1. Language: the rules you write
  2. JDK: javac + tools + library
  3. Bytecode: the .class files
  4. JVM: runs it on each OS

There is one kind of bytecode but a separate JVM for each OS. That split is what makes WORA possible.

General pattern: the entry point

java
// file: ClassName.java
public class ClassName {                       // code lives in a class
    public static void main(String[] args) {   // the JVM starts here
        // statements run top to bottom
    }
}

When a program starts, the JVM looks for the `public static void main(String[] args)` method. A backend server or a command-line tool, a standard Java application begins at this entry point.

A first entry point

The program below is the simplest Java application: one `main` method for the JVM to find. Bytecode compiled from this single `.java` file prints the same output on Windows, macOS and Linux alike.

java
// The main entry point of a simple Java application
public class Main {
  public static void main(String[] args) {
    System.out.println("Java is powerful!");
  }
}

Code breakdown

  1. A comment// The main entry point of a simple Java application

    A line starting with `//` is a comment. The compiler ignores it completely and it never reaches the bytecode; it is only for human readers.

  2. The classpublic class Main {

    This declares a public class called `Main`. Compiling it with `javac` produces one bytecode file, `Main.class`.

  3. Where the JVM startspublic static void main(String[] args) {

    After loading the class, the JVM calls this method. Because it is `static`, the JVM can call it without creating an object first.

  4. Output through the OSSystem.out.println("Java is powerful!");

    This uses `System.out` from the standard library to write one line to the console. The actual printing is handed by the JVM to the operating system.

You should see
Java is powerful!

Java versions and LTS releases

A new Java feature release comes out every six months, in March and September. Some of them are designated LTS (Long-Term Support) releases and receive security updates for years. Java 17, Java 21 and Java 25 are LTS releases, and most companies run an LTS version in production. The examples in this course are checked on Java 21.

There are several JDK builds, such as Oracle JDK, Eclipse Temurin, Amazon Corretto and the Microsoft Build of OpenJDK. They are all built from the OpenJDK source, so the language and the library are the same; what differs is the license and the support offered.

In real projects

In real projects

Teams do not leave the Java version to luck. The project README says something like 'requires JDK 21', and the build pins the compile target with `maven.compiler.release` in Maven or a toolchain setting in Gradle. For deployment they use a Docker image such as `eclipse-temurin:21`, so the laptop and the server run the same JVM version.

Android is an interesting example. You can write Android apps in the Java language, but on the phone they are run by the Android Runtime (ART), not by a standard JVM. It shows clearly that the language and the runtime are separate layers. When a new developer joins a team, one of the first things they are asked to do is check that both `java -version` and `javac -version` report the version the project needs.

Common mistakes

Common mistakes

Treating JRE, JDK and JVM as the same thing

'Java is installed' means nothing for compiling if only a runtime is present. It is also common for the IDE to use its own bundled JDK while the terminal uses a different version. When debugging, ask precisely: which JDK's `javac`, and which JVM?

Believing WORA makes everything identical

Bytecode is platform-neutral, but the JVM version still matters. A class compiled with Java 21 fails on a Java 17 JVM with `UnsupportedClassVersionError`, because the older JVM cannot read the newer bytecode format. File path styles (`\` versus `/`), line endings and native libraries can also differ between operating systems.

Assuming Java and JavaScript are related

Despite the similar names, they are completely separate languages. Code copied from a JavaScript tutorial will not work in Java.

Mini challenge

Mini challenge: which JVM am I on?

Use `System.getProperty` to write a program that prints the Java version, the Java vendor and the operating system it is running on. Notice how the same code gives different output on different machines.

  • Read the three properties `java.version`, `java.vendor` and `os.name`
  • Store each value in a `String` variable first
  • Print three labelled lines
  • Compare the version you get with the output of `java -version`
Main.java
public class Main {
    public static void main(String[] args) {
        // TODO: String version = System.getProperty(...);
        // TODO: read "java.vendor" and "os.name" the same way

        // TODO: print "Java version: ...", "Vendor: ..." and "OS: ..."
    }
}

Expected: The output depends on the machine, for example `Java version: 21.0.5`, `Vendor: Eclipse Adoptium` and `OS: Windows 11`. The version should match what `java -version` reports.

Show hint

`System.getProperty("java.version")` returns a String such as `"21.0.5"`. Read the other two the same way, changing only the property name.

Show solution
Main.java
public class Main {
    public static void main(String[] args) {
        String version = System.getProperty("java.version");
        String vendor = System.getProperty("java.vendor");
        String os = System.getProperty("os.name");

        System.out.println("Java version: " + version);
        System.out.println("Vendor: " + vendor);
        System.out.println("OS: " + os);
    }
}

Quick quiz 1 / 3

Which tool turns `Main.java` into `Main.class`, and which package does it come in?

Quick quiz 2 / 3

What happens if you run a `.class` file compiled with Java 21 on a Java 17 JVM?

Quick quiz 3 / 3

Why can a `Main.class` compiled on Windows run on a Linux server?

What to remember

  • The Java language is the rules, the JDK is the tools, and the JVM is the engine that runs bytecode
  • Compiling needs a JDK; running needs only a JVM
  • Bytecode is platform-neutral, but each operating system has its own JVM
  • In production pick an LTS version (such as 21) and keep it the same on laptops, servers and builds

Java Language Changes, Java SE 25 — Oracle

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Java Intro | Thuta Learning