ERRATA
Effective Java
by Joshua Bloch
Foreword by Guy Steele
 

 

This page contains the errata in the second edition of the book. The parenthesized number on the first line of each entry on this page is the printing in which the error was fixed.

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 me the relevant information, including page numbers.


Page 15: Item 2, Consider a builder when faced with many constructor parameters (6)
The sentence "The exception's detail method should indicate which invariant is violated (Item 63)." should be:
The exception's detail message should indicate which invariant is violated (Item 63).

Page 18: Item 3, Enforce the singleton property with a private constructor or an enum type (2)
The sentence "Often neither of these advantages is relevant, and the final-field approach is simpler" should be:
Often neither of these advantages is relevant, and the public-field approach is simpler

Page 23: Item 5, Avoid creating unnecessary objects (6)
The code example contains a fencepost error. The loop beginning for (long i = 0; i < Integer.MAX_VALUE; i++) { should begin:
for (long i = 0; i <= Integer.MAX_VALUE; i++) {

Page 75: Item 15, Minimize mutability (3)
The hashDouble method should be static and its reference to field re should be to the parameter val. The method should look like this:
private static int hashDouble(double val) {
    long longBits = Double.doubleToLongBits(val);
    return (int)(longBits ^ (longBits >>> 32));
}

Page 88: Item 17, Design and document for inheritance or else prohibit it (3)
In the documentation of the removeRange method, the reference to ArrayList should be to AbstractList. The sentence should read:
This call shortens the AbstractList by (toIndex - fromIndex) elements.

Page 111: Item 23, Don't use raw types in new code (3)
The sentence that begins, "As an added benefit, you no longer have to cast manually when removing elements" should begin:
As an added benefit, you no longer have to cast manually when retrieving elements

Page 121: Item 25, Prefer lists to arrays (2)
In the code example at the bottom of the page, the the loop should iterate over snapshot, not the original list! In other words, it should be:
for (Object o : snapshot)
    result = f.apply(result, o);

Page 125: Item 26, Favor generic types (3)
The reference to Item 44 should be to Item 56. The sentence containing the reference should end:
the conventional name for this parameter is E (Item 56).

Page 129: Item 27, Favor generic methods (3)
The reference to Item 44 should be to Item 56. The sentence containing the reference should be:
The naming conventions for type parameters are the same for generic methods as for generic types (Items 26, 56)

Page 152: Item 30, Use enums instead of int constants (3)
The reference to the JLS 14.2.1 should be to JLS 14.21. The sentence containing the reference should end:
the end of the method is technically reachable, even though it will never be reached [JLS, 14.21].

Page 155: Item 30, Use enums instead of int constants (3)
The break is misplaced. It should be before default: label, not after it. The code should be:
switch(this){
  case SATURDAY: case SUNDAY:
    overtimePay = hoursWorked * payRate / 2;
    break;
  default: // Weekdays
    ...

Page 163: Item 33, Use EnumMap instead of ordinal indexing (2)
In the code example at the bottom of the page, the fields src and dst should be made private (as per the advice of Item 13). In other words, it should be:
Public enum Transition {
    ...
    private final Phase src;
    private final Phase dst;
}

Page 167: Item 34, Emulate extensible enums with interfaces (6)
The description of the opSet parameter has an extra left parenthesis. It should be:
(<T extends Enum<T> & Operation> Class<T>)

Page 168: Item 34, Emulate extensible enums with interfaces (6)
The arguments 4 and 2 are reversed in the sentence that introduces the output of the example program. The sentence should read:
Both programs above will produce this output when run with command line arguments 4 and 2.

Page 170: Item 35, Prefer annotations to naming patterns (6)
The Sample class has seven static methods, not eight. The sentence describing the class should be:
The Sample class has seven static methods, four of which are annotated as tests.

Page 175: Item 35: Prefer annotations to naming patterns (2)
The sentence that ends "(Items 36 and Item 24)" should end:
(Items 36 and 24)

Page 181: Chapter 7, Methods (3)
The sentence that begins "Like Chapter 5, this chapter" should begin:
Like Chapter 4, this chapter

Page 185: Item 39, Make defensive copies when needed (3)
The start and end arguments to the IllegalArgumentException constructor should be this.start and this.end. It should be:
throw new IllegalArgumentException(this.start + " after " + this.end);

Page 207: Item 44, Write doc comments for all exposed api elements (3)
The bound of the wildcard type should be Throwable rather than Exception. In other words, the member declaration should be:
Class<? extends Throwable> value();

Page 207: Item 44, Write doc comments for all exposed api elements (6)
The file package-info.java must contain a package declaration. The sentence describing the file should be:
In addition to package-level doc comments, package-info.java must contain a package declaration and may contain package annotations.

Page 210: Item 45, Minimize the scope of local variables (3)
The parenthetical "(This region consists of the body of the loop as well as the initialization, test, and update preceding the body.)" should be:
(This region consists of the body of the loop and the code in parentheses between the for keyword and the body.)

Page 212: Item 46, Prefer for-each loops to traditional for loops (6)
The sentence "The iterator and the index variable occur three times in each loop, which gives you two chances to get them wrong." should be:
The iterator occurs three times in each loop, and the index variable four, which gives you many chances to use the wrong variable.

Page 229: Item 52, Refer to objects by their interfaces (3)
In the sentence that ends, "classes that implement an interface but provide extra methods not found in the interface—for example, LinkedHashMap," the class PriorityQueue should replace LinkedHashMap. The sentence should end:
classes that implement an interface but provide extra methods not found in the interface—for example, PriorityQueue

Page 232: Item 54, Use native methods judiciously (3)
The error message should be for MakeSet.java, not SetEx.java. It should be:
Note: MakeSet.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Page 261: Item 66, Synchronize access to shared mutable data (3)
The while loop and if statement that check the variable done should check stopRequested. The code should be:
while (!stopRequested)
  i++;

and
if (!stopRequested)
  while (true)
    i++;

Page 267: Item 67, Avoid excess synchronization (6)
The code example contains a copy-and-paste error. The code
catch(InterruptedException ex) {
    throw new AssertionError(ex.getCause());
}
should be:
catch(InterruptedException ex) {
    throw new AssertionError(ex);
}

Page 271: Item 68, Prefer executors and tasks to threads (6)
The first line should reference Item 49. It should be:
The first edition of this book contained code for a simple work queue [Bloch01, Item 49].

Page 293: Item 74, Implement Serializable judiciously (2)
The sentence that begins "Note that the initialized field is an atomic reference" should begin:
Note that the init field is an atomic reference
A sentence was added after the one that ends "the second thread might see the instance in an inconsistent state". It now reads:
the second thread might see the instance in an inconsistent state. This pattern, in which compareAndSet is used to manipulate an atomic reference to an enum, is a good general-purpose implementation of a thread-safe state machine.

Page 309: Item 77, For instance control, prefer enum types to readResolve (2)
In the Elvis class, the readResolve declaration should not say throws ObjectStreamException (as a matter of style). It should be:
priave Object readResolve() {
    return INSTANCE;
}

Page 311: Item 77, For instance control, prefer enum types to readResolve (6)
In the sentence that begins, "You could fix the problem by declaring the favorites field transient, but you're better off fixing it...", the reference to the favorites field should be to favoriteSongs. The sentence should begin:
You could fix the problem by declaring the favoriteSongs field transient, but you're better off fixing it...