Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Cover
Title Page
Copyright Page
Dedication
Contents
Contributors
Acknowledgments
Preface
Introduction
Part I OCA and OCP
1 Declarations and Access Control
Java Refresher
Identifiers and Keywords (OCA Objectives 1.2 and 2.1)
Legal Identifiers
Oracle’s Java Code Conventions
Define Classes (OCA Objectives 1.2, 1.3, 1.4, 6.6, and 7.6)
Source File Declaration Rules
Using the javac and java Commands
Using public static void main(String[ ] args)
Import Statements and the Java API
Static Import Statements
Class Declarations and Modifiers
Exercise 1-1: Creating an Abstract Superclass and Concrete Subclass
Use Interfaces (OCA Objective 7.6)
Declaring an Interface
Declaring Interface Constants
Declare Class Members (OCA Objectives 2.1, 2.2, 2.3, 2.4, 2.5, 4.1, 4.2, 6.2, and 6.6)
Access Modifiers
Nonaccess Member Modifiers
Constructor Declarations
Variable Declarations
Declare and Use enums (OCA Objective 1.2 and OCP Objective 2.5)
Declaring enums
Two-Minute Drill
Q&A Self Test
Self Test Answers
2 Object Orientation
Encapsulation (OCA Objectives 6.1 and 6.7)
Inheritance and Polymorphism (OCA Objectives 7.1, 7.2, and 7.3)
IS-A
HAS-A
Polymorphism (OCA Objectives 7.2 and 7.3)
Overriding / Overloading (OCA Objectives 6.1, 6.3, 7.2, and 7.3)
Overridden Methods
Overloaded Methods
Casting (OCA Objectives 7.3 and 7.4)
Implementing an Interface (OCA Objective 7.6)
Legal Return Types (OCA Objectives 2.2, 2.5, 6.1, and 6.3)
Return Type Declarations
Returning a Value
Constructors and Instantiation (OCA Objectives 6.4, 6.5, and 7.5)
Determine Whether a Default Constructor Will Be Created
Overloaded Constructors
Initialization Blocks
Statics (OCA Objective 6.2)
Static Variables and Methods
Two-Minute Drill
Q&A Self Test
Self Test Answers
3 Assignments
Stack and Heap—Quick Review
Literals, Assignments, and Variables (OCA Objectives 2.1, 2.2, 2.3, and Upgrade Objective 1.2)
Literal Values for All Primitive Types
Assignment Operators
Exercise 3-1: Casting Primitives
Scope (OCA Objectives 1.1 and 2.5)
Variable Initialization (OCA Objective 2.1)
Using a Variable or Array Element That Is Uninitialized and Unassigned
Local (Stack, Automatic) Primitives and Objects
Passing Variables into Methods (OCA Objective 6.8)
Passing Object Reference Variables
Does Java Use Pass-By-Value Semantics?
Passing Primitive Variables
Garbage Collection (OCA Objective 2.4)
Overview of Memory Management and Garbage Collection
Overview of Java’s Garbage Collector
Writing Code That Explicitly Makes Objects Eligible for Collection
Exercise 3-2: Garbage Collection Experiment
Two-Minute Drill
Q&A Self Test
Self Test Answers
4 Operators
Java Operators (OCA Objectives 3.1, 3.2, and 3.3)
Assignment Operators
Relational Operators
instanceof Comparison
Arithmetic Operators
Conditional Operator
Logical Operators
Two-Minute Drill
Q&A Self Test
Self Test Answers
5 Working with Strings, Arrays, and ArrayLists
Using String and StringBuilder (OCA Objectives 2.7 and 2.6)
The String Class
Important Facts About Strings and Memory
Important Methods in the String Class
The StringBuilder Class
Important Methods in the StringBuilder Class
Using Arrays (OCA Objectives 4.1 and 4.2)
Declaring an Array
Constructing an Array
Initializing an Array
Using ArrayList (OCA Objective 4.3)
When to Use ArrayLists
ArrayList Methods in Action
Important Methods in the ArrayList Class
Encapsulation for Reference Variables
Two-Minute Drill
Q&A Self Test
Self Test Answers
6 Flow Control and Exceptions
Using if and switch Statements (OCA Objectives 3.4 and 3.5—also Upgrade Objective 1.1)
if-else Branching
switch Statements (OCA, OCP, and Upgrade Topic)
Exercise 6-1: Creating a switch-case Statement
Creating Loops Constructs (OCA Objectives 5.1, 5.2, 5.3, 5.4, and 5.5)
Using while Loops
Using do Loops
Using for Loops
Using break and continue
Unlabeled Statements
Labeled Statements
Exercise 6-2: Creating a Labeled while Loop
Handling Exceptions (OCA Objectives 8.1, 8.2, 8.3, and 8.4)
Catching an Exception Using try and catch
Using finally
Propagating Uncaught Exceptions
Exercise 6-3: Propagating and Catching an Exception
Defining Exceptions
Exception Hierarchy
Handling an Entire Class Hierarchy of Exceptions
Exception Matching
Exception Declaration and the Public Interface
Rethrowing the Same Exception
Exercise 6-4: Creating an Exception
Common Exceptions and Errors (OCA Objective 8.5)
Where Exceptions Come From
JVM Thrown Exceptions
Programmatically Thrown Exceptions
A Summary of the Exam’s Exceptions and Errors
End of Part I—OCA
Two-Minute Drill
Q&A Self Test
Self Test Answers
Part II OCP
7 Assertions and Java 7 Exceptions
Working with the Assertion Mechanism (OCP Objective 6.5)
Assertions Overview
Enabling Assertions
Using Assertions Appropriately
Working with Java 7 Exception Handling (OCP Objectives 6.2 and 6.3)
Use the try Statement with multi-catch and finally Clauses
Autocloseable Resources with a try-with-resources Statement
Two-Minute Drill
Q&A Self Test
Self Test Answers
8 String Processing, Data Formatting, Resource Bundles
String, StringBuilder, and StringBuffer (OCP Objective 5.1)
Dates, Numbers, Currencies, and Locales (OCP Objectives 12.1, 12.4, 12.5, and 12.6)
Working with Dates, Numbers, and Currencies
Parsing, Tokenizing, and Formatting (OCP Objectives 5.1, 5.2, and 5.3)
A Search Tutorial
Locating Data via Pattern Matching
Tokenizing
Formatting with printf() and format()
Resource Bundles (OCP Objectives 12.2, 12.3, and 12.5)
Resource Bundles
Property Resource Bundles
Java Resource Bundles
Default Locale
Choosing the Right Resource Bundle
Two-Minute Drill
Q&A Self Test
Self Test Answers
9 I/O and NIO
File Navigation and I/O (OCP Objectives 7.1 and 7.2)
Creating Files Using the File Class
Using FileWriter and FileReader
Combining I/O Classes
Working with Files and Directories
The java.io.Console Class
Files, Path, and Paths (OCP Objectives 8.1 and 8.2)
Creating a Path
Creating Files and Directories
Copying, Moving, and Deleting Files
Retrieving Information about a Path
Normalizing a Path
Resolving a Path
Relativizing a Path
File and Directory Attributes (OCP Objective 8.3)
Reading and Writing Attributes the Easy Way
Types of Attribute Interfaces
Working with BasicFileAttributes
Working with DosFileAttributes
Working with PosixFileAttributes
Reviewing Attributes
DirectoryStream (OCP Objective 8.4)
FileVisitor (OCP Objective 8.4)
PathMatcher (OCP Objective 8.5)
WatchService (OCP Objective 8.6)
Serialization (Objective 7.2)
Two-Minute Drill
Q&A Self Test
Self Test Answers
10 Advanced OO and Design Patterns
IS-A and HAS-A (OCP Objectives 3.3 and 3.4)
Coupling and Cohesion
Coupling
Cohesion
Object Composition Principles (OCP Objective 3.4)
Polymorphism
Benefits of Composition
Singleton Design Pattern (OCP Objective 3.5)
What Is a Design Pattern?
Problem
Solution
Benefits
DAO Design Pattern (OCP Objective 3.6)
Problem
Solution
Benefits
Factory Design Pattern (OCP Objective 3.7)
Problem
Solution
Benefits
Two-Minute Drill
Q&A Self Test
Self Test Answers
11 Generics and Collections
toString(), hashCode(), and equals() (OCP Objectives 4.7 and 4.8)
The toString() Method
Overriding equals()
Overriding hashCode()
Collections Overview (OCP Objectives 4.5 and 4.6)
So What Do You Do with a Collection?
Key Interfaces and Classes of the Collections Framework
List Interface
Set Interface
Map Interface
Queue Interface
Using Collections (OCP Objectives 4.2, 4.4, 4.5, 4.6, 4.7, and 4.8)
ArrayList Basics
Autoboxing with Collections
The Java 7 “Diamond” Syntax
Sorting Collections and Arrays
Navigating (Searching) TreeSets and TreeMaps
Other Navigation Methods
Backed Collections
Using the PriorityQueue Class and the Deque Interface
Method Overview for Arrays and Collections
Method Overview for List, Set, Map, and Queue
Generic Types (OCP Objectives 4.1 and 4.3)
The Legacy Way to Do Collections
Generics and Legacy Code
Mixing Generic and Nongeneric Collections
Polymorphism and Generics
Generic Methods
Generic Declarations
Two-Minute Drill
Q&A Self Test
Self Test Answers
12 Inner Classes
Nested Classes (OCP Objective 2.4)
Inner Classes
Coding a “Regular” Inner Class
Referencing the Inner or Outer Instance from Within the Inner Class
Method-Local Inner Classes
What a Method-Local Inner Object Can and Can’t Do
Anonymous Inner Classes
Plain-Old Anonymous Inner Classes, Flavor One
Plain-Old Anonymous Inner Classes, Flavor Two
Argument-Defined Anonymous Inner Classes
Static Nested Classes
Instantiating and Using Static Nested Classes
Two-Minute Drill
Q&A Self Test
Self Test Answers
13 Threads
Defining, Instantiating, and Starting Threads (OCP Objective 10.1)
Defining a Thread
Instantiating a Thread
Starting a Thread
Thread States and Transitions (OCP Objective 10.2)
Thread States
Preventing Thread Execution
Sleeping
Exercise 13-1: Creating a Thread and Putting It to Sleep
Thread Priorities and yield( )
Synchronizing Code, Thread Problems (OCP Objectives 10.3 and 10.4)
Synchronization and Locks
Exercise 13-2: Synchronizing a Block of Code
Thread Deadlock
Thread Interaction (OCP Objectives 10.3 and 10.4)
Using notifyAll( ) When Many Threads May Be Waiting
Two-Minute Drill
Q&A Self Test
Self Test Answers
Exercise Answers
14 Concurrency
Concurrency with the java.util.concurrent Package
Apply Atomic Variables and Locks (OCP Objective 11.2)
Atomic Variables
Locks
Use java.util.concurrent Collections (OCP Objective 11.1) and Use a Deque (OCP Objective 4.5)
Copy-on-Write Collections
Concurrent Collections
Blocking Queues
Use Executors and ThreadPools (OCP Objective 11.3)
Identifying Parallel Tasks
How Many Threads Can You Run?
CPU-Intensive vs. I/O-Intensive Tasks
Fighting for a Turn
Decoupling Tasks from Threads
Use the Parallel Fork/Join Framework (OCP Objective 11.4)
Divide and Conquer
ForkJoinPool
ForkJoinTask
Two-Minute Drill
Q&A Self Test
Self Test Answers
15 JDBC
Starting Out: Introduction to Databases and JDBC
Talking to a Database
Bob’s Books, Our Test Database
Core Interfaces of the JDBC API (OCP Objective 9.1)
Connect to a Database Using DriverManager (OCP Objective 9.2)
The DriverManager Class
The JDBC URL
JDBC Driver Implementation Versions
Submit Queries and Read Results from the Database (OCP Objective 9.3)
All of Bob’s Customers
Statements
ResultSets
Updating ResultSets (Not on the Exam!)
When Things Go Wrong—Exceptions and Warnings
Use PreparedStatement and CallableStatement Objects (OCP Objective 9.6)
PreparedStatement
CallableStatement
Construct and Use RowSet Objects (OCP Objective 9.5)
Working with RowSets
JDBC Transactions (OCP Objective 9.4)
JDBC Transaction Concepts
Starting a Transaction Context in JDBC
Rolling Back a Transaction
Using Savepoints with JDBC
Two-Minute Drill
Q&A Self Test
Self Test Answers
Appendix A Serialization
Appendix B Classpaths and JARs
Appendix C About the Download
Index
← Prev
Back
Next →
← Prev
Back
Next →