Java Puzzlers Errata

This page contains the errata - those things that are wrong or lead you astray in the first edition of the book. Many of these errata were discovered by Yoshiki Shibata in the process of translating the book into Japanese, and we thank him for his diligent efforts.

If you find an error in the book, please check to see if it is already known before reporting it. If you do not find it in the list, please mail us the relevant information, including page numbers. All errata will be fixed in the next printing.

Page ix: Contents

The section, "A Glossary of Name Reuse" was omitted from the table of contents. Its entry should be:

A Glossary of Name Reuse........180


The name "Pablo Belver" is spelled incorrectly. The correct spelling is:

Pablo Bellver

Page xiv: Preface

The sentence that reads "An English translation encompassing both volumes [of Akiyoshi Kitaoka's Trick Eyes] is coming soon [Kitaoka05]." is no longer correct. We are happy to report that the English translation of Trick Eyes is now available! The sentence should now read:

An English translation encompassing both volumes is available [Kitaoka05]."

Page 3: Introduction

The final paragraph of the introduction begins:" The appendix of this book is a catalog of the traps and pitfalls in the Java platform." It should instead read:

Appendix A is a catalog of the traps and pitfalls in the Java platform.

The following paragraph should be added to the end of the introduction:

  Appendix B describes the optical illusions that decorate the book. This appendix explains the nature of each illusion and identifies the inventor, if known. Many bibliographic references are provided. This appendix is the place to go if you aren't sure why a given drawing constitutes an illusion, or if you simply want to know more about one of the illusions.

Page 82: Solution 38

The error message, "" has an incorrect line number. It should be:

Page 85: Solution 39

The reference to System.halt should read:


Page 103: Figure 1

Within the figure, the key entry "Execution of workout" has an incorrect function name. It should be:

Execution of workHard

The figure has the caption, "The execution of WorkOut. The application name is incorrectly capitalized. The caption should be:

The execution of Workout

Page 114: Solution 50

The sentence that begins, "This turns out be the most" should be:

This turns out to be the most

Page 118: Solution 52

The sentence at the bottom of the page is difficult to understand. It says, "C++ addresses the problem by changing the type of the object from the superclass type to the subclass type during construction." The following is a better explanation:

C++ addresses the problem by treating the type of the object as the superclass type instead of the subclass type during execution of the superclass constructor. In effect, the type of the object changes from the superclass type to the subclass once the superclass constructor finishes executing.

Page 132: Solution 56

The revised code in the solution throws Exception but doesn't have to. It should be:

