Wiley Java All In One Desk Reference For Dummies Apr 2005 ISBN 076458961X pdf

  

by Doug Lowe

Java

  

A L L - I N - O N E D E S K R E F E R E N C E

  

FOR

DUMmIES

  ‰

  ™ ® 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,

http://

  Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at 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

  About the Author

Doug Lowe has 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 Dummies books, 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.

  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.

  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 Media Development

  Project Editor: Kim Darosett Acquisitions Editor: Katie Feltman Copy Editor: Rebecca Senninger Technical Editor: John Purdum Editorial Manager: Leah Cameron Media Development Manager:

  Laura VanWinkle Media Development Supervisor:

  Richard Graves Editorial Assistant: Amanda Foxworth Cartoons: Rich Tennant (www.the5thwave.com)

  Composition Services Project Coordinator: Maridee Ennis Layout and Graphics: Andrea Dahl,

  Lauren Goddard, Stephanie D. Jumper, Melanee Prendergast, Heather Ryan, Julie Trippetti

  Proofreaders: John Greenough, Leeann Harney, Jessica Kramer, Arielle Mennelle, Carl Pierce

  Indexer: Ty Koontz 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

  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

  

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

  Table of Contents

Introduction..................................................................1

  About This Book...............................................................................................2 How to Use This Book .....................................................................................3 How This Book Is Organized...........................................................................3

  Book I: Java Basics .................................................................................3 Book II: Programming Basics ................................................................3 Book III: Object-Oriented Programming ..............................................4 Book IV: Strings, Arrays, and Collections............................................4 Book V: Programming Techniques .......................................................4 Book VI: Swing ........................................................................................4 Book VII: Web Programming .................................................................4 Book VIII: File and Database Programming .........................................4 Book IX: Fun and Games ........................................................................5 This book’s Web site ..............................................................................5

  Icons Used in This Book..................................................................................5 Where to Go from Here....................................................................................6

  

Book I: Java Basics .......................................................7

  Chapter 1: Welcome to Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 What Is Java, and Why Is It So Great?............................................................9 Platform independence .......................................................................10 Object orientation ................................................................................11 The Java API..........................................................................................12 The Internet...........................................................................................12 Comparing Java to Other Languages...........................................................13 Important Features of the Java Language ...................................................15 Type checking.......................................................................................15 Automatic memory management.......................................................17 Exception handling ..............................................................................17 On the Downside: Java’s Weaknesses .........................................................18 Java Version Insanity .....................................................................................19 What’s in a Name? ..........................................................................................20 Chapter 2: Installing and Using Java Tools . . . . . . . . . . . . . . . . . . . . . .21 Downloading and Installing the Java Development Kit.............................21 Downloading the JDK...........................................................................22 Installing the JDK..................................................................................23 Perusing the JDK folders .....................................................................23

  Java All-in-One Desk Reference For Dummies x

  Using Java’s Command-Line Tools...............................................................25 Compiling a program ...........................................................................26 Compiling more than one file..............................................................26 Using Java compiler options...............................................................27 Running a Java program ......................................................................29 Using the javap command...................................................................31 Other Java command-line tools..........................................................32

  Using Java Documentation............................................................................32 JS2E API Docs........................................................................................33 Java Language Specification ...............................................................34

  Chapter 3: Working with TextPad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Downloading and Installing TextPad ...........................................................35 Editing Source Files........................................................................................36 Compiling a Program .....................................................................................38 Running a Java Program................................................................................40 Running an Applet..........................................................................................41 Chapter 4: Using Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 Getting Some Perspective on Eclipse ..........................................................44 Understanding Projects.................................................................................46 Creating a Simple Project ..............................................................................47 Adding a Class File .........................................................................................52 Running a Program ........................................................................................56 Debugging a Java Program............................................................................57 Stepping through your programs .......................................................57 Examining variables .............................................................................59 Setting breakpoints ..............................................................................60 Refactoring Your Code...................................................................................61 Book II: Programming Basics .......................................63 Chapter 1: Java Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Looking At the Infamous Hello, World! Program ........................................65 Dealing with Keywords..................................................................................68 Working with Statements ..............................................................................70 Types of statements.............................................................................71 White space...........................................................................................71 Working with Blocks ......................................................................................72 Creating Identifiers ........................................................................................73 Crafting Comments ........................................................................................74 End-of-line comments ..........................................................................74 Traditional comments..........................................................................75 JavaDoc comments ..............................................................................76

  Table of Contents xi

  Introducing Object-Oriented Programming ................................................76 Understanding classes and objects ...................................................76 Understanding static methods ...........................................................76 Creating an object from a class ..........................................................77 A program that uses an object ...........................................................78 So what’s the difference?.....................................................................80

  Importing Java API Classes ...........................................................................81

  

