Free Essay

Dnfkdsfnklff

In:

Submitted By vijkal
Words 2596
Pages 11
CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/FM

CONTENTS AT A GLANCE

Part I
OCA and OCP
1

Declarations and Access Control

..............................

3

2

Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

83

3

Assignments

4

Operators

5

Working with Strings, Arrays, and ArrayLists

....................

257

6

Flow Control and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

307

..............................................

165

................................................

223

Part II
OCP
7

Assertions and Java 7 Exceptions

.............................

377

8

String Processing, Data Formatting, Resource Bundles . . . . . . . . . . . . . .

417

9

I/O and NIO

477

10

Advanced OO and Design Patterns

11

Generics and Collections

..............................................
............................

541

....................................

573

12

Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

681

13

Threads

713

14

Concurrency

15

JDBC

A

About the CD
Index

..................................................
..............................................

785

...................................................

841

............................................

947

....................................................

953

xiii

00-FM.indd xiii

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/FM

CONTENTS

Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Acknowledgments
....................................
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

v xxvii xxix xxxi 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3
4
6
6
7
9
10
11
13
13
15
17
23
24
24
27
28
29
42
49
50

xv

00-FM.indd xv

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

xvi

OCA/OCP Java SE 7 Programmer I & II Study Guide

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

60
61
68
75
81

Object Orientation

83

.............................

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

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 . . . . . . . . . . . . . . . . . . .

00-FM.indd xvi

84
88
92
93
96
100
100
106
113
116
122
122
124
126
130
134
138
140
141
149
154
163

166
168
168

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

Contents

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

Working with Strings, Arrays, and ArrayLists

185
188
194
194
195
196
199
199
200
202
207
209
212
220

224
224
226
232
235
240
241
247
249
255

. . . . . . . 257

Using String and StringBuilder (OCA Objectives 2.7 and 2.6) . . . . . .
The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

00-FM.indd xvii

172
178
182
185

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

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

xvii

258
258

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

xviii

OCA/OCP Java SE 7 Programmer I & II Study Guide

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

. . . . . . . . . . . . . . . . . . . . . 307

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 . . . . . . . . . . .

00-FM.indd xviii

264
265
269
271
273
274
275
277
289
289
292
293
294
296
298
305

308
308
313
320
321
321
323
323
330
331
331
333
334
335
336
339
341

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

Contents

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xix
342
343
344
345
347
353
353
354
355
355
356
357
357
361
364
373

Part II
OCP
7

Assertions and Java 7 Exceptions

. . . . . . . . . . . . . . . . . 377

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

00-FM.indd xix

378
379
382
386
389
389
396
404
406
414

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

xx

OCA/OCP Java SE 7 Programmer I & II Study Guide

8

String Processing, Data Formatting, Resource Bundles . . 417
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

418
419
431
432
443
446
451
454
454
456
457
458
459
463
466
474

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

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 . . . . . . . . . . . .

00-FM.indd xx

418

478
480
482
484
487
491
493
495
497
498
500
501
503
505
506
506

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

Contents

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

508
509
511
512
513
514
515
519
523
526
528
530
538

. . . . . . . . . . . . . . . 541

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

00-FM.indd xxi

xxi

542
543
543
544
545
548
549
549
549
550
551
554
555
555
556
559
560
560
560
563
565
567
571

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

xxii

OCA/OCP Java SE 7 Programmer I & II Study Guide

11 Generics and Collections

. . . . . . . . . . . . . . . . . . . . . . . . 573

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

00-FM.indd xxii

574
575
576
581
588
588
589
593
594
595
596
598
598
600
603
604
620
621
622
625
626
626
629
630
633
633
639
641
652
661
667
678

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

Contents

12 Inner Classes

xxiii

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681

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

688
690
691
692
693
696
697
699
700
702
704
710

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

00-FM.indd xxiii

683
683
685

714
717
718
720
728
729
731
731
733
734
738
744
747
753
755
760
765
769

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

xxiv

OCA/OCP Java SE 7 Programmer I & II Study Guide

Self Test Answers
Exercise Answers

14 Concurrency

...................................
...................................

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785

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

786
786
787
789
797
799
800
801
805
806
807
807
808
809
815
816
817
817
829
832
838

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

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 . . . . . . . . . . . . . . . . . .

00-FM.indd xxiv

781
784

842
844
847
851
853
854
858
860

9/2/2014 5:46:30 PM

CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/Front Matter

Contents

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

About the CD
Index

00-FM.indd xxv

xxv

861
861
863
868
889
901
906
908
910
913
914
921
922
922
924
926
932
935
944

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953

9/2/2014 5:46:30 PM

Similar Documents