| Intro |
|
| Who is this book for? |
|
xx | |
| What your brain is thinking |
|
xxi | |
| Metacognition |
|
xxiii | |
| Bend your brain into submission |
|
xxv | |
| What you need for this book |
|
xxvi | |
| Technical editors |
|
xxviii | |
| Acknowledgements |
|
xxix | |
|
|
|
|
|
|
2 | (3) |
|
|
|
5 | (1) |
|
|
|
6 | (1) |
|
|
|
7 | (1) |
|
|
|
8 | (3) |
|
Conditional branching (if tests) |
|
|
11 | (1) |
|
Coding the ``99 bottles of beer'' app |
|
|
12 | (2) |
|
|
|
14 | (2) |
|
Fireside chat: compiler vs. JVM |
|
|
16 | (2) |
|
|
|
18 | (8) |
|
|
|
|
Chair Wars (Brad the OO guy vs. Larry the procedural guy) |
|
|
26 | (3) |
|
Inheritance (an introduction) |
|
|
29 | (1) |
|
Overriding methods (an introduction) |
|
|
30 | (2) |
|
What's in a class? (methods, instance variables) |
|
|
32 | (2) |
|
|
|
34 | (2) |
|
|
|
36 | (1) |
|
|
|
37 | (3) |
|
|
|
40 | (8) |
|
|
|
|
Declaring a variable (Java cares about type) |
|
|
48 | (1) |
|
Primitive types (``I'd like a double with extra foam, please'') |
|
|
49 | (2) |
|
|
|
51 | (1) |
|
Reference variables (remote control to an object) |
|
|
52 | (1) |
|
Object declaration and assignment |
|
|
53 | (2) |
|
Objects on the garbage-collectible heap |
|
|
55 | (2) |
|
|
|
57 | (4) |
|
|
|
61 | (10) |
|
|
|
|
Methods use object state (bark different) |
|
|
71 | (1) |
|
Method arguments and return types |
|
|
72 | (3) |
|
Pass-by-value (the variable is always copied) |
|
|
75 | (2) |
|
|
|
77 | (1) |
|
Encapsulation (do it or risk humiliation) |
|
|
78 | (3) |
|
Using references in an array |
|
|
81 | (5) |
|
|
|
86 | (8) |
|
|
|
|
Building the Sink a Dot Com game |
|
|
94 | (2) |
|
Starting with the Simple Dot Com game (a simpler version) |
|
|
96 | (1) |
|
Writing prepcode (pseudocode for the game) |
|
|
97 | (3) |
|
Test code for Simple Dot Com |
|
|
100 | (1) |
|
Coding the Simple Dot Com game |
|
|
101 | (3) |
|
Final code for Simple Dot Com |
|
|
104 | (5) |
|
Generating random numbers with Math.random() |
|
|
109 | (1) |
|
Ready-bake code for getting user input from the command-line |
|
|
110 | (2) |
|
|
|
112 | (2) |
|
Casting primitives from a large size to a smaller size |
|
|
114 | (1) |
|
Converting a String to an int with Integer.parseInt() |
|
|
114 | (1) |
|
|
|
115 | (7) |
|
|
|
|
Analying the bug in the Simple Dot Com Game |
|
|
122 | (6) |
|
ArrayList (taking advantage of the Java API) |
|
|
128 | (6) |
|
Fixing the DotCom class code |
|
|
134 | (2) |
|
Building the real game (Sink a Dot Com) |
|
|
136 | (4) |
|
Prepcode for the real game |
|
|
140 | (2) |
|
|
|
142 | (5) |
|
|
|
147 | (3) |
|
Using the library (Java API) |
|
|
150 | (1) |
|
Using packages (import statements, fully-qualified names) |
|
|
151 | (3) |
|
Using the HTML API docs and reference books |
|
|
154 | (3) |
|
|
|
157 | (7) |
|
Better Living in Objectville |
|
|
|
Understanding inheritance (superclass and subclass relationships) |
|
|
164 | (2) |
|
Designing an inheritance tree (the Animal simulation) |
|
|
166 | (1) |
|
Avoiding duplicate code (using inheritance) |
|
|
167 | (1) |
|
|
|
168 | (5) |
|
IS-A and HAS-A (bathtub girl) |
|
|
173 | (3) |
|
What do you inherit from your superclass? |
|
|
176 | (2) |
|
What does inheritance really buy you? |
|
|
178 | (1) |
|
Polymorphism (using a supertype reference to a subclass object) |
|
|
179 | (7) |
|
Rules for overriding (don't touch those arguments and return types!) |
|
|
186 | (1) |
|
Method overloading (nothing more than method name re-use) |
|
|
187 | (1) |
|
|
|
188 | (8) |
|
|
|
|
Some classes just should not be instantiated |
|
|
196 | (1) |
|
Abstract classes (can't be instantiated) |
|
|
197 | (2) |
|
Abstract methods (must be implemented) |
|
|
199 | (3) |
|
|
|
202 | (2) |
|
Class Object (the ultimate superclass of everything) |
|
|
204 | (3) |
|
Taking objects out of an ArrayList (they come out as type Object) |
|
|
207 | (2) |
|
Compiler checks the reference type (before letting you call a method) |
|
|
209 | (1) |
|
Get in touch with your inner object |
|
|
210 | (1) |
|
|
|
211 | (1) |
|
Casting an object reference (moving lower on the inheritance tree) |
|
|
212 | (7) |
|
Deadly Diamond of Death (multiple inheritance problem) |
|
|
219 | (1) |
|
Using interfaces (the best solution!) |
|
|
220 | (5) |
|
|
|
225 | (7) |
|
Life and Death of an Object |
|
|
|
The stack and the heap, where objects and variables live |
|
|
232 | (1) |
|
|
|
233 | (1) |
|
Where local variables live |
|
|
234 | (1) |
|
Where instance variables live |
|
|
235 | (1) |
|
The miracle of object creation |
|
|
236 | (1) |
|
Constructors (the code that runs when you say new) |
|
|
237 | (2) |
|
Initializing the state of a new Duck |
|
|
239 | (2) |
|
The compiler can make a default (no-arg) constructor |
|
|
241 | (2) |
|
|
|
243 | (3) |
|
Superclass constructors (constructor chaining) |
|
|
246 | (6) |
|
Invoking overloaded constructors using this() |
|
|
252 | (2) |
|
|
|
254 | (2) |
|
Garbage Collection (and making objects eligible) |
|
|
256 | (6) |
|
|
|
262 | (8) |
|
|
|
|
Math class (do you really need an instance of it?) |
|
|
270 | (1) |
|
|
|
271 | (2) |
|
|
|
273 | (5) |
|
Constants (static final variables) |
|
|
278 | (4) |
|
Math methods (random(), round(), abs(), etc.) |
|
|
282 | (1) |
|
Wrapper classes (Integer, Boolean, Character, etc.) |
|
|
283 | (1) |
|
|
|
284 | (4) |
|
|
|
288 | (3) |
|
|
|
291 | (7) |
|
|
|
|
Making a music machine (the BeatBox) |
|
|
298 | (3) |
|
What if you need to call risky code? |
|
|
301 | (1) |
|
Exceptions say ``something bad may have happened...'' |
|
|
302 | (1) |
|
The compiler guarantees (it checks) that you're aware of the risks |
|
|
303 | (1) |
|
Catching exceptions using a try/catch (skateboarder) |
|
|
304 | (4) |
|
Flow control in try/catch blocks |
|
|
308 | (1) |
|
The finally block (no matter what happens, turn off the oven!) |
|
|
309 | (2) |
|
Catching multiple exceptions (the order matters) |
|
|
311 | (6) |
|
Declaring an exception (just duck it) |
|
|
317 | (2) |
|
|
|
319 | (2) |
|
Code Kitchen (making sounds) |
|
|
321 | (9) |
|
|
|
330 | (7) |
|
|
|
|
|
|
337 | (2) |
|
|
|
339 | (1) |
|
Implement a listener interface |
|
|
340 | (2) |
|
Getting a button's ActionEvent |
|
|
342 | (3) |
|
Putting graphics on a GUI |
|
|
345 | (2) |
|
Fun with paintComponent() |
|
|
347 | (1) |
|
|
|
348 | (4) |
|
Putting more than one button on a screen |
|
|
352 | (6) |
|
Inner classes to the rescue (make your listener an inner class) |
|
|
358 | (6) |
|
Animation (move it, paint it, move it, paint it, move it, paint it...) |
|
|
364 | (4) |
|
Code Kitchen (painting graphics with the beat of the music) |
|
|
368 | (8) |
|
|
|
376 | (6) |
|
|
|
|
|
|
382 | (1) |
|
Layout Managers (they control size and placement) |
|
|
383 | (2) |
|
Three Layout Managers (border, flow, box) |
|
|
385 | (1) |
|
BorderLayout (cares about five regions) |
|
|
386 | (4) |
|
FlowLayout (cares about the order and preferred size) |
|
|
390 | (3) |
|
BoxLayout (like flow, but can stack components vertically) |
|
|
393 | (2) |
|
JTextField (for single-line user input) |
|
|
395 | (1) |
|
JTextArea (for multi-line, scrolling text) |
|
|
396 | (2) |
|
JCheckBox (is it selected?) |
|
|
398 | (1) |
|
JList (a scrollable, selectable list) |
|
|
399 | (1) |
|
Code Kitchen (The Big One - building the BeatBox chat client) |
|
|
400 | (6) |
|
|
|
406 | (7) |
|
|
|
|
|
|
413 | (1) |
|
Writing a serialized object to a file |
|
|
414 | (1) |
|
Java input and output streams (connections and chains) |
|
|
415 | (1) |
|
|
|
416 | (3) |
|
Implementing the Serializable interface |
|
|
419 | (2) |
|
Using transient variables |
|
|
421 | (2) |
|
|
|
423 | (4) |
|
|
|
427 | (5) |
|
|
|
432 | (2) |
|
|
|
434 | (4) |
|
|
|
438 | (4) |
|
|
|
442 | (4) |
|
|
|
446 | (7) |
|
|
|
|
|
|
453 | (1) |
|
Connecting, sending, and receiving |
|
|
454 | (1) |
|
|
|
455 | (1) |
|
|
|
456 | (2) |
|
Reading data from a socket (using BufferedReader) |
|
|
458 | (1) |
|
Writing data to a socket (using PrintWriter) |
|
|
459 | (1) |
|
Writing the Daily Advice Client program |
|
|
460 | (3) |
|
|
|
463 | (1) |
|
|
|
464 | (2) |
|
|
|
466 | (4) |
|
|
|
470 | (2) |
|
Launching a new thread (make it, start it) |
|
|
472 | (1) |
|
The Runnable interface (the thread's job) |
|
|
473 | (2) |
|
Three states of a new Thread object (new, runnable, running) |
|
|
475 | (1) |
|
The runnable-running loop |
|
|
476 | (1) |
|
Thread scheduler (it's his decision, not yours) |
|
|
477 | (4) |
|
Putting a thread to sleep |
|
|
481 | (2) |
|
Making and starting two threads |
|
|
483 | (2) |
|
Concurrency issues: can this couple be saved? |
|
|
485 | (1) |
|
The Ryan and Monica concurrency problem, in code |
|
|
486 | (4) |
|
Locking to make things atomic |
|
|
490 | (1) |
|
|
|
491 | (1) |
|
The dreaded ``Lost Update'' problem |
|
|
492 | (2) |
|
Synchronized methods (using a lock) |
|
|
494 | (2) |
|
|
|
496 | (2) |
|
Multithreaded ChatClient code |
|
|
498 | (2) |
|
Ready-bake SimpleChatServer |
|
|
500 | (4) |
|
|
|
504 | (6) |
|
|
|
|
|
|
510 | (2) |
|
Keep your source code and class files separate |
|
|
512 | (1) |
|
Making an executable JAR (Java ARchives) |
|
|
513 | (1) |
|
Running an executable JAR |
|
|
514 | (1) |
|
Put your classes in a package! |
|
|
515 | (1) |
|
Preventing package name conflicts |
|
|
516 | (1) |
|
Packages must have a matching directory structure |
|
|
517 | (1) |
|
Compiling and running with packages |
|
|
518 | (1) |
|
|
|
519 | (1) |
|
Making an executable JAR (with packages) |
|
|
520 | (5) |
|
Java Web Start (JWS) for deployment from the web |
|
|
525 | (2) |
|
|
|
527 | (1) |
|
How to make and deploy a JWS application |
|
|
528 | (1) |
|
|
|
529 | (8) |
|
|
|
|
Java Remote Method Invocation (RMI), hands-on, very detailed |
|
|
537 | (16) |
|
|
|
553 | (6) |
|
Enterprise JavaBeans (EJB), a very quick look |
|
|
559 | (1) |
|
Jini, the best trick of all |
|
|
560 | (4) |
|
Building the really cool universal service browser |
|
|
564 | (12) |
|
|
|
576 | (2) |
|
|
|
|
BeatBoxFinal (client code) |
|
|
578 | (7) |
|
MusicServer (server code) |
|
|
585 | (3) |
|
|
|
|
|
|
588 | (1) |
|
|
|
589 | (1) |
|
|
|
590 | (1) |
|
|
|
591 | (1) |
|
|
|
592 | (1) |
|
|
|
593 | (1) |
|
Access levels and access modifiers (who sees what) |
|
|
594 | (2) |
|
String and StringBuffer methods |
|
|
596 | (1) |
|
|
|
597 | (1) |
|
|
|
598 | (9) |
| Index |
|
607 | |