Chapter 2: Working with Variables and Data Types . . . . . . . . . . . . . . .83

Declaring Variables ........................................................................................83 Declaring two or more variables in one statement..........................84 Declaring class variables.....................................................................84 Declaring instance variables...............................................................85 Declaring local variables .....................................................................86 Initializing Variables.......................................................................................88 Initializing variables with assignment statements ...........................88 Initializing variables with initializers .................................................89 Using Final Variables (Or Constants)...........................................................89 Working with Primitive Data Types .............................................................90 Integer types .........................................................................................91 Floating-point types .............................................................................93 The char type........................................................................................94 The boolean type..................................................................................95 Wrapper classes ...................................................................................96 Using Reference Types ..................................................................................96 Working with Strings......................................................................................98 Declaring and initializing strings........................................................98 Combining strings ................................................................................99 Converting primitives to strings ........................................................99 Converting strings to primitives ......................................................100 Converting and Casting Numeric Data ......................................................101 Automatic conversions......................................................................101 Type casting ........................................................................................102 Understanding Scope...................................................................................102 Shadowing Variables....................................................................................104 Printing Data with System.out....................................................................105 Standard input and output streams .................................................105 Using System.out and System.err.....................................................107 Getting Input with the Scanner Class ........................................................107 Importing the Scanner class .............................................................108 Declaring and creating a Scanner object.........................................109 Getting input .......................................................................................109 Getting Input with the JOptionPane Class ................................................111

  Java All-in-One Desk Reference For Dummies xii

  Chapter 3: Working with Numbers and Expressions . . . . . . . . . . . . .113 Working with Arithmetic Operators ..........................................................113 Dividing Integers ..........................................................................................116 Combining Operators ..................................................................................118 Using the Unary Plus and Minus Operators .............................................119 Using Increment and Decrement Operators .............................................120 Using the Assignment Operator .................................................................122 Using Compound Assignment Operators .................................................123 Using the Math Class ...................................................................................124 Constants of the Math class..............................................................125 Mathematical functions .....................................................................126 Creating random numbers ................................................................129 Rounding functions ............................................................................131 Formatting Numbers....................................................................................133 Weird Things about Java Math ...................................................................136 Integer overflow..................................................................................136 Floating-point weirdness ...................................................................137 Dividing by zero..................................................................................138 Chapter 4: Making Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 Using Simple Boolean Expressions ............................................................141 Using If Statements ......................................................................................144 Simple if statements...........................................................................144

  if-else statements................................................................................146 Nested if statements ..........................................................................147 else-if statements................................................................................151

  Mr. Spock’s Favorite Operators (The Logical Ones, of Course).............153 Using the ! operator ...........................................................................153 Using the & and && operators..........................................................154 Using the | and || operators ............................................................155 Using the ^ operator ..........................................................................156 Combining logical operators.............................................................157

  Using the Conditional Operator .................................................................159 Comparing Strings........................................................................................159

  Chapter 5: Going Around in Circles (Or, Using Loops) . . . . . . . . . . . .161 Your Basic while Loop .................................................................................162 The while statement ..........................................................................162 A counting loop ..................................................................................162 Breaking Out of a Loop................................................................................163 Looping Forever ...........................................................................................164 Letting the user decide when to quit...............................................165 Another way to let the user decide..................................................166 Using the continue Statement ....................................................................167

  do-while Loops .............................................................................................168 Validating Input from the User ...................................................................170

  Table of Contents xiii

  The Famous for Loop...................................................................................173 The formal format of the for loop ....................................................173 Scoping out the counter variable .....................................................176 Counting even numbers ....................................................................177 Counting backwards ..........................................................................177 for loops without bodies ...................................................................178 Ganging up your expressions ...........................................................179 Omitting expressions .........................................................................181 Breaking and continuing your for loops..........................................181

  Nesting Your Loops......................................................................................182 A simple nested for loop ...................................................................182 A guessing game .................................................................................183

  

