Errata

Java Cookbook

Errata for Java Cookbook

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted By Date submitted Date corrected
Printed
Page 3

http://www.japhar.org

NOW READS:
http://www.hungry.com/old-hungry/products/japhar/

Anonymous    Jul 01, 2005
Printed
Page 118

http://lavarand.sgi.com

NOW READS:
http:// www.lavarand.org

Anonymous    Jul 01, 2005
Printed
Page 155
6.1 footnote

"Date's getTime() returns the time in seconds" -> NOW READS "...milliseconds"

Anonymous    Jul 01, 2005
Printed
Page 165

The text under the example should read 2 11/12 years not 3 11/12 years
(from the end of 2000 to the end of 2003 is about 3 years, not 4).

Anonymous    Mar 09, 2009
Printed
Page 190
5 lines from bottom

List l ist = new ArrayList();
should be:
List list = new ArrayList();

Anonymous    Mar 09, 2009
Printed
Page 195
last paragraph

hashSet requires that an objects' hashCode() method returns the
same value as another object which is considered equal as well as
its equals method returning tue. The hashCode method is called first,
only if this returns the same value as one already in the set is the
equals method called.

Note from the Author or Editor:
Will clarify wording in the upcoming 3E. Thanks.

Anonymous   
Printed
Page 242-243
Singleton section

Serialization of a singleton class isn't handled (extra objects could be created via serialization). Also (should this be a separate submission?) in the variation with making all methods static it is claimed "but this works only if methods do not need to share state"--which is incredibly false. (static fields?)

Thirdly: singletonList() etc. are not singleton classes. They return a new instance every time they're called. emptyList() (or EMPTY_LIST) etc. are what you were looking for.

Note from the Author or Editor:
This will be corrected in the upcoming 3E.

Anonymous   
Printed
Page 254
1-line code example at top of page

1) It is probably preferable to use System.out.printf() instead of System.out.format();
they behave identically, but printf() is the name that has been used
for this functionality since the early 1970's in a wide range of programming languages
2) It is definitely better to omit the "1$", "2$" etc unless they
are in some weird order, since the defaults do the obvious thing.
3) The example should arguably end with the newline printer, %n

So the example could be re-written as follows (and will be in a new code file PrintfDemo):

System.out.printf("%04d - the year of %f%n", 1951, Math.PI);

Anonymous    Mar 09, 2009
Printed
Page 273
code in middle of the page

PrintWriter toSwedish = new PrinterWriter(

NOW READS:
PrintWriter toSwedish = new PrintWriter(

Anonymous    Jul 01, 2005
Printed
Page 354
13.8 solution code

g.drawImage(0, 0, myImage, this)

NOW READS:
g.drawImage(myImage,0, 0, this)

Anonymous    Jul 01, 2005
Printed
Page 355
loadImage() method, line 2

The commented line assigning an Image to the image variable:
Image = getImage(...

NOW READS:
image = getImage(...

Anonymous    Jul 01, 2005
Printed
Page 586
source code, after //Enable logging

DriverManager.setLogStream()

NOW READS:
DriverManager.setLogWriter(new PrintWriter(System.err));

Anonymous    Jul 01, 2005
Printed
Page 697
2nd code example, 2nd line

indexof
should be: indexOf

Anonymous    Mar 09, 2009
Printed
Page 702
Code following 1st paragraph,1st line

Lock theLock = ....

NOW READS:
Lock lock = ....

Anonymous    Jul 01, 2005
Printed
Page 710
near bottom

On p. 708 MAX was set to 10 and it looked like the list should be held to that size, but according to the text here on 710, "List size now 118" and "I'm happy to report that all is well with this."

Hopefully either the code could be changed to not seem to claim that the list will have an enforced maximum size or the "all's well" could be changed to mention that while there's been success with the deadlocking issues, threading is complex enough that problems remain, and look "here in another O'Reilly book" for how to deal with that.

I'm assuming above that the code doesn't actually lock, but examining it suggests that if the list is full (and the code enforces this) or if the list is empty, the producers or the consumers respectively might not get to the "if (done) break;" part of the code before waiting again.

Note from the Author or Editor:
Code was updated in the online repo some time ago and will be included in the upcoming 3d edition.

Anonymous   
Printed
Page 713
Recipe 24.8, example 24-13, the last lines of this example

Last line attempts to end the operation by setting the "done" flag to true.

// End of simulation - shut down gracefully
pc.done = true;

This flag controls both producer and consumer loops, and is a concurrent variable. It is written to by the main thread, and read by both producer and consumer threads.

Access to this flag is not synchronized. While writing a boolean value is defined as an atomic operation, this doesn't mean the value will be visible to any other threads.

In short, either access to this field should be synchronized or it should be marked "volatile". An alternative is to inject "poison objects" into the queue and have consumers terminate gracefully upon reading them.

(This error doesn't appear in example 24-12, where access to the field is properly synchronized.)

Note from the Author or Editor:
This also was changed in the code some time ago, by setting the "volatile" modifier.

Anonymous