Tải bản đầy đủ (.pdf) (361 trang)

Apress SCJD the sun java certified java developer exam with J2SE 5 2nd edition dec 2005 ISBN 1590595165 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.67 MB, 361 trang )


5165CH00_FM.qxd

11/16/05

11:58 AM

Page i

SCJD Exam with J2SE 5
Second Edition

Andrew Monkhouse and Terry Camerlengo


5165CH00_FM.qxd

11/16/05

11:58 AM

Page ii

SCJD Exam with J2SE 5, Second Edition
Copyright © 2006 by Andrew Monkhouse and Terry Camerlengo
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN (pbk): 1-59059-516-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence


of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Jason Gilmore
Technical Reviewer: Jim Yingst
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis,
Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Project Manager: Beth Christmas
Copy Edit Manager: Nicole LeClerc
Copy Editor: Liz Welch
Assistant Production Director: Kari Brooks-Copony
Production Editor: Lori Bring
Compositor: Dina Quan
Proofreader: Elizabeth Berry
Indexer: John Collin
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at in the Source Code section.
You will need to answer questions pertaining to this book in order to successfully download the code.


5165CH00_FM.qxd


11/16/05

11:58 AM

Page iii

Contents at a Glance
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii

PART 1

■■■

■CHAPTER 1
■CHAPTER 2
■CHAPTER 3

PART 2
■CHAPTER
■CHAPTER
■CHAPTER
■CHAPTER
■CHAPTER

PART 3


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Project Analysis and Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Project Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

■■■
4
5
6
7
8

Implementing a J2SE Project

Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The DvdDatabase Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Networking with RMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Networking with Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
The Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

■■■

■CHAPTER 9

Introduction and General
Development Considerations

Wrap-Up

Project Wrap-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295


■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

iii


5165CH00_FM.qxd

11/16/05

11:58 AM

Page iv


5165CH00_FM.qxd

11/16/05

11:58 AM

Page v

Contents
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii

PART 1


■■■

■CHAPTER 1

Introduction and General
Development Considerations

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
J2SE 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The SCJD Exam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The Certification Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Downloading the Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Documentation and Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Setting Up the J2SE 5 JDK and Environmental Variables . . . . . . . . . . . . 8
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

■CHAPTER 2

Project Analysis and Design . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Implementing a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Gathering Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Using Accepted Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . 14
Documenting Design Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Organizing a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
High-Level Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Design Decisions Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
v


5165CH00_FM.qxd

vi

11/16/05

11:58 AM

Page vi

■CONTENTS

Java Coding Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
File Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Source Code Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Formatting of Comments Within the Code . . . . . . . . . . . . . . . . . . 28
Suggested Coding Conventions for New Features in JDK 5 . . . . . 29
Javadoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Coding Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Working with Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Writing Documentation As You Go . . . . . . . . . . . . . . . . . . . . . . . . 47
Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

■CHAPTER 3

Project Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
What Are the Essential Requirements for the Sun
Certification Project? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Introducing the Sample Project . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Application Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

PART 2

■■■

■CHAPTER 4

Implementing a J2SE Project

Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Threading Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
A Brief Review of Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Java’s Multithreading Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 73
Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Locking in JDK 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Locking Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Understanding Thread Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Race Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Starvation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
tions . . . . . . . . . . . . . . . . . . . . . . . 104


5165CH00_FM.qxd

11/16/05

11:58 AM

Page vii

■CONTENTS

Thread Safety Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Using Thread Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Stopping, Suspending, Destroying, and Resuming . . . . . . . . . . . 106
Thread States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
More on Blocking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Multithreading with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Threading Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

■CHAPTER 5

The DvdDatabase Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Creating the Classes Required for

the DvdDatabase Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
The DVD Class: A Value Object . . . . . . . . . . . . . . . . . . . . . . . . . 119
Discussion Point: Handling Exceptions Not Listed in the
Supplied Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
The DvdDatabase Class: A Façade . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Accessing the Data: The DvdFileAccess Class . . . . . . . . . . . . . . . . . . 137
Discussion Point: Caching Records . . . . . . . . . . . . . . . . . . . . . . 148
The ReservationsManager Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Discussion Point: Identifying the Owner of the Lock . . . . . . . . . 150
Creating Our Logical Reserve Methods . . . . . . . . . . . . . . . . . . . 154
The Logical Release Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

■CHAPTER 6