public class BigProblem {
    public static void main(String [] args) {

Page 148: Solution 63

The sentence that ends, "a public parameterless constructor that does nothing beyond initializing the field of the instance that it creates." uses the singular, field, instead of the plural, fields. It should be:

a public parameterless constructor that does nothing beyond initializing the fields of the instance that it creates.

Page 148: Solution 63

In the sentence that begins, "Fixing the program is as simple as removing the void return type from the Names declaration". The class name Names should be MoreNames:

Fixing the program is as simple as removing the void return type from the MoreNames declaration


Page 149: Puzzle 64

The puzzle calls the println method, but the example output is on a single line, so it should call the print method:

for (int j = 0; j < MODULUS; j++)
    System.out.print(histogram[j] + " ");

Page 151: Solution 64

The sixth line contains the words, "the remainder when this value is divided by 2." The 2 should be 3:

the remainder when this value is divided by 3.

Page 151: Solution 64

The last line of the second paragraph ends "Integer.MIN_VALUE % 3 or 2.". The 2 should be -2:

Integer.MIN_VALUE % 3 or -2.

Page 155: Solution 65

This is the most embarrassing error found in Java Puzzlers to date. We were hoisted by our own petard! The suggested solution to Puzzle 65 is wrong:

  public int compare(Integer i1, Integer i2) {
      return (i2 < i1 ? -1 : (i2 == i1 ? 0 : 1));

We fell right into the trap that we described in Puzzle 32 (Curse of Looper)! The types of i1 and i1 are Integer. When we use the comparison operators < and >, we get auto-unboxing, which results in value comparisons. But the equality operator (==) results in an object reference identity comparison. As a consequence, this comparator is broken. It isn't broken enough to cause the sort to fail, but still, it's broken. It will incorrectly report that the first of two equal but non-identical Integer instances is greater than the second. To fix the problem, rewrite the comparator to avoid the use of the == operator:

  public int compare(Integer i1, Integer i2) {
      return (i2 < i1 ? -1 : (i2 > i1 ? 1 : 0));

The moral of this story is that most of the traps described in Java Puzzlers represent real dangers, no matter how skilled a programmer you are. If you have access to static analysis tool to help you find these problems in your own code, such as findbugs, we strongly recommend that you take advantage of them.

Page 156: Solution 65

The last paragraph has a sentence that reads, in part, "support for some from of integer arithmetic that does" The word from should be form:

support for some form of integer arithmetic that does

Page 168: Solution 70

The solution contains the sentences, "When the program calls printMessage from within the package hack, the package-private method hack.TypeIt.ClickIt.printMessage is run. This method prints Click, which explains the observed behavior." The sentences contain an incorrect package and an incorrect method name. The sentences should be:

When the program calls printMessage from within the package click, the package-private method click.CodeTalk.printMessage is run. This method prints Click, which explains the observed behavior.

Page 202: Solution 83

The method deepCopy begins static public Object deepCopy. It should begin:

public static Object deepCopy

Page 222: Solution 90

The comment for the first call to super should not refer to the Object constructor. The comment should be:

invokes Outer() constructor

Pages 222,223: Solution 90

The default constructor of a default access (package-private) class is package-private, so the Inner1 and Inner2 constructors should have the public modifiers removed. The definition of the Outer class on page 222 should be:

public class Outer {
    public Outer() { }

    class Inner1 extends Outer {
        Inner1() {
            super(); // invokes Outer() constructor

    class Inner2 extends Inner1 {
        Inner2() {
            super(); // invokes Inner1() constructor

The definition of the Outer class on page 223 should be similarly modified.

Page 227: Solution 91

A sentence in the fourth paragraph begins, "Because HashSet, HashMap, and HashTable are" The third class name is incorrectly capitalized. The sentence should begin:

Because HashSet, HashMap, and Hashtable are

Page 243: Trap 3.1

References [JLS3 4.2.3] should be:

[JLS 4.2.3]

Page 245: Trap 4.5

The wrong form of the verb "to represent" is used in the prescription. It should read:

Avoid exporting constant fields unless they represent true constants that will never change.

Page 253: Appendix A

The introductory sentence for Section 11 should be:

This section concerns the package

Page 261: Appendix A

The page numbers of the cushion and bulge illusions were reversed. The third complete paragraph on the page should begin:

The Cushion illusion (page 65)

The fourth sentence of this paragraph should begin:

The Bulge illusion (page 152)

Page 265: References
Page 6: Solution 1
Page 70: Solution 32

The reference labels [Boxing] and [Boute92] have been reversed. They should be:

[Boxing] Autoboxing. Sun Microsystems. 2004.

[Boute92] Boute, Raymond. "The Euclidean definition of the functions div and mod." In ACM Transactions on Programming Languages and Systems, Vol. 14, No. 2 (April 1992): 127-144.

As a result, the reference [Boxing] on p. 6 refers to the correct paper, but the paper's label should be:


The reference to [Boxing] on p. 70 uses the correct label but refers to the wrong paper. It should refer to the autoboxing web page.

Page 271: Index

The index term "/ division operator" refers to page 44. That page should not appear. The index entry should be:

/ division operator, 6

Page 276: Index

The index entry "instanceOf, 113-114" should be:

instanceof, 113-114

Page 279: Index

The index entry printWriter.printf should be:


Page 280: Index

The index entry "Serializable.readResolve, 203" should be:

   readResolve method and, 203

Page 282: Index

The index term "white space", subcategory "for grouping" has the wrong page number. It should be:



Copyright 2004-2005, Joshua Bloch and Neal Gafter