Chapter | Page | Description |
---|---|---|
4 | 61 | Should mention package protection (see footnote) (rw) |
4 | 64 | Exercise 5, method is called substring, not substr (rw) |
5 | 69 | Balls can run off screen (see balls at the edge of ball worlds) (jk) |
5 | 69 | No particular reason why FrameWidth and FrameHeight should be public, would be better as private (rw) |
5 | 73 | use of short names dx and dy, see short names |
5 | 77 | Sec 5.5, par 3, See earlier comments on package protection (rw) |
6 | 85 | method named dy easily confused with similarly named field in Chapter 5, suggest flip (jk) |
6 | 93 | First line of code, Fire=>fire (lower case f) (jk) |
7 | 117 | Line 8, should use x and y rather than recomputing e.getX() (nm) |
8 | 125 | 8.3 par 2, ``subclasses of PinBallTarget'' => implementations (see also page 162) (jane) |
10 | 162 | line 1, ``subclasses'' => ``implementations'' (PinBallTarget is an interface, not a class (nm) |
11 | 183 | Dicussion of clone misleading (see clones) |
11 | 187 | Line -10, "abc" should be ``new String("abc")'' (see footnote on String Constants) (mark) |
11 | 188 | Line 6, ``between objects'' => ``between objects of the class'' (rw) |
11 | 188 | Line -2, ``associative'' => ``transitive'' (rw) |
12 | 203 | par 2, remove ``is deferred, it'' (rw) |
13 | 228 | why is variable dlg final? (I don't know). (rw) |
14 | 236 | last par, first line, FilterInput => FilterInputStream (rw) |
20 | 340 | Line -8, variable sg => g (vl) |
21 | 351 | Applet paramaters and quotes, see footnote (rw) |
Here are some thoughts on how to first address the shallow versus deep copy issue. Having defined Box (on page 182), imagine we now define the following new class:
public class DoubleBox { public void setValue (int v) { innerBox.setValue(v); } public int getValue () { return innerBox.getValue(); } private Box innerBox = new Box(); public DoubleBox copy () { DoubleBox newValue = new DoubleBox(); newValue.innerBox = innerBox; return newValue; } }A careful examination, such as the following, will show that if we create a double box, and they copy it, they actually share the same data areas (we can even use figure 11.3 for this).
DoubleBox a = new DoubleBox(); a.setValue(18); DoubleBox b = a.copy(); a.setValue(23); System.out.println("b is " + b.getValue());This is a shallow copy. If, on the other hand, we replace the innerBox assignment in DoubleBox with the following:
public DoubleBox copy () { DoubleBox newValue = new DoubleBox(); newValue.innerBox = new Box(); newValue.innerBox.setValue (innerBox.getValue()); }Then we create a deep copy, as each doubleBox will have its own inner box.
(critical readers will note that the allocation of a new box is unnecessary, since it is already done when we create an instance of DoubleBox, but I haven't decided yet whether it contributes more or less to clarity to keep the assignment in the copy method).
Having (I hope) cleared up some of the confusion regarding shallow and deep copies, then in a separate section I should address the clone method.
The code at the top of page 184 should be as follows:
public Object clone () throws CloneNotSupportedException { DoubleBox newBox = (DoubleBox) super.clone(); return newBox; }That is, all that is necessary is to invoke the inherited method from the parent class. This creates a shallow copy. Note that the user is free to override this and provide new behavior if, for example, they want to create a deep copy. (Well, I suppose I should also mention some of the other differences, such as throwing an exeception, and the fact that the clone method must return an object and not a DoubleBox).
String a = new String("abc"); String b = new String("abc");However, since nowhere else do I ever use the new operator with string, a better solution might have been to use the wrapper class Double.
<param name=foo value=12> <param name="foo" value="12">If no quote marks are found, the next word is taken as the value of the string. Following this logic, the only time that quote marks are really necessary is if a value happens to contain a space or a special character (such as a right angle bracket).
Last Modified: 14 February 2000