Networking with RMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
What Is Serialization? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Using the serialver Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
The Serialization Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Customizing Serialization with the Externalizable Interface . . . . 169
Introducing RMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The Delivery Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
The Pros and Cons of Using RMI as a Networking Protocol . . . . 174
The Classes and Interfaces of RMI . . . . . . . . . . . . . . . . . . . . . . 175
What Is an RMI Factory? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

vii



5165CH00_FM.qxd

viii

11/16/05

11:58 AM

Page viii

■CONTENTS

■CHAPTER 7

Networking with Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Socket Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Why Use Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Socket Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
TCP and UDP Sockets Overview . . . . . . . . . . . . . . . . . . . . . . . . 201
TCP Socket Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
The DvdSocketClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Socket Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Multicast and Unicast Servers . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
The Server Socket Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The Application Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

■CHAPTER 8

The Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . 225
GUI Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Layout Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Human Interface Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Model-View-Controller Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Why Use the MVC Pattern? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
MVC in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Benefits of MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Drawbacks of MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Alternatives to MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Swing and the Abstract Windows Toolkit . . . . . . . . . . . . . . . . . . . . . . 237
Layout Manager Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
The JLabel Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
The JTextField Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
The JButton Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The JRadioButton Component . . . . . . . . . . . . . . . . . . . . . . . . . . 249
The JComboBox Component . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The BorderFactory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The JTable Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
The TableModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Using the TableModel with a JTable . . . . . . . . . . . . . . . . . . . . . 259
The JScrollPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260


5165CH00_FM.qxd


11/16/05

11:58 AM

Page ix

■CONTENTS

Bringing Denny’s DVDs Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Application Startup Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
The Client GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Specifying the Database Location . . . . . . . . . . . . . . . . . . . . . . . 273
The Server GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Swing Changes in J2SE 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Improve Default Look and Feel of Swing . . . . . . . . . . . . . . . . . . 289
Skins Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Adding Components to Swing Containers Has
Been Simplified . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

PART 3

■■■

■CHAPTER 9

Wrap-Up


Project Wrap-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Thread Safety and Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
The Choice Between RMI and Sockets . . . . . . . . . . . . . . . . . . . . . . . 296
Benefits of Using a Serialized Objects Over
Sockets Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Benefits of Using an RMI Solution . . . . . . . . . . . . . . . . . . . . . . . 299
The MVC Pattern in the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Locating the Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Compiling and Packaging the Application . . . . . . . . . . . . . . . . . . . . . 301
Creating a Manifest File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Running rmic on the Remote Package . . . . . . . . . . . . . . . . . . . . . . . . 304
Packaging the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Running the Denny’s DVDs Application . . . . . . . . . . . . . . . . . . . . . . . 306
Running the Client Application in Stand-alone Mode . . . . . . . . . 307
Running Denny’s DVDs Server . . . . . . . . . . . . . . . . . . . . . . . . . 307
Running the Client Application in Networked Mode . . . . . . . . . . 309
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Packaging Your Submission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

ix


5165CH00_FM.qxd

11/16/05


11:58 AM

Page x


5165CH00_FM.qxd

11/16/05

11:58 AM

Page xi

About the Authors
■ANDREW MONKHOUSE is a moderator on the JavaRanch web site, currently
moderating the SCJD and SCJA forums.
Andrew has passed SCJP 1.2, SCJP 1.4, SCJD, SCWCD, SCBCD, and Part I
of SCEA. He has been working with computers for too long (his first program
was written on mark-sense cards, which are similar to punch cards).
Andrew has worked in a number of positions from programmer,
to architect and IT manager, working on VMS, Unix, Macintosh, and
Microsoft operating systems. He’s built back-end, middleware, and front-end solutions for
a variety of industries. Andrew is an Australian at heart, although he is frequently in other
countries for work purposes.
■TERRY CAMERLENGO has over 9 years of software engineering experience
from numerous corporations, including Fortune 500s and dot-coms. He is
experienced in all phases of the software life cyle, with a focus on objectoriented technologies such as Java, C#, C++, and .NET. His expertise
includes front-end web design, server-side enterprise development, and
relational database modeling and development. Terry holds both Sun and
Microsoft certifications, and graduated with a degree in computer science

and philosophy from Ohio State University. Currently Terry works for Ohio State University’s
James Cancer Center in the Biomedical Informatics department as a senior developer and
research specialist and is pursuing advanced studies in computational biology.

