What Java is, how the JDK/JRE/JVM relate, installing the JDK, compiling and running your first program, and choosing an IDE.
Introduction & Environment Setup
Java is one of the most widely used programming languages in the world. It powers Android apps, enterprise backends, cloud services, and everything in between. This guide takes you from zero to deploying a REST API on a VPS - no prior Java experience required.
What is Java?
Java is a statically typed, compiled, object-oriented programming language. That means:
- Statically typed - you declare the type of every variable. The compiler catches type errors before the program runs.
- Compiled - you turn source code (
.javafiles) into bytecode (.classfiles) before running it. - Object-oriented - code is organized into classes and objects (covered in chapter 5).
Java was created in 1995 by James Gosling at Sun Microsystems. Its design philosophy is **"write once, run anywhere" ** - compiled Java code runs on any platform that has a Java Virtual Machine.
JDK, JRE, JVM - what is what?
These three terms come up constantly. Here is what they mean:
You need the JDK - it includes everything.
How Java code runs
Unlike languages like Python or JavaScript, Java has an explicit compilation step:
- You write source code in a
.javafile. javac(the Java compiler) compiles it into bytecode - a.classfile.java(the JVM launcher) runs the bytecode.
The bytecode is platform-independent. The same .class file runs on Windows, macOS, and Linux - as long as a JVM is
installed.
Installing the JDK
Download and install the latest LTS (Long Term Support) version. As of this writing, that is Java 21.
macOS (with Homebrew)
After installation, follow the instructions Homebrew prints to add it to your PATH. Typically:
Windows
Download the installer from Adoptium (Eclipse Temurin) and run it. Make sure to check "Add to PATH" during installation.
Linux (Ubuntu/Debian)
Verify the installation
Open a terminal and run:
Result:
The exact version may differ - anything Java 17 or higher is fine for this guide.
Also check the compiler:
Result:
Choosing an IDE
Java is verbose enough that a good IDE makes a big difference. Popular choices:
If you have no preference, start with IntelliJ IDEA Community Edition - it is free and has the best Java tooling.
For this guide, all examples are compiled and run from the terminal so you can use any editor.
Your first Java program
Create a file called Hello.java:
Compile it:
This creates Hello.class in the same directory. Run it:
Result:
Anatomy of the program
Let us break down every part:
-
public class Hello- every Java file contains a class. The class name must match the filename (Hello.java→class Hello).publicmeans other classes can access it. -
public static void main(String[] args)- the entry point. The JVM looks for this exact signature when you run the program.public- accessible from outside the classstatic- belongs to the class itself, not to an instance (more on this in chapter 5)void- returns nothingmain- the method name the JVM looks forString[] args- command-line arguments as an array of strings
-
System.out.println("Hello, world!")- prints text to the terminal followed by a newline.
println vs print
Result:
One file, one public class
Java enforces a strict rule: each .java file can contain at most one public class, and the filename must match
that class name.
If the filename does not match, javac will refuse to compile:
Packages
Packages organize classes into namespaces. They match the directory structure:
Compile and run from the src/ directory:
Result:
For the early chapters of this guide, we will skip packages to keep things simple. We introduce them properly in the project chapters.
Comments
Use comments to explain why, not what. The code should be clear enough to explain what it does.
Semicolons, braces, and whitespace
Java requires:
- A semicolon
;at the end of every statement - Curly braces
{}to define blocks (class bodies, method bodies, loops, etc.)
Whitespace (spaces, tabs, newlines) is ignored by the compiler, but consistent indentation is essential for readability. The Java convention is 4 spaces per indent level (not tabs).
jshell - interactive Java
Java includes jshell, an interactive REPL (Read-Eval-Print Loop) for quick experiments:
jshell is useful for trying things out. You do not need a class or main method - just type expressions.
How this guide is structured
Each chapter builds on the previous one. Code examples include expected output so you can verify your work. By chapter 12, you will have a running REST API on the internet.
Summary
- Java is a statically typed, compiled, object-oriented language.
- The JDK includes the compiler (
javac), the JVM (java), and standard libraries. - Install the latest LTS version (Java 21+) and use any IDE - IntelliJ IDEA Community is recommended.
- Java source (
.java) is compiled to bytecode (.class), then run by the JVM. - Every program needs a
public static void main(String[] args)method. - The filename must match the public class name.
- Use
jshellfor quick experiments.
Next up: Variables, Types & Operators - how Java handles data.