Java

From your first program to objects, collections, exceptions, files and a project of your own.

Each lesson has runnable examples and exercises. The Java compiler and runtime are real (OpenJDK 25), running in a small Linux machine inside this tab: no install, no server compiling for you. The first visit downloads about 35 MB, of which the first 8 MB before you can start; later visits start from your browser's copy.

Linux + OpenJDK 25 in your browser

21 lessons · 158 programs

Chapter 1 · Foundations

A program that computes, decides and repeats

  1. 1 Hello, Java

    Your first program, and the two commands that build and run it.

  2. 2 Inside the computer

    What a computer is made of, what happens when your program runs, and where its values live in memory.

  3. 3 Variables and arithmetic

    Store numbers, text and true/false in variables, change them, and meet integer division.

  4. 4 Reading compiler errors

    What javac tells you when a program is wrong, and how to read a stack trace.

  5. 5 Decisions and loops

    if, else if and else, for and while, the classic FizzBuzz, and a test of it all.

  6. 6 Methods and input

    Write your own methods, read input with Scanner, and split a program across files.

Chapter 2 · Objects

Things with state and behaviour, in packages

  1. 7 Classes and objects

    Make objects with new and see them on the heap, write classes with fields and methods, and extend a class.

  2. 8 Packages and projects

    Split a program into packages and folders, and build the whole folder with one javac command.

Chapter 3 · Data

Text, arrays, grids, lists and maps

  1. 9 Working with text

    Look inside a String, compare text with equals, format output with printf, and walk text one character at a time.

  2. 10 Arrays

    Keep many values under one name, loop over them, pass them to methods, and read an index that went too far.

  3. 11 Nested loops and 2D arrays

    Put a loop inside a loop, keep a grid in an array of arrays, and print a tic-tac-toe board.

  4. 12 Math and recursion

    Java's Math methods, random numbers you can repeat, and methods that call themselves.

  5. 13 Lists and maps

    ArrayList for a sequence that grows, HashMap to look values up by a key.

Chapter 4 · Robust, modern Java

Code that fails well and reads well

  1. 14 Exceptions

    Catch the errors you expect with try and catch, and throw your own from methods that check their input.

  2. 15 Interfaces and polymorphism

    Write an interface, implement it in several classes, and treat different objects the same way.

  3. 16 Enums, records and switch

    Name a fixed set of values with an enum, pick between them with switch, and hold plain data in a record.

  4. 17 Generics

    Write classes and methods that work for any type, limit that type with a bound, and meet the wrapper types.

  5. 18 Lambdas and streams

    Pass a short piece of code to a method, and filter, change and add up a list in one chain.

Chapter 5 · Real programs

A tool that reads files and ships

  1. 19 Files

    Write text into a file, read it back line by line, and turn a file of data into a report.

  2. 20 Practice: searching and sorting

    Write the classic searches and sorts by hand, then count, reverse and compare text.

  3. 21 A project of your own

    Build a small inventory program in packages that reads a data file, checks it and prints a report.