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.
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).
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
The code example contains a fencepost error. The loop beginningfor (long i = 0; i < Integer.MAX_VALUE; i++) {
should begin:for (long i = 0; i <= Integer.MAX_VALUE; i++) {
ThehashDouble
method should bestatic
and its reference to fieldre
should be to the parameterval
. The method should look like this:private static int hashDouble(double val) { long longBits = Double.doubleToLongBits(val); return (int)(longBits ^ (longBits >>> 32)); }
In the documentation of theremoveRange
method, the reference toArrayList
should be toAbstractList
. The sentence should read:This call shortens theAbstractList
by(toIndex - fromIndex)
elements.
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
In the code example at the bottom of the page, the the loop should iterate oversnapshot
, not the original list! In other words, it should be:for (Object o : snapshot) result = f.apply(result, o);
The reference to Item 44 should be to Item 56. The sentence containing the reference should end:the conventional name for this parameter isE
(Item 56).
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)
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].
int
constants (3)
Thebreak
is misplaced. It should be beforedefault:
label, not after it. The code should be:switch(this){ case SATURDAY: case SUNDAY: overtimePay = hoursWorked * payRate / 2; break; default: // Weekdays ...
EnumMap
instead of ordinal indexing (2)
In the code example at the bottom of the page, the fieldssrc
anddst
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; }
The description of theopSet
parameter has an extra left parenthesis. It should be:(<T extends Enum<T> & Operation> Class<T>)
The arguments4
and2
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 arguments4
and2
.
TheSample
class has seven static methods, not eight. The sentence describing the class should be:TheSample
class has seven static methods, four of which are annotated as tests.
The sentence that ends "(Items 36 and Item 24)" should end:(Items 36 and 24)
The sentence that begins "Like Chapter 5, this chapter" should begin:Like Chapter 4, this chapter
Thestart
andend
arguments to theIllegalArgumentException
constructor should bethis.start
andthis.end
. It should be:throw new IllegalArgumentException(this.start + " after " + this.end);
The bound of the wildcard type should beThrowable
rather thanException
. In other words, the member declaration should be:Class<? extends Throwable> value();
The filepackage-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.
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 thefor
keyword and the body.)
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.
In the sentence that ends, "classes that implement an interface but provide extra methods not found in the interface—for example,LinkedHashMap
," the classPriorityQueue
should replaceLinkedHashMap
. The sentence should end:classes that implement an interface but provide extra methods not found in the interface—for example,PriorityQueue
The error message should be forMakeSet.java
, notSetEx.java
. It should be:Note: MakeSet.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Thewhile
loop andif
statement that check the variabledone
should checkstopRequested
. The code should be:andwhile (!stopRequested) i++;if (!stopRequested) while (true) i++;
The code example contains a copy-and-paste error. The codeshould be:catch(InterruptedException ex) { throw new AssertionError(ex.getCause()); }catch(InterruptedException ex) { throw new AssertionError(ex); }
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].
Serializable
judiciously (2)
The sentence that begins "Note that theinitialized
field is an atomic reference" should begin:Note that theinit
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 whichcompareAndSet
is used to manipulate an atomic reference to an enum, is a good general-purpose implementation of a thread-safe state machine.
readResolve
(2)
In the Elvis class, the readResolve declaration should not saythrows ObjectStreamException
(as a matter of style). It should be:priave Object readResolve() { return INSTANCE; }
readResolve
(6)
In the sentence that begins, "You could fix the problem by declaring thefavorites
fieldtransient
, but you're better off fixing it...", the reference to thefavorites
field should be tofavoriteSongs
. The sentence should begin:You could fix the problem by declaring thefavoriteSongs
fieldtransient
, but you're better off fixing it...