Big Java

advertisement
Big Java
SECOND EDITION
Cay Horstmann
SAN JOSE STATE UNIVERSITY
WILEY
John Wiley & Sons, Inc.
Contents
Preface
Special Features
Chapter 1
XV
IfltroduCtiOfl
.1
What Is Programming?
.2
The Anatomy o f a Computer
2
.3
Translating Human-Readable Programs to Machine Code
.4
The Java Programming Language
3
10
.5
Becoming Familiär with Your Computer
.6
Compiling a Simple Program
.7
Errors
.8
The Compilation Process
Chapter 2
12
17
23
25
Usiflg ObjectS
33
2.1
Types and Variables
34
2.2
The Assignment Operator
2.3
Objects, Classes, and Methods
36
37
2.4
Method Parameters and Return Values
2.5
Number Types
2.6
Constructing Objects
2.7
Accessor and Mutator Methods
43
44
46
40
r
xxvi
Contents
2.8
Implementing a Test Program
2.9
The API Documentation
2.10
Object References
chapter 3
47
49
52
Implementing Classes
3.1
Black Boxes
3.2
Designing the Public Interface ofa Class
3.3
Commenting the Public Interface
3.4
Instance Fields
66
69
74
77
3.5
Implementing Constructors and Methods
3.6
Testing a Class
3.7
Categories of Variables
3.8
Implicit and Explicit Method Parameters
chapter 4
65
79
82
88
91
Fundamental Data Types
4.1
Number Types
4.2
Constants
103
104
110
4.3 Assignment, Increment, and Decrement
116
4.4 Arithmetic Operations and Mathematical Functions
4.5
Calling Static Methods
4.6
Strings
4.7
Reading Input
chapter 5
118
123
128
135
Programming Graphics
(Optional)
5.1
Frame Windows
152
5.2
Drawing Shapes
1 54
5.3
Craphical Shapes
5.4
Colors
5.5
Drawing Complex Shapes
5.6
Reading Text Input
5.7
Comparing Visual and Numerical Information
162
164
165
176
1 79
151
Contents
Chapter 6
xxvii
DecisiORS
6.1 The i f Statement
6.2 Comparing Values
6.3
6.4
189
190
196
Multiple Alternatives
201
Using Boolean Expressions
213
chapter 7
1
Iteration
7.1 whileLoops
231
232
7.2
for Loops
7.3
7.4
7.5
Nested Loops
248
Processing Sentinel Values
251
Random Numbers and Simulations
241
260
chapter 8 Arrays and Array Lists
8.1
8.2
8.3
8.4
8.5
Arrays
280
Array Lists
284
Wrappers and Auto-boxing
290
The Enhanced for Loop
292
Simple Array Algorithms
293
8.6 Two-Dimensional Arrays
8.7 Copying Arrays
303
chapter 9
9.1
9.2
298
Designing Classes
323
Choosing Classes
324
Cohesion and Coupling
326
9.3 Accessors, Mutators, and Immutable Classes
9.4 Side Effects
329
9.5 Preconditions and Postconditions
9.6 Static Methods
340
9.7 Static Fields
342
9.8
9.9
279
Scope
346
Packages
351
335
329
xxviii
Contents
chapter 1 0 Testing and Debugging
10.1 Unit Tests
372
10.2 Providing Test Input
376
10.3 Test Case Evaluation
380
10.4 Regression Testing and Test Coverage
10.5 Logging
387
10.6 Using a Debugger
389
10.7 A Sample Debugging Session
392
chapter 1 1
371
383
Interfaces and Polymorphism
.1 Using Interfaces for Code Reuse
410
.2 Converting Between Class and Interface Types
.3 Polymorphism
418
.4 Using Interfaces for Callbacks
420
.5 Inner Classes
424
.6 Processing Timer Events
427
.7 Accessing Surrounding Variables
431
chapter 1 2
12.1
12.2
12.3
12.4
Event Handling
409
416
(Optional)
Events, Event Sources, and Event Listeners
Building Applications with Buttons
447
Processing Text Input
451
Mouse Events
455
chapter 1 3
V
443
444
Inheritance
13.1 An Introduction to Inheritance
468
13.2 Inheritance Hierarchies
473
13.3 Inheriting Instance Fields and Methods
475
13.4 Subclass Construction
481
13.5 Converting Between Subclass and Superclass Types
13.6 Polymorphism
485
13.7 Access Control
492
13.8 Object: The Cosmic Superclass
495
467
482
Contents
chapter 1 4
xxix
Graphical User Interfaces
(Optional)
14.1 Using Inheritance to Customize Frames
516
14.2 Layout Management
518
14.3 Choices
520
14.4 Menüs
530
14.5 TextAreas
536
14.6 Exploring the Swing Documentation
538
chapter 1 5
15.1
15.2
15.3
15.4
15.5
1 5.6
'
551
Throwing Exceptions
552
Checked and Unchecked Exceptions
555
Catching Exceptions
558
The f i n a l l y Clause
561
Designing Your Own Exception Types
563
Case Study: A Complete Example
564
chapter 1 6
16.1
16.2
16.3
16.4
16.5
Exception Handling
515
Files and Streams
575
Reading and Writing Text Files
576
Text and Binary Formats
581
An Encryption Program
583
Random Access
588
Object Streams
594
chapter 1 7 Object-Oriented Design
1 7.1 The Software Life Cycle
608
17.2 Discovering Classes
614
17.3 Relationships Between Classes
617
1 7.4 Case Study: Printing an Invoice
622
17.5 Case Study: An Automatic Teller Machine
607
634
xxx
Contents
Chapter 1 8 RecursiOfl
18.1
Triangle Numbers
18.2
Permutations
663
664
668
18.3
Recursive Helper Methods
18.4
The Efficiency of Recursion
18.5
Mutual Recursions
676
678
687
-••
chapter 1 9 Sorting and Searching
703
19.1
Selection Sort
19.2
Profiling the Selection Sort Algorithm
704
19.3
Analyzing the Performance of the Selection Sort Algorithm
19.4
Merge Sort
19.5
Analyzing the Merge Sort Algorithm
19.6
Searching
19.7
Binary Search
19.8
Sorting Real Data
708
715
719
725
728
731
chapter 2 0 An Introduction to Data Structures
20.1
Using Linked Lists
20.2
Implementing Linked Lists
20.3
Abstract and Concrete Data Types
20.4
Stacks and Queues
Sets
21.2
Maps
741
742
747
758
762
chapter 2 1 Advanced Data Structures
21.1
711
776
781
21.3
HashTables
21.4
Computing Hash Codes
783
791
21.5
Binary Search Trees
21.6
Tree Traversal
796
21.7
Using Tree Sets and Tree Maps
21.8
Priority Queues
21.9
Heaps
807
815
816
21.10 The Heapsort Algorithm
827
811
775
Contents
xxxi
chapter 2 2 Generic Programming
841
22.1
Type Variables
842
22.2
Implementing Generic Classes
22.3
Ceneric Methods
22.4
Constraining Type Variables
22.5
Raw Types
844
852
853
856
chapter 2 3 Multithreading
23.1
Running Threads
23.2
Terminating Threads
(Advanced)
864
870
23.3
Race Conditions
23.4
Synchronizing Object Access
23.5
Avoiding Deadlocks
23.6
An Application of Threads 889
873
879
882
chapter 2 4 Internet Networking
24.1
The Internet Protocol
24.2
Application Level Protocols
A Client Program
913
24.4
A Server Program
916
24.5
URL Connections
905
910
924
Relational Databases
25.1
Organizing Database Information
25.2
Queries
25.3
Installing a Database
(Advanced)
935
936
945
953
25.4
Database Programming in Java
25.5
Case Study: A Bank Database
959
967
chapter 2 6 XML (Advanced)
26.1
(Advanced)
906
24.3
chapter 2 5
863
XML Tags and Documents
26.2
Parsing XML Documents
26.3
Creating XML Documents
26.4
Validating XML Documents
980
990
999
1006
979
xxxii
Contents
chapter 2 7
JavaServer Faces
27.1
A Simple JSF Program
27.2
JavaBeans Components
27.3
JSF Components
(Advanced)
1027
1028
1034
1043
27.4
Navigation Between Pages
27.5
A Three-Tier Application
1045
M
1048
Appendix A
Java Language Coding Guidelines
1063
Appendix B
The Basic Latin and Latin-1 Subsets of Unicode
1071
Appendix C
The Java Library
1074
Appendix D
A d a p t i n g j a v a 5 Programs to Older C o m p i l e r s
1120
Appendix E
Java Syntax S u m m a r y
1123
Appendix F
Java O p e r a t o r S u m m a r y
1135
Appendix G
Java Keyword S u m m a r y
1137
Appendix H
M e t r i c Conversion Factors
1140
Appendix I
HTML S u m m a r y
1141
AppendixJ
Tool S u m m a r y
1146
Appendix K
javadoc Summary
1149
Appendix L
N u m b e r Systems
1152
Appendix M
Bit and Shift O p e r a t i o n s
1158
Appendix N
UML S u m m a r y
1161
Glossary
1163
Index
1179
Illustration Credits
1215
Download