Below you will find the updates and bug reports for Volumes 1 and 2 of Core Java, Fifth Edition (J2SE 1.3/1.4).
http://www.horstmann.com/corejava.html should not be hyphenated.docs\api\index.html to docs/api/index.html.CM_PER_INCH = 2.54;.lastIndexOf., change "starting at index 0 or at fromIndex." to "starting at the end of the string or at fromIndex."
java.text.NumberFormat API entry:
String format(double number); from the end of
if (condition) statement1 else statement2;
else if {yourSales >= 1.5 * target) to else if (yourSales >= 1.5 * target).Your can retire in to You can retire in.GregorianCalendar calendar = new GregorianCalendar(). 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.”GregorianCalendar class makes it is simple..." to "As you can see, the GregorianCalendar class makes it simple..."
hireDay =tonew GregorianCalendar(year, month - 1, day);.
GregorianCalendar calendar
=new GregorianCalendar(year, month - 1, day).;
hireDay = calendar.getTime();.
Date class is not immutable—the setTime method can set the date to another point in time.
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.
String and Date classes are immutable" to "the String class is immutable"count and a static method getCount ” to “with a static field nextId and a static method getNextId.” private double salary.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++;
}
/home/user/classes to /home/user/classdir (5x) and c:\classes to c:\classdir (2x)if (n instanceof Double) Double d = (Double)n in the second note is uneven.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.
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."
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 ."
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."
Field f = cl.getField("name");
to
Field f = cl.getDeclaredField("name");
r += val.toString();
to
r += val; // calls val.toString()
(Note: The latter also works if val is null.)
while (cl != Object.class) to while (cl != null).
(This change is necessary so that the introspection works for the Object class itself).
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;
String and Date." to "belong to an immutable class, such as String".NumberFormat formatter
before the line
= NumberFormat.getCurrencyInstance();
start method is wrong. The correct indentation is shown in lines 36 through 57 on page 283 Frame., are the following ones”, to “the most important methods are the following ones”g.setPaint to g2.setPaint and g.drawString to g2.drawString.new URL(http to new URL("http.f.deriveFont(14); .to f.deriveFont(14F );
(Otherwise Font.deriveFont(int) is called, which sets the font style!)
double descent = bounds.getHeight() + bounds.getY();float fontHeight = metrics.getHeight();.with float descent = metrics.getDescent();.get-LineMetrics to getLineMetrics.getStringBounds methodif (keyCode == keyEvent.VK_RIGHT && event.isShiftDown()) ” to “if (keyCode == KeyEvent.VK_RIGHT && event.isShiftDown())”getModifiers method, change CONTROL_MASK to CTRL_MASK.find method from
@return the index of the first square that contains p
to
@return the first square that contains p
putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
below
putValue(Action.SMALL_ICON, icon);
static KeyStroke getKeyStroke(String description) on page 400.menuBar.addMenu(editMenu) to menuBar.add(editMenu).cutItem.setHorizontalTextPosition(SwingConstants.RIGHT)
to
cutItem.setHorizontalTextPosition(SwingConstants.LEFT)
public AboutDialog(JFrame owner) extends JDialog
{
. . .
}
to
public class AboutDialog extends JDialog
{
public AboutDialog(JFrame owner)
{
. . .
}
}
owner = null;
in the showDialog method after the line
if (dialog == null || dialog.getOwner() != owner)
JButton cancelButton = new JButton("Cancel");
okButton.addActionListener(new
to
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new
import java.aw1t.*; to import java.awt.*;.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
AppletFrame class:
public void setStream(String key, InputStream stream) {}
public InputStream getStream(String key) { return null; }
jar cfm MyArchive.jar manifest-additions.mf
to
jar ufm MyArchive.jar manifest-additions.mf
class AboutPanel extends JTextArea
to
class AboutPanel extends JPanel
JFrame frame = new JFrame();
to
JFrame frame = new EventTracerFrame();
super.fireActionPerformed(event);
textField.setText("No exception");
to
textField.setText("No exception");
super.fireActionPerformed(event);
toString method of the Hashtable class" to "It then uses the toString method of the Hash map class"e.writeData(in); to e.writeData(out);.
(n-1) * RECORD.SIZE to (n-1) * RECORD_SIZE.Manager constructor comments to
@param year the hire year @param month the hire month @param day the hire day
volatile from "not used" to "Ensure that a field is coherently accessed by multiple threads (see Volume 2)"strictfp .Use strict rules for floating-point computations" to the keyword table.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.