Updates: Fifth Edition

Below you will find the updates and bug reports for Volumes 1 and 2 of Core Java, Fifth Edition (J2SE 1.3/1.4).

Volume 1

Page xxi
The URL http://www.horstmann.com/corejava.html should not be hyphenated.
Page 23
In the tip, change docs\api\index.html to docs/api/index.html.
Page 46 Line -4
Change "method takes zero parameters" to "method takes no parameters"
Page 54
Remove the second semicolon after CM_PER_INCH = 2.54;.
Page 66
In the API note of lastIndexOf., change "starting at index 0 or at fromIndex." to "starting at the end of the string or at fromIndex."
Page 59
The three grey arrows in figure 3-1 are too dark. They should be a lighter shade of grey
Page 74
Add the following entry after the first three entries of the java.text.NumberFormat API entry:
Page 77
Remove the ; from the end of
if (condition) statement1 else statement2;
Page 78
Change else if {yourSales >= 1.5 * target) to else if (yourSales >= 1.5 * target).
Page 87
Change the numerator of the equation from "n × (n - 1) × (n - 1) × . . . × (n- k)" to "n × (n - 1) × (n - 2) × . . . × (n - k + 1)"
Page 83 Retirement.java
Change Your can retire in to You can retire in.
Page 90
In the CAUTION note, change “case” to “alternative” (3x)
Page 96
In the CAUTION note, change “an array with 100 element” to “an array with 100 elements
Page 108
Change "Instead, an array of five pointers is allocated" to "Instead, an array of ten pointers is allocated"
Page 126
Add a semicolon after GregorianCalendar calendar = new GregorianCalendar().
Page 127
Change “The variable weekday is set to 0 if the first day of the month is a Sunday, to 1 if it is a Monday, and so on.” to “The variable weekday is set to 1 (or Calendar.SUNDAY ) if the first day of the month is a Sunday, to 2 (or Calendar.MONDAY ) if it is a Monday, and so on.”
Page 127
Change "As you can see, the GregorianCalendar class makes it is simple..." to "As you can see, the GregorianCalendar class makes it simple..."
Page 131
Change
hireDay = new GregorianCalendar(year, month - 1, day);.
to
GregorianCalendar calendar
= new GregorianCalendar(year, month - 1, day).;
hireDay = calendar.getTime();.
Page 135 after the second note
Change "refererences" to "references".
Page 140
It turns out that the Date class is not immutable—the setTime method can set the date to another point in time.
  1. Reword the CAUTION note as follows [the red portions (stressed voice for aural screen readers) indicate changed text]:

    Be careful not to write accessor methods that return references to mutable objects. We violated that rule in our Employee class in which the getHireDay method returns an object of class Date:

    class Employee
    {
       . . .
       public Date getHireDay()
       {
          return hireDay;
       }
       . . .
       private Date hireDay;
    }

    This breaks the encapsulation! Consider the following rogue code:

    Employee Harry = . . .;
    Date d = harry.getHireDay();
    double tenYearsInMilliSeconds = 10 * 365.25 * 24 * 60 * 60 * 1000;
    d.setTime(d.getTime() - (long)tenYearsInMilliSeconds);
    

    The reason is subtle. Both d and harry.hireDay refer to the same object (see figure 4-5). Applying mutator methods to d automatically changes the private state of the employee object!

    If you need to return a reference to a mutable object, you should clone it first. A clone is an exact copy of an object that is stored in a new location. We will discuss cloning in detail in chapter 6. Here is the corrected code:

    class Employee
    {
       . . .
       public Date getHireDay()
       {
          return (Date)hireDay.clone();
       }
    }

    As a rule of thumb, always use clone whenever you need to return a copy of a mutable variable.

  2. In figure 4.5, change GregorianCalendar to Date.
Page 142
In the TIP, change "the String and Date classes are immutable" to "the String class is immutable"
Page 146
Change the last line from “with a static field count and a static method getCount ” to “with a static field nextId and a static method getNextId.”
Page 159
Change "There is a actually a third mechanism" to "There is actually a third mechanism"
Page 160
Add a semicolon after private double salary.
Page 163 ConstructorTest.java
Move the last two lines of the program
private int id;
private static int nextId;
above the object initialization block, like this:
// must define before use in initialization block
private int id;
private static int nextId;
// object initialization block
{
   id = nextId;
   nextId++;
}
Page 169/170
Change /home/user/classes to /home/user/classdir (5x) and c:\classes to c:\classdir (2x)
Page 192
Change "The compiler produces method tables" to "The virtual machine produces method tables".
Page 205:
Change "The Java Language Specification requires..." to "The Java Platform API Specification requires..."
Page 205:
The numbered list in the note uses the wrong font
Page 223
The spacing around the line if (n instanceof Double) Double d = (Double)n in the second note is uneven.
Page 230-232:
In ReflectionTest.java, change
System.out.print(Modifier.toString(c.getModifiers()));
System.out.print(" " + name + "(");

