Java Introduction

BEGINNER ~8 min read Tutorial

Java is one of the most widely used programming languages in the world. It first shipped in 1996, ran on everything from smart cards to enterprise servers, and is still the language behind the back-ends of countless banks, retailers, airlines, telecoms and cloud platforms. In this tutorial we install the Java Development Kit, write a small program, compile it and run it — by the end you will have the tools and the rhythm to keep going.

The goal of this first tutorial is not to teach you every keyword; that comes in the syntax tutorial. Instead we want you to be comfortable: comfortable installing the JDK, comfortable opening a terminal, comfortable compiling and running a program, and comfortable reading a basic stack trace if anything goes wrong. That comfort is the foundation everything else builds on.

What you will do

By the end of this tutorial you will have run your first Java program from the command line.

What is Java?

Java is a general-purpose, class-based, object-oriented programming language with strong static typing. Two design choices made Java what it is. First, the language compiles to bytecode rather than to native machine code; the Java Virtual Machine (JVM) then runs that bytecode. This is the “Write Once, Run Anywhere” promise: the same compiled .class file runs on Windows, macOS, Linux and any other platform for which a JVM exists. Second, Java manages memory for you with a garbage collector — you allocate objects with new and the JVM reclaims them when they are no longer reachable, eliminating an entire category of memory bugs that plague C and C++ programs.

Modern Java is developed in the open as the OpenJDK project, with a new feature release every six months and a Long-Term-Support (LTS) release roughly every two years. As of this writing, the LTS versions you are most likely to meet in production are Java 17 (LTS) and Java 21 (LTS). Unless you have a reason to pick an older version, install JDK 21.

1. Install the JDK

The JDK is the Java Development Kit: it contains the compiler (javac), the launcher (java), the documentation tool (javadoc), and the standard library. To write Java you need the JDK; to merely run a Java application, the smaller JRE is enough — but for learning, install the full JDK.

Pick one of the following distributions. They are all built from OpenJDK source; the differences are mostly support contracts and bundling.

  • Oracle JDK — the original, free for development, paid support for production at scale.
  • Adoptium / Eclipse Temurin — free, community-tested, widely used and our default recommendation.
  • Amazon Corretto — free, long-term, production-tested at AWS.
  • Microsoft Build of OpenJDK — free, supported by Microsoft.
  • Azul Zulu — free builds with commercial support options.

Download the installer for your operating system, run it, and verify the install from a terminal:

shell
$ java -version
openjdk version "21.0.4" 2024-07-16
OpenJDK Runtime Environment Temurin-21.0.4+...
OpenJDK 64-Bit Server VM Temurin-21.0.4+...

If you see version numbers printed without an error, the JDK is installed. If the command is not found, restart your terminal so it picks up the new PATH, or add the JDK bin directory to your PATH manually.

2. Write Hello World

Open a text editor (any plain-text editor works: VS Code, IntelliJ IDEA Community, Sublime, Nano). Create a file named HelloWorld.java with the following content:

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

The file name must match the public class name. Java is case-sensitive, so helloworld.java will not compile. The convention is for class names to use UpperCamelCase and method names lowerCamelCase.

3. Compile and Run

Open a terminal in the folder where you saved the file. Compile with javac; this produces a file named HelloWorld.class containing JVM bytecode. Run the bytecode with java:

shell
# compile source -> bytecode
$ javac HelloWorld.java

# run the bytecode
$ java HelloWorld
Hello, Java!

You should see Hello, Java! printed to your terminal. Congratulations — you have written, compiled and run a Java program from scratch. Everything else in this tutorial series is variations on this rhythm: write the source, compile it, run it, read the output.

4. Anatomy of the Program

Let's break the program down line by line so you understand each piece.

  • public class HelloWorld — declares a class named HelloWorld. Every Java program lives inside a class. The public keyword makes the class visible from outside its package.
  • public static void main(String[] args) — the entry point. The JVM looks for a method with this exact signature when you launch a class. static means you can call it without creating an instance first; void means it returns nothing; String[] args holds any command-line arguments.
  • System.out.println(…) — prints a line of text to standard output. System.out is a PrintStream object provided by the standard library.
  • Curly braces { } — group statements. Every class body, method body, and block is wrapped in braces.
  • Semicolons ; — terminate statements. Whitespace and indentation are not significant; the semicolon is.

5. Pick an IDE (Optional but Recommended)

For anything larger than a hello world, an Integrated Development Environment pays for itself within minutes. It highlights syntax errors as you type, suggests completions, refactors safely, runs tests with a keystroke, and integrates a debugger. The two most popular free choices for Java are:

  • IntelliJ IDEA Community Edition — the most polished Java IDE; free for non-commercial use including learning.
  • Visual Studio Code with the “Extension Pack for Java” — lighter, equally capable for tutorials; our pick if you already use VS Code.

If you prefer the terminal, the javac + java flow you just used is perfectly fine for the first few tutorials. As your projects grow, you will want a build tool such as Maven or Gradle to manage dependencies — we cover those later in the series.

6. Common First-Day Problems

If javac or java is not recognised

The JDK is not on your PATH. On Windows, edit the system environment variable PATH and append the path to the JDK bin folder (e.g. C:\Program Files\Java\jdk-21\bin). On macOS and Linux, add export PATH="/path/to/jdk/bin:$PATH" to your shell profile (~/.zshrc or ~/.bashrc).

Error: class HelloWorld is public, should be declared in a file named HelloWorld.java

Your file name does not match the public class name. Java requires that a public class lives in a file of the same name, case-sensitive, with the .java extension.

Error: could not find or load main class HelloWorld

Either you are running from the wrong directory, or you typed java HelloWorld.class (drop the .class), or there is a classpath issue. Make sure the .class file exists in the current directory and run java HelloWorld with no extension.

Exercises

  1. Change the printed text to Hello, <your name>! and recompile.
  2. Add a second System.out.println line that prints the Java version returned by System.getProperty("java.version").
  3. Print the result of 2 + 2 using System.out.println(2 + 2). Notice that you do not need quotes for numbers.
  4. Introduce a syntax error (remove a semicolon) and recompile. Read the error message — the line number tells you where to look.
Ready for the next step

You now have a working Java environment. In the next tutorial, we will examine the syntax of Java in more depth — packages, classes, methods, statements and comments.