Java Reference
In-Depth Information
< Day Day Up >
Chapter 1. Introduction
This topic 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 experienced programmer. Don't feel bad if you can't solve
them. They are grouped loosely according to the features they use, but don't assume that the trick to
a puzzle is related to its chapter heading; we reserve the right to mislead you.
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 topic is to entertain you with
puzzles while teaching you to avoid the underlying traps and pitfalls. By working through the
puzzles, you will 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.
This topic is meant to be read with a computer at your side. To get the most out of the puzzles,
you'll need a Java development environment, such as Sun's JDK [JDK-5.0] . It should support
release 5.0, as some of the puzzles rely on features introduced in this release. You can download the
source code for the puzzles from www.javapuzzlers.com . Unless you're a glutton for punishment,
we recommend that you do this before solving the puzzles. It's a heck of a lot easier than typing
them in yourself.
Most of the puzzles take the form of a short program that appears to do one thing but actually does
something else. It's your job to figure out what the program does. To get the most out of these
puzzles, we recommend that you take this approach:
1.
Study the program and try to 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.
Some of the puzzles require you to write a small amount of code. To get the most out of these
puzzles, we recommend that you try— at least briefly— to solve them without using a computer, and
then test your solution on a computer. If your code doesn't work, play around with it and see
whether you can make it work before reading the solution.
Unlike most puzzle topics, this one alternates between puzzles and their solutions. This allows you
to read the topic without flipping back and forth between puzzles and solutions. The topic 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.
 
 
Search WWH ::




Custom Search