intTypePromotion=1
ADSENSE

Java for dummies 5th edition

Chia sẻ: Phung Tuyet | Ngày: | Loại File: PDF | Số trang:435

135
lượt xem
45
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Explores how the new version of Java offers more robust functionality and new features such as closures to keep Java competitive with more syntax-friendly languages like Python and Ruby Covers object-oriented programming basics with Java, code reuse, the essentials of creating a Java program using the new JDK 7, creating basic Java objects, and new Eclipse features A companion web site offers all code from the book and bonus chapters Written by a Java trainer, Java For Dummies, 5th Edition will enable even novice programmers to start creating Java applications quickly and easily....

Chủ đề:
Lưu

Nội dung Text: Java for dummies 5th edition

  1. Programming Languages/Java ™ g Easier! 5th Edition Making Everythin Jumpin’ Java! The bestselling 5th Edition Java beginner’s book is now Open the book and find: ava fully updated for Java 7! J • Definitions of the many terms ® Java, the object-oriented programming language that works you’ll encounter on almost any computer, is what powers many of those cool • The grammar of Java multimedia applications. Thousands have learned Java Java • How to save time by reusing code programming from previous editions of this book — now it’s your turn! Whether you’re new to programming or already • All about if, for, switch, and know a little Visual Basic or C++, you’ll be doing Java in a jiffy. while statements • An overview of object-oriented • The Java scoop — get an overview of Java, the enhancements in programming Java 7, and the software tools you need • Hints about handling exceptions • Building blocks — learn to work with Java classes and methods and add comments ® • How to write Java applets • Get loopy — understand the value of variables and learn to control • Ten ways to avoid mistakes program flow with loops or decision-making statements • Class it up — explore classes and objects, constructors, and subclasses, and see how to reuse your code • A click ahead — experiment with variables and methods, use arrays and collections to juggle values, and create programs that respond to mouse clicks Learn to: • Combine several smaller programs to Go to Dummies.com® create a bigger program Visit the companion website at www.dummies.com/go/ for videos, step-by-step examples, javafordummies5e for lots of code samples that you can how-to articles, or to shop! • Work with new libraries, closure, parallel frameworks, and other new features use in your Java programs • Create basic Java objects and reuse code • Handle exceptions and events $29.99 US / $35.99 CN / £21.99 UK ISBN 978-0-470-37173-2 Barry Burd, PhD, is a professor of mathematics and computer science at Drew University. He frequently contributes to various online technology resources, including JavaBoutique.com, and is the author of Ruby On Rails Barry Burd, PhD For Dummies and the previous edition of this book. Author of Beginning Programming with Java For Dummies Burd www.it-ebooks.info
  2. Get More and Do More at Dummies.com® Start with FREE Cheat Sheets Cheat Sheets include • Checklists • Charts ile Apps • Common Instructions Mob • And Other Good Stuff! To access the Cheat Sheet created specifically for this book, go to www.dummies.com/cheatsheet/java Get Smart at Dummies.com Dummies.com makes your life easier with 1,000s of answers on everything from removing wallpaper to using the latest version of Windows. Check out our • Videos • Illustrated Articles There’s a Dummies App for This and That • Step-by-Step Instructions Plus, each month you can win valuable prizes by entering our Dummies.com sweepstakes. * With more than 200 million books in print and over 1,600 unique titles, Dummies is a global leader in how-to information. Now Want a weekly dose of Dummies? Sign up for Newsletters on you can get the same great Dummies information in an App. With • Digital Photography topics such as Wine, Spanish, Digital Photography, Certification, • Microsoft Windows & Office and more, you’ll have instant access to the topics you need to • Personal Finance & Investing know in a format you can trust. • Health & Wellness • Computing, iPods & Cell Phones To get information on all our Dummies apps, visit the following: • eBay • Internet www.Dummies.com/go/mobile from your computer. • Food, Home & Garden www.Dummies.com/go/iphone/apps from your phone. Find out “HOW” at Dummies.com *Sweepstakes not currently available in all countries; visit Dummies.com for official rules. www.it-ebooks.info
  3. Java ® FOR DUMmIES ‰ 5TH EDITION www.it-ebooks.info 01_9780470371732-ffirs.indd i 7/6/11 6:57 PM
  4. www.it-ebooks.info 01_9780470371732-ffirs.indd 01_9780470371732-ffirs.indd ii 7/6/11 6:57 PM
  5. Java ® FOR DUMmIES ‰ 5TH EDITION by Barry Burd www.it-ebooks.info 01_9780470371732-ffirs.indd iii 7/6/11 6:57 PM
  6. Java® For Dummies®, 5th Edition Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright © 2011 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 Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, 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, Making Everything Easier, 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 registered trademark of Oracle America, 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 WITH- OUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR 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 FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR- MATION 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 877-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 and by print-on-demand. Not all content that is available in standard print versions of this book may appear or be packaged in all book formats. If you have purchased a version of this book that did not include media that is referenced by or accom- panies a standard print version, you may request this media by visiting http://booksupport.wiley. com. For more information about Wiley products, visit us www.wiley.com. Library of Congress Control Number: 2011932274 ISBN: 978-0-470-37173-2 (pbk); ISBN: 978-1-118-12830-5 (ebk); ISBN: 978-1-118-12831-2 (ebk); ISBN: 978-1-118-12832-9 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 www.it-ebooks.info 01_9780470371732-ffirs.indd iv 7/6/11 6:57 PM
  7. About the Author Barry Burd received an M.S. degree in Computer Science at Rutgers University and a Ph.D. in Mathematics at 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 Android Application Development All-in-One For Dummies and Beginning Programming with Java For Dummies, both from Wiley Publishing, Inc. Dr. Burd lives in Madison, New Jersey, with his wife and two children. In his spare time, he enjoys being a workaholic. www.it-ebooks.info 01_9780470371732-ffirs.indd v 7/6/11 6:57 PM
  8. www.it-ebooks.info 01_9780470371732-ffirs.indd 01_9780470371732-ffirs.indd vi 7/6/11 6:57 PM
  9. Dedication for Jennie, Sam, and Harriet, Jennie and Benjamin, Katie and Abram, and Basheva www.it-ebooks.info 01_9780470371732-ffirs.indd vii 7/6/11 6:57 PM
  10. www.it-ebooks.info 01_9780470371732-ffirs.indd 01_9780470371732-ffirs.indd viii 7/6/11 6:57 PM
  11. Author’s Acknowledgments When asked to list his talents, Siddhartha replied “I can think. I can wait. I can fast.” Waiting is one of the three most important virtues. With this in mind, I thank Mary Bednarek, Andy Cummings, Katie Feltman, Paul Levesque, Virginia Sanders, and Brian Walls for their boundless patience during the creation of this 5th edition. www.it-ebooks.info 01_9780470371732-ffirs.indd ix 7/6/11 6:57 PM
  12. Publisher’s Acknowledgments We’re proud of this book; please send us your comments at http://dummies.custhelp.com. For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974, out- side the U.S. at 317-572-3993, or fax 317-572-4002. Some of the people who helped bring this book to market include the following: Acquisitions, Editorial, and Media Composition Services Development Project Coordinator: Sheree Montgomery Senior Project Editor: Paul Levesque Layout and Graphics: Stephanie Jumper, Acquisitions Editor: Katie Feltman Corrie Socolovitch, Laura Westhuis Copy Editors: Brian Walls and Virginia Sanders Proofreader: Toni Settle Technical Editor: John Mueller Indexer: Potomac Indexing, LLC Editorial Manager: Leah Cameron Media Development Project Manager: Laura Moss-Hollister Media Development Assistant Project Manager: Jenny Swisher Media Development Associate Producers: Josh Frank, Marilyn Hummel, Douglas Kuhn, and Shawn Patrick Editorial Assistant: Amanda Graham Sr. Editorial Assistant: Cherie Case 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 Kathy Nebenhaus, Vice President and Executive Publisher Composition Services Debbie Stailey, Director of Composition Services www.it-ebooks.info 01_9780470371732-ffirs.indd x 7/6/11 6:57 PM
  13. Contents at a Glance Introduction ................................................................ 1 Part I: Getting Started ................................................. 9 Chapter 1: All about Java ................................................................................................ 11 Chapter 2: All about Software ........................................................................................ 23 Chapter 3: Using the Basic Building Blocks.................................................................. 39 Part II: Writing Your Own Java Programs .................... 61 Chapter 4: Making the Most of Variables and Their Values ....................................... 63 Chapter 5: Controlling Program Flow with Decision-Making Statements ................. 93 Chapter 6: Controlling Program Flow with Loops ..................................................... 123 Part III: Working with the Big Picture: Object-Oriented Programming ................................... 137 Chapter 7: Thinking in Terms of Classes and Objects .............................................. 139 Chapter 8: Saving Time and Money: Reusing Existing Code .................................... 167 Chapter 9: Constructing New Objects ......................................................................... 195 Part IV: Savvy Java Techniques ................................ 217 Chapter 10: Putting Variables and Methods Where They Belong ........................... 219 Chapter 11: Using Arrays and Collections to Juggle Values ..................................... 249 Chapter 12: Looking Good When Things Take Unexpected Turns.......................... 281 Chapter 13: Sharing Names among the Parts of a Java Program ............................. 311 Chapter 14: Responding to Keystrokes and Mouse Clicks ....................................... 333 Chapter 15: Writing Java Applets ................................................................................ 351 Chapter 16: Using Java Database Connectivity .......................................................... 363 Part V: The Part of Tens ........................................... 373 Chapter 17: Ten Ways to Avoid Mistakes ................................................................... 375 Chapter 18: Ten Websites for Java .............................................................................. 381 Index ...................................................................... 383 www.it-ebooks.info 02_9780470371732-ftoc.indd xi 7/6/11 6:57 PM
  14. www.it-ebooks.info 02_9780470371732-ftoc.indd 02_9780470371732-ftoc.indd xii 7/6/11 6:57 PM
  15. Table of Contents Introduction ................................................................. 1 How to Use This Book ..................................................................................... 1 Conventions Used in This Book ..................................................................... 2 What You Don’t Have to Read........................................................................ 2 Foolish Assumptions ....................................................................................... 3 How This Book Is Organized .......................................................................... 4 Part I: Getting Started ............................................................................ 4 Part II: Writing Your Own Java Programs ........................................... 4 Part III: Working with the Big Picture: Object-Oriented Programming ...................................................................................... 5 Part IV: Savvy Java Techniques ........................................................... 5 Part V: The Part of Tens ........................................................................ 5 Icons Used in This Book ................................................................................. 6 Where to Go from Here ................................................................................... 7 Part I: Getting Started .................................................. 9 Chapter 1: All about Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 What You Can Do with Java ......................................................................... 12 Why You Should Use Java ............................................................................ 13 Getting Perspective: Where Java Fits In ..................................................... 14 Object-Oriented Programming (OOP)......................................................... 16 Object-oriented languages .................................................................. 16 Objects and their classes .................................................................... 18 What’s so good about an object-oriented language? ........................... 18 Refining your understanding of classes and objects....................... 21 What’s Next?................................................................................................... 22 Chapter 2: All about Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 Quick-Start Instructions ................................................................................ 23 What You Install on Your Computer ........................................................... 25 What is a compiler? ............................................................................. 26 What is a Java virtual machine? ......................................................... 28 Developing Software ............................................................................ 33 What is an Integrated Development Environment? ......................... 35 www.it-ebooks.info 02_9780470371732-ftoc.indd xiii 7/6/11 6:57 PM
  16. xiv Java For Dummies, 5th Edition Chapter 3: Using the Basic Building Blocks . . . . . . . . . . . . . . . . . . . . . .39 Speaking the Java Language ......................................................................... 39 The grammar and the common names ............................................. 40 The words in a Java program ............................................................. 41 Checking Out Java Code for the First Time ................................................ 43 Understanding a Simple Java Program ....................................................... 44 The Java class ...................................................................................... 44 The Java method .................................................................................. 45 The main method in a program ......................................................... 47 How you finally tell the computer to do something ........................ 49 Curly braces ......................................................................................... 51 And Now, a Few Comments .......................................................................... 53 Adding comments to your code......................................................... 54 What’s Barry’s excuse? ....................................................................... 58 Using comments to experiment with your code .............................. 58 Part II: Writing Your Own Java Programs ..................... 61 Chapter 4: Making the Most of Variables and Their Values . . . . . . . .63 Varying a Variable ......................................................................................... 63 Assignment Statements ................................................................................ 65 Understanding the Types of Values That Variables May Have ............... 67 Displaying Text .............................................................................................. 70 Numbers without Decimal Points ................................................................ 70 Combining Declarations and Initializing Variables............................................72 The Atoms: Java’s Primitive Types ............................................................. 73 The char type ....................................................................................... 74 The boolean type ................................................................................. 76 The Molecules and Compounds: Reference Types ................................... 77 An Import Declaration .................................................................................. 81 Creating New Values by Applying Operators ............................................ 83 Initialize once, assign often ................................................................ 85 The increment and decrement operators ........................................ 86 Assignment operators ......................................................................... 91 Chapter 5: Controlling Program Flow with Decision-Making Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Making Decisions (Java if Statements) ....................................................... 94 Guess the number ................................................................................ 94 She controlled keystrokes from the keyboard ................................. 95 Creating randomness .......................................................................... 97 The if statement ................................................................................... 98 The double equal sign ......................................................................... 99 Brace yourself ...................................................................................... 99 Indenting if statements in your code............................................... 100 Elseless in Ifrica.................................................................................. 101 www.it-ebooks.info 02_9780470371732-ftoc.indd xiv 7/6/11 6:57 PM
  17. xv Table of Contents Forming Conditions with Comparisons and Logical Operators ............ 102 Comparing numbers; comparing characters ................................. 102 Comparing objects............................................................................. 103 Importing everything in one fell swoop .......................................... 106 Java’s logical operators .................................................................... 106 Vive les nuls! ....................................................................................... 109 (Conditions in parentheses) ............................................................. 111 Building a Nest ............................................................................................. 112 Choosing among Many Alternatives (Java switch Statements)............. 114 Your basic switch statement ............................................................ 115 To break or not to break ................................................................... 118 Along comes Java 7............................................................................ 120 Chapter 6: Controlling Program Flow with Loops . . . . . . . . . . . . . . . .123 Repeating Instructions Over and Over Again (Java while Statements) .......................................................................... 124 Repeating a Certain Number of Times (Java for Statements)................ 127 The anatomy of a for statement ....................................................... 128 The world premiere of “Al’s All Wet” .............................................. 129 Repeating Until You Get What You Want (Java do Statements) ...............131 Reading a single character ............................................................... 134 File handling in Java .......................................................................... 135 Variable declarations and blocks .................................................... 136 Part III: Working with the Big Picture: Object-Oriented Programming ................................... 137 Chapter 7: Thinking in Terms of Classes and Objects. . . . . . . . . . . . .139 Defining a Class (What It Means to Be an Account) ................................ 140 A public class ..................................................................................... 142 Declaring variables and creating objects ....................................... 142 Initializing a variable ......................................................................... 145 Using an object’s fields ..................................................................... 145 One program; several classes .......................................................... 146 Defining a Method within a Class (Displaying an Account) ................... 146 An account that displays itself ......................................................... 147 The display method’s header........................................................... 148 Sending Values to and from Methods (Calculating Interest) ................. 149 Passing a value to a method ............................................................. 152 Returning a value from the getInterest method............................. 155 Making Numbers Look Good ...................................................................... 156 Hiding Details with Accessor Methods (Why You Shouldn’t Micromanage a Bank Teller)................................................................... 160 Good programming............................................................................ 160 Public lives and private dreams: Making a field inaccessible ...... 163 Enforcing rules with accessor methods.......................................... 165 www.it-ebooks.info 02_9780470371732-ftoc.indd xv 7/6/11 6:57 PM
  18. xvi Java For Dummies, 5th Edition Chapter 8: Saving Time and Money: Reusing Existing Code . . . . . . .167 Defining a Class (What It Means to Be an Employee) ............................. 168 The last word on employees ............................................................ 168 Putting your class to good use ......................................................... 170 Cutting a check................................................................................... 171 Working with Disk Files (A Brief Detour) ................................................. 172 Storing data in a file ........................................................................... 173 Copying and pasting code ................................................................ 173 Reading from a file ............................................................................. 174 Who moved my file? .......................................................................... 177 Adding directory names to your filenames .................................... 177 Reading a line at a time ..................................................................... 178 Defining Subclasses (What It Means to Be a Full-Time or Part-Time Employee) ............................................................................... 180 Creating a subclass ............................................................................ 182 Creating subclasses is habit-forming .............................................. 184 Using Subclasses ......................................................................................... 185 Making types match .......................................................................... 187 The second half of the story............................................................. 188 Overriding Existing Methods (Changing the Payments for Some of Your Employees)....................................................................... 189 A Java annotation .............................................................................. 191 Using methods from classes and subclasses ................................. 192 Chapter 9: Constructing New Objects . . . . . . . . . . . . . . . . . . . . . . . . . .195 Defining Constructors (What It Means to Be a Temperature) ............... 196 What is a temperature? ..................................................................... 196 What is a temperature scale? (Java’s enum type) ......................... 197 Okay, so then what is a temperature? ............................................. 197 What you can do with a temperature.............................................. 199 Calling new Temperature(32.0): A case study ............................... 201 Some things never change ................................................................ 205 More Subclasses (Doing Something about the Weather)....................... 206 Building better temperatures ........................................................... 206 Constructors for subclasses............................................................. 208 Using all this stuff .............................................................................. 209 The default constructor .................................................................... 210 A Constructor That Does More.................................................................. 211 Classes and methods from the Java API ......................................... 214 The SuppressWarnings annotation ................................................. 215 www.it-ebooks.info 02_9780470371732-ftoc.indd xvi 7/6/11 6:57 PM
  19. xvii Table of Contents Part IV: Savvy Java Techniques ................................. 217 Chapter 10: Putting Variables and Methods Where They Belong. . . . .219 Defining a Class (What It Means to Be a Baseball Player) ...................... 219 Another way to beautify your numbers .......................................... 220 Using the Player class ....................................................................... 221 Nine, count ’em, nine ......................................................................... 223 Don’t get all GUI on me...................................................................... 224 Tossing an exception from method to method.............................. 225 Making Static (Finding the Team Average) .............................................. 226 Why is there so much static? ........................................................... 228 Meet the static initializer .................................................................. 229 Displaying the overall team average ............................................... 230 Static is old hat................................................................................... 232 Could cause static; handle with care .............................................. 233 Experiments with Variables ....................................................................... 234 Putting a variable in its place ........................................................... 235 Telling a variable where to go .......................................................... 237 Passing Parameters ..................................................................................... 240 Pass by value ...................................................................................... 240 Returning a result .............................................................................. 242 Pass by reference ............................................................................... 243 Returning an object from a method ................................................ 245 Epilogue............................................................................................... 247 Chapter 11: Using Arrays and Collections to Juggle Values . . . . . . .249 Getting Your Ducks All in a Row ................................................................ 249 Creating an array in two easy steps ................................................ 251 Storing values ..................................................................................... 252 Tab stops and other special things ................................................. 255 Using an array initializer ................................................................... 255 Stepping through an array with the enhanced for loop ............... 256 Searching ............................................................................................ 258 Arrays of Objects ......................................................................................... 261 Using the Room class ........................................................................ 263 Yet another way to beautify your numbers ................................... 266 The conditional operator .................................................................. 267 Command Line Arguments ......................................................................... 267 Using command line arguments in a Java program ...................... 269 Checking for the right number of command line arguments ....... 271 www.it-ebooks.info 02_9780470371732-ftoc.indd xvii 7/6/11 6:57 PM
  20. xviii Java For Dummies, 5th Edition Using Java Collections ................................................................................ 272 Collection classes to the rescue ...................................................... 273 Using an ArrayList ............................................................................. 274 Using generics (hot stuff!)................................................................. 277 Testing for the presence of more data ............................................ 278 Chapter 12: Looking Good When Things Take Unexpected Turns . . . .281 Handling Exceptions ................................................................................... 282 The parameter in a catch clause...................................................... 286 Exception types.................................................................................. 287 Who’s going to catch the exception? .............................................. 289 Java 7 and the multi-catch clause .................................................... 295 Throwing caution to the wind .......................................................... 296 Doing useful things ............................................................................ 297 Our friends, the good exceptions .................................................... 298 Handle an Exception or Pass the Buck ..................................................... 299 Finishing the Job with a finally Clause ...................................................... 304 Close Those Files! ........................................................................................ 306 How to close a file .............................................................................. 307 A try statement with resources ....................................................... 307 Chapter 13: Sharing Names among the Parts of a Java Program . . . .311 Access Modifiers.......................................................................................... 312 Classes, Access, and Multipart Programs ................................................ 313 Members versus classes ................................................................... 313 Access modifiers for members......................................................... 314 Putting a drawing on a frame ........................................................... 316 Directory structure ............................................................................ 319 Making a frame ................................................................................... 320 Sneaking Away from the Original Code .................................................... 321 Default access..................................................................................... 323 Crawling back into the package ....................................................... 326 Protected Access ......................................................................................... 326 Putting non-subclasses in the same package ................................. 328 Access Modifiers for Java Classes ............................................................. 330 Public classes ..................................................................................... 330 Nonpublic classes .............................................................................. 331 Chapter 14: Responding to Keystrokes and Mouse Clicks . . . . . . . . .333 Go On . . . Click That Button ....................................................................... 333 Events and event handling ............................................................... 336 The Java interface .............................................................................. 336 Threads of execution......................................................................... 338 The keyword this ............................................................................... 339 Inside the actionPerformed method ............................................... 340 The serialVersionUID ........................................................................ 341 Responding to Things Other Than Button Clicks.................................... 341 Creating Inner Classes ................................................................................ 347 www.it-ebooks.info 02_9780470371732-ftoc.indd xviii 7/6/11 6:57 PM
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2