Chapter 6: Pulling a Switcheroo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187

  else-if Monstrosities.....................................................................................187 A Better Version of the Voter Machine Error Decoder Program ............189 Using the switch Statement ........................................................................190 A Boring Business Example Complete with Flowchart ...........................191 Putting if Statements Inside switch Statements .......................................193 Creating Character Cases............................................................................194 Falling through the Cracks..........................................................................195

  

Chapter 7: Adding Some Methods to Your Madness . . . . . . . . . . . . . .199

The Joy of Methods .....................................................................................199 The Basics of Making Methods...................................................................200 An example..........................................................................................201 Another example ................................................................................202 Methods That Return Values ......................................................................204 Declaring the method’s return type.................................................205 Using the return statement to return the value..............................205 Using a method that returns a type .................................................206 You gotta have a proper return statement......................................206 Another version of the guessing game program ............................208 Using Methods That Take Parameters ......................................................211 Declaring parameters ........................................................................211 Scoping out parameters ....................................................................212 Understanding pass-by-value............................................................213 Yet another example of the guessing game program.....................214

Chapter 8: Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217

Understanding Exceptions..........................................................................217 Witnessing an exception....................................................................219 Finding the culprit..............................................................................219 Catching Exceptions ....................................................................................220 A simple example ...............................................................................221 Another example ................................................................................222

  Java All-in-One Desk Reference For Dummies xiv

  Handling Exceptions with a Pre-emptive Strike .......................................223 Catching All Exceptions at Once ................................................................225 Displaying the Exception Message ............................................................226 Using a finally Block.....................................................................................227 Handling Checked Exceptions ....................................................................229

  The catch-or-throw compiler error..................................................229 Catching FileNotFoundException.....................................................230 Throwing the FileNotFoundException.............................................231 Throwing an exception from main ...................................................232 Swallowing exceptions.......................................................................232

  Throwing Your Own Exceptions.................................................................233

  Book III: Object-Oriented Programming ......................235

  Chapter 1: Understanding Object-Oriented Programming . . . . . . . . .237 What Is Object-Oriented Programming? ...................................................237 Understanding Objects................................................................................238 Objects have identity.........................................................................239 Objects have type...............................................................................240 Objects have state..............................................................................240 Objects have behavior.......................................................................241 The Life Cycle of an Object .........................................................................242 Working with Related Classes.....................................................................243 Inheritance ..........................................................................................243 Interfaces.............................................................................................244 Designing a Program with Objects.............................................................244 Diagramming Classes with UML.................................................................245 Drawing classes ..................................................................................246 Drawing arrows...................................................................................248 Chapter 2: Making Your Own Classes . . . . . . . . . . . . . . . . . . . . . . . . . .249 Declaring a Class ..........................................................................................249 Picking class names ...........................................................................250 What goes in the class body .............................................................250 Where classes go ................................................................................251 Working with Members ...............................................................................253 Fields ....................................................................................................253 Methods...............................................................................................253 Understanding visibility ....................................................................254 Getters and Setters ......................................................................................254 Overloading Methods ..................................................................................257 Creating Constructors .................................................................................258 Basic constructors .............................................................................258 Default constructors ..........................................................................259 Calling other constructors ................................................................260

  Table of Contents xv

  

