
- •Contents at a Glance
- •Contents
- •About the Authors
- •About the Technical Reviewer
- •Acknowledgments
- •Introduction
- •Oracle Java Certifications: Overview
- •FAQ 1. What are the different levels of Oracle Java certification exams?
- •FAQ 4. Is OCPJP 7 prerequisite for other Oracle certification exams?
- •FAQ 5. Should I take the OCPJP 7 or OCPJP 6 exam?
- •The OCPJP 7 Exam
- •FAQ 7. How many questions are there in the OCPJP 7 exam?
- •FAQ 8. What is the duration of the OCPJP 7 exam?
- •FAQ 9. What is the cost of the OCPJP 7 exam?
- •FAQ 10. What are the passing scores for the OCPJP 7 exam?
- •FAQ 11. What kinds of questions are asked in the OCPJP 7 exam?
- •FAQ 12. What does the OCPJP 7 exam test for?
- •FAQ 13. I’ve been a Java programmer for last five years. Do I have to prepare for the OCPJP 7 exam?
- •FAQ 14. How do I prepare for the OCPJP 7 exam?
- •FAQ 15. How do I know when I’m ready to take the OCPJP 7 exam?
- •Taking the OCPJP 7 Exam
- •FAQ 16. What are my options to register for the exam?
- •FAQ 17. How do I register for the exam, schedule a day and time for taking the exam, and appear for the exam?
- •The OCPJP 7 Exam: Pretest
- •Answers with Explanations
- •Post-Pretest Evaluation
- •Essentials of OOP
- •FunPaint Application: An Example
- •Foundations of OOP
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Class Fundamentals
- •Object Creation
- •Constructors
- •Access Modifiers
- •Public Access Modifier
- •Private Access Modifier
- •Protected and Default Access Modifier
- •Overloading
- •Method Overloading
- •Constructor Overloading
- •Overload resolution
- •Points to Remember
- •Inheritance
- •Runtime Polymorphism
- •An Example
- •Overriding Issues
- •Overriding: Deeper Dive
- •Invoking Superclass Methods
- •Type Conversions
- •Upcasts and Downcasts
- •Casting Between Inconvertible Types
- •Using “instanceof” for Safe Downcasts
- •Java Packages
- •Working with Packages
- •Static Import
- •Summary
- •Abstract Classes
- •Points to Remember
- •Using the “final” Keyword
- •Final Classes
- •Final Methods and Variables
- •Points to Remember
- •Using the “static” Keyword
- •Static Block
- •Points to Remember
- •Flavors of Nested Classes
- •Static Nested Classes (or Interfaces)
- •Points to Remember
- •Inner Classes
- •Points to Remember
- •Local Inner Classes
- •Points to Remember
- •Anonymous Inner Classes
- •Points to Remember
- •Enum Data Types
- •Points to Remember
- •Summary
- •Interfaces
- •Declaring and Using Interfaces
- •Points to Remember
- •Abstract Classes vs. Interfaces
- •Choosing Between an Abstract Class and an Interface
- •Object Composition
- •Composition vs. Inheritance
- •Points to Remember
- •Design Patterns
- •The Singleton Design Pattern
- •Ensuring That Your Singleton Is Indeed a Singleton
- •The Factory Design Pattern
- •Differences Between Factory and Abstract Factory Design Patterns
- •The Data Access Object (DAO) Design Pattern
- •Points to Remember
- •Summary
- •Generics
- •Using Object Type and Type Safety
- •Using the Object Class vs. Generics
- •Container Implementation Using the Object Class
- •Container Implementation Using Generics
- •Creating Generic Classes
- •Diamond Syntax
- •Interoperability of Raw Types and Generic Types
- •Generic Methods
- •Generics and Subtyping
- •Wildcard Parameters
- •Limitations of Wildcards
- •Bounded Wildcards
- •Wildcards in the Collections Class
- •Points to Remember
- •The Collections Framework
- •Why Reusable Classes?
- •Basic Components of the Collections Framework
- •Abstract Classes and Interfaces
- •Concrete Classes
- •List Classes
- •ArrayList Class
- •The ListIterator Interface
- •The LinkedList Class
- •The Set Interface
- •The HashSet Class
- •The TreeSet Class
- •The Map Interface
- •The HashMap Class
- •Overriding the hashCode() Method
- •The NavigableMap Interface
- •The Queue Interface
- •The Deque Interface
- •Comparable and Comparator Interfaces
- •Algorithms (Collections Class)
- •The Arrays Class
- •Methods in the Arrays Class
- •Array as a List
- •Points to Remember
- •Summary
- •Generics
- •Collections Framework
- •Processing Strings
- •String Searching
- •The IndexOf() Method
- •The regionMatches() Method
- •String Parsing
- •String Conversions
- •The Split() Method
- •Regular Expressions
- •Understanding regex Symbols
- •Regex Support in Java
- •Searching and Parsing with regex
- •Replacing Strings with regex
- •String Formatting
- •Format Specifiers
- •Points to Remember
- •Summary
- •Reading and Writing from Console
- •Understanding the Console Class
- •Formatted I/O with the Console Class
- •Special Character Handling in the Console Class
- •Using Streams to Read and Write Files
- •Character Streams and Byte Streams
- •Character Streams
- •Reading Text Files
- •Reading and Writing Text Files
- •“Tokenizing” Text
- •Byte Streams
- •Reading a Byte Stream
- •Data Streams
- •Writing to and Reading from Object Streams: Serialization
- •Serialization: Some More Details
- •Points to Remember
- •Summary
- •A Quick History of I/O APIs
- •Using the Path Interface
- •Getting Path Information
- •Comparing Two Paths
- •Using the Files Class
- •Checking File Properties and Metadata
- •Copying a File
- •Moving a File
- •Deleting a File
- •Walking a File Tree
- •Revisiting File Copy
- •Finding a File
- •Watching a Directory for Changes
- •Points to Remember
- •Summary
- •Introduction to JDBC
- •The Architecture of JDBC
- •Two-Tier and Three-Tier JDBC Architecture
- •Types of JDBC Drivers
- •Setting Up the Database
- •Connecting to a Database Using a JDBC Driver
- •The Connection Interface
- •Connecting to the Database
- •Statement
- •ResultSet
- •Querying the Database
- •Updating the Database
- •Getting the Database Metadata
- •Points to Remember
- •Querying and Updating the Database
- •Performing Transactions
- •Rolling Back Database Operations
- •The RowSet Interface
- •Points to Remember
- •Summary
- •Define the Layout of the JDBC API
- •Connect to a Database by Using a JDBC driver
- •Update and Query a Database
- •Customize the Transaction Behavior of JDBC and Commit Transactions
- •Use the JDBC 4.1 RowSetProvider, RowSetFactory, and RowSet Interfaces
- •Introduction to Exception Handling
- •Throwing Exceptions
- •Unhandled Exceptions
- •Try and Catch Statements
- •Programmatically Accessing the Stack Trace
- •Multiple Catch Blocks
- •Multi-Catch Blocks
- •General Catch Handlers
- •Finally Blocks
- •Points to Remember
- •Try-with-Resources
- •Closing Multiple Resources
- •Points to Remember
- •Exception Types
- •The Exception Class
- •The RuntimeException Class
- •The Error Class
- •The Throws Clause
- •Method Overriding and the Throws Clause
- •Points to Remember
- •Custom Exceptions
- •Assertions
- •Assert Statement
- •How Not to Use Asserts
- •Summary
- •Introduction
- •Locales
- •The Locale Class
- •Getting Locale Details
- •Resource Bundles
- •Using PropertyResourceBundle
- •Using ListResourceBundle
- •Loading a Resource Bundle
- •Naming Convention for Resource Bundles
- •Formatting for Local Culture
- •The NumberFormat Class
- •The Currency Class
- •The DateFormat Class
- •The SimpleDateFormat Class
- •Points to Remember
- •Summary
- •Introduction to Concurrent Programming
- •Important Threading-Related Methods
- •Creating Threads
- •Extending the Thread Class
- •Implementing the Runnable Interface
- •The Start( ) and Run( ) Methods
- •Thread Name, Priority, and Group
- •Using the Thread.sleep() Method
- •Using Thread’s Join Method
- •Asynchronous Execution
- •The States of a Thread
- •Two States in “Runnable” State
- •Concurrent Access Problems
- •Data Races
- •Thread Synchronization
- •Synchronized Blocks
- •Synchronized Methods
- •Synchronized Blocks vs. Synchronized Methods
- •Deadlocks
- •Other Threading Problems
- •Livelocks
- •Lock Starvation
- •The Wait/Notify Mechanism
- •Let’s Solve a Problem
- •More Thread States
- •timed_waiting and blocked States
- •waiting State
- •Using Thread.State enum
- •Understanding IllegalThreadStateException
- •Summary
- •Using java.util.concurrent Collections
- •Semaphore
- •CountDownLatch
- •Exchanger
- •CyclicBarrier
- •Phaser
- •Concurrent Collections
- •Apply Atomic Variables and Locks
- •Atomic Variables
- •Locks
- •Conditions
- •Multiple Conditions on a Lock
- •Use Executors and ThreadPools
- •Executor
- •Callable, Executors, ExecutorService, ThreadPool, and Future
- •ThreadFactory
- •The ThreadLocalRandom Class
- •TimeUnit Enumeration
- •Use the Parallel Fork/Join Framework
- •Useful Classes of the Fork/Join Framework
- •Using the Fork/Join Framework
- •Points to Remember
- •Summary
- •Using java.util.concurrent Collections
- •Applying Atomic Variables and Locks
- •Using Executors and ThreadPools
- •Using the Parallel Fork/Join Framework
- •Chapter 3: Java Class Design
- •Chapter 4: Advanced Class Design
- •Chapter 5: Object-Oriented Design Principles
- •Chapter 6: Generics and Collections
- •Chapter 7: String Processing
- •Chapter 8: Java I/O Fundamentals
- •Chapter 9: Java File I/O (NIO.2)
- •Chapter 10: Building Database Applications with JDBC
- •Chapter 11: Exceptions and Assertions
- •Chapter 12: Localization
- •Chapter 13: Threads
- •Chapter 14: Concurrency
- •OCPJP7 Exam (1Z0-804 a.k.a. Java SE 7 Programmer II) Topics
- •OCPJP 7 Exam (1Z0-805, a.k.a. Upgrade to Java SE 7 Programmer) Topics
- •Answers and Explanations
- •Answer Sheet
- •Answers and Explanations
- •Index

