Introduction |
|
xix | |
|
|
1 | (30) |
|
|
3 | (2) |
|
How Java Relates to C and C++ |
|
|
4 | (1) |
|
|
4 | (1) |
|
Java's Contribution to the Internet |
|
|
5 | (1) |
|
|
5 | (1) |
|
|
5 | (1) |
|
|
6 | (1) |
|
Java's Magic: The Bytecode |
|
|
6 | (1) |
|
|
7 | (1) |
|
Object-Oriented Programming |
|
|
8 | (2) |
|
|
9 | (1) |
|
|
9 | (1) |
|
|
10 | (1) |
|
Obtaining the Java Development Kit |
|
|
10 | (2) |
|
|
12 | (4) |
|
|
12 | (1) |
|
|
13 | (1) |
|
The First Sample Program Line by Line |
|
|
13 | (3) |
|
|
16 | (1) |
|
|
16 | (2) |
|
|
18 | (2) |
|
Try This 1-1 Converting Gallons to Liters |
|
|
20 | (1) |
|
|
21 | (3) |
|
|
21 | (2) |
|
|
23 | (1) |
|
|
24 | (2) |
|
Semicolons and Positioning |
|
|
26 | (1) |
|
|
26 | (1) |
|
Try This 1-2 Improving the Gallons-to-Liters Converter |
|
|
27 | (1) |
|
|
28 | (1) |
|
|
29 | (1) |
|
|
29 | (1) |
|
|
30 | (1) |
|
2 Introducing Data Types and Operators |
|
|
31 | (32) |
|
Why Data Types Are Important |
|
|
32 | (1) |
|
|
32 | (5) |
|
|
33 | (2) |
|
|
35 | (1) |
|
|
35 | (2) |
|
|
37 | (1) |
|
Try This 2-1 How Far Away Is the Lightning? ! |
|
|
38 | (1) |
|
|
39 | (3) |
|
Hexadecimal, Octal, and Binary Literals |
|
|
40 | (1) |
|
Character Escape Sequences! |
|
|
40 | (1) |
|
|
41 | (1) |
|
A Closer Look at Variables |
|
|
42 | (1) |
|
|
42 | (1) |
|
|
43 | (1) |
|
The Scope and Lifetime of Variables |
|
|
43 | (3) |
|
|
46 | (1) |
|
|
46 | (2) |
|
|
47 | (1) |
|
Relational and Logical Operators |
|
|
48 | (2) |
|
Short-Circuit Logical Operators |
|
|
50 | (1) |
|
|
51 | (1) |
|
|
51 | (2) |
|
Type Conversion in Assignments |
|
|
53 | (1) |
|
Casting Incompatible Types |
|
|
54 | (2) |
|
|
56 | (1) |
|
Try This 2-2 Display a Truth Table for the Logical Operators |
|
|
57 | (1) |
|
|
58 | (2) |
|
Type Conversion in Expressions |
|
|
58 | (2) |
|
|
60 | (1) |
|
|
60 | (3) |
|
3 Program Control Statements |
|
|
63 | (40) |
|
Input Characters from the Keyboard |
|
|
64 | (1) |
|
|
65 | (2) |
|
|
67 | (1) |
|
|
68 | (1) |
|
|
69 | (3) |
|
|
72 | (1) |
|
Try This 3-1 Start Building a Java Help System |
|
|
73 | (2) |
|
|
75 | (2) |
|
Some Variations on the for Loop |
|
|
77 | (1) |
|
|
78 | (1) |
|
|
79 | (1) |
|
|
79 | (1) |
|
Declaring Loop Control Variables Inside the for Loop |
|
|
80 | (1) |
|
|
81 | (1) |
|
|
81 | (2) |
|
|
83 | (2) |
|
Try This 3-2 Improve the Java Help System |
|
|
85 | (3) |
|
|
88 | (1) |
|
Use break as a Form of goto |
|
|
89 | (5) |
|
|
94 | (1) |
|
Try This 3-3 Finish the Java Help System |
|
|
95 | (4) |
|
|
99 | (1) |
|
|
100 | (3) |
|
4 Introducing Classes, Objects, and Methods |
|
|
103 | (32) |
|
|
104 | (4) |
|
The General Form of a Class |
|
|
105 | (1) |
|
|
106 | (2) |
|
|
108 | (1) |
|
Reference Variables and Assignment |
|
|
109 | (1) |
|
|
110 | (2) |
|
Adding a Method to the Vehicle Class |
|
|
110 | (2) |
|
|
112 | (1) |
|
|
113 | (2) |
|
|
115 | (4) |
|
Adding a Parameterized Method to Vehicle |
|
|
117 | (2) |
|
Try This 4-1 Creating a Help Class |
|
|
119 | (5) |
|
|
124 | (2) |
|
Parameterized Constructors |
|
|
126 | (1) |
|
Adding a Constructor to the Vehicle Class |
|
|
126 | (2) |
|
The new Operator Revisited |
|
|
128 | (1) |
|
|
128 | (1) |
|
|
129 | (1) |
|
Try This 4-2 Demonstrate Garbage Collection and Finalization |
|
|
130 | (2) |
|
|
132 | (2) |
|
|
134 | (1) |
|
5 More Data Types and Operators |
|
|
135 | (46) |
|
|
136 | (4) |
|
|
137 | (3) |
|
Try This 5-1 Sorting an Array |
|
|
140 | (2) |
|
|
142 | (1) |
|
|
142 | (1) |
|
|
143 | (2) |
|
Arrays of Three or More Dimensions |
|
|
144 | (1) |
|
Initializing Multidimensional Arrays |
|
|
144 | (1) |
|
Alternative Array Declaration Syntax |
|
|
145 | (1) |
|
Assigning Array References |
|
|
146 | (1) |
|
|
147 | (2) |
|
Try This 5-2 A Queue Class |
|
|
149 | (4) |
|
The For-Each Style for Loop |
|
|
153 | (5) |
|
Iterating Over Multidimensional Arrays |
|
|
156 | (2) |
|
Applying the Enhanced for |
|
|
158 | (1) |
|
|
158 | (7) |
|
|
159 | (1) |
|
|
160 | (2) |
|
|
162 | (1) |
|
|
162 | (2) |
|
Using a String to Control a switch Statement |
|
|
164 | (1) |
|
Using Command-Line Arguments |
|
|
165 | (1) |
|
|
166 | (8) |
|
The Bitwise AND, OR, XOR, and NOT Operators |
|
|
167 | (4) |
|
|
171 | (2) |
|
Bitwise Shorthand Assignments |
|
|
173 | (1) |
|
Try This 5-3 A ShowBits Class |
|
|
174 | (2) |
|
|
176 | (2) |
|
|
178 | (3) |
|
6 A Closer Look at Methods and Classes |
|
|
181 | (44) |
|
Controlling Access to Class Members |
|
|
182 | (5) |
|
|
183 | (4) |
|
Try This 6-1 Improving the Queue Class |
|
|
187 | (1) |
|
|
188 | (4) |
|
|
190 | (2) |
|
|
192 | (2) |
|
|
194 | (5) |
|
|
199 | (2) |
|
Try This 6-2 Overloading the Queue Constructor |
|
|
201 | (3) |
|
|
204 | (2) |
|
|
206 | (4) |
|
|
209 | (1) |
|
Try This 6-3 The Quicksort |
|
|
210 | (3) |
|
Introducing Nested and Inner Classes |
|
|
213 | (3) |
|
Varargs: Variable-Length Arguments |
|
|
216 | (6) |
|
|
217 | (3) |
|
Overloading Varargs Methods |
|
|
220 | (1) |
|
|
221 | (1) |
|
|
222 | (3) |
|
|
225 | (42) |
|
|
226 | (3) |
|
Member Access and Inheritance |
|
|
229 | (3) |
|
Constructors and Inheritance |
|
|
232 | (2) |
|
Using super to Call Superclass Constructors |
|
|
234 | (4) |
|
Using super to Access Superclass Members |
|
|
238 | (1) |
|
Try This 7-1 Extending the Vehicle Class |
|
|
239 | (3) |
|
Creating a Multilevel Hierarchy |
|
|
242 | (2) |
|
When Are Constructors Executed? |
|
|
244 | (2) |
|
Superclass References and Subclass Objects |
|
|
246 | (4) |
|
|
250 | (3) |
|
Overridden Methods Support Polymorphism |
|
|
253 | (2) |
|
|
255 | (4) |
|
Applying Method Overriding to TwoDShape |
|
|
255 | (4) |
|
|
259 | (4) |
|
|
263 | (2) |
|
final Prevents Overriding |
|
|
263 | (1) |
|
final Prevents Inheritance |
|
|
263 | (1) |
|
Using final with Data Members |
|
|
264 | (1) |
|
|
265 | (1) |
|
|
266 | (1) |
|
8 Packages and Interfaces |
|
|
267 | (32) |
|
|
268 | (4) |
|
|
269 | (1) |
|
Finding Packages and CLASSPATH |
|
|
270 | (1) |
|
|
270 | (2) |
|
Packages and Member Access |
|
|
272 | (2) |
|
|
273 | (1) |
|
Understanding Protected Members |
|
|
274 | (2) |
|
|
276 | (2) |
|
Java's Class Library Is Contained in Packages |
|
|
278 | (1) |
|
|
278 | (1) |
|
|
279 | (4) |
|
Using Interface References |
|
|
283 | (2) |
|
Try This 8-1 Creating a Queue Interface |
|
|
285 | (5) |
|
|
290 | (1) |
|
Interfaces Can Be Extended |
|
|
291 | (1) |
|
Default Interface Methods |
|
|
292 | (5) |
|
Default Method Fundamentals |
|
|
293 | (2) |
|
A More Practical Example of a Default Method |
|
|
295 | (1) |
|
Multiple Inheritance Issues |
|
|
296 | (1) |
|
Use static Methods in an Interface |
|
|
297 | (1) |
|
Final Thoughts on Packages and Interfaces |
|
|
298 | (1) |
|
|
298 | (1) |
|
|
299 | (30) |
|
|
301 | (1) |
|
Exception Handling Fundamentals |
|
|
301 | (3) |
|
|
302 | (1) |
|
A Simple Exception Example |
|
|
302 | (2) |
|
The Consequences of an Uncaught Exception |
|
|
304 | (3) |
|
Exceptions Enable You to Handle Errors Gracefully |
|
|
306 | (1) |
|
Using Multiple catch Statements |
|
|
307 | (1) |
|
Catching Subclass Exceptions |
|
|
308 | (1) |
|
|
309 | (1) |
|
|
310 | (2) |
|
|
311 | (1) |
|
A Closer Look at Throwable |
|
|
312 | (2) |
|
|
314 | (2) |
|
|
316 | (1) |
|
Three Recently Added Exception Features |
|
|
317 | (2) |
|
Java's Built-in Exceptions |
|
|
319 | (2) |
|
Creating Exception Subclasses |
|
|
321 | (2) |
|
Try This 9-1 Adding Exceptions to the Queue Class |
|
|
323 | (4) |
|
|
327 | (2) |
|
|
329 | (42) |
|
Java's I/O Is Built upon Streams |
|
|
331 | (1) |
|
Byte Streams and Character Streams |
|
|
331 | (1) |
|
|
331 | (1) |
|
The Character Stream Classes |
|
|
332 | (1) |
|
|
333 | (1) |
|
|
334 | (3) |
|
|
334 | (2) |
|
|
336 | (1) |
|
Reading and Writing Files Using Byte Streams |
|
|
337 | (6) |
|
|
337 | (4) |
|
|
341 | (2) |
|
Automatically Closing a File |
|
|
343 | (3) |
|
Reading and Writing Binary Data |
|
|
346 | (3) |
|
Try This 10-1 A File Comparison Utility |
|
|
349 | (1) |
|
|
350 | (3) |
|
Using Java's Character-Based Streams |
|
|
353 | (5) |
|
Console Input Using Character Streams |
|
|
353 | (4) |
|
Console Output Using Character Streams |
|
|
357 | (1) |
|
File I/O Using Character Streams |
|
|
358 | (3) |
|
|
358 | (1) |
|
|
359 | (2) |
|
Using Java's Type Wrappers to Convert Numeric Strings |
|
|
361 | (2) |
|
Try This 10-2 Creating a Disk-Based Help System |
|
|
363 | (7) |
|
|
370 | (1) |
|
11 Multithreaded Programming |
|
|
371 | (38) |
|
Multithreading Fundamentals |
|
|
372 | (1) |
|
The Thread Class and Runnable Interface |
|
|
373 | (1) |
|
|
374 | (5) |
|
|
377 | (2) |
|
Try This 11-1 Extending Thread |
|
|
379 | (2) |
|
Creating Multiple Threads |
|
|
381 | (3) |
|
Determining When a Thread Ends |
|
|
384 | (3) |
|
|
387 | (3) |
|
|
390 | (1) |
|
Using Synchronized Methods |
|
|
390 | (3) |
|
The synchronized Statement |
|
|
393 | (3) |
|
Thread Communication Using notify(), wait(), and notify All() |
|
|
396 | (6) |
|
An Example That Uses wait() and notify() |
|
|
397 | (5) |
|
Suspending, Resuming, and Stopping Threads |
|
|
402 | (4) |
|
Try This 11-2 Using the Main Thread |
|
|
406 | (2) |
|
|
408 | (1) |
|
12 Enumerations, Autoboxing, Static Import, and Annotations |
|
|
409 | (30) |
|
|
410 | (3) |
|
|
411 | (2) |
|
Java Enumerations Are Class Types |
|
|
413 | (1) |
|
The values() and valueOf() Methods |
|
|
413 | (2) |
|
Constructors, Methods, Instance Variables, and Enumerations |
|
|
415 | (2) |
|
Two Important Restrictions |
|
|
417 | (1) |
|
Enumerations Inherit Enum |
|
|
417 | (2) |
|
Try This 12-1 A Computer-Controlled Traffic Light |
|
|
419 | (5) |
|
|
424 | (1) |
|
|
424 | (2) |
|
|
426 | (1) |
|
|
427 | (2) |
|
Autoboxing/Unboxing Occurs in Expressions |
|
|
429 | (2) |
|
|
430 | (1) |
|
|
431 | (3) |
|
|
434 | (2) |
|
|
436 | (3) |
|
|
439 | (38) |
|
|
440 | (1) |
|
A Simple Generics Example |
|
|
441 | (7) |
|
Generics Work Only with Reference Types |
|
|
445 | (1) |
|
Generic Types Differ Based on Their Type Arguments |
|
|
445 | (1) |
|
A Generic Class with Two Type Parameters |
|
|
446 | (1) |
|
The General Form of a Generic Class |
|
|
447 | (1) |
|
|
448 | (3) |
|
|
451 | (3) |
|
|
454 | (3) |
|
|
457 | (2) |
|
|
459 | (1) |
|
|
460 | (2) |
|
Try This 13-1 Create a Generic Queue |
|
|
462 | (5) |
|
Raw Types and Legacy Code |
|
|
467 | (3) |
|
Type Inference with the Diamond Operator |
|
|
470 | (1) |
|
|
471 | (1) |
|
|
472 | (1) |
|
Some Generic Restrictions |
|
|
473 | (2) |
|
Type Parameters Can't Be Instantiated |
|
|
473 | (1) |
|
Restrictions on Static Members |
|
|
473 | (1) |
|
Generic Array Restrictions |
|
|
473 | (2) |
|
Generic Exception Restriction |
|
|
475 | (1) |
|
Continuing Your Study of Generics |
|
|
475 | (1) |
|
|
475 | (2) |
|
14 Lambda Expressions and Method References |
|
|
477 | (34) |
|
Introducing Lambda Expressions |
|
|
478 | (9) |
|
Lambda Expression Fundamentals |
|
|
479 | (1) |
|
|
480 | (2) |
|
Lambda Expressions in Action |
|
|
482 | (5) |
|
|
487 | (1) |
|
Generic Functional Interfaces |
|
|
488 | (2) |
|
Try This 14-1 Pass a Lambda Expression as an Argument |
|
|
490 | (5) |
|
Lambda Expressions and Variable Capture |
|
|
495 | (1) |
|
Throw an Exception from Within a Lambda Expression |
|
|
496 | (2) |
|
|
498 | (6) |
|
Method References to static Methods |
|
|
498 | (2) |
|
Method References to Instance Methods |
|
|
500 | (4) |
|
|
504 | (3) |
|
Predefined Functional Interfaces |
|
|
507 | (2) |
|
|
509 | (2) |
|
15 Applets, Events, and Miscellaneous Topics |
|
|
511 | (30) |
|
|
512 | (3) |
|
Applet Organization and Essential Elements |
|
|
515 | (1) |
|
|
516 | (1) |
|
A Complete Applet Skeleton |
|
|
516 | (1) |
|
Applet Initialization and Termination |
|
|
517 | (1) |
|
|
518 | (1) |
|
|
519 | (1) |
|
Try This 15-1 A Simple Banner Applet |
|
|
519 | (4) |
|
|
523 | (1) |
|
Passing Parameters to Applets |
|
|
524 | (1) |
|
|
525 | (2) |
|
|
527 | (1) |
|
The Delegation Event Model |
|
|
528 | (1) |
|
|
528 | (2) |
|
|
528 | (1) |
|
|
528 | (1) |
|
|
529 | (1) |
|
Event Listener Interfaces |
|
|
529 | (1) |
|
Using the Delegation Event Model |
|
|
530 | (4) |
|
Handling Mouse and Mouse Motion Events |
|
|
531 | (1) |
|
A Simple Mouse Event Applet |
|
|
531 | (3) |
|
|
534 | (4) |
|
The transient and volatile Modifiers |
|
|
535 | (1) |
|
|
535 | (1) |
|
|
535 | (1) |
|
|
536 | (1) |
|
|
537 | (1) |
|
|
538 | (3) |
|
|
541 | (38) |
|
The Origins and Design Philosophy of Swing |
|
|
543 | (2) |
|
Components and Containers |
|
|
545 | (2) |
|
|
545 | (1) |
|
|
546 | (1) |
|
The Top-Level Container Panes |
|
|
546 | (1) |
|
|
547 | (1) |
|
A First Simple Swing Program |
|
|
547 | (6) |
|
The First Swing Example Line by Line |
|
|
549 | (4) |
|
|
553 | (4) |
|
|
557 | (3) |
|
|
560 | (4) |
|
|
564 | (4) |
|
Try This 16-1 A Swing-Based File Comparison Utility |
|
|
568 | (5) |
|
Use Anonymous Inner Classes or Lambda Expressions to Handle Events |
|
|
573 | (2) |
|
|
575 | (2) |
|
|
577 | (2) |
|
|
579 | (36) |
|
|
581 | (2) |
|
|
581 | (1) |
|
The Stage and Scene Classes |
|
|
581 | (1) |
|
|
582 | (1) |
|
|
582 | (1) |
|
The Application Class and the Life-cycle Methods |
|
|
582 | (1) |
|
Launching a JavaFX Application |
|
|
583 | (1) |
|
A JavaFX Application Skeleton |
|
|
583 | (3) |
|
Compiling and Running a JavaFX Program |
|
|
586 | (1) |
|
|
587 | (1) |
|
A Simple JavaFX Control: Label |
|
|
587 | (2) |
|
|
589 | (5) |
|
|
590 | (1) |
|
Introducing the Button Control |
|
|
590 | (1) |
|
Demonstrating Event Handling and the Button |
|
|
591 | (3) |
|
Three More JavaFX Controls |
|
|
594 | (4) |
|
|
594 | (4) |
|
Try This 17-1 Use the CheckBox Indeterminate State |
|
|
598 | (9) |
|
|
599 | (5) |
|
|
604 | (3) |
|
Introducing Effects and Transforms |
|
|
607 | (6) |
|
|
607 | (2) |
|
|
609 | (1) |
|
Demonstrating Effects and Transforms |
|
|
610 | (3) |
|
|
613 | (1) |
|
|
614 | (1) |
|
|
615 | (58) |
|
Chapter 1 Java Fundamentals |
|
|
616 | (2) |
|
Chapter 2 Introducing Data Types and Operators |
|
|
618 | (1) |
|
Chapter 3 Program Control Statements |
|
|
619 | (3) |
|
Chapter 4 Introducing Classes, Objects, and Methods |
|
|
622 | (1) |
|
Chapter 5 More Data Types and Operators |
|
|
623 | (4) |
|
Chapter 6 A Closer Look at Methods and Classes |
|
|
627 | (5) |
|
|
632 | (2) |
|
Chapter 8 Packages and Interfaces |
|
|
634 | (2) |
|
Chapter 9 Exception Handling |
|
|
636 | (3) |
|
|
639 | (3) |
|
Chapter 11 Multithreaded Programming |
|
|
642 | (2) |
|
Chapter 12 Enumerations, Autoboxing, Static Import, and Annotations |
|
|
644 | (4) |
|
|
648 | (5) |
|
Chapter 14 Lambda Expressions and Method References |
|
|
653 | (3) |
|
Chapter 15 Applets, Events, and Miscellaneous Topics |
|
|
656 | (5) |
|
Chapter 16 Introducing Swing |
|
|
661 | (6) |
|
Chapter 17 Introducing JavaFX |
|
|
667 | (6) |
|
B Using Java's Documentation Comments |
|
|
673 | (8) |
|
|
674 | (4) |
|
|
675 | (1) |
|
|
675 | (1) |
|
|
675 | (1) |
|
|
675 | (1) |
|
|
675 | (1) |
|
|
676 | (1) |
|
|
676 | (1) |
|
|
676 | (1) |
|
|
676 | (1) |
|
|
676 | (1) |
|
|
676 | (1) |
|
|
677 | (1) |
|
|
677 | (1) |
|
|
677 | (1) |
|
|
677 | (1) |
|
|
677 | (1) |
|
|
678 | (1) |
|
|
678 | (1) |
|
|
678 | (1) |
|
The General Form of a Documentation Comment |
|
|
678 | (1) |
|
|
679 | (1) |
|
An Example That Uses Documentation Comments |
|
|
679 | (2) |
Index |
|
681 | |