best java booksbest java books
HappyCoders Glasses

Best Java Books
(Updated 2022)

Sven Woltmann
Sven Woltmann
February 5, 2022

You want to understand

  • ... what exactly volatile means and when and why you have to use it ... and also for which purposes volatile is not sufficient?
  • ... why (int) (char) (byte) -1 does not result in -1 but in 65535?
  • ... what the difference is between a = a + b and a += b? (Yes there is one – an important one in fact).
  • ... or what <T extends Object & Comparable<? super T>> means?

Then continue reading. In this article you'll find some of the most important books for advanced Java programmers – on best practices, pitfalls, concurrency, generics and lambdas.

Here, you'll find recommendations for books on software development in general (i.e., not Java-specific).

If you're looking for something more relaxing, you'll find some novels for nerds here.

(Disclosure: As an Amazon Associate I earn from qualifying purchases.)

Effective Java (Third Edition)

Effective Java - Joshua Bloch

by Joshua Bloch

Link to the book at Amazon

In this book, Joshua Bloch has compiled the most important dos and don'ts of Java programming: Best practices that help you write efficient, readable, and maintainable – and therefore future-proof – software.

The book is primarily aimed at experienced Java programmers – good knowledge of the language is required.

Some of the practices you probably already use in your daily life; others may be new to you and help you take your skills to the next level.

The book contains 90 articles ("items") divided into 11 topics. A lot of material – but you can get to grips with it bit by bit. The items each represent a self-contained unit.

Each article contains specific, easy-to-understand advice, accompanied by concise, clear source code examples.

Every Java programmer should have read this book. I recommend reading the book once entirely, and then, if you have to solve specific tasks in your daily programming routine, consult the corresponding articles again.

When buying this book (especially if you purchase second-hand), make sure that you acquire the current, third edition! The second edition is based on Java 6 and is no longer up-to-date.

Suitable as an audiobook? No, because of the numerous code examples.

Java Puzzlers: Traps, Pitfalls, and Corner Cases

java puzzlers - joshua bloch neal gafter

by Joshua Bloch and Neal Grafter

Link to the book at Amazon

In this highly entertaining book, Joshua Bloch and Neil Gafter, both co-developers of the Java platform, uncover numerous pitfalls and corner cases of the Java platform.

The book contains 95 puzzles in the form of a few lines long, seemingly trivial Java program. The reader's task is to predict what the program will do. The obvious answer is rarely the correct one – and even if you recognize the trap the authors have set for you, you often fall into a second, even more obscure one.

For each puzzle, the authors describe in detail the reasons for the unexpected behavior (with references to the Java Language Specification) and provide practical rules for avoiding the demonstrated behavior in the first place.

Some of the puzzles are heavily contrived to let exceptions from the depths of the Java Language Specification kick in, which I have never had to deal with in 25 years with Java. That's because most of these oddities don't occur if you write clean, readable code without "clever" tricks.

Many of the cases, however, you encounter regularly, such as the % operator, peculiarities of signed integer types, inaccuracies in floating point numbers, silent overflows, hidden (rather than overridden or overloaded) methods, or the initialization of static class variables, to name a few.

Every puzzle is exciting and instructive, and the explanations are humorous and entertaining. It's amusing that the names of some puzzles hint at the solution. But that doesn't diminish the fun, as you usually notice it only afterward at the big "aha" moment.

The book's only shortcoming is that it is at the level of Java 5. Some of the particularities no longer occur in modern Java versions – and there are certainly some new ones.

Suitable as an audiobook? No, because of the numerous code examples.

Java Concurrency in Practice

Java Concurrency in Practice

by Brian Goetz, Tim Peilers, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea

Link to the book at Amazon

Writing scalable applications that use the capabilities of today's multi-core CPUs while running reliably and with high performance is a challenge. Race conditions, deadlocks, and other liveness errors quickly creep in.

Brian Goetz and a team of experts from Java Specification Request 166 "Concurrency Utilities" describe in "Java Concurrency in Practice" the fundamental concepts of thread safety and the correct implementation of thread-safe applications in Java.