xi


5165CH00_FM.qxd

11/16/05

11:58 AM

Page xii


5165CH00_FM.qxd

11/16/05

11:58 AM

Page xiii

About the Technical Reviewer
■JIM YINGST studied engineering physics at the University of Arizona, but after graduating he
got sucked into the IT job market instead because, well, it seemed like a good idea at the time.
He now roams the West helping tech companies find solutions to their IT problems.
Jim is a sheriff (administrator) and longtime contributor at www.javaranch.com, where his
duties include answering Java questions, redirecting off-topic posts, and dealing with troublesome Australians.

He seems to spend most of his free time obsessively visiting bookstores. On rare occasions
he actually reads the stuff he buys, mostly science fiction. The rest of the time he’s probably
listening to obscure progressive rock bands or finding new Thai restaurants. Jim lives in
Boulder, Colorado.

xiii


5165CH00_FM.qxd

11/16/05

11:58 AM

Page xiv


5165CH00_FM.qxd

11/16/05

11:58 AM

Page xv

Acknowledgments
W

e would like to thank the following people:
• Mehran (Max) Habibi, who did so much work on the first edition and in getting this

edition started, as well as introducing us to each other and to the Apress staff
• Our technical editor Jim Yingst, who not only verified our writing but made so many
wonderful suggestions
• The fantastic staff at Apress for working with us on this project, and taking our raw work
and producing a polished publication
• Each other

Without the help of all these people, this book would not be anywhere near as good as
it is. We would also like to thank family, friends, and colleagues who put up with our bouncing
between being totally unsociable when there were deadlines to meet, and desperately trying
to catch up with everyone in the quiet times.
Andrew Monkhouse and Terry Camerlengo

xv


5165CH00_FM.qxd

11/16/05

11:58 AM

Page xvi


5165CH00_FM.qxd

11/16/05

11:58 AM


Page xvii

Introduction
T

he Sun Certified Developer for the Java 2 Platform assignment offers a unique opportunity
for Java developers to put their Java skills to practical use without requiring any specific development or runtime environment. The assignment also provides a great learning environment
as many different APIs can be used, and many alternative solutions can be provided. This
book introduces many of the concepts you will need to know in order to pass the SCJD
assignment.
Many developers are a little daunted by the scope of the assignment, as it covers everything
from a back-end database, a server application, a front-end application, API documentation,
and user documention. This book covers each section in detail, helping you gradually build
up your knowledge of each topic while working toward a sample project. This book will also
introduce you to the new features of JDK 5, providing contextual usage of the new APIs and
features within our sample project.
The Sun assignment deliberately does not specify an operating system platform or development environment to be used—all that is needed is a computer capable of running a
current version of the JDK. Throughout this book we have used JDK 5 on Windows 2000. Since
this book introduces JDK 5 features, and uses them throughout our sample project, you will
need JDK 5 to run our sample applications; however, the sample application is not dependent
on Windows 2000.
We hope you enjoy this book, and we look forward to hearing that you passed and any
comments you may have on this book. You can contact both authors at

xvii


5165CH00_FM.qxd


11/16/05

11:58 AM

Page xviii


5165CH01.qxd

11/16/05

12:01 PM

PART

Page 1

1

■■■

Introduction and
General Development
Considerations


5165CH01.qxd

11/16/05


12:01 PM

Page 2


5165CH01.qxd

11/16/05

12:01 PM

CHAPTER

Page 3

1

■■■

Introduction
W

elcome to The Sun Certified Java Developer Exam with J2SE 5, Second Edition. By taking
advantage of the new features of J2SE 5, passing the Sun Certified Java Developer (SCJD) exam
is easier than ever before. Features such as generics, the enhanced for loop, autoboxing and
unboxing of primitives, the new concurrency classes, and other new capabilities offer developers a richer and more robust tool set than ever before. This book and the accompanying
sample project will help you acquire the understanding necessary to pass the SCJD examination while learning the finer points of the J2SE Development Kit (JDK 5). If you have been
meaning to take the SCJD exam or you are ready to further explore the mysteries of Java, you
have found the right book.
The best way to learn a new skill is to use it. This is true in tennis, pottery, and yes, programming. With that principle firmly in mind, this book helps you learn about J2SE 5 while

