Skip to content
elephantoo

Introduction, JDK & JVM

Lesson 1 of 43 12 min read

What Java is, how the JDK, JRE and JVM fit together, installing JDK 21 and your first program.


Java is one of the most widely used programming languages on Earth. It powers Android apps, banking and trading systems, e-commerce back-ends, big-data tools like Kafka and Spark, and the Minecraft server you might have played on. It is strongly typed (the compiler checks your types before the program ever runs) and object-oriented (you model the world with classes and objects).

In this course we use Java 21, a Long-Term Support (LTS) release. Everything you learn also works on newer versions such as Java 25.

Why learn Java?#

  • Jobs. Java is a staple of enterprise software, so Java developers are in constant demand.
  • Stability. Code written 20 years ago still runs today. Java takes backwards compatibility seriously.
  • Performance. The JVM compiles hot code to native machine code while your program runs, so Java is fast.
  • A huge ecosystem. Spring Boot, Hibernate, JUnit, Maven, Gradle and thousands of open-source libraries.
  • Great teacher. Its explicit types and structure teach habits that transfer to every other language.

The JDK, JRE and JVM#

Three acronyms you will see everywhere:

TermStands forRole
JDKJava Development KitEverything you need to write Java: compiler (javac), java launcher, jshell, libraries, tools
JREJava Runtime EnvironmentEverything needed to run Java programs (JVM + standard library)
JVMJava Virtual MachineThe engine that executes Java bytecode

As a developer you always install the JDK, which already contains a runtime.

From source code to running program#

Output
Hello.java  --javac-->  Hello.class (bytecode)  --java (JVM)-->  running program
  1. You write human-readable source code in a .java file.
  2. The compiler javac checks it and translates it into bytecode, stored in a .class file. Bytecode is not tied to any CPU or operating system.
  3. The JVM loads the bytecode, verifies it is safe, and executes it. Frequently-used code is compiled to fast native code by the JIT (Just-In-Time) compiler.

Because a JVM exists for Windows, macOS, Linux and more, the same .class files run on all of them. That is the famous "write once, run anywhere" idea.

Installing JDK 21#

Pick the instructions for your system:

Terminal
# Ubuntu / Debian
sudo apt update
sudo apt install openjdk-21-jdk

# Fedora
sudo dnf install java-21-openjdk-devel

# macOS (Homebrew)
brew install openjdk@21

# Windows (PowerShell): Eclipse Temurin build
winget install EclipseAdoptium.Temurin.21.JDK

Then check both the runtime and the compiler are on your PATH:

Terminal
java -version
javac -version

You should see something like:

Output
openjdk version "21.0.5" 2024-10-15
javac 21.0.5

The exact patch number does not matter. If the command is "not found", open a new terminal (so the PATH refreshes) or, on Windows, make sure the installer's "Set JAVA_HOME" and "Add to PATH" options were ticked.

Tip: Many tools (Maven, Gradle, IDEs) look for the JAVA_HOME environment variable. On Linux you can find the JDK folder with readlink -f $(which javac); JAVA_HOME is the folder above bin.

Your first program#

Create a file named Hello.java. The filename must match the public class name, including capital letters:

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

What each piece means:

  • public class Hello declares a class named Hello. In Java, all code lives inside a class.
  • public static void main(String[] args) is the entry point. The JVM looks for exactly this method to start your program. args holds any command-line arguments.
  • System.out.println(...) prints a line of text to the console, then moves to the next line.
  • Every statement ends with a semicolon ;, and blocks of code live inside curly braces { }.

Compile and run:

Terminal
javac Hello.java   # produces Hello.class
java Hello         # runs it: class name, no .class suffix!

You should see:

Output
Hello, Elephantoo!

Tip: Since Java 11 you can run a single-file program directly with java Hello.java. The compile step happens in memory. It is perfect for small experiments, and you can use it for most examples in this course.

Command-line arguments#

Anything you type after the class name ends up in the args array:

Greet.java
public class Greet {
    public static void main(String[] args) {
        System.out.println("Number of arguments: " + args.length);
        if (args.length > 0) {
            System.out.println("Hello, " + args[0] + "!");
        }
    }
}
Terminal
java Greet.java Priya
Output
Number of arguments: 1
Hello, Priya!

Comments#

Comments are notes for humans; the compiler ignores them.

Java
// A single-line comment

/* A multi-line
   comment */

/**
 * A Javadoc comment: tools turn these into HTML documentation.
 */

How a Java file is structured#

A typical Java source file looks like this:

Java
package com.example.demo;   // optional: which package (folder) the class belongs to

import java.util.Scanner;  // optional: bring in classes from other packages

public class Demo {
    // fields: data belonging to the class or its objects

    // methods: behaviour
    public static void main(String[] args) {
        // statements go here, each ending with ;
    }
}

Java is case-sensitive: Hello, hello and HELLO are three different names. By convention, class names use PascalCase and methods and variables use camelCase.

Try things instantly with jshell#

The JDK ships with jshell, an interactive shell where you type Java and see results immediately:

Terminal
jshell
Output
jshell> 2 + 3
$1 ==> 5

jshell> "Java".length()
$2 ==> 4

jshell> /exit

It is ideal for checking what a method returns without writing a whole class.

Choosing an editor#

Any text editor works, but an IDE catches errors as you type and offers autocompletion:

  • IntelliJ IDEA Community Edition: free, the most popular Java IDE.
  • VS Code with the "Extension Pack for Java".
  • Eclipse: free and long-established.

For the first few lessons, a plain editor plus the terminal is a good way to see what is really happening.

Common beginner mistakes#

Error messageLikely cause
class Hello is public, should be declared in a file named Hello.javaFilename and class name differ
';' expectedMissing semicolon at the end of a statement
cannot find symbolA typo, or wrong capitalisation, e.g. system.out instead of System.out
Could not find or load main class Hello.classYou ran java Hello.class; drop the .class
error: no main method found / Main method not foundThe main signature is misspelt

Read compiler errors from the first one down. Later errors are often side effects of the first.

What's next#

You can now install the JDK, write, compile and run a Java program, and read the most common errors. Next we look at the data your programs work with: variables and types.

Check your understanding

Quick quiz

0/3 answered
  1. 1.What does the JVM do?

  2. 2.Which command compiles Hello.java?

  3. 3.You save public class Greeter in a file called Main.java and run javac Main.java. What happens?

Finished reading?

Mark this lesson complete to track your progress.