oracle certified professional java se 7 programmer …978-1-4302-4765-4/1.pdf · professional java...

24
Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 A Comprehensive OCPJP 7 Certification Guide S G Ganesh Tushar Sharma

Upload: vanque

Post on 20-Feb-2018

246 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

Oracle Certified Professional Java SE 7

Programmer Exams 1Z0-804 and 1Z0-805

A Comprehensive OCPJP 7 Certification Guide

S G GaneshTushar Sharma

Page 2: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805

Copyright © 2013 by SG Ganesh, Tushar Sharma

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

ISBN-13 (pbk): 978-1-4302-4764-7

ISBN-13 (electronic): 978-1-4302-4765-4

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

President and Publisher: Paul ManningLead Editor: Saswata MishraTechnical Reviewer: B V KumarEditorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,

Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Saswata Mishra, Matthew Moodie, Morgan Ertel, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh

Coordinating Editor: Jill BalzanoCopy Editor: Mary BehrCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com.

For information on translations, please e-mail [email protected], or visit www.apress.com.

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code.

Page 3: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

To my wonderful mom —Ganesh

To my caring parents, loving wife, and cute son—Tushar

Page 4: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams
Page 5: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

v

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

Page 6: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents at a GlanCe

vi

Appendix A: Exam Topics ■ ����������������������������������������������������������������������������������������������507

Appendix B: Mock Test – 1 ■ ��������������������������������������������������������������������������������������������513

Appendix C: Mock Test – 2 ■ ��������������������������������������������������������������������������������������������569

Index ���������������������������������������������������������������������������������������������������������������������������������619

Page 7: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

vii

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

Page 8: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

viii

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

Page 9: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

ix

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

Page 10: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

x

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

Page 11: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

xi

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

Page 12: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

xii

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

Page 13: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

xiii

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

Page 14: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

xiv

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

Page 15: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

xv

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

Page 16: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ Contents

xvi

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

Page 17: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

xvii

About the Authors

S G Ganesh is a practitioner working in the area of code quality management at Siemens Corporate Research and Technologies in Bangalore. He previously worked in HP’s C++ compiler team and was also a member of the C++ standardization committee. His areas of interests include OO design, design patterns, and programming languages. He is a Software Engineering Certified Instructor (IEEE certification) and has an OCPJP 7 certification.

Tushar Sharma is a researcher and practitioner at Siemens Corporate Research and Technologies–India. He earned an MS from the Indian Institute of Technology–Madras (IIT–Madras). His interests include OO software design, OO programming, refactoring, and design patterns. He has an OCPJP 7 certification.

Page 18: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams
Page 19: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

xix

About the Technical Reviewer

Dr. B V Kumar is currently the Director at Altius Inc and is responsible for delivering technology-based services for corporate clients. Dr. Kumar has a master’s degree in technology from the Indian Institute of Technology Kanpur and a PhD from the Indian Institute of Technology-Kharagpur. He has over 22 years of experience in the field of information technology at various levels and in organizations such as Computer Vision Corporation (Singapore), Parametric Technologies (Seoul, South Korea), Sun Microsystems (India), and Infosys Technologies Ltd.

Prior to initiating Altius Inc, Dr. Kumar was the Director and Chief Architect at the Global Technology Office of Cognizant Technology Solutions (India). He has been working on enterprise technology solutions for more than 12 years, focusing on Java, JEE, web services, service-oriented architecture, and open source technologies. He has also been working on content management systems through applications such as Joomla and Drupal and mobile platforms such as Android and iPhone.

As a Director of Altius Inc, Dr. Kumar is currently focusing on technology consultancy, technology training and evangelization, community development, and project support for corporate clients. Dr. Kumar has two patents in the IT space and has published many technological papers in national and international journals and conferences. He has also co-authored the following books on information technologies: J2EE Architecture (2007), Web Services 2e: An Introduction (2011), Implementing SOA Using Java EE (2009), and Secure Java: For Web Application Development (2010). Dr. Kumar is currently working on the second edition of Java EE 7 Architecture.

Page 20: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams
Page 21: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

xxi

Acknowledgments

Our first and foremost thanks go to our acquisitions editor Saswata Mishra, who played a key role from the conceptualization to the production stage of the book. Saswata, thank you for your excellent support—you made writing this book an enjoyable experience!

Our special thanks to book editor Robert Hutchinson. Robert played a major role in improving the quality of the presentation in this book. His attention to detail is amazing. Robert, thank you for turning our raw initial writes into a publishable manuscript.

Another special thanks to our technical reviewer, Dr. B V Kumar. His careful and critical review played a key role in improving the technical quality of the book.

We would like to convey our sincere thanks to the entire Apress team, especially Jeffrey Pepper, Ms. Jill Balzano, Ms. Mary Behr, and Ms. Anna Ishchenko for their excellent contributions in producing this book.

We would like to convey our sincere thanks to following readers who pointed out mistakes in the book: Sheila Weiss, Sebastiaan Heunis, John Doe, Steve Tarlton, Beto Montejo, Michael Klenk, Luca Aliberti, and Mikael Strand. We have rectified reported mistakes for a better reading/learning experience.

Both of us have spent countless hours writing chapters quite late into the night and during the weekends and holidays. With kids in our respective homes screaming for attention, it was only the support of our spouses that made writing this book possible.

—S G Ganesh and Tushar Sharma

Page 22: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams
Page 23: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

xxiii

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.

Page 24: Oracle Certified Professional Java SE 7 Programmer …978-1-4302-4765-4/1.pdf · Professional Java SE 7 Programmer Exams ... Oracle Certified Professional Java SE 7 Programmer Exams

■ IntroduCtIon

xxiv

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 BookAll 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 UsIn case of any queries, suggestions or corrections, please feel free to contact us at [email protected] or [email protected].