Intro Your brain on Java |
|
Who is this book for? |
|
xxii | |
What your brain is thinking |
|
xxiii | |
Metacognition |
|
xxv | |
Bend your brain into submission |
|
xxvii | |
What you need for this book |
|
xxviii | |
Technical editors |
|
xxx | |
Acknowledgements |
|
xxxi | |
|
|
|
Java takes you to new places |
|
|
|
|
2 | (5) |
|
|
7 | (1) |
|
|
8 | (1) |
|
|
9 | (2) |
|
|
11 | (2) |
|
Conditional branching (if tests) |
|
|
13 | (1) |
|
Coding the ``99 bottles of beer'' app |
|
|
14 | (2) |
|
|
16 | (2) |
|
Fireside chat: compiler vs. JVM |
|
|
18 | (2) |
|
|
20 | (8) |
|
|
|
I was told there would be objects |
|
|
|
Chair Wars (Brad the OO guy vs. Larry the procedural guy) |
|
|
28 | (3) |
|
Inheritance (an introduction) |
|
|
31 | (1) |
|
Overriding methods (an introduction) |
|
|
32 | (2) |
|
What's in a class? (methods, instance variables) |
|
|
34 | (2) |
|
|
36 | (2) |
|
|
38 | (1) |
|
|
39 | (3) |
|
|
42 | (8) |
|
|
|
Variables come in two flavors: primitive and reference |
|
|
|
Declaring a variable (Java cares about type) |
|
|
50 | (1) |
|
Primitive types (``I'd like a double with extra foam, please'') |
|
|
51 | (2) |
|
|
53 | (1) |
|
Reference variables (remote control to an object) |
|
|
54 | (1) |
|
Object declaration and assignment |
|
|
55 | (2) |
|
Objects on the garbage-collectible heap |
|
|
57 | (2) |
|
|
59 | (4) |
|
|
63 | (10) |
|
|
|
State affects behavior, behavior affects state |
|
|
|
Methods use object state (bark different) |
|
|
73 | (1) |
|
Method arguments and return types |
|
|
74 | (3) |
|
Pass-by-value (the variable is always copied) |
|
|
77 | (2) |
|
|
79 | (1) |
|
Encapsulation (do it or risk humiliation) |
|
|
80 | (3) |
|
Using references in an array |
|
|
83 | (5) |
|
|
88 | (8) |
|
|
|
Let's put some muscle in our methods |
|
|
|
Building the Sink a Dot Com game |
|
|
96 | (2) |
|
Starting with the Simple Dot Com game (a simpler version) |
|
|
98 | (2) |
|
Writing prepcode (pseudocode for the game) |
|
|
100 | (2) |
|
Test code for Simple Dot Com |
|
|
102 | (1) |
|
Coding the Simple Dot Com game |
|
|
103 | (3) |
|
Final code for Simple Dot Com |
|
|
106 | (5) |
|
Generating random numbers with Math.random() |
|
|
111 | (1) |
|
Ready-bake code for getting user input from the command-line |
|
|
112 | (2) |
|
|
114 | (3) |
|
Casting primitives from a large size to a smaller size |
|
|
117 | (1) |
|
Converting a String to an int with Integer.parseInt() |
|
|
117 | (1) |
|
|
118 | (8) |
|
|
|
Java ships with hundreds of pre-built classes |
|
|
|
Analying the bug in the Simple Dot Com Game |
|
|
126 | (6) |
|
ArrayList (taking advantage of the Java API) |
|
|
132 | (6) |
|
Fixing the DotCom class code |
|
|
138 | (2) |
|
Building the real game (Sink a Dot Com) |
|
|
140 | (4) |
|
Prepcode for the real game |
|
|
144 | (2) |
|
|
146 | (5) |
|
|
151 | (3) |
|
Using the library (Java API) |
|
|
154 | (1) |
|
Using packages (import statements, fully-qualified names) |
|
|
155 | (3) |
|
Using the HTML API docs and reference books |
|
|
158 | (3) |
|
|
161 | (7) |
|
Better Living in Objectville |
|
|
|
Plan your programs with the future in mind |
|
|
|
Understanding inheritance (superclass and subclass relationships) |
|
|
168 | (2) |
|
Designing an inheritance tree (the Animal simulation) |
|
|
170 | (1) |
|
Avoiding duplicate code (using inheritance) |
|
|
171 | (1) |
|
|
172 | (5) |
|
IS-A and HAS-A (bathtub girl) |
|
|
177 | (3) |
|
What do you inherit from your superclass? |
|
|
180 | (2) |
|
What does inheritance really buy you? |
|
|
182 | (1) |
|
Polymorphism (using a supertype reference to a subclass object) |
|
|
183 | (7) |
|
Rules for overriding (don't touch those arguments and return types!) |
|
|
190 | (1) |
|
Method overloading (nothing more than method name re-use) |
|
|
191 | (1) |
|
|
192 | (8) |
|
|
|
Inheritance is just the beginning |
|
|
|
Some classes just should not be instantiated |
|
|
200 | (1) |
|
Abstract classes (can't be instantiated) |
|
|
201 | (2) |
|
Abstract methods (must be implemented) |
|
|
203 | (3) |
|
|
206 | (2) |
|
Class Object (the ultimate superclass of everything) |
|
|
208 | (3) |
|
Taking objects out of an ArrayList (they come out as type Object) |
|
|
211 | (2) |
|
Compiler checks the reference type (before letting you call a method) |
|
|
213 | (1) |
|
Get in touch with your inner object |
|
|
214 | (1) |
|
|
215 | (1) |
|
Casting an object reference (moving lower on the inheritance tree) |
|
|
216 | (7) |
|
Deadly Diamond of Death (multiple inheritance problem) |
|
|
223 | (1) |
|
Using interfaces (the best solution!) |
|
|
224 | (6) |
|
|
230 | (6) |
|
Life and Death of an Object |
|
|
|
Objects are born and objects die |
|
|
|
The stack and the heap, where objects and variables live |
|
|
236 | (1) |
|
|
237 | (1) |
|
Where local variables live |
|
|
238 | (1) |
|
Where instance variables live |
|
|
239 | (1) |
|
The miracle of object creation |
|
|
240 | (1) |
|
Constructors (the code that runs when you say new) |
|
|
241 | (2) |
|
Initializing the state of a new Duck |
|
|
243 | (4) |
|
|
247 | (3) |
|
Superclass constructors (constructor chaining) |
|
|
250 | (6) |
|
Invoking overloaded constructors using this() |
|
|
256 | (2) |
|
|
258 | (2) |
|
Garbage Collection (and making objects eligible) |
|
|
260 | (6) |
|
|
266 | (8) |
|
|
|
|
|
Math class (do you really need an instance of it?) |
|
|
274 | (1) |
|
|
275 | (2) |
|
|
277 | (5) |
|
Constants (static final variables) |
|
|
282 | (4) |
|
Math methods (random(), round(), abs(), etc.) |
|
|
286 | (1) |
|
Wrapper classes (Integer, Boolean, Character, etc.) |
|
|
287 | (2) |
|
|
289 | (5) |
|
|
294 | (7) |
|
Date formatting and manipulation |
|
|
301 | (6) |
|
|
307 | (3) |
|
|
310 | (6) |
|
|
|
|
|
Making a music machine (the BeatBox) |
|
|
316 | (3) |
|
What if you need to call risky code? |
|
|
319 | (1) |
|
Exceptions say ``something bad may have happened...'' |
|
|
320 | (1) |
|
The compiler guarantees (it checks) that you're aware of the risks |
|
|
321 | (1) |
|
Catching exceptions using a try/catch (skateboarder) |
|
|
322 | (4) |
|
Flow control in try/catch blocks |
|
|
326 | (1) |
|
The finally block (no matter what happens, turn off the oven!) |
|
|
327 | (2) |
|
Catching multiple exceptions (the order matters) |
|
|
329 | (6) |
|
Declaring an exception (just duck it) |
|
|
335 | (2) |
|
|
337 | (2) |
|
Code Kitchen (making sounds) |
|
|
339 | (9) |
|
|
348 | (7) |
|
|
|
Face it, you need to make GUIs |
|
|
|
|
355 | (2) |
|
|
357 | (1) |
|
Implement a listener interface |
|
|
358 | (2) |
|
Getting a button's ActionEvent |
|
|
360 | (3) |
|
Putting graphics on a GUI |
|
|
363 | (2) |
|
Fun with paintComponent() |
|
|
365 | (1) |
|
|
366 | (4) |
|
Putting more than one button on a screen |
|
|
370 | (6) |
|
Inner classes to the rescue (make your listener an inner class) |
|
|
376 | (6) |
|
Animation (move it, paint it, move it, paint it, move it, paint it...) |
|
|
382 | (4) |
|
Code Kitchen (painting graphics with the beat of the music) |
|
|
386 | (8) |
|
|
394 | (6) |
|
|
|
|
|
|
400 | (1) |
|
Layout Managers (they control size and placement) |
|
|
401 | (2) |
|
Three Layout Managers (border, flow, box) |
|
|
403 | (1) |
|
BorderLayout (cares about five regions) |
|
|
404 | (4) |
|
FlowLayout (cares about the order and preferred size) |
|
|
408 | (3) |
|
BoxLayout (like flow, but can stack components vertically) |
|
|
411 | (2) |
|
JTextField (for single-line user input) |
|
|
413 | (1) |
|
JTextArea (for multi-line, scrolling text) |
|
|
414 | (2) |
|
JCheckBox (is it selected?) |
|
|
416 | (1) |
|
JList (a scrollable, selectable list) |
|
|
417 | (1) |
|
Code Kitchen (The Big One-building the BeatBox chat client) |
|
|
418 | (6) |
|
|
424 | (7) |
|
|
|
Objects can be flattened and inflated |
|
|
|
|
431 | (1) |
|
Writing a serialized object to a file |
|
|
432 | (1) |
|
Java input and output streams (connections and chains) |
|
|
433 | (1) |
|
|
434 | (3) |
|
Implementing the Serializable interface |
|
|
437 | (2) |
|
Using transient variables |
|
|
439 | (2) |
|
|
441 | (6) |
|
|
447 | (5) |
|
|
452 | (2) |
|
|
454 | (4) |
|
Splitting a String into tokens with split() |
|
|
458 | (4) |
|
|
462 | (4) |
|
|
466 | (7) |
|
|
|
Connect with the outside world |
|
|
|
|
473 | (1) |
|
Connecting, sending, and receiving |
|
|
474 | (1) |
|
|
475 | (1) |
|
|
476 | (2) |
|
Reading data from a socket (using BufferedReader) |
|
|
478 | (1) |
|
Writing data to a socket (using PrintWriter) |
|
|
479 | (1) |
|
Writing the Daily Advice Client program |
|
|
480 | (3) |
|
|
483 | (1) |
|
|
484 | (2) |
|
|
486 | (4) |
|
|
490 | (2) |
|
Launching a new thread (make it, start it) |
|
|
492 | (2) |
|
The Runnable interface (the thread's job) |
|
|
494 | (1) |
|
Three states of a new Thread object (new, runnable, running) |
|
|
495 | (1) |
|
The runnable-running loop |
|
|
496 | (1) |
|
Thread scheduler (it's his decision, not yours) |
|
|
497 | (4) |
|
Putting a thread to sleep |
|
|
501 | (2) |
|
Making and starting two threads |
|
|
503 | (2) |
|
Concurrency issues: can this couple be saved? |
|
|
505 | (1) |
|
The Ryan and Monica concurrency problem, in code |
|
|
506 | (4) |
|
Locking to make things atomic |
|
|
510 | (1) |
|
|
511 | (1) |
|
The dreaded ``Lost Update'' problem |
|
|
512 | (2) |
|
Synchronized methods (using a lock) |
|
|
514 | (2) |
|
|
516 | (2) |
|
Multithreaded ChatClient code |
|
|
518 | (2) |
|
Ready-bake SimpleChatServer |
|
|
520 | (4) |
|
|
524 | (9) |
|
|
|
Sorting is a snap in Java |
|
|
|
|
533 | (1) |
|
Sorting an ArrayList with Collections.sort() |
|
|
534 | (6) |
|
|
540 | (7) |
|
Sorting things that implement the Comparable interface |
|
|
547 | (5) |
|
Sorting things with a custom Comparator |
|
|
552 | (5) |
|
The collection API---lists, sets, and maps |
|
|
557 | (2) |
|
Avoiding duplicates with HashSet |
|
|
559 | (1) |
|
Overriding hashCode() and equals() |
|
|
560 | (7) |
|
|
567 | (7) |
|
Using wildcards for polymorphism |
|
|
574 | (2) |
|
|
576 | (6) |
|
|
|
|
|
|
582 | (2) |
|
Keep your source code and class files separate |
|
|
584 | (1) |
|
Making an executable JAR (Java ARchives) |
|
|
585 | (1) |
|
Running an executable JAR |
|
|
586 | (1) |
|
Put your classes in a package! |
|
|
587 | (2) |
|
Packages must have a matching directory structure |
|
|
589 | (1) |
|
Compiling and running with packages |
|
|
590 | (1) |
|
|
591 | (1) |
|
Making an executable JAR (with packages) |
|
|
592 | (5) |
|
Java Web Start (JWS) for deployment from the web |
|
|
597 | (3) |
|
How to make and deploy a JWS application |
|
|
600 | (1) |
|
|
601 | (13) |
|
|
|
Being remote doesn't have to be a bad thing |
|
|
|
Java Remote Method Invocation (RMI), hands-on, very detailed |
|
|
614 | (11) |
|
|
625 | (6) |
|
Enterprise JavaBeans (EJB), a very quick look |
|
|
631 | (1) |
|
Jini, the best trick of all |
|
|
632 | (4) |
|
Building the really cool universal service browser |
|
|
636 | (12) |
|
|
648 | (2) |
|
|
|
The final Code Kitchen project |
|
|
|
BeatBoxFinal (client code) |
|
|
650 | (7) |
|
MusicServer (server code) |
|
|
657 | (3) |
|
|
|
The Top Ten Things that didn't make it into the book |
|
|
|
|
660 | (17) |
Index |
|
677 | |