Special Features
xvii
Array Construction
282
Array Element Access
283
Assertion
337
Assignment
37
Block Statement
193
Calling a Superclass Constructor
481
Calling a Superclass Method
479
Cast
106
Class Definition
73
Constant Definition
112
Constructor Definition
72
Defining a Generic Class
846
Defining a Generic Method
853
Defining an Enumerated Type
213
Defining an Interface
414
Exception Specification
557
finally
Clause
562
General
try Block 559
Implementing an Interface 414Importing a Class from a Package 48Inheritance 471Inner Classes 425Instance Field Declaration 78Instantiating a Generic Class 843
Method Call 21Method Definition 71
Object Construction 45
Package Specification 352
Static Method Call 124
The “for each” Loop 293The for Statement 241The if Statement 192The instanceof Operator 484The return Statement 81The while Statement 236Throwing an Exception 555
Variable Definition 35
Alphabetical List of Syntax Boxes
jc4fm_7.fm Page xvii Wednesday, November 10, 2004 4:01 PM
xviii Table of Special Features
Chapter Common Errors How Tos Quality Tips
1 Introduction Omitting Semicolons 22
Misspelling Words 24
2 Using Objects Trying to Invoke a Constructor Like a Method 46
3 Implementing Classes Forgetting to Initialize Object References in a Constructor 90
Trying to Call a Method Without an Implicit Parameter 93
Designing and Implementing a Class 85
4 Fundamental Data Types
Integer Division 121
Unbalanced Parentheses 122
Roundoff Errors 125
Carrying Out Computations 125
Do Not Use Magic Numbers 115
Choose Descriptive Variable Names 115
White Space 122
Factor Out Common Code 123
5 Programming Graphics (Optional)
Drawing Graphical Shapes 170
Calculate Sample Data Manually 183
6 Decisions Using == to Compare Strings 198
The Dangling else Problem 210
Multiple Relational Operators 216
Confusing && and || Conditions 217
Brace Layout 193
Avoid Conditions with Side Effects 201
Prepare Test Cases Ahead of Time 211
7 Iteration Infinite Loops 236
Off-by-One Errors 237
Forgetting a Semicolon 246
A Semicolon Too Many 246
Implementing Loops 254 Use for Loops for Their Intended Purpose 245
Don’t Use != to Test the End of a Range 247
Symmetric and Asymmetric Bounds256
Count Iterations 256
jc4fm_7.fm Page xviii Wednesday, November 10, 2004 4:01 PM
Table of Special Features xix
Productivity Hints Advanced Topics Random Facts
Understand the File System 15
Have a Backup Strategy 16
Alternative Comment Syntax 20 The ENIAC and the Dawn of Computing 7
Don’t Memorize—Use Online Help 52
Testing Classes in an Interactive Environment 49
Mainframes—When Dinosaurs Ruled the Earth 55
The javadoc Utility 76
Using the Command Line Effectively 88
Calling One Constructor from Another 93
Electronic Voting Machines 94
Avoid Unstable Layout 117
Reading Exception Reports 130
Big Numbers 107
Binary Numbers 107
Combining Assignment and Arithmetic 118
Escape Sequences 131
Strings and the char Type 132
Formatting Numbers 137
Reading Input from a Dialog Box 139
The Pentium Floating-Point Bug 109
International Alphabets 133
Applets 158 The Evolution of the Internet 160
Computer Graphics 174
Indentation and Tabs 194
Keyboard Shortcuts for Mouse Operations 203
Copy and Paste in the Editor 204
Make a Schedule and Make Time for Unexpected Problems 212
The Selection Operator 195
The switch Statement 205
Enumerated Types 212
Lazy Evaluation of Boolean Operators 217
De Morgan’s Law 218
Artificial Intelligence 219
do Loops 238
Scope of Variables Defined in a for Loop Header 247
The “Loop and a Half” Problem 257
The break and continue Statements 258
Loop Invariants 265
Spaghetti Code 239
Correctness Proofs 267
jc4fm_7.fm Page xix Wednesday, November 10, 2004 4:01 PM
xx Table of Special Features
Chapter Common Errors How Tos Quality Tips
8 Arrays and Array Lists Bounds Errors 283
Uninitialized Arrays 284
Length and Size 289
Underestimating the Size of a Data Set 307
Working with Array Lists and Arrays 301
Prefer Parameterized Array Lists 289
Make Parallel Arrays into Arrays of Objects 307
9 Designing Classes Trying to Modify Primitive Type Parameters 331
Shadowing 349
Confusing Dots 356
Programming with Packages 356
Consistency 328
Minimize Side Effects 333
Don’t Change the Contents of Parameter Variables 333
10 Testing and Debugging
Debugging 398
11 Interfaces and Polymorphism
Forgetting to Define Implementing Methods as Public 415
Trying to Instantiate an Interface 418
Modifying the Signature in the Implementing Method 430
12 Event Handling (Optional)
Forgetting to Attach a Listener 450
Forgetting to Repaint 459
Implementing a Graphical User Interface (GUI) 453
13 Inheritance Confusing Super- and Subclasses 472
Shadowing Instance Fields 479
Failing to Invoke the Superclass Method 480
Accidental Package Access 493
Making Inherited Methods Less Accessible 493
Defining the equals Method with the WrongParameter Type 500
Forgetting to Clone 502
Clone Mutable Instance Fields in Accessor Methods 502
jc4fm_7.fm Page xx Wednesday, November 10, 2004 4:01 PM
Table of Special Features xxi
Productivity Hints Advanced Topics Random Facts
Array Initialization 284
Two-Dimensional Arrays with Variable Row Lengths 302
Multidimensional Arrays 302
Partially Filled Arrays 308
Methods with a Variable Number of Parameters 309
An Early Internet Worm 310
Global Search and Replace 350
Regular Expressions 350
Call by Value and Call by Reference 334
Class Invariants 339
Alternative Forms of Field Initialization 345
Static Imports 351
The Explosive Growth of Personal Computers 358
Batch Files and Shell Scripts 385 Reading Test Inputs from a File 379
Unit Testing with JUnit 386
The First Bug 399
The Therac-25 Incidents 400
Constants in Interfaces 416
Anonymous Classes 426
Operating Systems 433
Don’t Use a Container as a Listener 450
Code Reuse 454
Event Adapters 459 Programming Languages 460
Supply toString in All Classes 497
Abstract Classes 490
Final Methods and Classes 491
Protected Access 494
Inheritance and the toStringMethod 497
Inheritance and the equalsMethod 500
Implementing the clone Method 503
Enumerated Types Revisited 505
Scripting Languages 506
jc4fm_7.fm Page xxi Wednesday, November 10, 2004 4:01 PM
xxii Table of Special Features
Chapter Common Errors How Tos Quality Tips
14 Graphical User Interfaces (Optional)
Layout Management 528
15 Exception Handling Throw Early, Catch Late 560
Do Not Squelch Exceptions 560
Do Not Use catch and finally in the Same try Statement 562
Do Throw Specific Exceptions 564
16 Files and Streams Backslashes in File Names 578
Negative byte Values 582
Using Files and Streams 597
17 Object-Oriented Design
CRC Cards and UML Diagrams 619
18 Recursion Infinite Recursion 668
Tracing Through Recursive Methods 672
Thinking Recursively 673
19 Sorting and Searching The compareTo Method Can Return Any Integer, Not Just –1, 0, and 1 732
20 An Introduction to Data Structures
21 Advanced Data Structures
Forgetting to Define hashCode 795
Choosing a Container 813 Use Interface References to Manipulate Data Structures 780
22 Generic Programming Genericity and Inheritance 854
Writing Code That Does Not Work After Types Are Erased 857
Using Generic Types in a Static Context 858
jc4fm_7.fm Page xxii Wednesday, November 10, 2004 4:01 PM
Table of Special Features xxiii
Productivity Hints Advanced Topics Random Facts
Adding the main Method to the Frame Class 517
Visual Programming 544
The Ariane Rocket Incident 569
Use Object Streams 599 File Dialog Boxes 578
Command Line Arguments 579
Serializing Geometric Objects 599
Encryption Algorithms 586
Attributes and Methods in UML Diagrams 620
Multiplicities 621
Aggregation and Association 621
Programmer Productivity 613
Software Development—Art or Science? 655
The Limits of Computation 684
Insertion Sort 713
Oh, Omega, and Theta 715
The Quicksort Algorithm 722
The Parameterized Comparable Interface 733
The Comparator Interface 733
The First Programmer 724
The Iterable Interface and the “For Each” Loop 746
Static Inner Classes 758
Standardization 765
Reverse Polish Notation 810
Software Piracy 814
Wildcard Types 855
jc4fm_7.fm Page xxiii Wednesday, November 10, 2004 4:01 PM
Top Related