Java Puzzlers: Traps, Pitfalls, and Corner Cases

by Joshua Bloch and Neal Grafter

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.

Other Recommendations From This Books’s Genres

* Disclosure: We love sharing our favorite books with you! As an Amazon Associate, we earn a small commission from purchases you make through our links, which helps us continue creating content you enjoy.