The Barnes & Noble Review
Think you know Java? Really know it? Try the 95 Java puzzles in this book: You just might not know the language as well as you thought. Don’t worry if Java Puzzlers' mini-programs throw you for an occasional loop (or, perhaps, an exception). Each one illustrates some intriguing pitfall, trap, or surprise you really ought to know about. Read them. Think about them. Try them. Then let Joshua Bloch and Neal Gafter unravel them. By the time you’re done, you’ll be writing more robust, resilient, bug-resistant code.
Many of these puzzles are deceptively simple. Why does a method that purports to determine if its sole argument is an odd number fail 25 percent of the time? Why does a program that divides two numbers get the answer 5 when it ought to return 1000? (Lesson: “When working with large numbers, watch out for overflow -- it’s a silent killer.”) Some take you into shrouded areas of Java. For example, one puzzle tests your knowledge of the “question mark colon” conditional operator; you’ll only get it right if you know Java’s arcane rules for determining the result type of a conditional expression. Some are just plain devilishly difficult. For example, in one, the answer can only be found in compiler-generated bytecode.
The authors’ puzzles cover the waterfront: strings, characters, text, loops, iteration, exceptions, try-finally statements, classes, methods, fields, collections, dates, inheritance, overriding, threading, reflection, I/O, nested classes, generics, serialization, binary compatibility, and more. They won’t just stretch your brain: they’ll help you avoid problems you never knew existed. Bill Camarda, from the August 2005 Read Only
Read an Excerpt
Like many books, this one had a long gestation period. We've collected Java puzzles for as long as we've worked with the platform: since mid-1996, in case you're curious. In early 2001, we came up with the idea of doing a talk consisting entirely of Java puzzles. We pitched the idea to Larry Jacobs, then at Oracle, and he bought it hook, line, and sinker.
We gave the first "Java Puzzlers" talk at the Oracle Open World conference in San Francisco in November 2001. To add a bit of pizazz, we introduced ourselves as "Click and Hack, the Type-it Brothers" and stole a bunch of jokes from Tom and Ray Magliozzi of Car Talk fame. The presentation was voted best-in-show, and probably would have been even if we hadn't voted for ourselves. We knew we were on to something.
Dressed in spiffy blue mechanic's overalls emblazoned with the "cup and steam" Java logo, we recycled the Oracle talk at JavaOne 2002 to rave reviewsat least from our friends. In the years that followed, we came up with three more "Java Puzzlers" talks and presented them at countless conferences, corporations, and colleges in cities around the globe, from Oslo to Tokyo. The talks were almost universally well liked, and we got very little fruit thrown at us. In the March 2003 issue of Linux Magazine, we published an article consisting entirely of Java puzzles and received almost no hate mail. This book contains nearly all the puzzles from our talks and articles and many, many more.
Although this book draws attention to the traps and pitfalls of the Java platform, we do not mean to denigrate it in any way. It is because we love the Java platform that we've devoted nearly a decade of our professional lives to it. Every platform with enough power to do real work has some problems, and Java has far fewer than most. The better you understand the problems, the less likely you are to get hurt by them, and that's where this book comes in.
Most of the puzzles in the book focus on short programs that appear to do one thing but actually do something else. That's why we've chosen to decorate the book with optical illusionsdrawings that appear to be one thing but are actually another. Also, you can stare at them while you're trying to figure out what in the world the programs do.
Above all, we wanted this book to be fun. We sincerely hope that you enjoy solving the puzzles as much as we enjoyed writing them and that you learn as much from them as we did.
And by all means, send us your puzzlers! If you have a puzzle that you think belongs in a future edition of this book, write it on the back of a $20 bill and send it to us, or e-mail it to firstname.lastname@example.org. If we use your puzzle, we'll give you credit.
Last but not least, don't code like my brother.
Josh Bloch Neal Gafter San Jose, California May 2005