Preface, xvii
To the Reader, xvii
About This Book, xviii
Conventions, xx
Sample Code, xx
Acknowledgments, xxi
Chapter 1
An Introduction to Java, 1
Java as a Programming Tool, 2
Advantages of Java, 2
The Java "White Paper" Buzzwords, 3
Simple, 4
Object Oriented, 4
Distributed, 5
Robust, 5
Secure, 5
Architecture Neutral, 6
Portable, 7
Interpreted, 7
High Performance, 7
Multithreaded, 8
Dynamic, 8
Java and the Internet, 8
A Short History of Java, 9
Common Misconceptions About Java, 11
Chapter 2
The Java Programming Environment, 15
Installing the Java Software Development Kit, 15
Setting the Execution Path, 16
Installing the Library Source and Documentation, 17
Installing the Core Java Program Examples, 17
Navigating the Java Directories, 17
Development Environments, 18
Using the Command Line Tools, 19
Troubleshooting Hints, 20
Using an Integrated Development Environment, 21
Locating Compilation Errors, 22
Compiling and Running Programs from a Text Editor, 24
Graphical Applications, 27
Applets, 29
Chapter 3
Fundamental Programming Structures in Java, 35
A Simple Java Program, 35
Comments, 38
Data Types, 39
Integers, 39
Floating-Point Types, 40
The Character Type, 41
The boolean Type, 42
Variables, 42
Assignments and Initializations, 43
Constants, 43
Operators, 44
Increment and Decrement Operators, 45
Relational and boolean Operators, 45
Bitwise Operators, 46
Mathematical Functions and Constants, 47
Conversions Between Numeric Types, 47
Casts, 48
Parentheses and Operator Hierarchy, 49
Strings, 49
Concatenation, 50
Substrings, 50
String Editing, 50
Testing Strings for Equality, 52
Reading the On-line API Documentation, 53
Reading Input, 56
Formatting Output, 57
Control Flow, 60
Block Scope, 60
Conditional Statements, 61
Indeterminate Loops, 64
Determinate Loops, 68
Multiple Selections--the switch Statement, 70
Breaking Control Flow, 71
Big Numbers, 74
Arrays, 76
Array Initializers and Anonymous Arrays, 77
Copying Arrays, 77
Command Line Parameters, 79
Sorting an Array, 79
Multidimensional Arrays, 82
Ragged Arrays, 85
Chapter 4
Objects and Classes, 89
Introduction to Object-Oriented Programming, 89
The Vocabulary of OOP, 91
Objects, 91
Relationships Between Classes, 92
Contrasting OOP with Traditional Procedural Programming Techniques, 94
Using Existing Classes, 96
Objects and Object Variables, 96
The GregorianCalendar Class of the ]ava Library, 98
Building Your Own Classes, 104
An Employee Class, 104
Using Multiple Source Files, 107
Analyzing the Employee Class, 108
First Steps with Constructors, 108
The Methods of the Employee Class, 110
Method Access to Private Data, 113
Private Methods, 113
Final Instance Fields, 113
Static Fields and Methods, 114
Static Fields, 114
Constants, 114
Static Methods, 115
Factory Methods, 116
The main Method, 116
Method Parameters, 118
Object Construction, 124
Overloading, 124
Default Field Initialization, 124
Default Constructors, 125
Explicit Field Initialization, 125
Parameter Names, 126
Calling Another Constructor, 127
Initialization Blocks, 127
Object Destruction and the finalize Method, 131
Packages, 131
Using Packages, 132
Documentation Comments, 139
How to Insert Comments, 139
Class Comments, 139
Method Comments, 140
Field Comments, 140
General Comments, 141
Package and Overview Comments, 142
How to Extract Comments, 142
Class Design Hints, 142
Chapter 5
Inheritance, 145
Extending Classes, 145
Inheritance Hierarchies, 152
Polymorphism, 152
Dynamic Binding, 153
Preventing Inheritance: Final Classes and Methods, 155
Casting, 156
Abstract Classes, 158
Protected Access, 162
Obi ect: The Cosmic Superclass, 163
The equals and tostring methods, 163
Generic Programming, 169
Array Lists, 171
Object Wrappers, 177
The Class Class, 180
Reflection, 183
Using Reflection to Analyze the Capabilities of Classes, 184
Using Reflection to Analyze Objects at Run Time, 188
Using Reflection to Write Generic Array Code, 192
Method Pointers.t, 195
Design Hints for Inheritance, 199
Chapter 6
Interfaces and Inner Classes, 201
Interfaces, 201
Properties of Interfaces, 205
Interfaces and Abstract Classes, 206
Interfaces and Callbacks, 207
Object Cloning, 210
Inner Classes, 215
Using an Inner Class to Access Object State, 216
Special Syntax Rules for Inner Classes, 219
Are Inner Classes Useful? Are They Actually Necessary?
Are They Secure?, 220
Local Inner Classes, 222
Static Inner Classes, 227
Proxies, 230
Properties of Proxy Classes, 233
Chapter 7
Graphics Programming, 235
Introduction to Swing, 235
Creating a Frame, 238
Frame Positioning, 241
Displaying Information in a Panel, 246
2D Shapes, 251
Colors, 258
Filling Shapes, 259
Text and Fonts, 262
Images, 270
Chapter 8
Event Handling 277
Basics of Event Handling, 278
Example: Handling a button click, 280
Becoming Comfortable with Inner Classes, 285
Turning Components into Event Listeners, 287
Example: Changing the Look and Feel, 289
Example: Capturing Window Events, 291
The AWT Event Hierarchy, 295
Semantic and Low-Level Events in the AWT, 297
Event Handling Summary, 297
Low-Level Event Types, 300
Keyboard Events, 300
Mouse Events, 305
Focus Events, 313
Actions, 316
Multicasting, 324
The Event Queue, 326
Adding Custom Events, 327
Chapter 9
User Interface Components with Swing, 335
The Model-View-Controller Design Pattern, 336
A Model-View-Controller Analysis of Swing Buttons, 340
An Introduction to Layout Management, 341
Border Layout, 343
Panels, 344
Grid Layout, 346
Text Input, 350
Text Fields, 350
Password Fields, 356
Formatted Input Fields, 356
Text Areas, 371
Labels and Labeling Components, 374
Selecting and Editing Text, 376
Making Choices, 378
Check Boxes, 378
Radio Buttons, 381
Borders, 385
Combo Boxes, 389
Sliders, 392
The JSpinner Component, 398
Menus, 406
Building Menus, 406
Icons in Menu Items, 408
Check Box and Radio Button Menu Items, 410
Pop-up Menus, 411
Keyboard Mnemonics and Accelerators, 412
Enabling and Disabling Menu Items, 415
Tool Bars, 419
Tool Tips, 421
Sophisticated Layout Management, 424
Box Layout, 426
The Grid Bag Layout, 430
The Spring Layout, 436
Using No Layout Manager, 446
Custom Layout Managers, 446
Traversal Order, 450
Dialog Boxes, 452
Option Dialogs, 452
Creating Dialogs, 462
Data Exchange, 466
File Dialogs, 473
Color Choosers, 483
Chapter 10
Deploying Applets and Applications, 491
Applet Basics, 491
A Simple Applet, 494
Viewing an Applet, 494
Converting Applications to Applets, 496
Life Cycle of an Applet, 498
Security Basics, 499
Pop-Up Windows in Applets, 500
The Applet HTML Tags and Attributes, 502
Applet Attributes for Positioning, 502
Applet Attributes for Code, 504
Applet Attributes for Java-Challenged Viewers, 505
The obi ect Tag, 506
Passing Information to Applets, 506
Multimedia, 511
URLs, 511
Obtaining Multimedia Files, 512
The Applet Context, 513
lnter-Applet Communication, 514
Displaying Items in the Browser, 514
A Boobnark Applet, 516
It's an Applet. It's an Application. It's Both!, 518
JAR Files, 523
JAR Caching, 524
Packaging Applications, 525
The Manifest, 525
Self-Running JAR files, 526
Resources, 527
Optional Packages, 530
Sealing, 531
Java Web Start, 532
The JNLP API, 534
Storing Application Preferences, 544
Property Sets, 545
The Preferences API, 551
Chapter 11
Exceptions and Debugging, 557
Dealing with Errors, 558
The Classification of Exceptions, 559
Advertising the Exceptions That a Method Throws, 560
How to Throw an Exception, 562
Creating Exception Classes, 563
Catching Exceptions, 564
Catching Multiple Exceptions, 566
Rethrowing Exceptions, 566
Chained Exceptions, 569
Stack Frames, 570
A Final Look at Java Error and Exception Handling, 572
Some Tips on Using Exceptions, 576
Logging, 578
Basic Logging, 579
Advanced Logging, 579
Changing the Log Manager Configuration, 581
Localization, 582
Handlers, 582
Filters, 585
Formatters, 586
Assertions, 593
Enabling and Disabling Assertions, 594
Usage Hints for Assertions, 595
Debugging Techniques, 597
Useful Tricks for Debugging, 597
Using a Console Window, 600
Tracing A WT Events, 602
The AWT Robot, 605
Profiling, 609
Coverage Testing, 612
Using a Debugger, 613
The ]DB Debugger, 614
The Sun ONE Studio Debugger, 619
Chapter 12
Streams and Files, 621
Streams, 621
Reading and Writing Bytes, 622
The Complete Stream Zoo, 624
Layering Stream Filters, 626
Data Streams, 629
Random-Access File Streams, 632
ZIP File Streams, 641
Putting Streams to Use, 649
Writing Delimited Output, 649
String Tokenizers and Delimited Text, 650
Reading Delimited Input, 651
Random-Access Streams, 654
Object Streams, 661
Storing Objects of Variable Type, 661
Object Serialization File Format, 665
The Problem of Saving Object References, 668
Output Format for Object References, 675
Modifying the Default Serialization Mechanism, 677
Serializing Typesafe Enumerations, 679
Versioning, 680
Using Serialization for Cloning, 682
File Management, 684
New I/O, 689
Memory-Mapped Files, 690
File Locking, 694
Regular Expressions, 698
Appendix, 707
Index, 709