detailing the strategies, skills, and information needed to pass the SCJD exam. Sun Microsystems designed the SCJD exam to be a realistic example of what a professional Java developer
can expect to encounter in the real world. The SCJD exam covers a large portion of J2SE,
including Remote Method Invocation (RMI), threading, file input/output (I/O), and Swing.
The sample project, Denny’s DVDs (introduced in Chapter 3), is designed to explore the
same concepts that the SCJD exam does. Unlike on the SCJD exam, however, the underlying
concepts are explained in detail. When you have finished this book, you will have learned the
skills necessary to take and pass Sun’s exam.
The two major topics discussed in this chapter are
• Finding out how to download and take the SCJD exam
• Understanding the goals of this book

J2SE 5
J2SE 5 is a major update, designed to improve ease of development, increase scalability, provide for additional monitoring and manageability, and enhance the Java desktop clients. While
J2SE 5 offers a slew of new and exciting features, this book focuses on bread-and-butter topics
such as threading, RMI, Swing, sockets, exception chaining, logging, and serialization. Once
you understand the foundations, everything else follows naturally.

3


5165CH01.qxd

4

11/16/05

12:01 PM

Page 4


CHAPTER 1 ■ INTRODUCTION

The SCJD Exam
The SCJD certification is a comprehensive test used by Sun to verify the skills of advanced
Java programmers. It is generally considered a strong benchmark of competence. This book
focuses on the features of J2SE 5 that are relevant to this exam. Adequate preparation is essential to pass the exam. Due to its difficulty, the opportunity to take the SCJD exam is only
available to programmers who have already passed the Sun Certified Java Programmer (SCJP)
exam. Fortunately, this book explains the concepts you need to know to pass the SCJD exam.

■Note Three Sun Certified Java Programmer exams are currently available: one for certification on each
of the J2SE 1.2, J2SE 1.4, or J2SE 5.0 platforms, respectively. While the questions for each of these certifications differ slightly (for example, the J2SE 1.2 exam has Abstract Windowing Toolkit [AWT] questions),
you only need to be certified in any one of these three certifications in order to be eligible for the SCJD
certification.

The Certification Process
The SCJD certification process consists of two parts. The first part is an assignment consisting
of a custom-designed sample project with a sample data file, an interface to be implemented,
and specific requirements. You complete the assignment and return it for grading. Information on how to register for the assignment and download the assignment instructions and
data file are presented in the next section of this chapter.
When you have completed the assignment and returned it for grading, you move on to
the second part of the certification process, which is a written test designed to confirm that
you wrote the assignment you submitted, and to investigate the understanding that led to the
design and implementation decisions made during the first part of the exam. You may take up
to 90 minutes to complete the written exam; however, since there are currently only four questions on the exam, you should find that you have more than enough time to complete it.
It is not possible to have notes or material with you for that exam. As such, it is best to
take the written exam as soon as possible after you have submitted your assignment, while
all the details are still fresh in your mind. Both sections are graded at once, even though the
coding section is collected first. This means that the second exam will ask generic questions
rather than specific ones about your individual project. You need a holistic understanding to
pass both parts of the SCJD exam.


■Caution The assignment you submit will not be passed to an assessor for grading until after you have
completed the essay exam. If you do not take the essay exam, you will not receive any warning that your
assignment is not being assessed—it will just sit in limbo until you finally do take the essay exam.


5165CH01.qxd

11/16/05

12:01 PM

Page 5

CHAPTER 1 ■ INTRODUCTION

The goal of the SCJD exam is to validate your understanding of the most important Java
skills, including threading, RMI, sockets, serialization, file I/O, and Swing. Each assignment
project can be unique, testing these features to different degrees. For example, while you must
write a server capable of handling concurrent requests, the interface provided might change
which classes you allow multiple threads to run on. Or your requirements could call for strict
search requirements versus more general searching ability. This book gathers together everything you need to know about all the relevant topics and integrates in the relevant changes in
J2SE 5.
Sun also requires you to use a current JDK (one that has not been superseded by a newer
JDK by more than 18 months) for developing your solution, ensuring that candidates stay current with the latest features of the JDK. A list of release dates may be found at http://java.
sun.com/j2se/codenames.html.

