Contents
1 AN INTRODUCTION TO JAVA
Java As a Programming Platform
The Java “White Paper” Buzzwords
Simple
Object Oriented
Network-Savvy
Robust
Secure
Architecture Neutral
Portable
Interpreted
High Performance
Multithreaded
Dynamic
Java Applets and the Internet
A Short History of Java
Common Misconceptions about Java
2 THE JAVA PROGRAMMING ENVIRONMENT
Installing the Java Development Kit
Downloading the JDK
Setting the Execution Path
Installing the Library Source and Documentation
Installing the Core Java Program Examples
Navigating the Java Directories
Choosing a Development Environment
Using the Command-Line Tools
Troubleshooting Hints
Using an Integrated Development Environment
Locating Compilation Errors
Running a Graphical Application
Building and Running Applets
3 FUNDAMENTAL PROGRAMMING STRUCTURES IN JAVA
A Simple Java Program
Comments
Data Types
Integer Types
Floating-Point Types
The char Type
The boolean Type
Variables
Initializing Variables
Constants
Operators
Increment and Decrement Operators
Relational and boolean Operators
Bitwise Operators
Mathematical Functions and Constants
Conversions between Numeric Types
Casts
Parentheses and Operator Hierarchy
Enumerated Types
Strings
Substrings
Concatenation
Strings Are Immutable
Testing Strings for Equality
Code Points and Code Units
The String API
Reading the On-Line API Documentation
Building Strings
Input and Output
Reading Input
Formatting Output
File Input and Output
Control Flow
Block Scope
Conditional Statements
Loops
Determinate Loops
Multiple Selections——The switch Statement
Statements That Break Control Flow
Big Numbers
Arrays
The “for each” Loop
Array Initializers and Anonymous Arrays
Array Copying
Command-Line Parameters
Array Sorting
Multidimensional Arrays
Ragged Arrays
4 OBJECTS AND CLASSES
Introduction to Object-Oriented Programming
Classes
Objects
Identifying Classes
Relationships between Classes
Using Predefined Classes
Objects and Object Variables
The GregorianCalendar Class of the Java Library
Mutator and Accessor Methods
Defining Your Own Classes
An Employee Class
Use of Multiple Source Files
Dissecting the Employee Class
First Steps with Constructors
Implicit and Explicit Parameters
Benefits of Encapsulation
Class-Based Access Privileges
Private Methods
Final Instance Fields
Static Fields and Methods
Static Fields
Static Constants
Static Methods
Factory Methods
The main Method
Method Parameters
Object Construction
Overloading
Default Field Initialization
Default Constructors
Explicit Field Initialization
Parameter Names
Calling Another Constructor
Initialization Blocks
Object Destruction and the finalize Method
Packages
Class Importation
Static Imports
Addition of a Class into a Package
Package Scope
The Class Path
Setting the Class Path
Documentation Comments
Comment Insertion
Class Comments
Method Comments
Field Comments
General Comments
Package and Overview Comments
Comment Extraction
Class Design Hints
5 INHERITANCE
Classes, Superclasses, and Subclasses
Inheritance Hierarchies
Polymorphism
Dynamic Binding
Preventing Inheritance: Final Classes and Methods
Casting
Abstract Classes
Protected Access
Object: The Cosmic Superclass
The equals Method
Equality Testing and Inheritance
The hashCode Method
The toString Method
Generic Array Lists
Accessing Array List Elements
Compatibility between Typed and Raw Array Lists
Object Wrappers and Autoboxing
Methods with a Variable Number of Parameters
Enumeration Classes
Reflection
The Class Class
A Primer on Catching Exceptions
Using Reflection to Analyze the Capabilities of Classes
Using Reflection to Analyze Objects at Runtime
Using Reflection to Write Generic Array Code
Method Pointers!
Design Hints for Inheritance
6 INTERFACES AND INNER CLASSES
Interfaces
Properties of Interfaces
Interfaces and Abstract Classes
Object Cloning
Interfaces and Callbacks
Inner Classes
Use of an Inner Class to Access Object State
Special Syntax Rules for Inner Classes
Are Inner Classes Useful? Actually Necessary? Secure?
Local Inner Classes
Accessing final Variables from Outer Methods
Anonymous Inner Classes
Static Inner Classes
Proxies
Properties of Proxy Classes
7 GRAPHICS PROGRAMMING
Introducing Swing
Creating a Frame
Positioning a Frame
Frame Properties
Determining a Good Frame Size
Displaying Information in a Component
Working with D Shapes
Using Color
Using Special Fonts for Text
Displaying Images
8 EVENT HANDLING
Basics of Event Handling
Example: Handling a Button Click
Becoming Comfortable with Inner Classes
Creating Listeners Containing a Single Method Call
Example: Changing the Look and Feel
Adapter Classes
Actions
Mouse Events
The AWT Event Hierarchy
Semantic and Low-Level Events
9 USER INTERFACE COMPONENTS WITH SWING
Swing and the Model-View-Controller Design Pattern
Design Patterns
The Model-View-Controller Pattern
A Model-View-Controller Analysis of Swing Buttons
Introduction to Layout Management
Border Layout
Grid Layout
Text Input
Text Fields
Labels and Labeling Components
Password Fields
Text Areas
Scroll Panes
Choice Components
Checkboxes
Radio Buttons
Borders
Combo Boxes
Sliders
Menus
Menu Building
Icons in Menu Items
Checkbox and Radio Button Menu Items
Pop-Up Menus
Keyboard Mnemonics and Accelerators
Enabling and Disabling Menu Items
Toolbars
Tooltips
Sophisticated Layout Management
The Grid Bag Layout
Group Layout
Using No Layout Manager
Custom Layout Managers
Traversal Order
Dialog Boxes
Option Dialogs
Creating Dialogs
Data Exchange
File Dialogs
Color Choosers
10 DEPLOYING APPLICATIONS AND APPLETS
JAR Files
The Manifest
Executable JAR Files
Resources
Sealing
Java Web Start
The Sandbox
Signed Code
The JNLP API
Applets
A Simple Applet
The Applet HTML Tag and Its Attributes
The object Tag
Use of Parameters to Pass Information to Applets
Accessing Image and Audio Files
The Applet Context
Storage of Application Preferences
Property Maps
The Preferences API
11 EXCEPTIONS, LOGGING, ASSERTIONS, AND DEBUGGING
Dealing with Errors
The Classification of Exceptions
Declaring Checked Exceptions
How to Throw an Exception
Creating Exception Classes
Catching Exceptions
Catching Multiple Exceptions
Rethrowing and Chaining Exceptions
The finally Clause
Analyzing Stack Trace Elements
Tips for Using Exceptions
Using Assertions
Assertion Enabling and Disabling
Using Assertions for Parameter Checking
Using Assertions for Documenting Assumptions
Logging
Basic Logging
Advanced Logging
Changing the Log Manager Configuration
Localization
Handlers
Filters
Formatters
A Logging Recipe
Debugging Tips
Using a Console Window
Tracing AWT Events
Letting the AWT Robot Do the Work
Using a Debugger
12 GENERIC PROGRAMMING
Why Generic Programming?
Who Wants to Be a Generic Programmer?
Definition of a Simple Generic Class
Generic Methods
Bounds for Type Variables
Generic Code and the Virtual Machine
Translating Generic Expressions
Translating Generic Methods
Calling Legacy Code
Restrictions and Limitations
Type Parameters Cannot Be Instantiated with Primitive Types
Runtime Type Inquiry Only Works with Raw Types
You Cannot Throw or Catch Instances of a Generic Class
Arrays of Parameterized Types Are Not Legal
You Cannot Instantiate Type Variables
Type Variables Are Not Valid in Static Contexts of Generic Classes
Beware of Clashes After Erasure
Inheritance Rules for Generic Types
Wildcard Types
Supertype Bounds for Wildcards
Unbounded Wildcards
Wildcard Capture
Reflection and Generics
Using Class[T] Parameters for Type Matching
Generic Type Information in the Virtual Machine
13 COLLECTIONS
Collection Interfaces
Separating Collection Interfaces and Implementation
Collection and Iterator Interfaces in the Java Library
Concrete Collections
Linked Lists
Array Lists
Hash Sets
Tree Sets
Object Comparison
Queues and Deques
Priority Queues
Maps
Specialized Set and Map Classes
The Collections Framework
Views and Wrappers
Bulk Operations
Converting between Collections and Arrays
Algorithms
Sorting and Shuffling
Binary Search
Simple Algorithms
Writing Your Own Algorithms
Legacy Collections
The Hashtable Class
Enumerations
Property Maps
Stacks
Bit Sets
14 MULTITHREADING
What Are Threads?
Using Threads to Give Other Tasks a Chance
Interrupting Threads
Thread States
New Threads
Runnable Threads
Blocked and Waiting Threads
Terminated Threads
Thread Properties
Thread Priorities
Daemon Threads
Handlers for Uncaught Exceptions
Synchronization
An Example of a Race Condition
The Race Condition Explained
Lock Objects
Condition Objects
The synchronized Keyword
Synchronized Blocks
The Monitor Concept
Volatile Fields
Deadlocks
Lock Testing and Timeouts
Read/Write Locks
Why the stop and suspend Methods Are Deprecated
Blocking Queues
Thread-Safe Collections
Efficient Maps, Sets, and Queues
Copy on Write Arrays
Older Thread-Safe Collections
Callables and Futures
Executors
Thread Pools
Scheduled Execution
Controlling Groups of Tasks
Synchronizers
Semaphores
Countdown Latches
Barriers
Exchangers
Synchronous Queues
Example: Pausing and Resuming an Animation
Threads and Swing
Running Time-Consuming Tasks
Using the Swing Worker
The Single-Thread Rule
Appendix
Index