Java
Java
A single, progressive Java book that takes you from absolute beginner to advanced. Thirty-seven lessons in six chapters, read in strict order. Each one derives the language's behaviour from one core idea instead of listing features. Every example is compiled and run, and a three-move "Intuition" standard turns rules into things you can re-derive. The throughline: the compiler is a proof-checker you run before your program ever touches data.
Suggest an editJava
Most Java tutorials hand you a pile of features and ask you to remember them. This one does the opposite. It finds the one generative idea underneath each topic, and shows every rule as a consequence of that idea. By the end of each lesson, you should be able to re-derive the rules, not just recite them — and to predict what a snippet prints, or whether it even compiles, before you run it.
The book is a single text, meant to be read from top to bottom. It starts by assuming you've never written a line of code in any language, and it ends on generics, concurrency, the memory model, and modern idioms. Early lessons introduce every term before using it; later lessons assume you've done the earlier ones, and move at a fluent reader's pace. What never changes is the standard of explanation: every claim is backed by code that was actually compiled and run, and every rule comes with its cost. One idea runs from the first page to the last — Java checks your program twice, at compile time and at run time, and the two stages fail in different ways — so learning to read both kinds of message is learning the language.
Reading conventions
- Every code block with a Run button is real. Synapse compiles and runs it in a sandboxed Java 25 (OpenJDK) runner — the same one that produced every
Output:block in this book. Click ▶ Run, edit the code, and run it again. The output you see is the output we verified. - When the lesson is a compile error, the block is labelled
Compiler error:, and it shows the realjavacdiagnostic — because in Java, many of the most important lessons happen before the program runs. - Blocks without a Run button are deliberate. They're either short fragments, or programs shown precisely because they need keyboard input that the sandbox can't supply. Their output is still shown, and still real.
- Every lesson is built on a thesis stated in its first paragraph, and follows a fixed rhythm: concept → code → verified output → analysis ("what happened") → an Intuition box ("why it must be so").
📘 How to read the Intuition boxes. Each one is built in three moves:
- The mechanism — what the compiler and the JVM are actually doing.
- A concrete bite — a specific, runnable failure (often a real compiler error), shown so the trap is visible.
- The earned rule — the decision heuristic, now justified rather than asserted, plus its cost.
This note repeats, word for word, at the top of every chapter overview, so a returning reader can skip it.
The six chapters
The book climbs through six chapters. Earlier chapters are prerequisites for later ones, and the topics that matter most come back later at greater depth. This is the spiral: for example, == vs .equals gets a gentle pass with strings in Chapter 1, a rigorous "object model" pass in Chapter 3, and a third pass with the equals/hashCode contract in Chapter 4 — each one referring back rather than re-teaching.
Curriculum map
Chapter 1 — First Steps (zero programming background) — you compile and run your first program and work with values, numbers, text, and I/O.
- What Java Is & Running Code
- Variables & Primitive Types
- Numbers & Arithmetic
- Strings, the Basics
- Input & Output
Chapter 2 — Control Flow (beginner) — make decisions and repeat work; first real programs.
Chapter 3 — Classes & Objects (early intermediate — the OOP leap) — the move from procedural code to objects, and the reference model underneath.
- Defining Classes & Creating Objects
- Encapsulation & Access Modifiers
staticvs Instance- References, Equality & the Object Model
Chapter 4 — Core Libraries & Data Structures (intermediate) — the standard data structures and the contracts that make objects usable in them.
- Strings in Depth
- The Collections Framework
- Sets & Maps
equals&hashCode- Generics
- Enums & Records
- Dates & Times
Chapter 5 — Robust OOP & Error Handling (intermediate → advanced) — the full object model: polymorphism, abstraction, errors, and the bridge to functional Java.
- Inheritance & Polymorphism
- Abstract Classes & Interfaces
- Exceptions
- Nested & Anonymous Classes; Lambdas
- Sealed Classes & Pattern Matching
- Packages, Modules & the Build
Chapter 6 — Advanced & Idiomatic (advanced) — functional Java, concurrency and the memory model, I/O, modern idioms, shipping, and localization.
- Functional Java & the Streams API
- Concurrency: the Basics
- Concurrency: Coordination
- Concurrency: High-Level & Virtual Threads
- The Java Memory Model & Performance
- I/O, Files & NIO.2
- Modern Java Idioms & the Type System
- Testing, Tooling & Packaging
- Localization
Three reading paths
Path A — Your first programming language ever. Start at lesson 1 and walk straight through. Don't skip ahead: each lesson assumes the one before it. Type in every example, then change it and predict the new output — or whether it still compiles — before you run it. At first, Chapters 1–2 are the whole game: values, decisions, loops, methods. Everything after that is built on them.
Path B — You program in another language, but you're new to Java. Skim Chapter 1 for the syntax and the surprises (integer division truncates, int overflow wraps silently, == vs .equals), then slow down at Chapter 3 — Classes & Objects. The reference model (stack vs heap, values vs references, aliasing) and the equality/hashCode contract are where Java differs most from scripting-language mental models — and where assumptions carried over from other languages cause the subtlest bugs.
Path C — You know Java, and want to fill gaps. Use the chapter map to find your boundary, and read forward from there. The highest-leverage deep passes are the object model (lesson 15), equals/hashCode (19), generics and erasure (20), the Streams API (29), and concurrency with the memory model (30–33).
A note on the runner
Every runnable block compiles and runs in an isolated Java 25 (OpenJDK) sandbox — no setup, no install, nothing to break. The sandbox compiles your code as a class named Main, so the examples here use that name. Each block runs on its own (state doesn't carry over from one block to the next), so self-contained examples repeat any setup they need. Anonymous runs work; signing in lets you edit and run, and raises your hourly quota.
One limit is worth knowing early: the sandbox can't pause to read keyboard input. So the interactive Scanner(System.in) programs in Input & Output are shown statically, each paired with a runnable twin that reads from a fixed source. You do not need Java installed locally to read this book — though by Chapter 6 you'll want it, and Testing, Tooling & Packaging, near the end of the book, shows how to set it up properly.