to

System.out.print("   " + Modifier.toString(c.getModifiers()));
System.out.print(" " + name + "(");
and make the same changes to print the fields and methods.
Page 232
1. Change "The getFields method returns an array containing Field objects for the public fields." to "The getFields method returns an array containing Field objects for the public fields of this class or its superclasses."
2. Change "The getDeclaredField method returns an array of Field objects for all fields." to "The getDeclaredFields method returns an array of Field objects for all fields of this class ."
3. Change the description of the getMethods/getDeclaredMethods methods to: "return an array containing Method objects: getMethods returns public methods and includes inherited methods, getDeclaredMethods returns all methods of this class or interface, but does not include inherited methods."
Page 234
Change
Field f = cl.getField("name");

to

Field f = cl.getDeclaredField("name");
Page 235 and 237 ObjectAnalyzerTest.java
Change
r += val.toString();

to

r += val; // calls val.toString()

(Note: The latter also works if val is null.)

Page 235 and 237: ObjectAnalyzerTest.java
Change while (cl != Object.class) to while (cl != null).

(This change is necessary so that the introspection works for the Object class itself).

Page 238 ObjectAnalyzerTest.java
Change
if (!f.get(a).equals(f.get(b)))
   return false;

to

if (f.get(a) == null) { if (f.get(b) != null) return false; }
else if (!f.get(a).equals(f.get(b))) return false;
Page 253
Change "-1 if the first employee's salary is less than 0" to "-1 if the first employee's salary is less than the second employee's salary"
Page 264
Change "belong to an immutable class, such as String and Date." to "belong to an immutable class, such as String".
Page 278
Add the line
NumberFormat formatter

before the line

