Upload
vuongkhuong
View
215
Download
0
Embed Size (px)
Citation preview
The Sun Certified Java Developer Exam
with J2SE 1.4
MEHRAN HABIBI, JEREMY PATTERSON, AND lERRY CAMERLENGO
APress Media, LLC
The Sun Certified Java Developer Exam with J2SE 1.4 Copyright © 2002 by Mehran Habibi, Jeremy Patterson, and Terry Camerlengo OriginaLLy published by Apress in 2002
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 978-1-59059-030-0 ISBN 978-1-4302-1104-4 (eBook) DOI 10.1007/978-1-4302-1104-4
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 ofthe trademark owner, with no intention ofinfringement ofthe trademark.
Technical Reviewer: Brian Sam-Bodden
Editorial Directors: Dan Appleman, Gary Cornell, lason Gilmore, Simon Hayes, Karen Watterson, lohn Zukowski
Managing Editor and Production Editor: Grace Wong
Project Manager: Alexa Stuart
Copy Editor: Nicole LeClerc
Compositor: Susan Glinert
Artist: Cara Brunk, Blue Mud Productions
Indexer: Ann Rogers
Cover Designer and Manufacturing Manager: Tom Debolski
Marketing Manager: Stephanie Rodriguez
In the United States, phone 1-800-SPRINGER, email [email protected]. or visit http://www.springer-ny.com.
Outside the United States, fax +496221 345229, email [email protected], or visit http://www.springer.de.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 947l0. Phone 510-549-5930, fax: 510-549-5939, email [email protected], or visit http://www.apress.com.
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 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.
This book is dedicated to my mother, City Habibi. I cannot imagine a stronger person, and I cannot imagine a kinder one.
Mehran Habibi
This book is dedicated to my parents, Bruce and Mary Lynn Patterson, and to my future wife, Mary McCarron. I cannot
thank you enough for all of your support.
Jeremy Patterson
I would like to dedicate all my efforts on this book to my father, Nino Camerlengo.
Terry Camerlengo
Contents at a Glance
About the Authors ............................................................................................... xi
About the Technical Reviewer ..................................................................... xiii
Acknowledgments ................................................................................................... xv
Chapter 1 Introduction .............................................................................. 1
Chapter 2 Project Analysis and Design ........................................... 13
Chapter 3 Evaluating the Sample Project ....................................... 61
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Threading .................................................................................. 77
Networking with Sockets and RMI ................................ 139
The Graphical User Interface ....................................... 189
NIO .............................................................................................. 263
Project Wrap-up ................................................................... 315
Index ...................................................................................................................... 339
v
Contents
About the Authors ....................................................................................... xi
About the Technical Reviewer ...................................................... ... xiii
Acknowledgments ............................................................................................. xv
Chapter 1 Introduction ........................................................................ 1
J2SE 1.4 ................................................................................................................... 2 The SCJO Exam (310-027) .................................................................................. 2
The Structure of the Exam .................................................................................. 2 Downloading the Exam ....................................................................................... 3 Documentation and Questions .......................................................................... 3
Who Should Read This Book .............................................................................. 4
About This Book .................................................................................................... 4
Setting Up the J2SE 1.4 SOK and Environmental Variables ............. 6 Configuring System Properties ........................................................................... 7
Summary ................................................................................................................... 11
Chapter 2 Project Analysis and Design ................................ 13
Implementing a Project ................................................................................... 14 Getting Started ................................................................................................... 14 Gathering Requirements ................................................................................... 15 Prototyping the GUI .......................................................................................... 15 UsingAccepted Design Patterns ...................................................................... 15 Documenting Design Decisions ....................................................................... 16 Testing ................................................................................................................ 16
Organizing a Project ....................................................................................... 17 High-Level Oocumentation .............................................................................. 18
Design Decisions Document ............................................................................ 19
Java Coding Conventions ................................................................................ 19 Variable Conventions ........................................................................................ 20 Method Conventions ......................................................................................... 21 Formatting Conventions ................................................................................... 22 Comment Conventions ..................................................................................... 22 Naming Conventions ........................................................................................ 23 Exception Conventions ..................................................................................... 24
vii
Contents
Javadoc .................................................................................................................... 27 Javadoc in Depth ................................................................................................ 27 Javadoc Style Guidelines .................................................................................... 29 Formatting Javadoc Comments ........................................................................ 32 Running the Javadoc Utility .............................................................................. 36 New Javadoc Features ........................................................................................ 37
Working with Packages ...................................................................................... 39 Best Practices ..................................................................................................... 41
Assertions ............................................................................................................ 42 Logging ................................................................................................................ 43
Summary .................................................................................................................... 58
Chapter 3 Evaluating the Sampie Project ........................... 61
Introducing the Sample Project .................................................................. 61 Starting Point ...................................................................................................... 62 Running the Client ............................................................................................. 65
Application Overview ........................................................................................ 70 Modifications ...................................................................................................... 71
Summary .................................................................................................................... 74
Chapter 4 Threading ............................................................................... 77
Understanding the Fundamentals .................................................................. 78 A Brief Review of Threads .................................................................................. 79 Multithreading ......................................................................... ........................... 80 Java's Multithreading Concepts ...................................................... .................. 80 Waiting ................................................................................................................ 81 Locks .................................................................................................................... 94 U nderstanding Thread Safety ......................................................................... 1 03
Using Thread Objects ...................................................................................... 111 Stopping, Suspending, Destroying, and Resuming ....................................... 111 Thread States .................................................................................................... 112 More on Blocking ............................................................................................. 113 Synchronization ............................................................................................... 115
Threads and NIO ................................................................................................. 118 FileChannels ..................................................................................................... 118 Other Channels ................................................................................................. 119
Threads and Swing ............................................................................................ 119 Multithreading with Swing ...................................................... ........................ 120 General Principles ofThreadingwith Swing .................................................. 121
Threading Best Practices ............................................................................. 122
viii
Contents
How Threading Affects the Code Base .................................................... 124 The Class Structure .......................................................................................... 124 Discussion Point: HowWill the DVDDatabase Be Used? ............................. 126 Discussion Point: Reserving and Releasing DVDs ........................................ 128 Discussion Point: Locking and Unlocking ..................................................... 132 New db Package Structure .............................................................................. 132 Discussion Point: Reading and Writing ......................................................... 133
Summary ................................................................................................................. 135
Chapter 5 Networking wi th Sockets and RMI ................... 139
What 1s Serialization? ................................................................................. 141 Using the Serialver Tool .................................................................................. 142 The Serialization Process ................................................................................ 144 Complete Contral with Externalizable ........................................................... 147 Socket Overview ............................................................................................... 150 An Introduction to RMI ................................................................................... 166
Summary ................................................................................................................. 184
Chapter 6 The Graphical User Interface ........................... 189
GU1 Concepts ...................................................................................................... 190 Layout Concepts .............................................................................................. 191 Human Interface Concepts ............................................................................. 193 An Example Analysis ........................................................................................ 200
Model/View/Controller Pattern ................................................................. 204 Why U se the MVC Pattern? ............................................................................. 204
MVC in Detail ................................................................................................... 205 AReal-WorldExample ofMVC: Denny's DVDs ............................................ 208 Benefits of MVC ............................................................................................... 212 Drawbacks ofMVC .......................................................................................... 213 Alternatives to MVC ......................................................................................... 213
Abstract Window Toolkit .............................................................................. 214 AWf Review ...................................................................................................... 214
Swing ...................................................................................................................... 224 Differences BetweenAWf and Swing ............................................................ 224 Swing-Specific Components ........................................................................... 231
Bringing Denny's DVDs Together ............................................................... 240
New Swing Additions in J2SE 1. 4 ............................................................. 253 AWf Focus Architecture ................................................................................. 253 Drag and Drop ................................................................................................. 253
Summary ................................................................................................................. 260
ix
Contents
Chapter 7 NIO ............................................................................................ 263
NIO: The Future of Java IIO ...................................................................... 264 Advantages ofNIO ............................................................................................ 264 Changes to Existing File 11 0 ............................................................................ 265 Changes to NetworkIlO .................................................................................. 266
Buffers .................................................................................................................. 269 HowBuffersWork ............................................................................................ 269 Buffers in J2SE 1.4 ...................................................... ....................................... 270
Channels ................................................................................................................ 281 The Relevant Channels .................................................................................... 281 ByteChannels in NIO ........................................................................................ 281 FileChannels in NIO ......................................................................................... 283 SocketChannels in NIO .................................................................................... 290
Regular Expressions ........................................................................................ 300 What Regular Expressions Are .......................................................... ............... 300 How Regular Expressions Are Useful .................................................. ............ 301 Regular Expressions in J2SE 1.4 ...................................................... ................. 301
A Simple Example of the Regular Expressions Classes .................................. 303 Introduction to Regular Expression Syntax .................................................... 305
Summary .................................................................................................................. 313
Chapter 8 Project Wrap-up ............................................................. 315
Thread Safety and Locking ........................................................................... 316 The Choke Between RMI and Sockets ........................................................... 317
The MVC Pattern in the Gur ......................................................................... 318 Locating the Code Samples ........................................................................... 318 Compiling and Packaging the Application ............................................ 319 Creating a Manifest File ............................................................................. 320
Running rmic on the Remote Package ....................................................... 321 Packaging the Applieation ........................................................................... 322 Running DennyJs DVDs in Loeal Mode ....................................................... 324 Running DennyJs DVDs in Network Mode ................................................... 326
Testing .................................................................................................................. 329 Summary .................................................................................................................. 335
Index .................................................................................................................... 339
x
About the Authors
MEHRAN HABIBI is an application architect with BankOne in Columbus, Ohio, where he resides with his lovely wife, Angela. Mehran has over 7 years of IT experience, induding positions with IBM, Executive Jet, UUNET, BankOne, and OCLC, in addition to working as a university lecturer, independent consultant, and Java certification trainer. Technologies of interest to hirn indude Web services, wireless technologies, EJBs, Servlets, XMLlXSLT, C#, and Visual Basic .NET. Mehran's professional focus has been on architecture, project leadership, mentoring, team leadership, and programming from the mid-tier on back. Mehran holds certifications in both VC++ and Java 2, and he graduated with a bachelor's of science degree in software engineering from The Ohio State University. Mehran is an amateur boxer, teaches martial arts at The Ohio State University, enjoys soccer, and has ruined his chess by playing too many speed games. You can contact Mehran at coach@influxs. corno
TERRY CAMERLENGO has over 5 years of software engineering experience with numerous Fortune 500 companies. He is experienced in all phases ofthe software life cyde, with a focus in object-oriented technologies such as C++ and Java. His expertise spans from front -end technologies such as ASp, JSP, and Servlets, to server-side skills in EJB, COM, and database design and development. Terry holds both Sun and Microsoft certifications in SQL Server, MFC, Visual C++, and Java. He graduated with degrees in computer science and philosophy from The
Ohio State University. He is currently employed as a software developer at NetJets in Columbus, Ohio.
xi
About the Authors
xii
JEREMY PATIERSON graduated summa cum laude from The Ohio State Universitywith a bachelor's degree in fine arts. Jeremy's professional career has included many positions in the field of software engineering with a holistic focus on the integration of software solutions from the back to the front end. Jeremy is a Sun Certified Java Programmer and has been programming in Java for 4 years. His skills include a wide range of Java technologies such as JSP, Servlets, Java 2D Cincluding the Java Advanced Imaging API), Swing, XML, Enterprise JavaBeans, and various under-ground technologies. His other interests include the pursuit of better software through intelligent interface design and adherence to usability standards. Jeremy is an experienced educator, having taught professional Java certification courses. In his spare time, he enjoys playing with his numerous computer systems and exploring any cool new technology he can get his hands on. He is also, unashamedly, an avid Macintosh fan.
About the Technical Reviewer
BRIAN SAM-BoDDEN holds dual bachelor's degrees from Ohio Wesleyan University in computer science and physics. Brian has over 7 years of experience working with object technologies, first as aDelphi developer and since 1997 as a Java convert. Brian is a Sun Certified Programmer, Developer, and Architect for the Java 2 platform. He works as an independent Java consultant and trainer. His company, Integrallis Software, LLC, provides training and technical consulting services in Java. When not coding or reading technical books, Brian enjoys playing soccer and
doing Brazilian Jiu Jitsu.
xiii
Acknowledgments
Mehran Habibi's Acknowledgments
I'o UKE TO THANK Jason Gilmore for making this project possible, and the amazing people at Apress, including Alexa Stuart, Grace Wong, Stephanie Rodriguez, and Nicole LeClerc. I would very specifically like to thank my sister, Marjan Frost, for all her support; Terry and Jeremy for their friendship and expertise; and my wife, Angela Young, far everything good.
I would like to acknowledge, in no particular order, Jim Pinkim, Robert Graffagnino, Ricardo Wilson, various kind friends who took long afternoons and straightened out some of my more loopy ideas, and the kind people at the Starbucks in Grandview, Ohio, who kept the hot tea coming as I finished this book.
Jeremy Patterson's Acknowledgments
My THANKS GO OUT first and foremost to my fellow authors, Mehran and Terry. Deepest thanks also to Jason Gilmore and everyone else at Apress who made this book possible.
Terry Camerlengo's Acknowledgments
1b JAS ON GILMORE and all of the wonderful people at Apress, thanks for the opportunity. I would also like to express my gratitude to Mehran far including me in this endeavor, to both Jeremy and Mehran for all of the fabulous evenings spent working out the details, and finally thanks go to Johnny DiLoretto for his literary assistance.
xv