Advanced

Advanced & Idiomatic: Overview

Chapter 6 of the Java book: functional Java, concurrency and the memory model, I/O, modern idioms, shipping and localization. Covers the Streams API, threads and the happens-before rule, high-level concurrency and virtual threads, the JIT and GC, NIO.2 files, how the modern features combine into data-oriented design, testing, builds and packaging, and localization with Locale, resource bundles and MessageFormat. Every example is compiled and run.

Suggest an edit

Advanced & Idiomatic: Overview

Welcome to Chapter 6, the summit. You now have the full language and the standard library behind you. These nine lessons cover what separates competent Java from idiomatic, production-grade Java:

  • declarative data processing with streams;
  • the hard truths of concurrency — races, coordination, and the Java Memory Model;
  • how the JVM actually runs your code and reclaims its memory;
  • modern file I/O;
  • how the modern type-system features compose into one coherent style;
  • the testing, tooling and packaging that ship your code;
  • and the localization that lets it speak its readers' languages.

There are nine lessons, in this order:

  1. Functional Java & the Streams API — lazy pipelines, collectors, Optional, and the parallel-stream hazard.
  2. Concurrency: the Basics — what a thread is, race conditions, synchronized, and happens-before.
  3. Concurrency: Coordination — wait/notify, a real deadlock and its escapes, ReentrantLock, latches, semaphores, and BlockingQueue.
  4. Concurrency: High-Level & Virtual Threads — executors, atomics, CompletableFuture, and virtual threads.
  5. The Java Memory Model & Performance — volatile, happens-before in depth, safe publication, the JIT, and garbage collection.
  6. I/O, Files & NIO.2 — Path/Files, the stream name clash, and bytes vs characters.
  7. Modern Java Idioms & the Type System — records + sealed + patterns as one data-oriented design.
  8. Testing, Tooling & Packaging — JUnit 5, build tools, dependencies, and executable JARs.
  9. Localization — Locale, NumberFormat, localized dates, resource bundles, and MessageFormat.

Every code block with a ▶ Run button is live. Where behavior is nondeterministic or project-level, the concurrency, performance and tooling lessons include real captured runs instead (a data race, JIT and GC logs, a mvn test summary). The habit that matters most in this chapter is knowing the cost. Streams, parallelism, immutability and abstractions all come with trade-offs, and senior judgment means choosing among them deliberately.

📘 How to read the Intuition boxes. Each one is built in three moves:

  1. The mechanism — what the compiler and the JVM are actually doing.
  2. A concrete bite — a specific, runnable failure (often a real compiler error), shown so the trap is visible.
  3. The earned rule — the decision heuristic, now justified rather than asserted, plus its cost.
Mark as readBook versionfd26460