30
Mastering Java 2, J2SE 1.4 John Zukowski San Francisco London

Mastering · 2013-07-23 · D. ISCLAIMER. SYBEX makes no warranty or representation, either expressed or implied, with respect to the Software or its contents, quality, perform-ance,

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • Mastering™Java™ 2, J2SE 1.4

    John Zukowski

    San Francisco London

    4022c00.qxd 3/22/02 4:03 PM Page iii

    File AttachmentC1.jpg

  • 4022c00.qxd 3/22/02 4:03 PM Page ii

  • MasteringJava 2, J2SE 1.4

    4022c00.qxd 3/22/02 4:03 PM Page i

  • 4022c00.qxd 3/22/02 4:03 PM Page ii

  • Mastering™Java™ 2, J2SE 1.4

    John Zukowski

    San Francisco London

    4022c00.qxd 3/22/02 4:03 PM Page iii

  • Associate Publisher: Richard Mills

    Acquisitions and Developmental Editor: Diane Lowery

    Editors: Carol Henry, Pete Gaughan, Susan Berge

    Production Editor: Kylie Johnston

    Technical Editor: Robert Castaneda

    Book Designer: Maureen Forys, Happenstance Type-O-Rama

    Graphic Illustrator: Tony Jonick

    Electronic Publishing Specialist: Maureen Forys, Happenstance Type-O-Rama

    Proofreaders: Dave Nash, Nanette Duffy, Laurie O’Connell, Yariv Rabinovitch, Nancy Riddiough

    Indexer: Ted Laux

    CD Coordinator: Dan Mummert

    CD Technician: Kevin Ly

    Cover Designer: Design Site

    Cover Illustrator: Sergie Loobkoff, Design Site

    Copyright © 2002 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. The authors created reusable code inthis publication expressly for reuse by readers. Sybex grants readers limited permission to reuse the code found in this publication or its accompa-nying CD-ROM so long as authors are attributed in any application containing the reusabe code and the code itself is never distributed, postedonline by electronic transmission, sold, or commercially exploited as a stand-alone product. Aside from this specific exception concerning reusablecode, no part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photo-copy, photograph, magnetic, or other record, without the prior agreement and written permission of the publisher.

    Library of Congress Card Number: 2001096981

    ISBN: 0-7821-4022-X

    SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries.

    Mastering is a trademark of SYBEX Inc.

    Screen reproductions produced with FullShot 99. FullShot 99 © 1991–1999 Inbit Incorporated. All rights reserved.FullShot is a trademark of Inbit Incorporated.

    Netscape Communications, the Netscape Communications logo, Netscape, and Netscape Navigator are trademarks of Netscape CommunicationsCorporation.

    Netscape Communications Corporation has not authorized, sponsored, endorsed, or approved this publication and is not responsible for its con-tent. Netscape and the Netscape Communications Corporate Logos are trademarks and trade names of Netscape Communications Corporation.All other product names and/or logos are trademarks of their respective owners.

    Internet screen shot(s) using Microsoft Internet Explorer 5.5, reprinted by permission from Microsoft Corporation.

    TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following thecapitalization style used by the manufacturer.

    The author and publisher have made their best efforts to prepare this book, and the content is based upon final release software whenever possible.Portions of the manuscript may be based upon pre-release versions supplied by software manufacturer(s). The author and the publisher make norepresentation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of any kindincluding but not limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused oralleged to be caused directly or indirectly from this book.

    Manufactured in the United States of America

    10 9 8 7 6 5 4 3 2 1

    4022c00.qxd 3/22/02 4:03 PM Page iv

  • The media and/or any online materials accompanying this book thatare available now or in the future contain programs and/or text files(the “Software”) to be used in connection with the book. SYBEXhereby grants to you a license to use the Software, subject to the termsthat follow. Your purchase, acceptance, or use of the Software will con-stitute your acceptance of such terms.

    The Software compilation is the property of SYBEX unless otherwiseindicated and is protected by copyright to SYBEX or other copyrightowner(s) as indicated in the media files (the “Owner(s)”). You are herebygranted a single-user license to use the Software for your personal, non-commercial use only. You may not reproduce, sell, distribute, publish, cir-culate, or commercially exploit the Software, or any portion thereof,without the written consent of SYBEX and the specific copyrightowner(s) of any component software included on this media.

    In the event that the Software or components include specific licenserequirements or end-user agreements, statements of condition, dis-claimers, limitations or warranties (“End-User License”), those End-User Licenses supersede the terms and conditions herein as to thatparticular Software component. Your purchase, acceptance, or use of theSoftware will constitute your acceptance of such End-User Licenses.

    By purchase, use or acceptance of the Software you further agree tocomply with all export laws and regulations of the United States assuch laws and regulations may exist from time to time.

    REUSABLE CODE IN THIS BOOKThe authors created reusable code in this publication expressly for reusefor readers. Sybex grants readers permission to reuse for any purpose thecode found in this publication or its accompanying CD-ROM so longas all of the authors are attributed in any application containing thereusable code, and the code itself is never sold or commerciallyexploited as a stand-alone product.

    SOFTWARE SUPPORTComponents of the supplemental Software and any offers associatedwith them may be supported by the specific Owner(s) of that material,but they are not supported by SYBEX. Information regarding anyavailable support may be obtained from the Owner(s) using the infor-mation provided in the appropriate read.me files or listed elsewhereon the media.

    Should the manufacturer(s) or other Owner(s) cease to offer supportor decline to honor any offer, SYBEX bears no responsibility. Thisnotice concerning support for the Software is provided for your infor-mation only. SYBEX is not the agent or principal of the Owner(s),and SYBEX is in no way responsible for providing any support forthe Software, nor is it liable or responsible for any support provided, ornot provided, by the Owner(s).

    WARRANTYSYBEX warrants the enclosed media to be free of physical defects for a period of ninety (90) days after purchase. The Software is not

    available from SYBEX in any other form or media than that enclosedherein or posted to www.sybex.com. If you discover a defect in themedia during this warranty period, you may obtain a replacement ofidentical format at no charge by sending the defective media, postageprepaid, with proof of purchase to:

    SYBEX Inc.Product Support Department1151 Marina Village ParkwayAlameda, CA 94501Web: http://www.sybex.com

    After the 90-day period, you can obtain replacement media of identi-cal format by sending us the defective disk, proof of purchase, and acheck or money order for $10, payable to SYBEX.

    DISCLAIMERSYBEX makes no warranty or representation, either expressed orimplied, with respect to the Software or its contents, quality, perform-ance, merchantability, or fitness for a particular purpose. In no eventwill SYBEX, its distributors, or dealers be liable to you or any otherparty for direct, indirect, special, incidental, consequential, or otherdamages arising out of the use of or inability to use the Software orits contents even if advised of the possibility of such damage. In theevent that the Software includes an online update feature, SYBEXfurther disclaims any obligation to provide this feature for any specificduration other than the initial posting.

    The exclusion of implied warranties is not permitted by somestates. Therefore, the above exclusion may not apply to you. This war-ranty provides you with specific legal rights; there may be other rightsthat you may have that vary from state to state. The pricing of thebook with the Software by SYBEX reflects the allocation of risk andlimitations on liability contained in this agreement of Terms andConditions.

    SHAREWARE DISTRIBUTIONThis Software may contain various programs that are distributed asshareware. Copyright laws apply to both shareware and ordinary com-mercial software, and the copyright Owner(s) retains all rights. If youtry a shareware program and continue using it, you are expected to reg-ister it. Individual programs differ on details of trial periods, registra-tion, and payment. Please observe the requirements stated inappropriate files.

    COPY PROTECTIONThe Software in whole or in part may or may not be copy-protected orencrypted. However, in all cases, reselling or redistributing these fileswithout authorization is expressly forbidden except as specificallyprovided for by the Owner(s) therein.

    Software License Agreement: Terms and Conditions

    4022c00.qxd 3/22/02 4:03 PM Page v

  • ForewordJava has firmly established itself as the programming language of choice for today’s network-aware applications and has proven extremely effective as a general programming language. Java domi-nates because it provides a secure, portable, expressive programming environment and supports theseamless and invisible distribution of software across the Net.

    Over the past 20 years, I have experienced a wide variety of developer headaches that Java elimi-nates or ameliorates dramatically. For example, as the designer of the widely used ANTLR translatorgenerator, I had huge portability hassles with C/C++ (variations in libraries and the size of an inte-ger). In various jobs, I “reinvented the wheel” innumerable times for lack of a rich library or infra-structure. I wasted untold hours building and debugging code to talk to remote machines, andtracking down memory leaks in non-garbage-collected languages. For want of a remote debugginginterface, I once even had to debug a program with an LED and an oscilloscope!

    Java’s portability is legendary among server-side developers. As the main developer of thejGuru.com website application, I’ve had to shuffle the software between Linux, PC, and Mac OS Xduring various stages of development, testing, and deployment. I’ve become so used to Java’s porta-bility that I never question my ability to run the website application on whatever operating system Iwant. Lest you think that only server-side Java code will run everywhere, consider that I have alsoused the same Java-based IDE on all three platforms without trouble. That’s amazing!

    Programmers are more productive in Java than in any other common language. The mere fact thatyou don’t have to write code to track and de-allocate your own memory is a huge improvement. Javahas a simple but effective threading model built in, which makes building servers, network-centricservices, and responsive GUIs much easier. The breadth and depth of Java’s standard libraries is trulystaggering. In C, it’s pretty hard to get ahead when your standard library amounts to printf().

    Java was designed around the idea that code would float around a network to trusted anduntrusted machines, from trusted and untrusted sources. Consequently, Java includes infrastructurefor signing and validating code, checking execution rights, and even protecting you against yourself.In C/C++, for example, you can overwrite buffers that stomp other variables, causing really strangebehavior. Besides being insanely difficult to track down, these buffer overflows are the main culprit inmost security breaches. In Java, all code runs in a “sandbox” that prevents malicious code from exe-cuting privileged methods, overflowing the buffers, or generally doing anything that might jeopardizesystem integrity.

    Java allows you to send data across the WWW, as well as the code to interpret that data. BeforeJava arrived in 1995, websites could only send data to your browser. A server can now send a Javaapplet seamlessly and invisibly to your browser, where it begins to execute like any other client-sideapplication but with security constraints automatically in place. The ability to automatically install thelatest version of a piece of software on your client’s computer dramatically reduces the transactioncost of delivering and updating the software. With old technology, most large companies employarmies of system administrators to go around updating their company’s IT software. Needless to say,someone’s computer is always out-of-date, and upgrades are infrequent due to effort and cost.

    Java’s market share is still growing and is heavily supported by books, newsgroups, mailing lists, and websites such as Sun’s Java Developer Connection, IBM’s developerWorks, and

    4022c00.qxd 3/22/02 4:03 PM Page vi

  • viiFOREWORD

    jGuru.com. Most importantly, you can get the source to all the libraries and so on. Remember—the code is the documentation.

    And now to John Zukowski. If I had to vote for the person who knows the most about Java andits libraries, I’d vote for John. He also gets my vote for the man who drinks the most Coke and theman married to the nicest woman. Back in 1995, jGuru (then MageLang Institute) scoured the Java mailing lists looking to hire a guru. We consistently found that John posted insightful and well-written messages. Further, John’s activity level indicated he didn’t sleep much. He’s also a comedian. Iloved it when John showed up for our company retreat a few years back driving a sleek rented Jaguar.To this day, he swears that it was the only car they had.

    You should buy this book because John knows precisely how to teach you Java. He’s been workingwith Java since day one. He has honed his examples over the years to show you exactly what you needto know and avoid what you don’t. John’s tutorials written for jGuru were often rated most popularby our students and readers. The CD included with this book has all you need to get started buildingeverything from simple programs to web servers. So, don’t hesitate—you’ll be glad you spent themoney!

    —Terence ParrChief Scientist & Cofounder, jGuru.com

    4022c00.qxd 3/22/02 4:03 PM Page vii

  • AcknowledgmentsWrapping up a book is always an enjoyable time. Getting to the point of completion can be along and hard journey. After doing this eight times, you’d think it would get easier.

    As always, there are many people involved who helped get my originally horrible writing into thewell-formed content you hold in your hands now. At Sybex, I’d like to thank production editor KylieJohnston, acquisitions/development editor Diane Lowery, and editor Carol Henry for hanging inuntil the end. Special thanks for my technical editor Rob Castaneda of Customware for finding thetechnical problems in the examples. Kevin Ly and Dan Mummert did a great job compiling the CD.Other important contributors were compositor Maureen Forys and a crack team of proofreaders.

    Also, thanks goes out to Terence Parr for writing up the foreword. Ter seems to forget that if Ihadn’t rented the Jaguar he’d have had to pick me up at the airport at three in the morning. Also, asBrian Christeson (another former jGuru employee) will tell Ter, the front seat adjustment would onlymove forward and not back, so by the end of the week Brian had his knees up to his chin into thedashboard. And I couldn’t put the convertible down because it rained all week.

    Some people not involved in the development of the book deserve mention, too. Congratulationsgo out to Melissa and Steve Pelton for the end of their freedom and their impending bundle of joy.Thanks to Matt Groening for creating The Simpsons, for the entertainment they bring and for gettingme away from the computer. Then there is my cousin Rachel, who decided not to move back toBoston; best of luck with the future hubby, Todd. And to Heinz for creating green and purpleketchup. Why be normal?

    Of course, I can’t forget to thank my wife, Lisa, and our Old English Sheepdog, Jaeger. May hesoon have a brother.

    —John Zukowski

    4022c00.qxd 3/22/02 4:03 PM Page viii

  • Contents at a Glance

    Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

    Part 1 • Foundations of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    Chapter 1 • Introducing Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    Chapter 2 • Getting Started with the Java 2 SDK . . . . . . . . . . . . . . . . . . . . . . . . 11

    Chapter 3 • Working with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

    Chapter 4 • Datatypes, Modifiers, and Expressions . . . . . . . . . . . . . . . . . . . . . . . 43

    Chapter 5 • Packaging Class and Interface Access . . . . . . . . . . . . . . . . . . . . . . . . 71

    Chapter 6 • Arrays and Flow-Control Statements . . . . . . . . . . . . . . . . . . . . . . . . 91

    Chapter 7 • Exception Handling and Assertions . . . . . . . . . . . . . . . . . . . . . . . . 113

    Chapter 8 • Threads and Multithreading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

    Part 2 • Exploring the Java Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

    Chapter 9 • Standard Java Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

    Chapter 10 • Animation and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

    Chapter 11 • 2D Graphics and Imaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

    Chapter 12 • GUI Layout Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

    Chapter 13 • Swing GUI Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

    Chapter 14 • Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

    Chapter 15 • Advanced Swing Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417

    Chapter 16 • Transferring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

    Chapter 17 • Java Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513

    4022c00.qxd 3/22/02 4:03 PM Page ix

  • Chapter 18 • Advanced Applet Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 563

    Chapter 19 • Streams and Input/Output Programming . . . . . . . . . . . . . . . . . . . 603

    Chapter 20 • Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665

    Chapter 21 • JavaBeans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685

    Chapter 22 • Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

    Chapter 23 • Java Extension Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759

    Appendix A • Java Language Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807

    Appendix B • Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

    Glossary of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845

    Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861

    CONTENTS AT A GLANCEx

    4022c00.qxd 3/22/02 4:03 PM Page x

  • ContentsIntroduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

    Part 1 • Foundations of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    Chapter 1 • Introducing Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    Java History 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Examining the Java Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Interpreted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Secure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Architecture-Neutral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Portable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8High-Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Multithreaded. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Dynamic Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    Understanding Microsoft’s View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    Chapter 2 • Getting Started with the Java 2 SDK . . . . . . . . . . . . . . . . . . . . . . . 11

    Applets vs. Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12The Java Runtime Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Applets and Applications: What’s the Difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    Working with the Java 2 SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13SDK Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Downloading and Installing the SDK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    Creating Java Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Creating Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    Chapter 3 • Working with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

    An Introduction to OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33From Objects to Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    Designing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Abstraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Information Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    4022c00.qxd 3/22/02 4:03 PM Page xi

  • Abstraction, Take 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Association. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

    Chapter 4 • Datatypes, Modifiers, and Expressions . . . . . . . . . . . . . . . . . . . . . 43

    Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43An Example of javadoc Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

    Initializing Variables with Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Special Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

    Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59String Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

    Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Storage, Lifetime, and Behavior Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Accessibility Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

    Chapter 5 • Packaging Class and Interface Access . . . . . . . . . . . . . . . . . . . . . . 71

    Casting for Converting Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Casting between Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Casting between Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    Using this and super for Constructor and Member References . . . . . . . . . . . . . . . . . . . . . 74For Constructor References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74For Member References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

    Java’s Object Memory Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Using Java Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

    Defining an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79The implements Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Working with Inner Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

    Using Java Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86The import Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Resolving Class Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Packages and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87The package Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Compiling and Running Packaged Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

    Recommended Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

    CONTENTSxii

    4022c00.qxd 3/22/02 4:03 PM Page xii

  • Chapter 6 • Arrays and Flow-Control Statements . . . . . . . . . . . . . . . . . . . . . . 91

    Using Arrays in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Declaring and Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Copying and Cloning Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Sorting and Searching Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Using Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

    Using Flow-Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Flow-Breaking Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

    Chapter 7 • Exception Handling and Assertions. . . . . . . . . . . . . . . . . . . . . . . 113

    Exception Handling Constructs in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113The Exception Hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114An Example of Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Common Throwable Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

    Managing Methods That Throw Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Throwing Exceptions Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Creating Customized Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Chaining Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Reading a Stack Dump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Working with the finally Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Checking Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

    Using the assert Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Enabling Assertion Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Enabling Runtime Assertion Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

    Chapter 8 • Threads and Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

    Multithreading Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Creating and Running a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128The Thread-Control Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129The Thread’s Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132Thread Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133Priorities and Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134Getting Information about Threads and Thread Groups . . . . . . . . . . . . . . . . . . . . . . 135Running Periodic Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Demonstrating Priorities and Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

    Advanced Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146Thread Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146Interthread Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Thread Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Daemon Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Pooling Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

    xiiiCONTENTS

    4022c00.qxd 3/22/02 4:03 PM Page xiii

  • Part 2 • Exploring the Java Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

    Chapter 9 • Standard Java Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

    Utilitarian Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174Package java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174Package java.util and java.math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Package java.io and java.nio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Package java.text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Package java.beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176Security Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

    Multimedia Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176Package java.awt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Package javax.swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Package java.awt.print and javax.print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Package javax.sound. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

    Distributed Computing Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Package java.net. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Package javax.xml, org.w3c.dom, and org.xml.sax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Package java.sql and javax.sql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Package java.rmi, javax.rmi, and org.omg. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Package javax.naming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

    Chapter 10 • Animation and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

    Graphics Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Creating the Base Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Drawing Lines and Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186Drawing Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Drawing Ovals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Drawing Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191Managing State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

    Animation Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Working with Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Using MediaTracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Doing Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210Buffering Drawing Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Scaling Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Cropping Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

    Playing Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Hints for Faster Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

    Chapter 11 • 2D Graphics and Imaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

    Graphics Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Moving to Graphics2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Antialiased Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234Drawing Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

    CONTENTSxiv

    4022c00.qxd 3/22/02 4:03 PM Page xiv

  • Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243Compositing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

    Text Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249Extended Font Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249Advanced Text Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

    Image Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Flexible Image Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Extended Imaging Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256Image Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

    Graphics Device Hookups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Graphics Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260Graphics Display Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261Full-Screen Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

    Chapter 12 • GUI Layout Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

    Java’s GUI Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Designing GUIs with Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

    The Container and Component Client Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270The FlowLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276The BorderLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277The CardLayout Manager and JTabbedPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281JSplitPane. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284The GridLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285The BoxLayout Manager and Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287The SpringLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292The GridBagLayout Manager and GridBagConstraints . . . . . . . . . . . . . . . . . . . . . . . 296

    Layout Manager Internals: Interface LayoutManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304

    Chapter 13 • Swing GUI Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

    Component: The Superclass of AWT GUI Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305Event-Handling Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311Methods for Moving and Resizing Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313Methods for Position and Geometry Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313Graphics and Rendering Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314Layout Manager Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314Methods for Focus Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315Methods for Painting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315Methods for Changing and Querying State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316Image-Related Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Input Method Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Pop-up Menu Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Component Peer Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317And the Rest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

    JComponent: The Superclass of Swing GUI Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Adding Swing Components to Your GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

    Adding Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

    xvCONTENTS

    4022c00.qxd 3/22/02 4:03 PM Page xv

  • Sharing AbstractButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331Working with Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337Grouping with JPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338Adding Labels to Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Adding Toggle Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341Adding Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343Adding Radio Buttons into Button Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Adding Combo Box (Pull-Down) Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346Adding Scroll Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348Adding Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349Providing Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351Adding Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353Hiding Password Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355Placing Text Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356Displaying HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Spinning Input Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359Formatting Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360Dragging Toolbars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Working with Internal Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363Setting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Supporting Assistive Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366Constructing New Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

    Using Menu System Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368Adding Menu Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368Adding Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368Adding Menu Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369Adding Pop-Up Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370A Program to Construct a Menu Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

    Chapter 14 • Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

    Delegation-Based Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375Event Types and Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378

    Action Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380Adjustment Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383Ancestor Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385Component Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387Container Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389Focus Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391Hyperlink Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Internal Frame Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395Item Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397Key Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400Menu Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406Mouse Wheel Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409Pop-up Menu Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Window Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

    CONTENTSxvi

    4022c00.qxd 3/22/02 4:03 PM Page xvi

  • Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416

    Chapter 15 • Advanced Swing Capabilities. . . . . . . . . . . . . . . . . . . . . . . . . . . 417

    Advanced Swing Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417Managing Listener Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417Abstracting Action Listening . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419Registering Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422Interacting with the Focus Subsystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424Working with Multiple Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

    Creating User Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430Message Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431Color Choosers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438File Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441

    Model/View/Controller Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450Using ButtonModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451Understanding Button Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452Pluggable Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

    Complex Swing Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454Lists and Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480

    Client Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491

    Chapter 16 • Transferring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

    Transferable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493Understanding Data Flavors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494

    Transferring with the Clipboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496Transferring Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497Transferring Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502

    Drag and Drop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

    Chapter 17 • Java Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513

    The Java 2 Collections Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513Vectors, Stacks, and Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514Dictionaries, Hash Tables, and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522Working with Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528Bit Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531Collections and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537Lists and ListIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542Synchronization and Readability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545Algorithms and Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547

    xviiCONTENTS

    4022c00.qxd 3/22/02 4:03 PM Page xvii

  • Using Java’s Math Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549Working with the java.lang.Math Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549Working with the java.math Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551Formatting Numerical Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554

    Dates and Calendars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556Working with the Date Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556Working with the Calendar Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557Formatting Date Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560

    Chapter 18 • Advanced Applet Programming. . . . . . . . . . . . . . . . . . . . . . . . . 563

    Purely GUI-Driven Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563Example: A Stack-Based Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563

    Deploying Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586Working with the Java Plug-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587Jumping Off with Java Web Start. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589

    Optimizing Starting Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596Keeping Executables Small. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596Minimizing the Number of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597Making Use of Java Archive (JAR) Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597Being Quick on Your Feet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598Obfuscating Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

    Chapter 19 • Streams and Input/Output Programming. . . . . . . . . . . . . . . . . 603

    Java’s File-Management Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604Java’s File Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604Java’s RandomAccessFile Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608

    Java’s I/O Stream-Manipulation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614Input Streams and Readers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614Tokenizing Input Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635Output Streams and Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635

    Object Persistence and Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644Criteria for Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644Creating Output Streams for Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645Using Object Input Streams for De-serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . 646Serialization Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648Writing and Reading an Object Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648A Serialization Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649transient vs. ObjectStreamField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653XML Encoding and Decoding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653

    The New I/O Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656NIO Buffers, Channels, and Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659

    Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664

    CONTENTSxviii

    4022c00.qxd 3/22/02 4:03 PM Page xviii

  • Chapter 20 • Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665

    History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665Overview of Java 1.4 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668

    Simple Printing Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673Showing a Printer Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674Working with Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676

    Using StreamPrintService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680Printing Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684

    Chapter 21 • JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685

    The JavaBeans Component Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685The Java Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686

    Bean Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687JavaBean Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687Bean Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690Bean Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693Bean Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693

    Inspecting and Customizing Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693The Bean Introspection Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694Bean Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

    The Beans Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695Installing the BDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696Using the BDK BeanBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697Creating a New Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702

    Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

    Chapter 22 • Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

    Java’s Networking Protocols: TCP/IP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713IP: The Heart of Internet Data Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . 713TCP: For Guaranteed Delivery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714

    Internet Addressing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717Looking Up a Textual Address. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718Getting a Numeric Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719

    Communicating with Remote Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720Low-Level Communication Using UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720Connecting to Servers Using TCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723Connecting to an SMTP Mail Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725Connecting to an HTTP Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729Fetching Other Web Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732Using Password Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738

    Reading from Socket Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741Nonblocking Reads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743Writing Server Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748

    A Simple Server Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750The Client for the Sample Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751

    Nonblocking Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753Moving On… a Word About Factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755

    xixCONTENTS

    4022c00.qxd 3/22/02 4:03 PM Page xix

  • Chapter 23 • Java Extension Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759

    Java 2 Platform, Standard Edition (J2SE). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759Java Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764Java Database Connectivity (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767Remote Method Invocation (RMI), CORBA, and Java IDL . . . . . . . . . . . . . . . . . . . 772An RMI Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774Java Native Interface (JNI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785

    Java 2 Platform, Enterprise Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786JavaServer Pages (JSP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794JavaMail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796Java Naming and Directory Interface (JNDI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799Java Message Service (JMS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800Enterprise JavaBeans (EJB). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802

    Java Communications (COMM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802Moving On… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805

    Appendix A • Java Language Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807

    Applet Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823Swing Component Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825Command-Line Tool Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

    Appendix B • Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

    Finding Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840Forums. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841Mailing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841Newsgroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841General Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843

    User Groups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844Product Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844

    Glossary of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845

    Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861

    CONTENTSxx

    4022c00.qxd 3/22/02 4:03 PM Page xx

  • IntroductionWelcome to Mastering Java 2, J2SE 1.4, the book that provides comprehensive coverage ofthe latest version of the Java platform. This book takes you through introductory, intermediate, andadvanced topics to lead you on your way to becoming a proficient Java programmer.

    A Road MapIs this book for you? Although it’s designed with a logical sequence in mind, most readers will notpick it up and read it from cover to cover. Depending on your particular background, the followingsummaries will help you figure out how to make the book suit your individual needs.

    For Non-C/C++/C# Programmers

    If you’re new to Java and the C/C++/C# style of programming, you’ll probably need to workthrough the whole book. You’ll find some concepts will be similar to those of other languages, but inorder to get a grasp on how Java does things, you should work through each of the examples yourself.You might want to take a break of a few hours or days between chapters to make sure you understandthe material you’ve covered.

    For C/C++/C# Programmers

    Due to the similarities between C/C++/C# and Java, you C/C++/C# programmers can probablybreeze through several of this book’s chapters. C programmers will be able to skim through most ofChapters 4 and 6; C++ programmers can skim those and Chapter 3 as well, plus the sections onclasses in Chapter 5, and Chapter 7 if you have experience with exception handling. Certainly thesechapters are worth reviewing—for the assertion facility in Chapter 7, for instance—but they do not demand the same attention as is warranted for the completely new material in the remainder ofthe book.

    For Java Programmers

    If you have played with Java on your own and decided it was time to get a book to help you out,most of Part 1 will probably be review for you. Browse through it to see if there’s anything you mighthave missed in your prior travels. And pay special attention to Chapters 7 and 8, which cover excep-tion handling and multithreading, respectively. Topics such as inner classes in Chapter 5, assertions inChapter 7, and timer threads and thread pooling in Chapter 8 are all worthy of at least a review.

    For Everyone

    Once you have your bearings, use the table of contents to find the areas that interest you most. Chap-ters 10 and 11 together describe animation and graphics programming within Java. Chapters 12, 13,14, and 15 go together; they focus on building and using rich Java clients. (Some people might find ithelpful to read Chapter 13 before Chapter 12.) Chapter 16, on drag and drop, builds somewhat onthe other Java client chapters (at least). If you’re particularly interested in network programming (the

    4022c00.qxd 3/22/02 4:03 PM Page xxi

  • subject of Chapter 22), you need to have a grasp of I/O first (covered in Chapter 19), since network-ing is anchored on input and output streams. Printing (Chapter 20), too, requires some knowledge ofI/O and graphics. You can read the other chapters in almost any order.

    The examples given in each chapter will clarify the concepts explained there, and reviewing thecode will give you a better understanding of the topic. All the source code is on the enclosed CD, soyou don’t need to type in the examples yourself.

    Features and Structure of This BookThe goal of this book is simple: to make you productive with Java as quickly as possible. You’ll find agreat deal of information here; use the table of contents in the front and/or the extensive index inthe back to locate the information you need. Following are brief descriptions of what’s in the bookand where you can find it.

    Part 1: Foundations of Java

    The first part of the book introduces you to Java—its history, the language itself, and the programmingconcepts. Chapter 1 starts off with a lesson on what Java is and where it came from. Chapter 2 gets youstarted using the Java development environment. In Chapter 3, you learn about object-oriented pro-gramming basics. Chapter 4 describes the Java language grammar. In Chapter 5, you start to build upyour understanding of Java by learning about classes, interfaces, and packages. Chapter 6 explains howJava deals with arrays and flow control. In Chapter 7, you learn about Java’s exception-handling mech-anisms. Finally, in Chapter 8, you learn how to create multithreaded programs in Java.

    Part 2: Exploring the Java Libraries

    The second part of the book is an examination of the Java libraries (or packages in Java-speak). Start with the brief overview in Chapter 9 of the various Java packages and their parts before you move into more depth. Chapter 10 describes the basic animation capabilities of Java, and Chapter 11 coversthe more advanced 2D capabilities. Chapter 12 describes the windowing package and how you canposition objects on the screen. In Chapter 13, you learn about some of the objects a user interactswith, and Chapter 14 explains how to deal with those interactions through events. In Chapter 15, youdiscover some of the advanced objects users can interact with. Chapter 16 explores the mechanismsavailable to transfer information between applications. Data structure support classes for dealing withcollections are explained in Chapter 17, along with other utilitarian libraries. In Chapter 18, tech-niques for deploying Java programs are explored. Chapter 19 introduces you to I/O programmingthrough Java streams. And Chapter 20 explores how to print from within your Java programs.

    You’ll find more specialized topics toward the end of the book. Chapter 21 discusses the JavaBeansAPI and how it stretches your software development budget. Chapter 22 teaches you how to makeyour programs Internet-savvy.

    The final chapter, Chapter 23, provides a “where to go from here” look at other Java APIs. There’sonly so much you can fit into one book, so this last chapter introduces you to some advanced JavaAPIs. It also presents APIs that are a part of the Java 2 Enterprise Edition (J2EE) rather than theJ2SE that we do explore here in this book.

    INTRODUCTIONxxii

    4022c00.qxd 3/22/02 4:03 PM Page xxii

  • What’s on the CD?On the book’s CD you’ll find a variety of goodies:

    ◆ All the source code from the examples in the book.

    ◆ The 1.4 version of the Software Development Kit (SDK) from Sun Microsystems, suitablefor Windows, Solaris, and Linux. This is the released version, not an early access version.

    ◆ A stimulating collection of tools for you to play with. This includes the award-winning Inte-grated Development Environment (IDE) JBuilder 6 Personal Edition (with a 30-day trial forthe Enterprise Edition); a trial edition of the Optimizeit Suite to help you monitor threadperformance; the open source tools Tomcat, Ant, and JUnit; and the Beans DevelopmentKit (BDK).

    ◆ The final piece of the CD is a trial edition of a tool called JIndent. You can use this to refor-mat (pretty-print) your source code after you’re done with it. See the Readme file on the CDfor additional details.

    ConventionsThis book uses various conventions to help you find the information you need quickly. Tips, Notes,and Warnings, described just below, are placed throughout the book to help you locate importanthighlights quickly.

    Tip This is a tip. Tips contain helpful hints and information to make you more productive with Java faster.

    Note This is a note. Notes contain extra information related to the discussion at hand.

    Warning This is a warning. Warnings contain information that flags potential trouble spots.

    In addition, the book takes advantage of various font styles. Bold font in text indicates somethingthat the user types (in a text field, for instance). A monospaced program font is used for programcode and Java elements, and you’ll find boldface used there, too, for emphasis.

    The program code itself follows the standard conventions for capitalization used by the Java API.For example, in class names each word is capitalized; and in function names each word but the firstone is capitalized. The code formatting follows standard programming conventions: a left brace isplaced at the end of a line (or the start of the next line), the right brace is on its own line, and inden-tation is used to highlight the grouping of code.

    You’ll find class composition tables spread throughout the book, listing and describing the mem-bers of the various classes. In these tables, an underlined name means the member is static. A name initalic means the member is protected. To find out what static and protected mean, you’ll need to readChapter 4.

    Java Technical SupportWhen you need help, there are several sources available for technical support. See Appendix B forlists of FAQs, forums, mailing lists, newsgroups, user groups, and more.

    xxiiiINTRODUCTION

    4022c00.qxd 3/22/02 4:03 PM Page xxiii

  • About SybexSybex offers many books at all levels of expertise. For instance, if you’re interested in moving on toWeb services after this, consider getting SOAP Programming with Java (Sybex, 2002). Or if you encountera problem that is not Java related, another Sybex offering may provide the solution. For the latest cat-alog, contact

    Sybex Inc.1151 Marina Village ParkwayAlameda, CA 94501

    You can also visit the Sybex Web page at http://www.sybex.com, where you will find a searchablecatalog.

    Now that you’ve seen what’s in store for you inside Mastering Java 2, J2SE 1.4, move onward andenjoy it.

    INTRODUCTIONxxiv

    4022c00.qxd 3/22/02 4:03 PM Page xxiv

  • Part 1Foundations

    of JavaIn this section:◆ Chapter 1: Introducing Java◆ Chapter 2: Getting Started with the Java 2

    Software Development Kit◆ Chapter 3: Working with Java Objects◆ Chapter 4: Datatypes, Modifiers, and Expressions◆ Chapter 5: Java Classes, Interfaces, and Packages◆ Chapter 6: Arrays and Flow-Control Statements◆ Chapter 7: Exception Handling and Assertions◆ Chapter 8: Threads and Multithreading

    4022c01.qxd 3/22/02 4:03 PM Page 1

  • 4022c01.qxd 3/22/02 4:03 PM Page 2

  • Chapter 1

    Introducing JavaOnce upon a time there was an island in Indonesia known by the name of Java. The people onthe island lived a peaceful existence, going about their business and their daily routines for thou-sands of years. One day in the early 1990s, a company by the name of Sun Microsystems createda new programming language called Oak. Since Oak was internal to Sun, the name didn’t mattermuch, and the people on Java continued to go on their merry way, ignorant of what was in storefor them.

    A few years later, Sun was ready to make Oak a real product. Unfortunately, Oak’s name didn’t pass the trademark test. Some other choices, like Silk, Ruby, and WRL (for WebRunnerLanguage), didn’t cut the mustard, either, but eventually the name Java won out and made its way through a legal review. (Apparently, having conflicting names for a programming languageand an island doesn’t matter. What can you expect from a company named after a star?) Then on May 23, 1995, Java was launched at Sun’s annual SunWorld conference, as the 12th of about20 different announcements. Little did those Indonesians know what was going to happen ontheir little island.

    Java, the platform, began to evolve, and Microsoft thought it important enough to incorpo-rate into their Internet Explorer browser. Back on the island of Java, the volcanoes were gettingrestless. Then in 1997, Sun sued Microsoft over some Java incompatibilities, and Krakatoa justblew. (Okay, so Java has about 10 major volcanic eruptions each year; it may have just been acoincidence.) As the lawsuit progressed further and further, life on the island got bad. Economicunrest in Jakarta created calls for President Suharto to step down, and East Timor demandedpolitical autonomy. Near bloody battles ensued on the Sun Java front, with an eventual $20 mil-lion dollar settlement with Microsoft in 2001. Coincidence? Just ask then President Wahid whathe has to say.

    4022c01.qxd 3/22/02 4:03 PM Page 3

  • Java History 101Okay, enough of the convoluted history of Java the island and Java the platform. And yes, that is Java,the platform. What began in 1995 as just another programming language is now formally known as aplatform. Beginning as a platform-neutral, Internet-friendly development language, Java has evolvedinto a means of creating programs for just about anything. Let’s step back to the beginning and seehow it came about.

    Back in January 1991, a project by the name of Green began. The Green project’s purpose was tocome up with a way of controlling set-top boxes, those smart cable TV access boxes. Because thehardware in the consumer devices was always changing, it became apparent to James Gosling, the“father” of Java, that C++ wasn’t appropriate for the job. As a result, the language called Oak wascreated; it was less susceptible to bugs and wouldn’t crash the whole system. It initially ran on aHammer technology device called *7 (Star7). Cavorting across the touch screen was this little digitalcharacter named Duke, a helper agent, who would go off and do various tasks. (He later turned intothe Java mascot.)

    Note If you’ve never seen Duke before, he looks like a cross between a Star Trek communicator and an upside-down toothwith a witch’s hat, with a big red nose and Screen Bean–style arms. (Screen Beans are those clip art characters you frequentlysee running all over the place in PowerPoint presentations.)

    Oak was used to bid on a project for a TV set–top operating system. The bid failed, and Sunrefused a purchase offer from Trip Hawkins (then CEO of Gamemaker/3DO), so Sun had all thisleftover stuff it didn’t know what to do with. Thankfully, the Internet revolution had begun with therelease of Mosaic, the predecessor to the Netscape Navigator Web browser. Oak was repurposed in1994 for the Web, and a browser called WebRunner (later renamed HotJava) was created to show offthe newfound technology of what eventually became Java.

    By now it was time to take Java public. At the now infamous SunWorld conference, Sun’s JohnGage and Netscape’s Marc Andreesen announced Java to the world. Netscape committed to incorpo-rate Java into the next release of their browser, and by the end of the year companies such as IBM,Oracle, Borland, Adobe, Macromedia, Lotus, Spyglass, and Intuit had incorporated Java into theirproducts. Even Microsoft licensed Java, committing themselves to incorporating Java into their prod-ucts, operating systems, and development tools.

    This initial version of Java was so small it fit on a floppy. The core interpreter was about 100KB.The math library added 20KB. The code to support integration with C libraries was just 50KB. Themajority of the class libraries fit in 375KB, leaving just the platform-specific graphics libraries thatvaried in size. Overall, Java was just around a megabyte and could run in a machine with 1MB ofROM and 1MB of RAM. (*7 had 4MB, though.)

    Jump ahead six years, and Java has evolved considerably. No longer confined to run in the browser,you’ll find Java programs everywhere—from Web servers, to jewelry with flashing lights, to smartertoasters, and even in the originally planned set-top boxes. The core libraries have grown considerably,too, with the latest release (Java 2 Standard Edition, version 1.4) occupying 30 to 40MB just for itsruntime environment. Sure, Java can do more now, and that is essentially why you have the Java Plat-form, which is the combination of the programming language, the standard libraries, and the run-time environment. And, in this book, it’s that platform you’ll learn about.

    Chapter 1 INTRODUCING JAVA4

    4022c01.qxd 3/22/02 4:03 PM Page 4