Table Of Content
01_588745 ffirs.qxd 3/16/05 9:31 PM Page iv
01_588745 ffirs.qxd 3/16/05 9:31 PM Page i
Beginning
Programming
with Java
™
FOR
DUMmIES
‰
2
ND EDITION
by Barry Burd
01_588745 ffirs.qxd 3/16/05 9:31 PM Page ii
Beginning Programming with Java™For Dummies®, 2nd Edition
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 permitted
under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permis-
sion 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 REP-
RESENTATIONS 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 PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE-
ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON-
TAINED 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 FUR-
THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE
INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS 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: 2005923219
ISBN-13: 978-0-7645-8874-7
ISBN-10: 0-7645-8874-5
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
2B/QU/QU/QV/IN
01_588745 ffirs.qxd 3/16/05 9:31 PM Page iii
About the Author
Dr. Barry Burdhas an M.S. in Computer Science from Rutgers University, and
a Ph.D. in Mathematics from the University of Illinois. As a teaching assistant
in Champaign-Urbana, Illinois, he was elected five times to the university-wide
List of Teachers Ranked as Excellent by their Students.
Since 1980, Dr. Burd has been a professor in the Department of Mathematics
and Computer Science at Drew University in Madison, New Jersey. When he’s
not lecturing at Drew University, Dr. Burd leads training courses for profes-
sional programmers in business and industry. He has lectured at conferences
in the United States, Europe, Australia, and Asia. He is the author of several
articles and books, including Java 2 For Dummiesand Eclipse For Dummies,
both published by Wiley Publishing, Inc.
Dr. Burd lives in Madison, New Jersey, with his wife and two children. For
hobbies he enjoys anything that wastes his and everyone else’s time.
01_588745 ffirs.qxd 3/16/05 9:31 PM Page iv
01_588745 ffirs.qxd 3/16/05 9:31 PM Page v
Dedication
For Harriet, Sam and Jennie, Sam and Ruth, Abram and Katie, Benjamin and
Jennie
Author’s Acknowledgments
Author’s To-Do List, February 13, 2005:
Item:Send chocolate to Christine Berman — the book’s project editor and
copy editor. As anyone who reads Chapter 4 learns, chocolate is one of the
most precious commodities on earth. So when I give chocolate, I give it
thoughtfully and intentionally.
The only thing that rivals chocolate’s goodness is the benefit of a good
night’s sleep. But with a 19-month-old child in the house, Christine probably
isn’t getting enough sleep. Even so, she has the time and patience to work on
my manuscript. Yes, Christine deserves special thanks.
Item:Have a plaque erected in honor of Steven Hayes, your acquisitions
editor at Wiley. While you dragged your heels, Steve kept on insisting that
you write this book. (Sure, you wanted a long vacation instead of a big book
project, but who cares? He was right; you were wrong.)
Item:Send a thank-you note to tech editor Jim Kelly who helped polish your
original work and, miraculously, didn’t make a lot of extra work for you.
Item:Recommend your agent Laura Lewin to other computer book authors.
Ifit weren’t for Laura, you’d still be roaming the book exhibits and looking
needy at the technology conferences.
Item:Visit Frank Thornton, Bonnie Averbach, and Herbert Putz at Temple
University. Thank them for steering you to a career as a professor. In any
other career, you’d have no time left to write. (And by the way, while you’re
in Philly, don’t forget to stop for a cheesesteak.)
Item:Send e-mail to Gaisi Takeuti at the University of Illinois, and to William
Wisdom and Hughes LeBlanc at Temple University. Thank them for teaching
you about Symbolic Logic. It’s made your life as a computer scientist and
mathematician much richer.
Item:Spend more time with your family. (Remind them that you’re the guy
who wandered around the house before this book project got started.) Renew
your pledge to clean up after yourself. Don’t be so highstrung, and finish each
sentence that you start. Remember that you can never fully return the love
they’ve given you, but you should always keep trying.
01_588745 ffirs.qxd 3/16/05 9:31 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:Christine Berman
Layout and Graphics:Andrea Dahl,
Acquisitions Editor: Steve Hayes Joyce Haughey, Lynsey Osborn,
Melanee Prendergast, Heather Ryan
Copy Editor:Christine Berman
Proofreaders: Leeann Harney, Jessica Kramer,
Technical Editor:Jim Kelly
Carl William Pierce, Dwight Ramsey,
Editorial Manager:Carol Sheehan TECHBOOKS Production Services
Media Development Manager:Laura VanWinkle Indexer: TECHBOOKS Production Services
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_588745 ftoc.qxd 3/16/05 9:23 PM Page vii
Contents at a Glance
Introduction.................................................................1
Part I: Revving Up........................................................7
Chapter 1: Getting Started.................................................................................................9
Chapter 2: Setting Up Your Computer...........................................................................23
Chapter 3: Running Programs.........................................................................................33
Part II: Writing Your Own Java Programs.....................45
Chapter 4: Exploring the Parts of a Program................................................................47
Chapter 5: Composing a Program..................................................................................65
Chapter 6: Using the Building Blocks: Variables, Values, and Types.........................89
Chapter 7: Numbers and Types....................................................................................103
Chapter 8: Numbers? Who Needs Numbers?..............................................................121
Part III: Controlling the Flow.....................................139
Chapter 9: Forks in the Road........................................................................................141
Chapter 10: Which Way Did He Go?.............................................................................157
Chapter 11: How to Flick a Virtual Switch...................................................................181
Chapter 12: Around and Around It Goes.....................................................................195
Chapter 13: Piles of Files: Dealing with Information Overload.................................215
Chapter 14: Creating Loops within Loops...................................................................233
Chapter 15: The Old Runaround...................................................................................245
Part IV: Using Program Units....................................269
Chapter 16: Using Loops and Arrays...........................................................................271
Chapter 17: Programming with Objects and Classes.................................................289
Chapter 18: Using Methods and Variables from a Java Class...................................303
Chapter 19: Creating New Java Methods.....................................................................325
Chapter 20: Oooey GUI Was a Worm............................................................................347
Part V: The Part of Tens ............................................359
Chapter 21: Ten Sets of Web Links...............................................................................361
Chapter 22: Ten Useful Classes in the Java API..........................................................367
Index.......................................................................371
02_588745 ftoc.qxd 3/16/05 9:23 PM Page viii