Chapter 3: Working with Statics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265

Understanding Static Fields and Methods ...............................................265 Working with Static Fields...........................................................................266 Using Static Methods ...................................................................................267 Counting Instances ......................................................................................268 Preventing Instances ...................................................................................271 Using Static Initializers ................................................................................271

Chapter 4: Using Subclasses and Inheritance . . . . . . . . . . . . . . . . . . .273

Introducing Inheritance...............................................................................273 Plains, trains, and automobiles ........................................................274 Playing games .....................................................................................275 A businesslike example .....................................................................276 Inheritance hierarchies......................................................................276 Creating Subclasses .....................................................................................277 Overriding Methods.....................................................................................278 Protecting Your Members ...........................................................................279 Using this and super in Your Subclasses ..................................................280 Inheritance and Constructors ....................................................................281 Using final......................................................................................................283 Final methods .....................................................................................283 Final classes ........................................................................................283 Casting Up and Down ..................................................................................284 Determining an Object’s Type ....................................................................286 Poly What? ....................................................................................................287 Creating Custom Exceptions ......................................................................289 The Throwable hierarchy..................................................................289 Creating an exception class ..............................................................290 Throwing a custom exception ..........................................................291

Chapter 5: Using Abstract Classes and Interfaces . . . . . . . . . . . . . . .293

Using Abstract Classes ................................................................................293 Using Interfaces............................................................................................296 Creating a basic interface..................................................................296 Implementing an interface ................................................................297 Using an interface as a type ..............................................................298 More Things You Can Do with Interfaces..................................................299 Adding fields to an interface .............................................................299 Extending interfaces ..........................................................................299 Using interfaces for callbacks...........................................................300

Chapter 6: Using the Object and Class Classes . . . . . . . . . . . . . . . . . .305

The Mother of All Classes: Object..............................................................305 Every object is an Object ..................................................................305 Using Object as a type .......................................................................306 Methods of the Object class .............................................................307

  Java All-in-One Desk Reference For Dummies xvi

  The toString Method....................................................................................309 Using toString .....................................................................................309 Overriding toString ............................................................................310

  The equals Method ......................................................................................311 Using equals ........................................................................................312 Overriding the equals method..........................................................313

  The clone Method ........................................................................................316 Implementing the clone method ......................................................317 Using clone to create a shallow copy ..............................................320 Creating deep copies .........................................................................321

  The Class Class.............................................................................................327

  Chapter 7: Using Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 Declaring Inner Classes ...............................................................................329 Understanding inner classes.............................................................330 An example..........................................................................................330 Using Static Inner Classes ...........................................................................333 Using Anonymous Inner Classes ................................................................334 Creating an anonymous class ...........................................................335 Tick Tock with an anonymous class ................................................336 Chapter 8: Packaging and Documenting Your Classes . . . . . . . . . . .339 Working with Packages................................................................................339 Importing classes and packages.......................................................339 Creating your own packages.............................................................340 An example..........................................................................................342 Putting Your Classes in a JAR File ..............................................................343

  jar command-line options .................................................................344 Archiving a package ...........................................................................345 Adding a jar to your classpath .........................................................346 Running a program directly from an archive..................................346

  Using JavaDoc to Document Your Classes................................................347 Adding JavaDoc comments...............................................................347 Using the javadoc command.............................................................350 Viewing JavaDoc pages......................................................................351

  Book IV: Strings, Arrays, and Collections ....................353

  Chapter 1: Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355 Reviewing Strings .........................................................................................355 Using the String Class ..................................................................................357 Finding the length of a string ............................................................359 Making simple string modifications.................................................360 Extracting characters from a string .................................................360 Extracting substrings from a string .................................................361

  Table of Contents xvii

  Splitting up a string ............................................................................363 Replacing parts of a string ................................................................365

  Using the StringBuilder and StringBuffer Classes....................................365 Creating a StringBuilder object ........................................................366 Using StringBuilder methods ............................................................367 A StringBuilder example....................................................................369