Contents
List of Figures
List of Tables
List of Examples
Foreword
Preface
1 Basics of Java Programming
1.1 Introduction
1.2 Classes
Declaring Members: Fields and Methods
1.3 Objects
Class Instantiation, Reference Values, and References
Object Aliases
1.4 Instance Members
Invoking Methods
1.5 Static Members
1.6 Inheritance
1.7 Aggregation
1.8 Tenets of Java
Review Questions
1.9 Java Programs
1.10 Sample Java Application
Essential Elements of a Java Application
Compiling and Running an Application
Review Questions
Chapter Summary
Programming Exercise
2 Language Fundamentals
2.1 Basic Language Elements
Lexical Tokens
Identifiers
Keywords
Literals
Integer Literals
Floating-Point Literals
Boolean Literals
Character Literals
String Literals
White Spaces
Comments
Review Questions
2.2 Primitive Data Types
Integer Types
The char Type
The Floating-Point Types
The boolean
Type
Review Questions
2.3 Variable Declarations
Declaring and Initializing Variables
Reference Variables
2.4 Initial Values for Variables
Default Values for Fields
Initializing Local Variables of Primitive Data Types
Initializing Local Reference Variables
Lifetime of Variables
Review Questions
Chapter Summary
Programming Exercise
3 Declarations
3.1 Class Declarations
3.2 JavaBeans Standard
Naming Patterns for Properties
Naming Patterns for the Event Model
3.3 Method Declarations
Statements
Instance Methods and the Object Reference this
Method Overloading
3.4 Constructors
The Default Constructor
Overloaded Constructors
Review Questions
3.5 Enumerated Types
Declaring Typesafe Enums
Using Typesafe Enums
Declaring Enum Constructors and Members
Implicit Static Methods for Enum Types
Inherited Methods from the Enum
Class
Extending Enum Types: Constant-Specific Class Bodies
Declaring Typesafe Enums Revisited
Review Questions
3.6 Arrays
Declaring Array Variables
Constructing an Array
Initializing an Array
Using an Array
Anonymous Arrays
Multidimensional Arrays
Review Questions
3.7 Parameter Passing
Passing Primitive Data Values
Passing Reference Values
Passing Arrays
Array Elements as Actual Parameters
final
Parameters
3.8 Variable Arity Methods
Calling a Varargs Method
Varargs and Non-Varargs Method Calls
3.9 The main()
Method
Program Arguments
Review Questions
Chapter Summary
Programming Exercises
4 Access Control
4.1 Java Source File Structure
4.2 Packages
Defining Packages
Using Packages
Compiling Code into Packages
Running Code from Packages
4.3 Searching for Classes
4.4 The JAR Utility
4.5 System Properties
Review Questions
4.6 Scope Rules
Class Scope for Members
Block Scope for Local Variables
4.7 Accessibility Modifiers for Top-Level Type Declarations
4.8 Other Modifiers for Classes
abstract
Classes
final
Classes
Review Questions
4.9 Member Accessibility Modifiers
public
Members
protected
Members
Default Accessibility for Members
private
Members
Review Questions
4.10 Other Modifiers for Members
static
Members
final
Members
abstract
Methods
synchronized
Methods
native
Methods
transient
Fields
volatile
Fields
Review Questions
Chapter Summary
Programming Exercise
5 Operators and Expressions
5.1 Conversions
Widening and Narrowing Primitive Conversions
Widening and Narrowing Reference Conversions
Boxing and Unboxing Conversions
Other Conversions
5.2 Type Conversion Contexts
Assignment Context
Method Invocation Context
Casting Context of the Unary Type Cast Operator: (type)
Numeric Promotion Context
5.3 Precedence and Associativity Rules for Operators
5.4 Evaluation Order of Operands
Left-Hand Operand Evaluation First
Operand Evaluation before Operation Execution
Left to Right Evaluation of Argument Lists
5.5 The Simple Assignment Operator =
Assigning Primitive Values
Assigning References
Multiple Assignments
Type Conversions in Assignment Context
Review Questions
5.6 Arithmetic Operators: *, /, %, +, -
Arithmetic Operator Precedence and Associativity
Evaluation Order in Arithmetic Expressions
Range of Numeric Values
Unary Arithmetic Operators: -, +
Multiplicative Binary Operators: *, /, %
Additive Binary Operators: +, -
Numeric Promotions in Arithmetic Expressions
Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -=
Review Questions
5.7 The Binary String Concatenation Operator +
5.8 Variable Increment and Decrement Operators: ++, --
The Increment Operator ++
The Decrement Operator --
Review Questions
5.9 Boolean Expressions
5.10 Relational Operators: <, <=, >, >=
5.11 Equality
Primitive Data Value Equality: ==, !=
Object Reference Equality: ==, !=
Object Value Equality
5.12 Boolean Logical Operators: !, ^, &, |
Operand Evaluation for Boolean Logical Operators
Boolean Logical Compound Assignment Operators: &=, ^=, |=
5.13 Conditional Operators: &&, ||
Short-Circuit Evaluation
Review Questions
5.14 The Conditional Operator: ?:
5.15 Other Operators: new, []
, instanceof
Chapter Summary
Programming Exercise
6 Control Flow
6.1 Overview of Control Flow Statements
6.2 Selection Statements
The Simple if
Statement
The if-else
Statement
The switch
Statement
Review Questions
6.3 Iteration Statements
The while
Statement
The do-while
Statement
The for(;;)
Statement
The for(:)
Statement
6.4 Transfer Statements
Labeled Statements
The break
Statement
The continue
Statement
The return
Statement
Review Questions
6.5 Stack-Based Execution and Exception Propagation
6.6 Exception Types
The Exception
Class
The RuntimeException
Class
The Error
Class
Checked and Unchecked Exceptions
Defining New Exceptions
6.7 Exception Handling: try
, catch
, and finally
The try
Block
The catch
Block
The finally
Block
6.8 The throw
Statement
6.9 The throws
Clause
Review Questions
6.10 Assertions
The assert
Statement and the AssertionError Class
Compiling Assertions
Runtime Enabling and Disabling of Assertions
Using Assertions
Review Questions
Chapter Summary
Programming Exercises
7 Object-Oriented Programming
7.1 Single Implementation Inheritance
Inheritance Hierarchy
Relationships: is-a and has-a
The Supertype-Subtype Relationship
7.2 Overriding Methods
Instance Method Overriding
Covariant return
in Overriding Methods
Overriding vs. Overloading
7.3 Hiding Members
Field Hiding
Static Method Hiding
7.4 The Object Reference super
Review Questions
7.5 Chaining Constructors Using this()
and super()
The this()
Constructor Call
The super()
Constructor Call
Review Questions
7.6 Interfaces
Defining Interfaces
Abstract Method Declarations
Implementing Interfaces
Extending Interfaces
Interface References
Constants in Interfaces
Review Questions
7.7 Arrays and Subtyping
Arrays and Subtype Covariance
Array Store Check
7.8 Reference Values and Conversions
7.9 Reference Value Assignment Conversions
7.10 Method Invocation Conversions Involving References
Overloaded Method Resolution
7.11 Reference Casting and the instanceof
Operator
The Cast Operator
The instanceof
Operator
Review Questions
7.12 Polymorphism and Dynamic Method Lookup
7.13 Inheritance Versus Aggregation
7.14 Basic Concepts in Object-Oriented Design
Encapsulation
Cohesion
Coupling
Review Questions
Chapter Summary
Programming Exercises
8 Nested Type Declarations
8.1 Overview of Nested Type Declarations
8.2 Static Member Types
Declaring and Using Static Member Types
Accessing Members in Enclosing Context
8.3 Non-Static Member Classes
Instantiating Non-Static Member Classes
Accessing Members in Enclosing Context
Review Questions
8.4 Local Classes
Accessing Declarations in Enclosing Context
Instantiating Local Classes
8.5 Anonymous Classes
Extending an Existing Class
Implementing an Interface
Instantiating Anonymous Classes
Accessing Declarations in Enclosing Context
Review Questions
Chapter Summary
Programming Exercise
9 Object Lifetime
9.1 Garbage Collection
9.2 Reachable Objects
9.3 Facilitating Garbage Collection
9.4 Object Finalization
9.5 Finalizer Chaining
9.6 Invoking Garbage Collection Programmatically
Review Questions
9.7 Initializers
9.8 Field Initializer Expressions
9.9 Static Initializer Blocks
9.10 Instance Initializer Blocks
9.11 Constructing Initial Object State
Review Questions
Chapter Summary
10 Fundamental Classes
10.1 Overview of the java.lang
Package
10.2 The Object
Class
Review Questions
10.3 The Wrapper Classes
Common Wrapper Class Constructors
Common Wrapper Class Utility Methods
Numeric Wrapper Classes
The Character
Class
The Boolean
Class
Review Questions
10.4 The String Class
Immutability
Creating and Initializing Strings
The CharSequence
Interface
Reading Characters from a String
Comparing Strings
Character Case in a String
Concatenation of Strings
Searching for Characters and Substrings
Extracting Substrings
Converting Primitive Values and Objects to Strings
Formatting Values
Pattern Matching
Review Questions
10.5 The StringBuilder and the StringBuffer Classes
Thread-Safety
Mutability
Constructing String Builders
Reading and Changing Characters in String Builders
Constructing Strings from String Builders
Appending, Inserting, and Deleting Characters in String Builders
Controlling String Builder Capacity
Review Questions
Chapter Summary
Programming Exercises
11 Files and Streams
11.1 Input and Output
11.2 The File Class
Querying the File System
File or Directory Existence
File and Directory Permissions
Listing Directory Entries
Creating New Files and Directories
Renaming Files and Directories
Deleting Files and Directories
11.3 Byte Streams: Input Streams and Output Streams
File Streams
Filter Streams
Reading and Writing Binary Values
Review Questions
11.4 Character Streams: Readers and Writers
Print Writers
Writing Text Files
Reading Text Files
Using Buffered Writers
Using Buffered Readers
The Standard Input, Output, and Error Streams
Comparison of Byte Streams and Character Streams
11.5 The Console
Class
Review Questions
11.6 Object Serialization
The ObjectOutputStream
Class
The ObjectInputStream
Class
Customizing Object Serialization
Serialization and Inheritance
Review Questions
Chapter Summary
Programming Exercise
12 Localization, Pattern Matching, and Formatting
12.1 The java.util.Locale
Class
12.2 The java.util.Date
Class
12.3 The java.util.Calendar
Class
Static Factory Methods to Create a Calendar
Interoperability with the Date Class
Selected get and set Methods
Manipulating a Calendar
Comparing Calendars
12.4 The java.text.DateFormat Class
Static Factory Methods to Create a Date/Time Formatter
Formatting Dates
Parsing Strings to Date/Time
Managing the Calendar and the Number Formatter
12.5 The java.text.NumberFormat Class
Static Factory Methods to Create a Number Formatter
Formatting Numbers and Currency
Parsing Strings to Numbers
Specifying the Number of Digits
Review Questions
12.6 String Pattern Matching Using Regular Expressions
Regular Expression Fundamentals
Escaping Metacharacters
The java.util.regex.Pattern
Class
The java.util.regex.Matcher
Class
The java.util.Scanner
Class
Review Questions
12.7 Formatting Values
Overview
Defining Format Specifiers
Conversion Categories and Formatting Conversions
Selected Format Exceptions
Using the format()
Method
Review Questions
Chapter Summary
Programming Exercises
13 Threads
13.1 Multitasking
13.2 Overview of Threads
13.3 The Main Thread
13.4 Thread Creation
Implementing the Runnable
Interface
Extending the Thread
Class
Review Questions
13.5 Synchronization
Locks
Synchronized Methods
Synchronized Blocks
Review Questions
13.6 Thread Transitions
Thread States
Thread Priorities
Thread Scheduler
Running and Yielding
Sleeping and Waking Up
Waiting and Notifying
Joining
Blocking for I/O
Thread Termination
Deadlocks
Review Questions
Chapter Summary
Programming Exercises
14 Generics
14.1 Introducing Generics
14.2 Generic Types and Parameterized Types
Generic Types
Parameterized Types
Generic Interfaces
Extending Generic Types
Raw Types and Unchecked Warnings
14.3 Collections and Generics
14.4 Wildcards
The Subtype Covariance Problem with Parameterized Types
Wildcard Types
Subtype Covariance: ? extends Type
Subtype Contravariance: ? super Type
Subtype Bivariance: ?
Subtype Invariance: Type
Some Restrictions on Wildcard Types
14.5 Using References of Wildcard Parameterized Types
Generic Reference Assignment
Using Parameterized References to Call Set and Get Methods
14.6 Bounded Type Parameters
Multiple Bounds
Review Questions
14.7 Implementing a Simplified Generic Stack
14.8 Generic Methods and Constructors
Generic Method Declaration
Calling Generic Methods
14.9 Wildcard Capture
Capture Conversion
14.10 Flexibility with Wildcard Parameterized Types
Nested Wildcards
Wildcard Parameterized Types as Formal Parameters
Flexible Comparisons with Wildcards
Recursive Bounds
14.11 Type Erasure
Bridge Methods
14.12 Implications for Overloading and Overriding
Method Signature
Implications for Overloading
Implications for Overriding
14.13 Limitations and Restrictions on Generic Types
Reifiable Types
Implications for instanceof operator
Implications for Casting
Implications for Arrays
Implications for Varargs
Implications for Exception Handling
Implications for Nested Classes
Other Implications
Review Questions
Chapter Summary
Programming Exercises
15 Collections and Maps
15.1 Comparing Objects
The equals()
Method
The hashCode()
Method
The Comparable<E>
Interface
The Comparator<E>
Interface
Review Questions
15.2 The Java Collections Framework
Core Interfaces
Implementations
15.3 Collections
Basic Operations
Bulk Operations
Iterators
Array Operations
Review Questions
15.4 Sets
The HashSet<E>
and LinkedHashSet<E>
Classes
15.5 The SortedSet<E>
and NavigableSet<E>
Interfaces
The SortedSet<E>
Interface
The NavigableSet<E>
Interface
The TreeSet<E>
Class
15.6 Lists
The ArrayList<E>
, LinkedList<E>
, and Vector<E>
Classes
15.7 Queues
The Queue<E>
Interface
The PriorityQueue<E>
and LinkedList<E>
Classes
The Deque<E>
Interface
The ArrayDeque<E>
and LinkedList<E>
Class
Review Questions
15.8 Maps
Basic Operations
Bulk Operations
Collection Views
15.9 Map Implementations
The HashMap<K,V>
, LinkedHashMap<K,V>
, and Hashtable<K,V>
Classes
15.10 The SortedMap<K,V>
and NavigableMap<K,V>
Interfaces
The SortedMap<K,V>
Interface
The NavigableMap<K,V>
Interface
The TreeMap<K,V>
Class
Review Questions
15.11 Working with Collections
Ordering Elements in Lists
Searching in Collections
Changing Elements in Collections
Sorting Arrays
Searching in Arrays
Creating List Views of Arrays
Miscellaneous Utility Methods in the Arrays Class
Review Questions
Chapter Summary
Programming Exercises
A Taking the SCJP 1.6 Exam
A.1 Preparing for the Programmer Exam
Obtaining an Exam Voucher
Signing Up for the Test
Contact Information
After Taking the Exam
A.2 Registering for the Exam
A.3 How the Examination Is Conducted
The Testing Locations
Utilizing the Allotted Time
The Exam Program
A.4 The Questions
Types of Questions Asked
Types of Answers Expected
Topics Covered by the Questions
A.5 Moving on to Other Java Technology Exams
B Objectives for the SCJP 1.6 Exam
C Objectives for the SCJP 1.6 Upgrade Exam
D Annotated Answers to Review Questions
E Solutions to Programming Exercises
F Mock Exam
G Number Systems and Number Representation
G.1 Number Systems
Binary, Octal, and Hexadecimal Number System
Converting Binary Numbers to Decimals
Converting Octal and Hexadecimal Numbers to Decimals
G.2 Relationship between Binary, Octal, and Hexadecimal Numbers
G.3 Converting Decimals
Converting Decimals to Binary Numbers
Converting Decimals to Octal and Hexadecimal Numbers
G.4 Representing Integers
Calculating 2’s Complement
Index