A Java Puzzlers Sampler
This sampler contains one puzzle from each chapter of Java Puzzlers by Joshua Bloch and Neal Gafter (Addison Wesley, 2005). The book is filled with brainteasers about the Java programming language and its core libraries. Anyone with a working knowledge of Java can understand these puzzles, but many of them are tough enough to challenge even the most experiencedprogrammer. Most of the puzzles exploit counterintuitive or obscure behaviors that can lead to bugs. These behaviors are known as traps, pitfalls, and corner cases. Every platform has them, but Java has far fewer than other platforms of comparable power. The goal of the book is to entertain you with puzzles while teaching you to avoid the underlying traps and pitfalls. By working through the puzzles, youwill become less likely to fall prey to these dangers in your code and more likely to spot them in code that you are reviewing or revising. The book is meant to be read with a computer at your side. You’ll need a Java development environment, such as Sun’s JDK. It should support release 5.0, as some puzzles rely on features introduced in this release. Most of the puzzles take the form of a shortprogram that appears to do one thing but actually does something else. That’s why we’ve chosen to decorate the book with optical illusions—drawings that appear to be one thing but are actually another. It’s your job to figure out each the program does. To get the most out of these puzzles, we recommend that you take this approach:
A Java Puzzlers Sampler
1. Study the program and tryto predict its behavior without using a computer. If you don’t see a trick, keep looking. 2. Once you think you know what the program does, run it. Did it do what you thought it would? If not, can you come up with an explanation for the behavior you observed? 3. Think about how you might fix the program, assuming it is broken. 4. Then and only then, read the solution. Unlike most puzzle books,this one alternates between puzzles and their solutions. This allows you to read the book without flipping back and forth between puzzles and solutions. The book is laid out so that you must turn the page to get from a puzzle to its solution, so you needn’t fear reading a solution accidentally while you’re still trying to solve a puzzle. We encourage you to read each solution, even if you succeed insolving the puzzle. The solutions contain analysis that goes well beyond a simple explanation of the program’s behavior. They discuss the relevant traps and pitfalls, and provide lessons on how to avoid falling prey to these hazards. Like most best-practice guidelines, these lessons are not hard-and-fast rules, but you should violate them only rarely and with good reason. Most solutions containreferences to relevant sections of The Java™ Language Specification, Third Edition [JLS]. These references aren’t essential to understanding the puzzles, but they are useful if you want to delve deeper into the language rules underlying the puzzles. Similarly, many solutions contain references to relevant items in Effective Java™ Programming Language Guide [EJ]. These references are useful if youwant to delve deeper into best practices. Some solutions contain discussions of the language or API design decisions that led to the danger illustrated by the puzzle. These “lessons for language designers” are meant only as food for thought and, like other food, should be taken with a grain of salt. Language design decisions cannot be made in isolation. Every language embodies thousands of designdecisions that interact in subtle ways. A design decision that is right for one language may be wrong for another. The book contains two appendices. Appendix A is a catalog of the traps and pitfalls in the Java platform. It provides a concise taxonomy of the anomalies exploited by the puzzles, with references back to the puzzles and to other relevant resources. Appendix B is describes the optical...
Leer documento completo
Regístrate para leer el documento completo.