| |
| |
Intro: Your brain on Java | |
| |
| |
Who is this book for? | |
| |
| |
What your brain is thinking | |
| |
| |
Metacognition | |
| |
| |
Bend your brain into submission | |
| |
| |
What you need for this book | |
| |
| |
Technical editors | |
| |
| |
Acknowledgements | |
| |
| |
| |
Breaking the Surface: Java takes you to new places | |
| |
| |
The way Java works | |
| |
| |
Code structure in Java | |
| |
| |
Anatomy of a class | |
| |
| |
The main() method | |
| |
| |
Looping | |
| |
| |
Conditional branching (if tests) | |
| |
| |
Coding the "99 bottles of beer" app | |
| |
| |
Phrase-o-matic | |
| |
| |
Fireside chat: compiler vs. JVM | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
A Trip to Objectville: I was told there would be objects | |
| |
| |
Chair Wars (Brad the OO guy vs. Larry the procedural guy) | |
| |
| |
Inheritance (an introduction) | |
| |
| |
Overriding methods (as introduction) | |
| |
| |
What's in a class? (methods, instance variables) | |
| |
| |
Making your first object | |
| |
| |
Using main() | |
| |
| |
Guessing Game code | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Know Your Variables: Variables come in two flavors: primitive and reference | |
| |
| |
Declaring a variable (Java cares about type) | |
| |
| |
Primitive types ("I'd like a double with extra foam, please") | |
| |
| |
Java keywords | |
| |
| |
Reference variables (remote control to an object) | |
| |
| |
Object declaration and assignment | |
| |
| |
Objects on the garbage-collectible heap | |
| |
| |
Arrays (a first look) | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
How Objects Behave: State affects behavior, behavior affects state | |
| |
| |
Methods use object state (bark different) | |
| |
| |
Method arguments and return types | |
| |
| |
Pass-by-value (the variable is always copied) | |
| |
| |
Getters and Setters | |
| |
| |
Encapsulation (do it or risk humiliation) | |
| |
| |
Using references in an array | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Extra-Strength Methods: Let's put some muscle in our methods | |
| |
| |
Building the Sink a Dot Com game | |
| |
| |
Starting with the Simple Dot Com game (a simpler version) | |
| |
| |
Writing prepcode (pseudocode for the game) | |
| |
| |
Test code for Simple Dot Com | |
| |
| |
Coding the Simple Dot Com game | |
| |
| |
Final code for Simple Dot Com | |
| |
| |
Generating random numbers with Math.random() | |
| |
| |
Ready-bake code for getting user input from the command-line | |
| |
| |
Looping with for loops | |
| |
| |
Casting primitives from a large size to a smaller size | |
| |
| |
Converting a String to an int with Integer.parseInt() | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Using the Java Library: Java ships with hundreds of pre-built classes | |
| |
| |
Analying the bug in the Simple Dot Com Game | |
| |
| |
ArrayList (taking advantage of the Java API) | |
| |
| |
Fixing the DotCom class code | |
| |
| |
Building the real game (Sink a Dot Com) | |
| |
| |
Prepcode for the real game | |
| |
| |
Code for the real game | |
| |
| |
Boolean expressions | |
| |
| |
Using the library (Java API) | |
| |
| |
Using packages (import statements, fully-qualified names) | |
| |
| |
Using the HTML API docs and reference books | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Better Living in Objectville: Plan your programs with the future in mind | |
| |
| |
Understanding inheritance (superclass and subclass relationships) | |
| |
| |
Designing an inheritance tree (the Animal simulation) | |
| |
| |
Avoiding duplicate code (using inheritance) | |
| |
| |
Overriding methods | |
| |
| |
IS-A and HAS-A (bathtub girl) | |
| |
| |
What do you inherit from your superclass? | |
| |
| |
What does inheritance really buy you? | |
| |
| |
Polymorphism (using a supertype reference to a subclass object) | |
| |
| |
Rules for overriding (don't touch those arguments and return types!) | |
| |
| |
Method overloading (nothing more than method name re-use) | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Serious Polymorphism: Inheritance is just the beginning | |
| |
| |
Some classes just should not be instantiated | |
| |
| |
Abstract classes (can't be instantiated) | |
| |
| |
Abstract methods (must be implemented) | |
| |
| |
Polymorphism in action | |
| |
| |
Class Object (the ultimate superclass of everything) | |
| |
| |
Taking objects out of an ArrayList (they come out as type Object) | |
| |
| |
Compiler checks the reference type (before letting you call a method) | |
| |
| |
Get in touch with your inner object | |
| |
| |
Polymorphic references | |
| |
| |
Casting an object reference (moving lower on the inheritance tree) | |
| |
| |
Deadly Diamond of Death (multiple inheritance problem) | |
| |
| |
Using interfaces (the best solution!) | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Life and Death of an Object: Objects are born and objects die | |
| |
| |
The stack and the heap, where objects and variables live | |
| |
| |
Methods on the stack | |
| |
| |
Where local variables live | |
| |
| |
Where instance variables live | |
| |
| |
The miracle of object creation | |
| |
| |
Constructors (the code that runs when you say new) | |
| |
| |
Initializing the state of a new Duck | |
| |
| |
The compiler can make a default (no-arg) constructor | |
| |
| |
Overloaded constructors | |
| |
| |
Superclass constructors (constructor chaining) | |
| |
| |
Invoking overloaded constructors using this() | |
| |
| |
Life of an object | |
| |
| |
Garbage Collection (and making objects eligible) | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Numbers Matter: Do the Math | |
| |
| |
Math class (do you really need an instance of it?) | |
| |
| |
Static methods | |
| |
| |
Static variables | |
| |
| |
Constants (static final variables) | |
| |
| |
Math methods (random(), round(), abs(), etc.) | |
| |
| |
Wrapper classes (Integer, Boolean, Character, etc.) | |
| |
| |
Number formatting | |
| |
| |
Date formatting | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Risky Behavior: Stuff happens | |
| |
| |
Making a music machine (the BeatBox) | |
| |
| |
What if you need to call risky code? | |
| |
| |
Exceptions say "something bad may have happened..." | |
| |
| |
The compiler guarantees (it checks) that you're aware of the risks | |
| |
| |
Catching exceptions using a try/catch (skateboarder) | |
| |
| |
Flow control in try/catch blocks | |
| |
| |
The finally block (no matter what happens, turn off the oven!) | |
| |
| |
Catching multiple exceptions (the order matters) | |
| |
| |
Declaring an exception (just duck it) | |
| |
| |
Handle or declare law | |
| |
| |
Code Kitchen (making sounds) | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
A Very Graphic Story: Face it, you need to make GUIs | |
| |
| |
Your first GUI | |
| |
| |
Getting a user event | |
| |
| |
Implement a listener interface | |
| |
| |
Getting a button's ActionEvent | |
| |
| |
Putting graphics on a GUI | |
| |
| |
Fun with paintComponent() | |
| |
| |
The Graphics2D object | |
| |
| |
Putting more than one button on a screen | |
| |
| |
Inner classes to the rescue (make your listener an inner class) | |
| |
| |
Animation (move it, paint it, move it, paint it, move it, paint it...) | |
| |
| |
Code Kitchen (painting graphics with the beat of the music) | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Work on your Swing: Swing is easy | |
| |
| |
Swing Components | |
| |
| |
Layout Managers (they control size and placement) | |
| |
| |
Three Layout Managers (border, flow, box) | |
| |
| |
BorderLayout (cares about five regions) | |
| |
| |
FlowLayout (cares about the order and preferred size) | |
| |
| |
BoxLayout (like flow, but can stack components vertically) | |
| |
| |
JTextField (for single-line user input) | |
| |
| |
JTextArea (for multi-line, scrolling text) | |
| |
| |
JCheckBox (is it selected?) | |
| |
| |
JList (a scrollable, selectable list) | |
| |
| |
Code Kitchen (The Big One - building the BeatBox chat client) | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Saving Objects: Objects can be flattened and inflated | |
| |
| |
Saving object state | |
| |
| |
Writing a serialized object to a file | |
| |
| |
Java input and output streams (connections and chains) | |
| |
| |
Object serialization | |
| |
| |
Implementing the Serializable interface | |
| |
| |
Using transient variables | |
| |
| |
Deserializing an object | |
| |
| |
Writing to a text file | |
| |
| |
java.io.File | |
| |
| |
Reading from a text file | |
| |
| |
StringTokenizer | |
| |
| |
CodeKitchen | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Make a Connection: Connect with the outside world | |
| |
| |
Chat program overview | |
| |
| |
Connecting, sending, and receiving | |
| |
| |
Network sockets | |
| |
| |
TCP ports | |
| |
| |
Reading data from a socket (using BufferedReader) | |
| |
| |
Writing data to a socket (using PrintWriter) | |
| |
| |
Writing the Daily Advice Client program | |
| |
| |
Writing a simple server | |
| |
| |
Daily Advice Server code | |
| |
| |
Writing a chat client | |
| |
| |
Multiple call stacks | |
| |
| |
Launching a new thread (make it, start it) | |
| |
| |
The Runnable interface (the thread's job) | |
| |
| |
Three states of a new Thread object (new, runnable, running) | |
| |
| |
The runnable-running loop | |
| |
| |
Thread scheduler (it's his decision, not yours) | |
| |
| |
Putting a thread to sleep | |
| |
| |
Making and starting two threads | |
| |
| |
Concurrency issues: can this couple be saved? | |
| |
| |
The Ryan and Monica concurrency problem, in code | |
| |
| |
Locking to make things atomic | |
| |
| |
Every object has a lock | |
| |
| |
The dreaded "Lost Update" problem | |
| |
| |
Synchronized methods (using a lock) | |
| |
| |
Deadlock! | |
| |
| |
Multithreaded ChatClient code | |
| |
| |
Ready-bake SimpleChatServer | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Release Your Code: It's time to let go | |
| |
| |
Deployment options | |
| |
| |
Keep your source code and class files separate | |
| |
| |
Making an executable JAR (Java ARchives) | |
| |
| |
Running an executable JAR | |
| |
| |
Put your classes in a package! | |
| |
| |
Preventing package name conflicts | |
| |
| |
Packages must have a matching directory structure | |
| |
| |
Compiling and running with packages | |
| |
| |
Compiling with -d | |
| |
| |
Making an executable JAR (with packages) | |
| |
| |
Java Web Start (JWS) for deployment from the web | |
| |
| |
The jnlp file | |
| |
| |
How to make and deploy a JWS application | |
| |
| |
Exercises and puzzles | |
| |
| |
| |
Distributed Computing: Being remote doesn't have to be a bad thing | |
| |
| |
Java Remote Method Invocation (RMI), hands-on, very detailed | |
| |
| |
Servlets (a quick look) | |
| |
| |
Enterprise JavaBeans (EJB), a very quick look | |
| |
| |
Jini, the best trick of all | |
| |
| |
Building the really cool universal service browser | |
| |
| |
The End | |
| |
| |
| |
Appendix A: The final Code Kitchen project | |
| |
| |
BeatBoxFinal (client code) | |
| |
| |
MusicServer (server code) | |
| |
| |
| |
Appendix B: The Top Ten Things that didn't make it into the book | |
| |
| |
Bit manipulation | |
| |
| |
Immutability | |
| |
| |
Assertions | |
| |
| |
Block scope | |
| |
| |
Linked invocations | |
| |
| |
Overriding equals() | |
| |
| |
Access levels and access modifiers (who sees what) | |
| |
| |
String and StringBuffer methods | |
| |
| |
Multidimensional arrays | |
| |
| |
Collections | |
| |
| |
Index | |