The book is divided into four parts:

  1. The first part is about basics like atomicity and visibility, intrinsic locks ("synchronized"), safe publication of objects, thread-safe composition, and concurrency building blocks concurrent collections and synchronizers like semaphores, latches, and barriers.
  2. The second part is about parallelizing applications using tasks and the executor framework. It explains the configuration of thread pools and the safe termination of tasks, threads, thread pools, and applications.
  3. The third part is about liveness (deadlocks, livelocks, thread starvation), measuring performance and improving it by reducing lock contention, and writing tests for concurrent algorithms.
  4. The fourth part covers advanced topics such as explicit locks, condition queues (wait/notify), atomic variables, non-blocking algorithms, and the Java Memory Model. Building on this, the functionality of many of the topics presented in part one, such as object publishing, concurrent collections, and synchronizer classes, is explained.

All explanations are accompanied by excellent real-world code examples. Most show frequently made but incorrect implementations first, followed by the correct ones.

The authors successfully explain an extremely complex topic in a relatively simple way. Of particular note is the excellent explanation of the Java Memory Model and the "happens-before" relationship.

Nevertheless, the book is not an easy read, and it takes time to understand all the details. I recommend reading the book at least twice.

Even though the book is on the level of Java 6, it is still up-to-date. The basics of thread safety have not changed since, and all features added in later Java versions (like the fork/join framework, CompletableFuture, or parallel streams) are based on them.

Suitable as an audiobook? No, due to numerous extensive code examples.

P.S.: If you want to go deeper into the advanced topics and read about specific types of memory fences and advanced memory access methods like release/acquire and opaque, I recommend Doug Lea's "Concurrent Programming in Java" as a follow-up reading.

Java Generics and Collections

Java Generics and Collections - Maurice Naftalin and Philip Wadler

by Maurice Naftalin and Philip Wadler

Link to the book at Amazon

While these days, most Java developers can skip the book’s introduction to generics, the book quickly addresses advanced topics such as Subtyping, Wildcards, Wildcard captures, and Bounds – topics that every Java programmer has probably had to deal with before, but has usually walked through compiler errors rather than knowing what they did.

In the second part of the book, the author presents the essential Collection interfaces and classes of the Java Collection Framework – Sets, Queues, Lists, and Maps, as well as their implementations – and compares them regarding concurrent programming and performance.

Any advanced Java programmer should be familiar with these topics.

Suitable as an audiobook? No, due to numerous code examples.

Get Your Hands Dirty on Clean Architecture: A hands-on guide to creating clean web applications with code examples in Java

get your hands dirty on clean architecture

by Tom Hombergs

Link to the book at Amazon

What is hexagonal architecture? What are its advantages? And how do you implement a hexagonal application in Java? Tom Hombergs answers these questions in "Get Your Hands Dirty on Clean Architecture".

The author first describes the problems of the classic layered architecture and explains how the "Clean Architecture" described by Robert C. Martin and the "Hexagonal Architecture" introduced by Alistair Cockburn (which is comparable in principle) can better meet the requirements of a modern application (maintainability, adaptability, extensibility) – namely by placing the business domain (and not technical details) at the center of the architecture.

After a short theory follows the practice: Step by step, the author develops an exemplary Java application with entities, use cases, ports, and adapters. He presents different mapping strategies for the adapters and discusses their advantages and disadvantages.

He details how to cut and test components and proposes a package structure that makes the architectural style visible.

After all application components are finished, the author shows how they can be assembled – firstly with pure Java and secondly using the Spring Framework. Thus, he impressively demonstrates that even the implementation of dependency injection can be merely an interchangeable detail of the software.

After that, the author shows how the application can be modularized using separate build artifacts, e.g., Maven modules, so the architectural style cannot be easily bypassed. Unfortunately, the author does not address how the Java module system could also perform this task.

Finally, he discusses what shortcuts we can take when mapping between layers and what technical debt we need to be aware of.

The book is excellently structured and written understandably and concisely throughout. The author has managed to fit into just 138 pages what takes up more than four times the space in "Designing Hexagonal Architecture with Java" (a book I do not recommend and therefore do not list here). An outstanding achievement!