= NumberFormat.getCurrencyInstance();
Page 281
The indentation of the code in the start method is wrong. The correct indentation is shown in lines 36 through 57 on page 283
Page 281
The opening brace in the 5th line of the first code example should be bold, to match the bold closing brace in the 4th line from the end
Page 303 4th line from bottom
Change “the most important methods, inherited from the base class Frame., are the following ones”, to “the most important methods are the following ones”
Page 324 Lines 2 and 3
Change g.setPaint to g2.setPaint and g.drawString to g2.drawString.
Page 331 Line 1
Change new URL(http to new URL("http.
Page 331 Line 6
Change f.deriveFont(14); .to f.deriveFont(14F );
(Otherwise Font.deriveFont(int) is called, which sets the font style!)
Page 332
  1. Change "The height of the rectangle is the sum of ascent and descent" to "The height of the rectangle is the sum of ascent, descent, and leading."
  2. Change "Thus, you can obtain string width, height, ascent, and descent as follows:" to "Thus, you can obtain string width, height, and ascent as follows:"
  3. Remove the line double descent = bounds.getHeight() + bounds.getY();
  4. Change "If you need to know the leading or total height" to "If you need to know the descent or leading"
  5. Replace the line float fontHeight = metrics.getHeight();.with float descent = metrics.getDescent();.
Page 332 13th line from bottom
Change get-LineMetrics to getLineMetrics.
Page 335 API note for the getStringBounds method
Change "The height of the rectangle equals the sum of ascent and descent" to "The height of the rectangle equals the sum of ascent, descent, and leading."
Page 377
Change “if (keyCode == keyEvent.VK_RIGHT && event.isShiftDown()) ” to “if (keyCode == KeyEvent.VK_RIGHT && event.isShiftDown())
Page 382
In the description of the getModifiers method, change CONTROL_MASK to CTRL_MASK.
Page 389 MouseTest.java
Change the comment of the find method from
@return the index of the first square that contains p

to

@return the first square that contains p
Page 394
Add the lines
putValue(Action.SHORT_DESCRIPTION,
   "Set panel color to " + name.toLowerCase());

below

putValue(Action.SMALL_ICON, icon);
Page 400/401
The Parameters section on top of page 401 should be placed above the bullet static KeyStroke getKeyStroke(String description) on page 400.
Page 410
In the Caution note, change "you can pass pass a mask of 0" to "you can pass a mask of 0".
Page 419
Change "The quick brown fox jumped over the lazy dog" to "The quick brown fox jumps over the lazy dog"
Page 420
Change "But if the user presses a key, then the controller may tell the view to scroll." to "But if the user presses a cursor key, then the controller may tell the view to scroll."
Page 439 Paragraph 4
Change "It not not always obvious" to "It is not always obvious"
Page 481
Change menuBar.addMenu(editMenu) to menuBar.add(editMenu).
Page 484
Change "by default, the menu icons are placed to the right of the menu text" to "by default, the menu item text is placed to the right of the icon".
Page 484
Change
cutItem.setHorizontalTextPosition(SwingConstants.RIGHT)

to

cutItem.setHorizontalTextPosition(SwingConstants.LEFT)
Page 484
Change “moves the menu item text to the right of the icon” to “moves the menu item text to the left of the icon”
Page 511
Change "Here are details about the box layout manager does:" to "Here are details about what the box layout manager does:"
Page 517
Change “see the listing in Example 9-13” to “see the listing in Example 9-14
Page 545
Change
public AboutDialog(JFrame owner) extends JDialog
{
   . . .
}

to

public class AboutDialog extends JDialog
{
   public AboutDialog(JFrame owner)
   {
      . . .
   }
}
Page 551 - 555 DataExchangeTest.java
  1. On page 555, remove
    owner = null;
    

    in the showDialog method after the line

    if (dialog == null || dialog.getOwner() != owner)
  2. On page 553, change
    JButton cancelButton = new JButton("Cancel");
    okButton.addActionListener(new

    to

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new
    
Page 571 ColorChooserTest.java Line 1
Change import java.aw1t.*; to import java.awt.*;.
Page 589
Change “the calculator application from Chapter 7” to “the calculator application from Chapter 9”
Page 602
Change
http://java.sun.com/products/plugin/1.1/jinstall-11-win32.cab#Version=1,3,0,0

to

http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0
Page 613
Change “You can tell the browser to show the applet in another window” to “You can tell the browser to show the document in another window”
Page 623 AppletFrame.java
To make this file compile with SDK 1.4, add these two lines to the AppletFrame class:
public void setStream(String key, InputStream stream) {}
public InputStream getStream(String key) { return null; }
Page 626
Change
jar cfm MyArchive.jar manifest-additions.mf

to

jar ufm MyArchive.jar manifest-additions.mf
Page 631 ResourceTest.java Line 21
Change
class AboutPanel extends JTextArea

to

class AboutPanel extends JPanel
Page 672 EventTracerTest.java
Change
JFrame frame = new JFrame();

to

JFrame frame = new EventTracerFrame();
Page 655 ExceptTest
Swap lines 157 and 158, that is, change
super.fireActionPerformed(event);
textField.setText("No exception");

to

textField.setText("No exception");
super.fireActionPerformed(event);
Page 660
Change "It then uses the toString method of the Hashtable class" to "It then uses the toString method of the Hash map class"
Page 666 Paragraph before the C++ note
Change "then the compiler loads the inner class" to "then the virtual machine loads the inner class".
Page 736
Change e.writeData(in); to e.writeData(out);.
Page 736
Change (n-1) * RECORD.SIZE to (n-1) * RECORD_SIZE.
Page 744 ObjectFileTest.java
Change the Manager constructor comments to
@param year the hire year
@param month the hire month
@param day the hire day
Page 778
Change the entry for volatile from "not used" to "Ensure that a field is coherently accessed by multiple threads (see Volume 2)"
Page 778
Add a table entry "strictfp .Use strict rules for floating-point computations" to the keyword table.

Volume 2

Page xxi
Change "The book that you hold in your hands is the second volume of the fourth edition..." to "The book that you hold in your hands is the the second volume of the fifth edition..."
Page 163 Figure 2-11
Change "LinkedSet" to "LinkedList"
Page 180 API for rotate.
Change "the list to reverse" to "the list to rotate"

Thanks to Dan Blanks, Gary Boackle, Ditlev Brodersen, Michael Ciaccio, Mike Cardosa, Bruce J Cathey, Jason Cisneros, Gill Cleeren, Philip B. Copp III, Mike Cordes, Victor Cosby, Andrew Crites, Jonathan Crowther, William Dietrich, Paul Dyer, Mike Doolittle, Charles Evans, Don Gaze, Matthew Gillman, Fred Graf, Alexander Gülich , Steighton Haley, Mazhar Islam, Arnold Juster, Al Layton, Fox Louie, Howard Lum, Dan Manning, Charles MacLeod, Eleanor Mencher, Marlene Miller, Jeff Morgan, Joseph Ng, John Nguyen, Seamus O'Keefe, Douglas Parent, Matt Payne, György Pongor, Boris Pulatov, John C. Roberts, Joe Rudy, Dave Sandilands, Brian S. Scott, David Sheth, Sean A. Smith, Bill Sorensen, Jack Stover, Christopher Steele, Mark Stratman, Christopher Tan, Jeff Tash, Andreas Thomassen, Leonardo Topa, Thierry Tung, Russell Van Bert, Heikki Virkkunen, Robert G. Walker, Norman Witkin, Baihao Yuan and a number of anonymous contributors for their bug reports!

Last Modified: 2002-10-28.