Table Of Content01_58961X ffirs.qxd 3/29/05 3:24 PM Page iv
01_58961X ffirs.qxd 3/29/05 3:24 PM Page i
Java
™
ALL-IN-ONE DESK REFERENCE
FOR
DUMmIES
‰
by Doug Lowe
01_58961X ffirs.qxd 3/29/05 3:24 PM Page ii
Java™All-in-One Desk Reference For Dummies®
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2005 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-
ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing,
Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://
www.wiley.com/go/permissions.
Trademarks:Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the
Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade
dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United
States and other countries, and may not be used without written permission. Java is a trademark of Sun
Microsystems, Inc. All other trademarks are the property of their respective owners. Wiley Publishing,
Inc., is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS
OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND
SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PAR-
TICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE
ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD
WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR
OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT
PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR
DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK
AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR
THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMEN-
DATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK
MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2005923064
ISBN-13: 978-0-7645-8961-4
ISBN-10: 0-7645-8961-X
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1O/RU/QU/QV/IN
01_58961X ffirs.qxd 3/29/05 3:24 PM Page iii
About the Author
Doug Lowehas been writing computer programming books since the guys who
invented Java were still in high school. He’s written books on COBOL, Fortran,
Visual Basic, for IBM mainframe computers, mid-range systems, PCs, Web pro-
gramming, and probably a few he’s forgotten about. He’s the author of more
than 30 For Dummiesbooks, such as Networking For Dummies (7th Edition),
Networking For Dummies All-in-One Desk Reference, PowerPoint 2003 For
Dummies, and Internet Explorer 6 For Dummies. He lives in that sunny All-
American City Fresno, California, where the motto is, “It’s a sunny, All-American
City,” with his wife and the youngest of his three daughters. He’s also one of
those obsessive-compulsive decorating nuts who puts up tens of thousands of
lights at Christmas and creates computer-controlled Halloween decorations
that rival Disney’s Haunted Mansion. Maybe his next book should be Tacky
Holiday Decorations For Dummies.
01_58961X ffirs.qxd 3/29/05 3:24 PM Page iv
01_58961X ffirs.qxd 3/29/05 3:24 PM Page v
Dedication
To Debbie, Rebecca, Sarah, and Bethany.
Author’s Acknowledgments
I’d like to thank project editor Kim Darosett, who did a great job of managing
all the editorial work that was required to put this book together in spite of a
short schedule and oft-missed deadlines, and acquisitions editor Katie Feltman
who made the whole project possible. I’d also like to thank John Purdum who
gave the entire manuscript a thorough technical review, tested every line of
code, and offered many excellent suggestions, as well as copy editor Rebecca
Senninger who made sure the i’s were crossed and the t’s were dotted (oops,
reverse that!). And, as always, thanks to all the behind-the-scenes people who
chipped in with help I’m not even aware of.
01_58961X ffirs.qxd 3/29/05 3:24 PM Page vi
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments through our online registration form located
at www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Composition Services
Media Development
Project Coordinator:Maridee Ennis
Project Editor:Kim Darosett
Layout and Graphics: Andrea Dahl,
Acquisitions Editor:Katie Feltman Lauren Goddard, Stephanie D. Jumper,
Melanee Prendergast, Heather Ryan,
Copy Editor:Rebecca Senninger
Julie Trippetti
Technical Editor:John Purdum
Proofreaders: John Greenough, Leeann Harney,
Editorial Manager:Leah Cameron Jessica Kramer, Arielle Mennelle,
Media Development Manager: Carl Pierce
Laura VanWinkle Indexer:Ty Koontz
Media Development Supervisor:
Richard Graves
Editorial Assistant:Amanda Foxworth
Cartoons:Rich Tennant (www.the5thwave.com)
Publishing and Editorial for Technology Dummies
Richard Swadley,Vice President and Executive Group Publisher
Andy Cummings,Vice President and Publisher
Mary Bednarek,Executive Acquisitions Director
Mary C. Corder,Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele,Vice President and Publisher
Joyce Pepple,Acquisitions Director
Composition Services
Gerry Fahey,Vice President of Production Services
Debbie Stailey,Director of Composition Services
02_58961X ftoc.qxd 3/29/05 3:24 PM Page vii
Contents at a Glance
Introduction.................................................................1
Book I: Java Basics.......................................................7
Chapter 1: Welcome to Java..............................................................................................9
Chapter 2: Installing and Using Java Tools...................................................................21
Chapter 3: Working with TextPad...................................................................................35
Chapter 4: Using Eclipse..................................................................................................43
Book II: Programming Basics.......................................63
Chapter 1: Java Programming Basics.............................................................................65
Chapter 2: Working with Variables and Data Types.....................................................83
Chapter 3: Working with Numbers and Expressions.................................................113
Chapter 4: Making Choices............................................................................................141
Chapter 5: Going Around in Circles (Or, Using Loops)..............................................161
Chapter 6: Pulling a Switcheroo...................................................................................187
Chapter 7: Adding Some Methods to Your Madness.................................................199
Chapter 8: Handling Exceptions...................................................................................217
Book III: Object-Oriented Programming......................235
Chapter 1: Understanding Object-Oriented Programming........................................237
Chapter 2: Making Your Own Classes..........................................................................249
Chapter 3: Working with Statics...................................................................................265
Chapter 4: Using Subclasses and Inheritance.............................................................273
Chapter 5: Using Abstract Classes and Interfaces.....................................................293
Chapter 6: Using the Object and Class Classes..........................................................305
Chapter 7: Using Inner Classes.....................................................................................329
Chapter 8: Packaging and Documenting Your Classes..............................................339
Book IV: Strings, Arrays, and Collections....................353
Chapter 1: Working with Strings...................................................................................355
Chapter 2: Using Arrays.................................................................................................371
Chapter 3: Using the ArrayList Class...........................................................................397
Chapter 4: Using the LinkedList Class.........................................................................409
Chapter 5: Creating Generic Collection Classes.........................................................419
02_58961X ftoc.qxd 3/29/05 3:24 PM Page viii
Book V: Programming Techniques ..............................431
Chapter 1: Programming Threads................................................................................433
Chapter 2: Network Programming................................................................................453
Chapter 3: Using Regular Expressions.........................................................................475
Chapter 4: Using Recursion...........................................................................................491
Book VI: Swing.........................................................505
Chapter 1: Swinging into Swing....................................................................................507
Chapter 2: Handling Events...........................................................................................521
Chapter 3: Getting Input from the User.......................................................................537
Chapter 4: Choosing from a List...................................................................................563
Chapter 5: Using Layout Managers..............................................................................585
Book VII: Web Programming......................................603
Chapter 1: Creating Applets..........................................................................................605
Chapter 2: Creating Servlets.........................................................................................613
Chapter 3: Using Java Server Pages.............................................................................633
Chapter 4: Using JavaBeans..........................................................................................647
Book VIII: Files and Databases..................................663
Chapter 1: Working with Files.......................................................................................665
Chapter 2: Using File Streams.......................................................................................679
Chapter 3: Database for $100, Please...........................................................................703
Chapter 4: Using JDBC to Connect to a Database......................................................717
Chapter 5: Working with XML.......................................................................................733
Book IX: Fun and Games...........................................751
Chapter 1: Fun with Fonts and Colors.........................................................................753
Chapter 2: Drawing Shapes...........................................................................................767
Chapter 3: Using Images and Sound.............................................................................789
Chapter 4: Animation and Game Programming..........................................................803
Index.......................................................................821