Suitable as an audiobook? No, due to numerous diagrams and code examples.

By the way, you can find a tutorial series on hexagonal architecture in the software craftsmanship section of this website.

Concurrent Programming in Java: Design Principles and Pattern, 2nd Edition

Concurrent Programming in Java
Created with GIMP

by Doug Lea

Link to the book at Amazon

Concurrency has always been a first-class citizen in Java. Threads, monitors (keyword "synchronized"), and guarded blocks ("wait"/"notify") have been part of the language since Java 1.0.

"Concurrent Programming in Java" is from 1999 and thus on the level of Java 1.2. Nevertheless, the described challenges of concurrent programming (security, liveness, performance) are still relevant today. So are the presented principles and design patterns, like synchronization and confinement, monitors, guarded waits and notifications, latches, worker threads, futures, fork/join, and many more.

Abstracting from this, the author introduces more complex, reusable utility classes for initiating and coordinating concurrent activities. Most of these classes are today – in an evolved form – part of the JDK. In the JavaDoc of these classes (e.g., Semaphore, ReentrantLock, Executor, or CountDownLatch – and in newer ones like CompletableFuture or StampedLock), you will find the signature of Doug Lea, the author of this book.

The book is not a light read. The writing style is academic and highly challenging. I had to read many paragraphs more than once and keep cross-referencing to other parts of the book to understand. With the examples, it is sometimes advisable to read the theory first, then the source code, and then the theory again. Every few pages, you have to take a break.

It took me several weeks to work through the entire work. But it is definitely worth the effort!

Once you have internalized the basics of this book, you will be able to write performant, thread-safe code and quickly recognize non-thread-safe code as well as the causes of race conditions. That will make you one of the most sought-after Java programmers.

Suitable as an audiobook? No, due to numerous diagrams and code examples.

Mastering Lambdas: Java Programming in a Multicore World

Mastering Lambdas - Maurice Naftalin

by Maurice Naftalin

Link to the book at Amazon

Mastering Lambdas is an excellent book about Lambdas and Streams, introduced in Java 8. Despite their presence in the title, Lambdas occupy only about the first quarter of the book. The major part describes Streams: how to use them, how to create them, and how to terminate them using Collectors and Reducers.

I recommend this book to both beginners and advanced users. Beginners can use the book as a step-by-step introduction to the functionality of Lambda and Streams. Advanced readers learn how to develop Spliterators to generate streams as well as custom Collectors and Reducers.

Suitable as an audiobook? No, due to numerous diagrams and code examples.

Bad Tests, Good Tests

Bad Tests Good Tests

by Tomek Kaczanowski

Link to the book at Amazon

Tests and test-driven development (TDD) have arrived in most IT teams. Tests are supposed to assure the correct behavior of new and existing features, to survive refactorings unscathed, as documentation to make it easier for new developers to get started with the code, and – on top of that – lead to clean(er) code.

Unfortunately, many tests are not worth the time spent developing them. They are poorly readable and maintainable. And they often need adjustments during refactorings because they don't verify the result of an operation but how the operation was executed.

Bad Tests, Good Tests uses numerous examples (about half of the book contains code) to explain what constitutes bad tests – examples as the author has found them in real projects. The author then contrasts each example with specific suggestions for improvement.

The range of shortcomings goes from poor readability (e.g., long setup or assertion blocks) and obvious errors (e.g., forgotten assertions) to incomplete tests (that don't fail despite functionality changes) and subtle errors (e.g., exceptions thrown in places other than expected) to completely useless tests (that, e.g., verify the test library instead of your code).

The book is exceptionally hands-on and suitable for both beginners and advanced developers.

Beginners don't have to repeat the mistakes that others have made before them. Advanced programmers will surely recognize one or the other anti-pattern from their daily work and can fill their toolbox with new techniques.

The author writes understandably and humorously. And he explicitly points out that one may also break his rules if it serves the cause in the individual case.

A clear recommendation for every Java developer.

Suitable as an audiobook? No, due to countless code examples.