Introduction, JDK & JVM
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:
As a developer you always install the JDK, which already contains a runtime.
From source code to running program#
- You write human-readable source code in a
.javafile. - The compiler
javacchecks it and translates it into bytecode, stored in a.classfile. Bytecode is not tied to any CPU or operating system. - 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:
Then check both the runtime and the compiler are on your PATH:
You should see something like:
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_HOMEenvironment variable. On Linux you can find the JDK folder withreadlink -f $(which javac);JAVA_HOMEis the folder abovebin.
Your first program#
Create a file named Hello.java. The filename must match the public class name, including capital letters:
What each piece means:
public class Hellodeclares a class namedHello. 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.argsholds 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:
You should see:
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:
Comments#
Comments are notes for humans; the compiler ignores them.
How a Java file is structured#
A typical Java source file looks like this:
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:
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#
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
1.What does the JVM do?
2.Which command compiles
Hello.java?3.You save
public class Greeterin a file calledMain.javaand runjavac Main.java. What happens?
Finished reading?
Mark this lesson complete to track your progress.