Table Of ContentJJaavvaa™™ ffoorr PPrrooggrraammmmeerrss:: DDeeiitteell®® DDeevveellooppeerr SSeerriieess
bbyy PPaauull JJ.. DDeeiitteell -- DDeeiitteell && AAssssoocciiaatteess,, IInncc..;; HHaarrvveeyy MM.. DDeeiitteell -- DDeeiitteell && AAssssoocciiaatteess,, IInncc..
PPuubblliisshheerr:: PPrreennttiiccee HHaallll
PPuubb DDaattee:: FFeebbrruuaarryy 1122,, 22000099
PPrriinntt IISSBBNN--1100:: 00--1133--770000112299--00
PPrriinntt IISSBBNN--1133:: 997788--00--1133--770000112299--33
WWeebb IISSBBNN--1100:: 00--1133--770011885522--55
WWeebb IISSBBNN--1133:: 997788--00--1133--770011885522--99
PPaaggeess:: 11220000
OOvveerrvviieeww
PPRRAACCTTIICCAALL,, EEXXAAMMPPLLEE--RRIICCHH CCOOVVEERRAAGGEE OOFF::
CCllaasssseess,, OObbjjeeccttss,, EEnnccaappssuullaattiioonn,, IInnhheerriittaannccee,, PPoollyymmoorrpphhiissmm,, IInntteerrffaacceess,, NNeesstteedd CCllaasssseess
IInntteeggrraatteedd OOOOPP CCaassee SSttuuddiieess:: TTiimmee,, GGrraaddeeBBooookk,, EEmmppllooyyeeee
IInndduussttrriiaall--SSttrreennggtthh,, 9955--PPaaggee OOOODD//UUMMLL®® 22 AATTMM CCaassee SSttuuddyy
JJaavvaaSSeerrvveerr™™ FFaacceess,, AAjjaaxx--EEnnaabblleedd WWeebb AApppplliiccaattiioonnss,, WWeebb SSeerrvviicceess,, NNeettwwoorrkkiinngg
JJDDBBCC™™,, SSQQLL,, JJaavvaa DDBB,, MMyySSQQLL®®
TThhrreeaaddss aanndd tthhee CCoonnccuurrrreennccyy AAPPIIss
II//OO,, TTyyppeess,, CCoonnttrrooll SSttaatteemmeennttss,, MMeetthhooddss
AArrrraayyss,, GGeenneerriiccss,, CCoolllleeccttiioonnss
EExxcceeppttiioonn HHaannddlliinngg,, FFiilleess
GGUUII,, GGrraapphhiiccss,, GGrroouuppLLaayyoouutt,, JJDDIICC
UUssiinngg tthhee DDeebbuuggggeerr aanndd tthhee AAPPII DDooccss
AAnndd mmoorree……
TThhee pprraaccttiicciinngg pprrooggrraammmmeerr''ss DDEEIITTEELL®® gguuiiddee ttoo JJaavvaa™™ ddeevveellooppmmeenntt aanndd tthhee PPoowweerrffuull JJaavvaa™™ PPllaattffoorrmm
WWrriitttteenn ffoorr pprrooggrraammmmeerrss wwiitthh aa bbaacckkggrroouunndd iinn hhiigghh--lleevveell llaanngguuaaggee pprrooggrraammmmiinngg,, tthhiiss bbooookk aapppplliieess tthhee DDeeiitteell
ssiiggnnaattuurree lliivvee--ccooddee aapppprrooaacchh ttoo tteeaacchhiinngg pprrooggrraammmmiinngg aanndd eexxpplloorreess tthhee JJaavvaa llaanngguuaaggee aanndd JJaavvaa AAPPIIss iinn ddeepptthh.. TThhee
bbooookk pprreesseennttss tthhee ccoonncceeppttss iinn tthhee ccoonntteexxtt ooff ffuullllyy tteesstteedd pprrooggrraammss,, ccoommpplleettee wwiitthh ssyynnttaaxx sshhaaddiinngg,, ccooddee hhiigghhlliigghhttiinngg,,
lliinnee--bbyy--lliinnee ccooddee ddeessccrriippttiioonnss aanndd pprrooggrraamm oouuttppuuttss.. TThhee bbooookk ffeeaattuurreess 222200 JJaavvaa aapppplliiccaattiioonnss wwiitthh oovveerr 1188,,000000 lliinneess ooff
pprroovveenn JJaavvaa ccooddee,, aanndd hhuunnddrreeddss ooff ttiippss tthhaatt wwiillll hheellpp yyoouu bbuuiilldd rroobbuusstt aapppplliiccaattiioonnss..
SSttaarrtt wwiitthh aann iinnttrroodduuccttiioonn ttoo JJaavvaa uussiinngg aann eeaarrllyy ccllaasssseess aanndd oobbjjeeccttss aapppprrooaacchh,, tthheenn rraappiiddllyy mmoovvee oonn ttoo mmoorree
aaddvvaanncceedd ttooppiiccss,, iinncclluuddiinngg GGUUII,, ggrraapphhiiccss,, eexxcceeppttiioonn hhaannddlliinngg,, ggeenneerriiccss,, ccoolllleeccttiioonnss,, JJDDBBCC™™,, wweebb--aapppplliiccaattiioonn
ddeevveellooppmmeenntt wwiitthh JJaavvaaSSeerrvveerr™™ FFaacceess,, wweebb sseerrvviicceess aanndd mmoorree.. YYoouu''llll eennjjooyy tthhee DDeeiitteellss'' ccllaassssiicc ttrreeaattmmeenntt ooff oobbjjeecctt--
oorriieenntteedd pprrooggrraammmmiinngg aanndd tthhee OOOODD//UUMMLL®® AATTMM ccaassee ssttuuddyy,, iinncclluuddiinngg aa ccoommpplleettee JJaavvaa iimmpplleemmeennttaattiioonn.. WWhheenn yyoouu''rree
ffiinniisshheedd,, yyoouu''llll hhaavvee eevveerryytthhiinngg yyoouu nneeeedd ttoo bbuuiilldd oobbjjeecctt--oorriieenntteedd JJaavvaa aapppplliiccaattiioonnss..
TThhee DDEEIITTEELL®® DDeevveellooppeerr SSeerriieess iiss ddeessiiggnneedd ffoorr pprraaccttiicciinngg pprrooggrraammmmeerrss.. TThhee sseerriieess pprreesseennttss ffooccuusseedd ttrreeaattmmeennttss ooff
eemmeerrggiinngg tteecchhnnoollooggiieess,, iinncclluuddiinngg JJaavvaa™™,, CC++++,, ..NNEETT,, wweebb sseerrvviicceess,, IInntteerrnneett aanndd wweebb ddeevveellooppmmeenntt aanndd mmoorree..
Copyright
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.
Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations
have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty
of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential
damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales,
which may include electronic versions and/or custom covers and content particular to your business, training goals,
marketing focus, and branding interests. For more information, please contact:
U. S. Corporate and Government Sales
(800) 382-3419
[email protected]
For sales outside the U. S., please contact:
International Sales
[email protected]
Visit us on the Web: informit.com/PH
Library of Congress Cataloging-in-Publication Data
On file
© 2009 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission
must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or
transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For
information regarding permissions, write to:
Pearson Education, Inc.
Rights and Contracts Department
One Lake Street
Upper Saddle River, NJ 07458
ISBN-13: 978-0-13-700129-3
Text printed in the United States on recycled paper at R.R. Donnelley in Crawfordsville, Indiana.
First printing, January 2009
Trademarks
DEITEL, the double-thumbs-up bug and Dive Into are registered trademarks of Deitel and Associates, Inc.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States
and other countries. Pearson Education is independent of Sun Microsystems, Inc.
Microsoft, Internet Explorer and the Windows logo are either registered trademarks or trademarks of Microsoft
Corporation in the United States and/or other countries.
UNIX is a registered trademark of The Open Group.
Dedication
To Mark L. Taub,
Editor-in-Chief of Prentice Hall Professional
Thank you for being our friend and mentor in professional publishing. It's a privilege to work with someone who
so loves the challenges of the leading edge.
Paul and Harvey
Deitel® Series Page
How to Program Series
Java How to Program, 7/E
C++ How to Program, 6/E
Visual C++® 2008 How to Program, 2/E
C How to Program, 5/E
Internet & World Wide Web How to Program, 4/E
Visual Basic® 2008 How to Program
Visual C#® 2008 How to Program, 3/E
Small Java™ How to Program, 6/E
Small C++ How to Program, 5/E
Simply Series
Simply C++: An Application-Driven Tutorial Approach
Simply Java™ Programming: An Application-Driven Tutorial Approach
Simply C#: An Application-Driven Tutorial Approach
Simply Visual Basic® 2008, 3/E: An Application-Driven Tutorial Approach
SafariX Web Books
www.deitel.com/books/SafariX.html
C++ How to Program, 5/E & 6/E
Java How to Program, 6/E & 7/E
Simply C++: An Application-Driven Tutorial Approach
Simply Visual Basic 2008: An Application-Driven Tutorial Approach, 3/E
Small C++ How to Program, 5/E
Small Java How to Program, 6/E
Visual Basic 2008 How to Program
Visual C# 2008 How to Program, 3/E
Deitel Developer Series
AJAX, Rich Internet Applications and Web Development for Programmers
C++ for Programmers
C# 2008 for Programmers, 3/E
Java for Programmers
Javascript for Programmers
LiveLessons Video Learning Products
www.deitel.com/books/LiveLessons/
Java Fundamentals Parts 1 and 2
C# Fundamentals Parts 1 and 2
C++ Fundamentals Parts 1 and 2
JavaScript Fundamentals Parts 1 and 2
To follow the Deitel publishing program, please register for the free Deitel® Buzz Online e-mail newsletter at:
www.deitel.com/newsletter/subscribe.html
To communicate with the authors, send e-mail to:
[email protected]
For information on government and corporate Dive-Into® Series on-site seminars offered by Deitel & Associates, Inc.
worldwide, visit:
www.deitel.com/training/
or write to
[email protected]
For continuing updates on Prentice Hall/Deitel publications visit:
www.deitel.com
www.prenhall.com/deitel
Check out our Resource Centers for valuable web resources that will help you master Visual C#, other important
programming languages, software and Internet- and web-related topics:
www.deitel.com/ResourceCenters.html
Deitel Resource Centers
Our Resource Centers focus on the vast amounts of free content available online. Find resources, downloads, tutorials,
documentation, books, e-books, journals, articles, blogs, RSS feeds and more on many of today's hottest
programming and technology topics. For the most up-to-date list of our Resource Centers, visit:
www.deitel.com/ResourceCenters.html
Let us know what other Resource Centers you'd like to see! Also, please register for the free Deitel® Buzz Online e-
mail newsletter at:
www.deitel.com/newsletter/subscribe.html
Computer Science
Functional Programming
Regular Expressions
Programming
ASP.NET 3.5
Adobe Flex
Ajax
Apex
ASP.NET Ajax
ASP.NET
C
C++
C++ Boost Libraries
C++ Game Programming
C#
Code Search Engines and Code Sites
Computer Game Programming
CSS 2.1
Dojo
Facebook Developer Platform
Flash 9
Functional Programming
Java
Java Certification and Assessment Testing
Java Design Patterns
Java EE 5
Java SE 6
Java SE 7 (Dolphin) Resource Center
JavaFX
JavaScript
JSON
Microsoft LINQ
Microsoft Popfly
.NET
.NET 3.0
.NET 3.5
OpenGL
Perl
PHP
Programming Projects
Python
Regular Expressions
Ruby
Ruby on Rails
Silverlight
Visual Basic
Visual C++
Visual Studio Team System
Web 3D Technologies
Web Services
Windows Presentation Foundation
XHTML
XML
Games and Game Programming
Computer Game Programming
Computer Games
Mobile Gaming
Sudoku
Internet Business
Affiliate Programs
Competitive Analysis
Facebook Social Ads
Google AdSense
Google Analytics
Google Services
Internet Advertising
Internet Business Initiative
Internet Public Relations
Link Building
Location-Based Services
Online Lead Generation
Podcasting
Search Engine Optimization
Selling Digital Content
Sitemaps
Web Analytics
Website Monetization
YouTube and AdSense
Java
Java
Java Certification and Assessment Testing
Java Design Patterns
Java EE 5
Java SE 6
Java SE 7 (Dolphin) Resource Center
JavaFX
Microsoft
ASP.NET
ASP.NET 3.5
ASP.NET Ajax
C#
DotNetNuke (DNN)
Internet Explorer 7 (IE7)
Microsoft LINQ
.NET
.NET 3.0
.NET 3.5
SharePoint
Silverlight
Visual Basic
Visual C++
Visual Studio Team System
Windows Presentation Foundation
Windows Vista
Microsoft Popfly
Open Source & LAMP Stack
Apache
DotNetNuke (DNN)
Eclipse
Firefox
Linux
MySQL
Open Source
Perl
PHP
Python
Ruby
Software
Apache
DotNetNuke (DNN)
Eclipse
Firefox
Internet Explorer 7 (IE7)
Linux
MySQL
Open Source
Search Engines
SharePoint
Skype
Web Servers
Wikis
Windows Vista
Web 2.0
Alert Services
Attention Economy
Blogging
Building Web Communities
Community Generated Content
Facebook Developer Platform
Facebook Social Ads
Google Base
Google Video
Google Web Toolkit (GWT)
Internet Video
Joost
Location-Based Services
Mashups
Microformats
Recommender Systems
RSS
Social Graph
Social Media
Social Networking
Software as a Service (SaaS)
Virtual Worlds
Web 2.0
Web 3.0
Widgets
Dive Into Web 2.0 eBook
Web 2 eBook
Other Topics
Computer Games
Computing Jobs
Gadgets and Gizmos
Ring Tones
Sudoku
Preface
"Live in fragments no longer, only connect."
—Edgar Morgan Foster
Welcome to Java and Java for Programmers! At Deitel & Associates, we write programming language professional
books and textbooks for Prentice Hall, deliver corporate training worldwide and develop Internet businesses. This book
was a joy to create. It reflects recent changes to the Java language and to the preferred ways of teaching and learning
programming.
Features
Here's some key features of Java for Programmers:
The book uses Java Standard Edition 6; we carefully audited the manuscript against the Java Language
Specification.
The book is object-oriented throughout and the treatment of OOP is clear and accessible.
The early classes and objects presentation features Time, Employee and GradeBook class case studies that weave
their way through multiple sections and chapters, gradually introducing deeper OO concepts.
The book contains a rich treatment of GUI and graphics.
We tuned our object-oriented presentation to use the latest version of the UML™ (Unified Modeling
Language™)—the UML™ 2—the industry-standard graphical language for modeling object-oriented systems.
We include an optional OOD/UML 2 automated teller machine (ATM) case study in Chapters 1–8 and 10. An
appendix contains the complete code implementation. Check out the back cover testimonials.
We include several substantial object-oriented web programming case studies.
Chapter 20, Accessing Databases with JDBC, includes JDBC 4 and uses the Java DB/Apache Derby and MySQL
database management systems. The chapter features a database-driven address book case study that
demonstrates prepared (pre-compiled) statements and JDBC 4's automatic driver discovery.
Chapter 21, JavaServer Faces™ Web Applications, and Chapter 22, Ajax-Enabled JavaServer™ Faces Web
Applications, introduce web application development with JavaServer Faces (JSF) technology and use it with the
Netbeans IDE to build web applications quickly and easily. Chapter 21 includes examples on building web
application GUIs, handling events, validating forms and session tracking. Chapter 22 discusses developing Ajax-
enabled web applications. The chapter features a database-driven multitier web address book application that
allows users to add contacts and search for contacts. The application uses Ajax-enabled JSF components to
suggest contact names while the user types a name to locate.
Chapter 23, JAX-WS Web Services, uses a tools-based approach to creating and consuming web services—a
signature Web 2.0 capability. Case studies include developing blackjack and airline reservation web services.
We use a tools-based approach for rapid web applications development; all the tools are available free for
download.
We discuss key software engineering community concepts, such as Web 2.0, Ajax, SOA, web services, open
source software, design patterns, refactoring, extreme programming, agile software development, rapid
prototyping and more.
Chapter 18, Multithreading, benefitted from the guidance of Brian Goetz and Joseph Bowbeer—co-authors of
Java Concurrency in Practice, Addison-Wesley, 2006.
We discuss the SwingWorker class for developing multithreaded user interfaces.
Description:AJAX, Rich Internet Applications and Web Development for Programmers. C++ for Programmers Google Web Toolkit (GWT). Internet Video. Joost.