Chapter 1:An Introduction to Java 1
1.1 Java As a Programming Platform 1
1.2 The Java"White Paper"Buzzwords 2
1.2.1 Simple 3
1.2.2 Object-Oriented 4
1.2.3 Network-Savvy 4
1.2.4 Robust 5
1.2.5 Secure 5
1.2.6 Architecture-Neutral 6
1.2.7 Portable 7
1.2.8 Interpreted 7
1.2.9 High-Performance 8
1.2.10 Multithreaded 8
1.2.11 Dynamic 8
1.3 Java Applets and the Internet 9
1.4 A Short History of Java 10
1.5 Common Misconceptions about Java 13
Chapter 2:The Java Programming Environment 17
2.1 Installing the Java Development Kit 18
2.1.1 Downloading the JDK 18
2.1.2 Setting the Executable Path 20
2.1.3 Installing the Library Source and Documentation 22
2.1.4 Installing the Core Java Program Examples 23
2.1.5 Navigating the Java Directories 24
2.2 Choosing a Development Environment 24
2.3 Using the Command-Line Tools 25
2.3.1 Troubleshooting Hints 27
2.4 Using an Integrated Development Environment 28
2.4.1 Locating Compilation Errors 32
2.5 Running a Graphical Application 33
2.6 Building and Running Applets 36
Chapter 3:Fundamental Programming Structures in Java 41
3.1 A Simple Java Program 42
3.2 Comments 45
3.3 Data Types 46
3.3.1 Integer Types 47
3.3.2 Floating-Point Types 48
3.3.3 The char Type 49
3.3.4 The boolean Type 51
3.4 Variables 52
3.4.1 Initializing Variables 53
3.4.2 Constants 54
3.5 Operators 55
3.5.1 Increment and Decrement Operators 56
3.5.2 Relational and boolean Operators 57
3.5.3 Bitwise Operators 58
3.5.4 Mathematical Functions and Constants 59
3.5.5 Conversions between Numeric Types 60
3.5.6 Casts 61
3.5.7 Parentheses and Operator Hierarchy 62
3.5.8 Enumerated Types 63
3.6 Strings 64
3.6.1 Substrings 64
3.6.2 Concatenation 64
3.6.3 Strings Are Immutable 65
3.6.4 Testing Strings for Equality 67
3.6.5 Empty and Null Strings 68
3.6.5 Code Points and Code Units 68
3.6.6 The String API 69
3.6.7 Reading the Online API Documentation 72
3.6.8 Building Strings 74
3.7 Input and Output 76
3.7.1 Reading Input 76
3.7.2 Formatting Output 79
3.7.3 File Input and Output 84
3.8 Control Flow 86
3.8.1 Block Scope 86
3.8.2 Conditional Statements 87
3.8.3 Loops 91
3.8.4 Determinate Loops 95
3.8.5 Multiple Selections—The switch Statement 99
3.8.6 Statements That Break Control Flow 102
3.9 Big Numbers 105
3.10 Arrays 107
3.10.1 The""for each""Loop 109
3.10.2 Array Initializers and Anonymous Arrays 110
3.10.3 Array Copying 111
3.10.4 Command-Line Parameters 112
3.10.5 Array Sorting 113
3.10.6 Multidimensional Arrays 116
3.10.7 Ragged Arrays 120
Chapter 4:Objects and Classes 125
4.1 Introduction to Object-Oriented Programming 126
4.1.1 Classes 127
4.1.2 Objects 128
4.1.3 Identifying Classes 129
4.1.4 Relationships between Classes 129
4.2 Using Predefined Classes 132
4.2.1 Objects and Object Variables 132
4.2.2 The GregorianCalendar Class of the Java Library 136
4.2.3 Mutator and Accessor Methods 138
4.3 Defining Your Own Classes 145
4.3.1 An Employee Class 145
4.3.2 Use of Multiple Source Files 148
4.3.3 Dissecting the Employee Class 149
4.3.4 First Steps with Constructors 150
4.3.5 Implicit and Explicit Parameters 152
4.3.6 Benefits of Encapsulation 153
4.3.7 Class-Based Access Privileges 156
4.3.8 Private Methods 156
4.3.9 Final Instance Fields 157
4.4 Static Fields and Methods 157
4.4.1 Static Fields 157
4.4.2 Static Constants 158
4.4.3 Static Methods 159
4.4.4 Factory Methods 161
4.4.5 The main Method 161
4.5 Method Parameters 164
4.6 Object Construction 171
4.6.1 Overloading 171
4.6.2 Default Field Initialization 172
4.6.3 The Constructor with No Arguments 173
4.6.4 Explicit Field Initialization 174
4.6.5 Parameter Names 175
4.6.6 Calling Another Constructor 176
4.6.7 Initialization Blocks 176
4.6.8 Object Destruction and the finalize Method 181
4.7 Packages 182
4.7.1 Class Importation 182
4.7.2 Static Imports 184
4.7.3 Addition of a Class into a Package 185
4.7.4 Package Scope 188
4.8 The Class Path 190
4.8.1 Setting the Class Path 192
4.9 Documentation Comments 193
4.9.1 Comment Insertion 193
4.9.2 Class Comments 194
4.9.3 Method Comments 195
4.9.4 Field Comments 196
4.9.5 General Comments 196
4.9.6 Package and Overview Comments 197
4.9.7 Comment Extraction 198
4.10 Class Design Hints 199
Chapter 5:Inheritance 203
5.1 Classes,Superclasses,and Subclasses 204
5.1.1 Inheritance Hierarchies 212
5.1.2 Polymorphism 213
5.1.3 Dynamic Binding 214
5.1.4 Preventing Inheritance:Final Classes and Methods 217
5.1.5 Casting 218
5.1.6 Abstract Classes 221
5.1.7 Protected Access 227
5.2 Object:The Cosmic Superclass 228
5.2.1 The equals Method 228
5.2.2 Equality Testing and Inheritance 230
5.2.3 The hashCode Method 234
5.2.4 The toString Method 236
5.3 Generic Array Lists 243
5.3.1 Accessing Array List Elements 246
5.3.2 Compatibility between Typed and Raw Array Lists 249
5.4 Object Wrappers and Autoboxing 251
5.5 Methods with a Variable Number of Parameters 254
5.6 Enumeration Classes 256
5.7 Reflection 258
5.7.1 The Class Class 259
5.7.2 A Primer on Catching Exceptions 261
5.7.3 Using Reflection to Analyze the Capabilities of Classes 263
5.7.4 Using Reflection to Analyze Objects at Runtime 269
5.7.5 Using Reflection to Write Generic Array Code 274
5.7.6 Invoking Arbitrary Methods 278
5.8 Design Hints for Inheritance 282
Chapter 6:Interfaces and Inner Classes 285
6.1 Interfaces 286
6.1.1 Properties of Interfaces 292
6.1.2 Interfaces and Abstract Classes 294
6.2 Object Cloning 295
6.3 Interfaces and Callbacks 302
6.4 Inner Classes 305
6.4.1 Use of an Inner Class to Access Object State 307
6.4.2 Special Syntax Rules for Inner Classes 311
6.4.3 Are Inner Classes Useful?Actually Necessary?Secure? 312
6.4.4 Local Inner Classes 315
6.4.5 Accessing final Variables from Outer Methods 315
6.4.6 Anonymous Inner Classes 318
6.4.7 Static Inner Classes 322
6.5 Proxies 326
6.5.1 Properties of Proxy Classes 331
Chapter 7:Graphics Programming 333
7.1 Introducing Swing 334
7.2 Creating a Frame 339
7.3 Positioning a Frame 342
7.3.1 Frame Properties 345
7.3.2 Determining a Good Frame Size 345
7.4 Displaying Information in a Component 350
7.5 Working with 2D Shapes 356
7.6 Using Color 365
7.7 Using Special Fonts for Text 369
7.8 Displaying Images 378
Chapter 8:Event Handling 383
8.1 Basics of Event Handling 383
8.1.1 Example:Handling a Button Click 386
8.1.2 Becoming Comfortable with Inner Classes 391
8.1.3 Creating Listeners Containing a Single Method Call 394
8.1.4 Example:Changing the Look-and-Feel 395
8.1.5 Adapter Classes 399
8.2 Actions 403
8.3 Mouse Events 411
8.4 The AWT Event Hierarchy 419
8.4.1 Semantic and Low-Level Events 421
Chapter 9:User Interface Components with Swing 425
9.1 Swing and the Model-View-Controller Design Pattern 426
9.1.1 Design Patterns 426
9.1.2 The Model-View-Controller Pattern 428
9.1.3 A Model-View-Controller Analysis of Swing Buttons 432
9.2 Introduction to Layout Management 433
9.2.1 Border Layout 437
9.2.2 Grid Layout 439
9.3 Text Input 443
9.3.1 Text Fields 444
9.3.2 Labels and Labeling Components 446
9.3.3 Password Fields 447
9.3.4 Text Areas 448
9.3.5 Scroll Panes 449
9.4 Choice Components 452
9.4.1 Checkboxes 452
9.4.2 Radio Buttons 454
9.4.3 Bordes 458
9.4.4 Combo Boxes 462
9.4.5 Sliders 466
9.5 Menus 473
9.5.1 Menu Building 473
9.5.2 Icons in Menu Items 476
9.5.3 Checkbox and Radio Button Menu Items 477
9.5.4 Pop-UP Menus 479
9.5.5 Keyboard Mnemonics and Accelerators 480
9.5.6 Enabling and Disabling Menu Items 483
9.5.7 Toolbars 488
9.5.8 Tooltips 490
9.6 Sophisticated Layout Management 492
9.6.1 The Grid Bag Layout 494
9.6.1.1 The gridx,gridy,gridwidth,and gridheight Parameters 496
9.6.1.2 Weight Fields 496
9.6.1.3 The fill and anchor Parameters 497
9.6.1.4 Padding 497
9.6.1.5 AlternativeMethodtoSpecifythe gridx,gridy,gridwidth,and gridheight Parameters 497
9.6.1.6 A Helper Class to Tame the Grid Bag Constraints 499
9.6.2 Group Layout 505
9.6.3 Using No Layout Manager 516
9.6.4 Custom Layout Managers 516
9.6.5 Traversal Order 521
9.7 Dialog Boxes 522
9.7.1 Option Dialogs 523
9.7.2 Creating Dialogs 533
9.7.3 Data Exchange 538
9.7.4 File Dialogs 545
9.7.5 Color Choosers 557