- •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
For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them.
Contents at a Glance
About the Authors xvii About the Technical Reviewer xix Acknowledgments xxi Introduction xxiii
■■Chapter 1: The OCPJP 7 Exam: FAQ 1 ■■Chapter 2: Pretest 11 ■■Chapter 3: Java Class Design 45 ■■Chapter 4: Advanced Class Design 87 ■■Chapter 5: Object-Oriented Design Principles 113 ■■Chapter 6: Generics and Collections 147 ■■Chapter 7: String Processing 205 ■■Chapter 8: Java I/O Fundamentals 225 ■■Chapter 9: Java File I/O (NIO.2) 251 ■■Chapter 10: Building Database Applications with JDBC 281 ■■Chapter 11: Exceptions and Assertions 317 ■■Chapter 12: Localization 361 ■■Chapter 13: Threads 393 ■■Chapter 14: Concurrency 435 ■■Chapter 15: OCPJP 7 Quick Refresher 485
v
■ Contents at a Glance
■■Appendix A: Exam Topics 507 ■■Appendix B: Mock Test – 1 513 ■■Appendix C: Mock Test – 2 569
Index 619
vi
Introduction
This book is a comprehensive guide to preparing for the OCPJP 7 exam. This book covers the exam objectives of both OCPJP exams, Java SE 7 Programmer II (1Z0-804 exam) and Upgrade to Java SE 7 Programmer (1Z0-805 exam). The main objective of this book is to prepare the reader to take the OCPJP 7 exam and pass it with ease.
The book covers all of the exam topics for Java SE 7 Programmer II (1Z0-804 exam). The chapters and sections in this book map one-to-one to the exam objectives and subtopics. This one-to-one mapping between chapters and the exam objectives ensures that we cover only the topics to the required breadth and depth—no more, no less. If you’re taking Upgrade to Java SE 7 Programmer (1Z0-805 exam), see Appendix A for the key to how the exam topics map to the chapters of this book.
A reader will find lots and lots of sample questions in the form of a pretest, numerous sample questions within each chapter, and two full-length mock tests. These sample questions not only help the reader prepare for taking the exam but also set realistic expectations for what the reader will find on the exam.
There are many features in this book designed to present the content in a smooth, example-driven flow to improve your reading and study experience. For instance, the chapters provide numerous programming and real-world examples to help you internalize each of the presented concepts. Additionally, in each chapter we use visual cues (such as caution signs and exam tips) to direct your attention to important and interesting aspects of the concepts that are of particular relevance to the OCPJP 7 exam.
Prerequisites
Since the OCAJP 7 (a.k.a. Java SE 7 Programmer I/1Z0-803) exam is a prerequisite for the more comprehensive OCPJP 7 exam (1Z0-804), we assume that the reader is already familiar with the fundamentals of the language. We focus only on the OCPJP 7 exam objectives, on the presumption that the reader has a working knowledge in Java.
Target Audience
This book is for you if any of the following is true:
•If you are a student or a Java programmer aspiring to crack the OCPJP 7 exam.
•If you have already passed any of the older versions of the OCPJP 7 exam (such as the SCJP 5 exam). This book will prepare you for the Upgrade to OCPJP 7 exam (1Z0-805).
•If you’re a trainer for OCPJP 7 exam. You can use this book as training material for OCPJP 7 exam preparation.
•If you just want to refresh your knowledge of Java programming or gain a better understanding of various Java APIs.
Please note, however, that this book is neither a tutorial for learning Java nor a comprehensive reference book for Java.
xxiii
■ Introduction
Roadmap for Reading This Book
To get the most out of reading this book, we recommend you follow these steps:
Step 0: Make sure you have JDK 7 installed on your machine and you’re able to compile and run Java programs. Step 1: First read the FAQ in Chapter 1 and get familiar with the exam (you may want to skip irrelevant questions
or questions for which you already know the answers).
Step 2: Check the exam topics (Appendix A) and mark the topics you’re not familiar with or comfortable with. Read the chapters or sections corresponding to the topics you’ve marked for preparation.
Step 3: Take the pretest in Chapter 2. If you’ve answered all the questions correctly for an exam chapter, you may want to skip reading the corresponding chapter. For those exam topics in which you did not scored well, mark those chapters and read them first. Try out as many sample programs as possible while you read the chapters.
Step 4: Once you feel you are ready to take the exam, take the first mock test (Appendix B). If you don’t pass it, go back to the chapters in which you are weak, read them, and try out more code relating to those topics. Once you’re confident, attempt the second mock test (Appendix C). If you’ve prepared well, you should be able to pass it.
Step 5: Register for the exam and take the exam based on your performance in the mock tests. The day before taking the exam, read Chapter 15, “OCPJP 7 Quick Refresher.”
On Coding Examples in This Book
All the programs in this book are self-contained programs (with necessary import statements). You can download the source code of the programs from www.apress.com/9781430247647.
We’ve tested the coding examples in this book in two compilers, Oracle’s Java compiler JDK 7 (javac) and the Eclipse Compiler for Java (ecj). For the error messages, we’ve provided javac’s error messages. It is important that you use a Java compiler and a JVM that supports Java 7.
Java is a platform-independent language, but there are certain features that are better explained with a specific platform. Since Windows is the most widely used OS today, some of the programming examples (specifically some of the programs in the NIO.2 chapter) are written with the Windows OS in mind. You may require minor modifications to the programs to get them working under other OSs (Linux, MAC OS, etc).
Contact Us
In case of any queries, suggestions or corrections, please feel free to contact us at sgganesh@gmail.com or tusharsharma@ieee.org.
xxiv