Contents
About the Authors xvii About the Technical Reviewer xix Acknowledgments xxi Introduction xxiii
■Chapter 1: The OCPJP 7 Exam: FAQ |
1 Oracle Java Certifications: Overview |
|
...................................................................................................................................................... |
|
1 |
FAQ 1 What are the different levels of Oracle Java certification exams? ............................................................ |
1 |
|
FAQ 2 Can you compare the specifications of the 1Z0-803, 1Z0-804, and 1Z0-805 exams in relation |
|
|
to OCAJP 7 and OCPJP 7 certification? ................................................................................................................ |
|
2 |
FAQ 3 OCAJP 7 certification is a prerequisite for OCPJP 7 certification via the 1Z0-804 exam |
|
|
Does that mean that I have to take the OCAJP 7 exam before I can take the OCPJP 7 exam? ............................ |
4 |
|
FAQ 4 Is OCPJP 7 prerequisite for other Oracle certification exams? .................................................................. |
4 |
|
FAQ 5 Should I take the OCPJP 7 or OCPJP 6 exam? .......................................................................................... |
|
4 |
FAQ 6 How does the Oracle OCPJP 7 exam differ from the OCPJP 6 and OCPJP 5 exams |
|
|
(and the previous Sun versions SCJP 6 and SCJP 5)? .......................................................................................... |
|
4 |
The OCPJP 7 Exam ....................................................................................................................... |
|
5 |
FAQ 7 How many questions are there in the OCPJP 7 exam? ............................................................................. |
|
5 |
FAQ 8 What is the duration of the OCPJP 7 exam? .............................................................................................. |
|
5 |
FAQ 9 What is the cost of the OCPJP 7 exam? .................................................................................................... |
|
6 |
FAQ 10 What are the passing scores for the OCPJP 7 exam? ............................................................................. |
|
6 |
FAQ 11 What kinds of questions are asked in the OCPJP 7 exam? ..................................................................... |
6 |
|
FAQ 12 What does the OCPJP 7 exam test for? ................................................................................................... |
|
6 |
FAQ 13 I’ve been a Java programmer for last five years Do I have to prepare for the OCPJP 7 exam? ............. |
7 |
|
FAQ 14 How do I prepare for the OCPJP 7 exam? ............................................................................................... |
|
7 |
FAQ 15 How do I know when I’m ready to take the OCPJP 7 exam? .................................................................. |
8 |
|
|
|
vii |
■ Contents
Taking the OCPJP 7 Exam 8
FAQ 16. What are my options to register for the exam? 8
FAQ 17. How do I register for the exam, schedule a day and time for taking the exam, and appear
for the exam? 8 FAQ 18. What are the key things I need to remember before taking the exam and on the day of exam? 9
■■Chapter 2: Pretest 11
The OCPJP 7 Exam: Pretest 12 Answers with Explanations 36 Post-Pretest Evaluation 42
■■Chapter 3: Java Class Design 45
Essentials of OOP 46
FunPaint Application: An Example 46 Foundations of OOP 46
Class Fundamentals 48
Object Creation 49 Constructors 49 Access Modifiers 51
Overloading 55
Method Overloading 56 Constructor Overloading 57 Overload resolution 60 Points to Remember 63
Inheritance 63
Runtime Polymorphism 65 Type Conversions 74
Java Packages 77
Working with Packages 78
Summary 84
viii
■ Contents
■■Chapter 4: Advanced Class Design 87
Abstract Classes 87
Points to Remember 89
Using the “final” Keyword 90
Final Classes 90 Final Methods and Variables 91 Points to Remember 91
Using the “static” Keyword 92
Static Block 93 Points to Remember 94
Flavors of Nested Classes 94
Static Nested Classes (or Interfaces) 96 Inner Classes 97 Local Inner Classes 99 Anonymous Inner Classes 102
Enum Data Types 103
Points to Remember 106
Summary 111
■■Chapter 5: Object-Oriented Design Principles 113
Interfaces 113
Declaring and Using Interfaces 114 Abstract Classes vs. Interfaces 116
Object Composition 119
Composition vs. Inheritance 120
Design Patterns 123
The Singleton Design Pattern 128 The Factory Design Pattern 132 The Data Access Object (DAO) Design Pattern 139
Summary 145
ix
■ Contents
■■Chapter 6: Generics and Collections 147
Generics 148
Using Object Type and Type Safety 148 Using the Object Class vs. Generics 150 Creating Generic Classes 154 Diamond Syntax 157 Interoperability of Raw Types and Generic Types 158 Generic Methods 160 Generics and Subtyping 162 Wildcard Parameters 163 Points to Remember 170
The Collections Framework 171
Why Reusable Classes? 171 Basic Components of the Collections Framework 171 Algorithms (Collections Class) 192 The Arrays Class 195 Points to Remember 200
Summary 203
Generics 203 Collections Framework 204
■■Chapter 7: String Processing 205
Processing Strings 205
String Searching 205 String Parsing 208
Regular Expressions 211
Understanding regex Symbols 211 Regex Support in Java 212
String Formatting 218
Format Specifiers 218 Points to Remember 221
Summary 224
x
■ Contents
■■Chapter 8: Java I/O Fundamentals 225
Reading and Writing from Console 225
Understanding the Console Class 225
Using Streams to Read and Write Files 229
Character Streams and Byte Streams 230 Character Streams 230 Byte Streams 237
Points to Remember 246 Summary 249
■■Chapter 9: Java File I/O (NIO.2) 251
A Quick History of I/O APIs 251 Using the Path Interface 252
Getting Path Information 254 Comparing Two Paths 257
Using the Files Class 258
Checking File Properties and Metadata 259 Copying a File 264 Moving a File 265 Deleting a File 266
Walking a File Tree 267
Revisiting File Copy 270
Finding a File 271 Watching a Directory for Changes 273 Points to Remember 276 Summary 280
■■Chapter 10: Building Database Applications with JDBC 281
Introduction to JDBC 281
The Architecture of JDBC 282 Two-Tier and Three-Tier JDBC Architecture 283
xi
■ Contents
Types of JDBC Drivers 283 Setting Up the Database 284
Connecting to a Database Using a JDBC Driver 285
The Connection Interface 285 Connecting to the Database 286
Querying and Updating the Database 288
Statement 288
Performing Transactions 300
The RowSet Interface 306
Summary 313
Define the Layout of the JDBC API 313 Connect to a Database by Using a JDBC driver 314 Update and Query a Database 314 Customize the Transaction Behavior of JDBC and Commit Transactions 315 Use the JDBC 4.1 RowSetProvider, RowSetFactory, and RowSet Interfaces 315
■■Chapter 11: Exceptions and Assertions 317
Introduction to Exception Handling 317
Throwing Exceptions 318 Unhandled Exceptions 319
Try-with-Resources 332
Closing Multiple Resources 333 Points to Remember 335
Exception Types 336
The Exception Class 336 The RuntimeException Class 338 The Error Class 341 The Throws Clause 343 Points to Remember 346
Custom Exceptions 347
xii
■ Contents
Assertions 350
Assert Statement 350 How Not to Use Asserts 352
Summary 358
■■Chapter 12: Localization 361
Introduction 362 Locales 362
The Locale Class 362
Resource Bundles 366
Using PropertyResourceBundle 367 Using ListResourceBundle 369
Loading a Resource Bundle 372
Naming Convention for Resource Bundles 372
Formatting for Local Culture 376
The NumberFormat Class 377 The DateFormat Class 381
Points to Remember 387 Summary 390
■■Chapter 13: Threads 393
Introduction to Concurrent Programming 393
Important Threading-Related Methods 394 Creating Threads 395 Asynchronous Execution 403
The States of a Thread 405
Two States in “Runnable” State 406
Concurrent Access Problems 407
Data Races 407 Thread Synchronization 408 Deadlocks 413
xiii
■ Contents
Other Threading Problems 415 The Wait/Notify Mechanism 416 More Thread States 425
Summary 433
■■Chapter 14: Concurrency 435
Using java.util.concurrent Collections 435
Semaphore 436 CountDownLatch 438 Exchanger 440 CyclicBarrier 442 Phaser 444 Concurrent Collections 447
Apply Atomic Variables and Locks 450
Atomic Variables 450 Locks 453 Conditions 456
Use Executors and ThreadPools 462
Executor 463 Callable, Executors, ExecutorService, ThreadPool, and Future 464 ThreadFactory 468 The ThreadLocalRandom Class 469 TimeUnit Enumeration 469
Use the Parallel Fork/Join Framework 470
Useful Classes of the Fork/Join Framework 471 Using the Fork/Join Framework 472
Points to Remember 478 Summary 482
xiv
■ Contents
■■Chapter 15: OCPJP 7 Quick Refresher 485
Chapter 3: Java Class Design 486 Chapter 4: Advanced Class Design 487 Chapter 5: Object-Oriented Design Principles 488 Chapter 6: Generics and Collections 489 Chapter 7: String Processing 491 Chapter 8: Java I/O Fundamentals 493 Chapter 9: Java File I/O (NIO.2) 494 Chapter 10: Building Database Applications with JDBC 496 Chapter 11: Exceptions and Assertions 499 Chapter 12: Localization 501 Chapter 13: Threads 503 Chapter 14: Concurrency 504
■■Appendix A: Exam Topics 507
OCPJP7 Exam (1Z0-804 a.k.a. Java SE 7 Programmer II) Topics 507
1. Java Class Design (G&S Chapter 3) 507 2. Advanced Class Design (G&S Chapter 4) 508 3. Object-Oriented Design Principles (G&S Chapter 5) 508 4. Generics and Collections (G&S Chapter 6) 508 5. String Processing (G&S Chapter 7) 508 6. Exceptions and Assertions (G&S Chapter 11) 509 7. Java I/O Fundamentals (G&S Chapter 8) 509 8. Java File I/O (NIO.2) (G&S Chapter 9) 509 9. Building Database Applications with JDBC (G&S Chapter 10) 509 10. Threads (G&S Chapter 13) 509 11. Concurrency (G&S Chapter 14) 510 12. Localization (G&S Chapter 12) 510
xv

■ Contents
OCPJP 7 Exam (1Z0-805, a.k.a. Upgrade to Java SE 7 Programmer) Topics 510
1. Language Enhancements (G&S Chapters 6, 11) 510
2. Design Patterns (G&S Chapter 5) 510 3. Database Applications with JDBC (G&S Chapter 10) 511 4. Concurrency (G&S Chapters 13, 14) 511 5. Localization (G&S Chapter 12) 511 6. Java File I/O (NIO.2) (G&S Chapter 9) 511
■■Appendix B: Mock Test – 1 513
Answer Sheet 555 Answers and Explanations 556
■■Appendix C: Mock Test – 2 569
Answer Sheet 606 Answers and Explanations 607
Index 619
xvi