Downloading the Assignment
You can register for the assignment and examination by visiting />training/certification/java/java_devj2se.html in the United States. Many other countries
also allow online registration—you can view contact details at />world_training.html. After paying for the assignment, you will receive an e-mail from Sun

telling you exactly how to download the Java archive (JAR) files that contain the assignment
instructions. Receiving this e-mail may take a few days, or it may happen that same day. As
soon as you receive the JAR files, make a couple of copies and store them safely. It is very
expensive to get a second copy of the assignment to match the subject of this section.

■Tip The Sun Education web site lists the web address where you may download your assignment. You
may be able to download your assignment before you have received an e-mail specifying that your account
has been configured for downloading.

Documentation and Questions
You are probably going to have questions regarding the requirements of the exam. Generally,
Sun will not answer these questions. This may be because they want to see how well you can
choose between different solutions (and describe why you made your decisions). It may be
because they are trying to emulate real-world conditions where the client is not always willing
to communicate. It may even be because answering questions for each test applicant is an
untenable task. In any case, it is very important that you articulate your questions and deal
with them in the documentation you must create as part of your assignment deliverables. If
nothing else, you should document your assumptions and choices. For more help, we suggest
using the excellent resources available at JavaRanch () and the various helpful Java certification groups on Yahoo.
Chapter 2 provides good suggestions on how to work with Javadoc-style comments and
offers some industry best practices. Don’t use outlandish naming conventions or even Hungarian notation. If possible, use whatever style the material itself uses. As far as the SCJD exam
is concerned, Sun really wants you to color inside the lines.

5


5165CH01.qxd

6


11/16/05

12:01 PM

Page 6

CHAPTER 1 ■ INTRODUCTION

Who Should Read This Book
This book is for the working Java professional who needs an introduction to J2SE 5 and has an
eye toward learning the material needed to pass the Sun Certified Java Developer exam. The
SCJD exam gives programmers a slice of what they can expect on a real-world assignment,
and you have to be ready for that challenge. A developer who has passed, or could pass, the
Sun Certified Java Programmer (SCJP) exam will feel at home here. A developer with less than
six months of experience should probably supplement this book with some of the excellent
Java books available from Apress or other publishers.
This book describes in detail many features of the JDK, some of which have been part of
standard Java for many years, and some of which have only been introduced in JDK 5. The
only assumption we have made in this book is that the reader will be familiar enough with
Java to pass the SCJP exam—so we do not need to spend time explaining the basics of the
language (for example, the difference between an int and a long). However, we do go into
details of changes to the language, so candidates who have not yet learned the JDK 5
language enhancements can discover them here.

About This Book
This book addresses the SCJD certification, which is one of several Java certification exams
offered by Sun Microsystems. The SCJD and SCEA (Sun Certified Enterprise Architect) certificates require candidates to complete projects, whereas the other certifications only require
theory-based exams in which the candidate typically has multichoice questions to answer. As
far as programming goes, the SCJD exam is the most challenging of the exams that Sun offers,
and that is precisely why it is the focus of this book.

This book is divided into three parts. Part 1 focuses on general development considerations and outlines a sample project. Part 2 teaches necessary concepts from the ground up,
while facilitating both understanding and implementation. Part 3 concludes the book with a
discussion of design and implementation decisions made and possible alternative paths.
A sample project is provided that offers challenges similar to those you’ll find on the SCJD
exam while introducing and taking advantage of the relevant new features of J2SE 5. Each
topic related to the exam is explained in detail, and trade-offs are considered. Where appropriate, parallel development paths are explored and implemented.
Where applicable, chapters briefly discuss the design patterns being used and offer a brief
explanation of those patterns. We strongly encourage you to purchase or download some pattern resources. Various web sites offer insightful tutorials, including the Sun site (http://www.
sun.com) and TheServerSide.com (). There are also various
excellent books on the topic, including Head First Design Patterns, by Elisabeth Freeman, Eric
Freeman, Bert Bates, and Kathy Sierra (O'Reilly, 2004).
Throughout this book, we present numerous examples that aid in the development of a
real-world Java application. Each chapter contributes directly to this application by addressing a critical topic such as threading, Swing, or networking. The text explores questions that
naturally arise in these topics and explains how the challenges can be met. More important,
the trade-offs and implications of these choices are discussed.


×