ebook img

Sams teach yourself Java 2 in 21 days PDF

841 Pages·5.697 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Sams teach yourself Java 2 in 21 days

Rogers Cadenhead Laura Lemay Teach Yourself Java 2 21 in Days FOURTH EDITION 800 East 96th Street, Indianapolis, Indiana, 46240 USA Sams Teach Yourself Java 2 in 21 Days, ACQUISITIONSEDITOR Scott Meyers Fourth Edition MANAGINGEDITOR Copyright ©2004 by Sams Publishing Charlotte Clapp All rights reserved. No part of this book shall be reproduced,stored in a retrieval PROJECTEDITOR system,or transmitted by any means,electronic,mechanical,photocopying,record- Dan Knott ing,or otherwise,without written permission from the publisher. No patent liability COPYEDITOR is assumed with respect to the use of the information contained herein. Although Geneil Breeze every precaution has been taken in the preparation of this book,the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed INDEXER for damages resulting from the use of the information contained herein. Chris Barrick International Standard Book Number:0-672-32628-0 PROOFREADER Paula Lowell Library of Congress Catalog Number:2003110452 TECHNICALEDITOR Printed in the United States of America John Purdum First Printing:May 2004 PUBLISHINGCOORDINATOR 07 06 05 10 9 8 7 6 5 Vanessa Evans Trademarks MULTIMEDIADEVELOPER Dan Scherf All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy DESIGNER of this information. Use of a term in this book should not be regarded as affecting Gary Adair the validity of any trademark or service mark. PAGELAYOUT Michelle Mitchell Warning and Disclaimer Julie Parks Every effort has been made to make this book as complete and as accurate as possi- ble,but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the informa- tion contained in this book or from the use of the CD or programs accompanying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information,please contact: U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S.,please contact: International Sales [email protected] Contents at a Glance Introduction 1 WEEK 1 The Java Language 7 Day 1 Getting Started with Java 9 2 The ABCs of Programming 35 3 Working with Objects 63 4 Lists,Logic,and Loops 87 5 Creating Classes and Methods 113 6 Packages,Interfaces,and Other Class Features 143 7 Threads,Exceptions,and Assertions 181 WEEK 2 The Java Class Library 215 Day 8 Data Structures 217 9 Working with Swing 245 10 Building a Swing Interface 271 11 Arranging Components on a User Interface 299 12 Responding to User Input 329 13 Using Color,Fonts,and Graphics 351 14 Writing Java Applets and Java Web Start Applications 375 WEEK 3 Java Programming 411 Day 15 Working with Input and Output 413 16 Serializing and Examining Objects 441 17 Communicating Across the Internet 469 18 JavaSound 501 19 Creating and Using JavaBeans 521 20 Reading and Writing Data Using JDBC and XML 543 21 Writing Java Servlets and JavaServer Pages 585 Appendixes 629 Appendix A Choosing Java 631 B Using the Java Development Kit 643 C Programming with the Java Development Kit 667 D Using the NetBeans Integrated Development Environment 689 E Writing Java 1.1 Applets 699 F Creating Web Services with XML-RPC 721 G Regular Expressions 739 H Where to Go from Here:Java Resources 753 I The Book’s Web Site 759 Index 761 Contents Introduction 1 WEEK 1 The Java Language 7 DAY 1 Getting Started with Java 9 The Java Language ..............................................................................................10 History of the Language ..................................................................................10 Introduction to Java ........................................................................................11 Selecting a Development Tool ........................................................................12 Object-Oriented Programming ............................................................................13 Objects and Classes ..............................................................................................15 Object Reuse ....................................................................................................16 Attributes and Behavior ........................................................................................17 Attributes of a Class of Objects ......................................................................18 Behavior of a Class of Objects ........................................................................19 Creating a Class ..............................................................................................19 Running the Program ......................................................................................21 Organizing Classes and Class Behavior ..............................................................24 Inheritance ......................................................................................................24 Creating a Class Hierarchy ..............................................................................26 Inheritance in Action ......................................................................................28 Single and Multiple Inheritance ......................................................................29 Interfaces ........................................................................................................30 Packages ..........................................................................................................31 Summary ..............................................................................................................31 Q&A ......................................................................................................................33 Quiz ......................................................................................................................33 Questions ........................................................................................................33 Answers ..........................................................................................................34 Certification Practice ......................................................................................34 Exercises ..............................................................................................................34 DAY 2 The ABCs of Programming 35 Statements and Expressions ..................................................................................36 Variables and Data Types ......................................................................................37 Creating Variables ............................................................................................38 Naming Variables ............................................................................................39 Variable Types ................................................................................................40 Assigning Values to Variables ........................................................................42 Constants ........................................................................................................42 Comments ............................................................................................................44 Literals ..................................................................................................................45 Number Literals ..............................................................................................45 Boolean Literals ..............................................................................................46 Character Literals ............................................................................................47 String Literals ..................................................................................................47 Expressions and Operators ..................................................................................48 Arithmetic ........................................................................................................49 More About Assignment ..................................................................................51 Incrementing and Decrementing ....................................................................52 Comparisons ....................................................................................................53 Logical Operators ............................................................................................54 Operator Precedence ........................................................................................55 String Arithmetic ..................................................................................................57 Summary ..............................................................................................................58 Q&A ......................................................................................................................59 Quiz ......................................................................................................................60 Questions ........................................................................................................60 Answers ..........................................................................................................60 Certification Practice ......................................................................................61 Exercises ..............................................................................................................61 DAY 3 Working with Objects 63 Creating New Objects ..........................................................................................63 Usingnew ........................................................................................................64 WhatnewDoes ................................................................................................66 A Note on Memory Management ..................................................................66 Accessing and Setting Class and Instance Variables ............................................67 Getting Values ..................................................................................................67 Changing Values ..............................................................................................68 Class Variables ................................................................................................69 Calling Methods ....................................................................................................70 Nesting Method Calls ......................................................................................71 Class Methods ................................................................................................72 References to Objects ..........................................................................................73 Casting and Converting Objects and Primitive Types ..........................................75 Casting Primitive Types ..................................................................................76 Casting Objects ................................................................................................77 Converting Primitive Types to Objects and Vice Versa ..................................78 Comparing Object Values and Classes ................................................................80 Comparing Objects ..........................................................................................81 Determining the Class of an Object ................................................................82 Summary ..............................................................................................................83 Q&A ......................................................................................................................83 Quiz ......................................................................................................................84 Questions ........................................................................................................84 Answers ..........................................................................................................85 Certification Practice ......................................................................................85 Exercises ..............................................................................................................86 vi Sams Teach Yourself Java 2 in 21 Days DAY 4 Lists, Logic, and Loops 87 Arrays ....................................................................................................................87 Declaring Array Variables ..............................................................................88 Creating Array Objects ....................................................................................89 Accessing Array Elements ..............................................................................90 Changing Array Elements ..............................................................................91 Multidimensional Arrays ................................................................................93 Block Statements ..................................................................................................94 ifConditionals ....................................................................................................95 switchConditionals ..............................................................................................97 forLoops ............................................................................................................102 whileanddoLoops ............................................................................................105 whileLoops ..................................................................................................105 do-whileLoops ............................................................................................107 Breaking Out of Loops ......................................................................................107 Labeled Loops ..............................................................................................108 The Conditional Operator ..............................................................................109 Summary ............................................................................................................110 Q&A ....................................................................................................................110 Quiz ....................................................................................................................111 Questions ......................................................................................................111 Answers ........................................................................................................111 Certification Practice ....................................................................................111 Exercises ............................................................................................................112 DAY 5 Creating Classes and Methods 113 Defining Classes ................................................................................................114 Creating Instance and Class Variables ................................................................114 Defining Instance Variables ..........................................................................114 Class Variables ..............................................................................................115 Creating Methods ................................................................................................116 Defining Methods ..........................................................................................116 ThethisKeyword ........................................................................................118 Variable Scope and Method Definitions ......................................................119 Passing Arguments to Methods ....................................................................120 Class Methods ..............................................................................................121 Creating Java Applications ................................................................................122 Helper Classes ..............................................................................................123 Java Applications and Command-Line Arguments ............................................124 Passing Arguments to Java Applications ......................................................124 Handling Arguments in Your Java Application ............................................125 Creating Methods with the Same Name,Different Arguments ........................126 Constructor Methods ..........................................................................................130 Basic Constructor Methods ..........................................................................131 Calling Another Constructor Method ............................................................131 Overloading Constructor Methods ................................................................132 Contents vii Overriding Methods ............................................................................................134 Creating Methods That Override Existing Methods ....................................134 Calling the Original Method ........................................................................135 Overriding Constructors ................................................................................136 Finalizer Methods ..............................................................................................138 Summary ............................................................................................................139 Q&A ....................................................................................................................139 Quiz ....................................................................................................................140 Questions ......................................................................................................140 Answers ........................................................................................................141 Certification Practice ....................................................................................141 Exercises ............................................................................................................142 DAY 6 Packages, Interfaces, and Other Class Features 143 Modifiers ............................................................................................................144 Access Control for Methods and Variables ..................................................145 Static Variables and Methods ..............................................................................150 Final Classes,Methods,and Variables ..............................................................152 Variables ........................................................................................................153 Methods ........................................................................................................153 Classes ..........................................................................................................154 Abstract Classes and Methods ............................................................................154 Packages ..............................................................................................................155 Using Packages ..................................................................................................156 Full Package and Class Names ....................................................................156 TheimportDeclaration ................................................................................157 Class Name Conflicts ....................................................................................159 A Note About Classpathand Where Classes Are Located ..........................159 Creating Your Own Packages ............................................................................160 Picking a Package Name ..............................................................................160 Creating the Folder Structure ........................................................................161 Adding a Class to a Package ........................................................................161 Packages and Class Access Control ..............................................................161 Interfaces ............................................................................................................162 The Problem of Single Inheritance ..............................................................162 Interfaces and Classes ..................................................................................163 Implementing and Using Interfaces ..............................................................163 Implementing Multiple Interfaces ................................................................164 Other Uses of Interfaces ................................................................................164 Creating and Extending Interfaces ....................................................................165 New Interfaces ..............................................................................................165 Methods Inside Interfaces ............................................................................166 Extending Interfaces ......................................................................................166 Creating an Online Storefront ......................................................................167 Inner Classes ......................................................................................................173 Summary ............................................................................................................176 Q&A ....................................................................................................................176 viii Sams Teach Yourself Java 2 in 21 Days Quiz ....................................................................................................................177 Questions ......................................................................................................177 Answers ........................................................................................................177 Certification Practice ....................................................................................178 Exercises ............................................................................................................179 DAY 7 Threads, Exceptions, and Assertions 181 Exceptions ..........................................................................................................182 Exception Classes ..........................................................................................184 Managing Exceptions ........................................................................................185 Exception Consistency Checking ..................................................................185 Protecting Code and Catching Exceptions ....................................................186 ThefinallyClause ......................................................................................188 Declaring Methods That Might Throw Exceptions ............................................190 ThethrowsClause ........................................................................................191 Which Exceptions Should You Throw? ........................................................192 Passing On Exceptions ..................................................................................192 throws and Inheritance ..................................................................................194 Creating and Throwing Your Own Exceptions ..................................................194 Throwing Exceptions ....................................................................................195 Creating Your Own Exceptions ....................................................................195 Combiningthrows,try,andthrow ..............................................................196 When and When Not to Use Exceptions ............................................................197 When to Use Exceptions ..............................................................................197 When Not to Use Exceptions ........................................................................197 Bad Style Using Exceptions ..........................................................................198 Assertions ............................................................................................................199 Threads ................................................................................................................202 Writing a Threaded Program ........................................................................202 A Threaded Application ................................................................................204 Stopping a Thread ........................................................................................208 Summary ............................................................................................................209 Q&A ....................................................................................................................210 Quiz ....................................................................................................................211 Questions ......................................................................................................211 Answers ........................................................................................................211 Certification Practice ....................................................................................212 Exercises ............................................................................................................212 WEEK 2 The Java Class Library 215 DAY 8 Data Structures 217 Data Structures ....................................................................................................217 Java Data Structures ............................................................................................218 Iterator ........................................................................................................219 Bit Sets ..........................................................................................................221 Vectors ..........................................................................................................224 Looping Through Data Structures ................................................................227 Contents ix Stacks ............................................................................................................230 Map ................................................................................................................231 Hashtables ......................................................................................................233 Generics ..............................................................................................................238 Summary ............................................................................................................241 Q&A ....................................................................................................................241 Quiz ....................................................................................................................242 Questions ......................................................................................................242 Answers ........................................................................................................242 Certification Practice ....................................................................................243 Exercises ............................................................................................................244 DAY 9 Working with Swing 245 Creating an Application ......................................................................................246 Creating an Interface ....................................................................................247 Developing a Framework ..............................................................................249 Creating a Component ..................................................................................251 Adding Components to a Container ..............................................................252 Working with Components ................................................................................253 Image Icons ..................................................................................................254 Labels ............................................................................................................256 Text Fields ....................................................................................................257 Text Areas ......................................................................................................258 Scrolling Panes ..............................................................................................259 Check Boxes and Radio Buttons ..................................................................261 Combo Boxes ................................................................................................263 Lists ....................................................................................................................265 Summary ............................................................................................................266 Q&A ....................................................................................................................267 Quiz ....................................................................................................................268 Questions ......................................................................................................268 Answers ........................................................................................................269 Certification Practice ....................................................................................269 Exercises ............................................................................................................270 DAY 10 Building a Swing Interface 271 Swing Features ....................................................................................................272 Setting the Look and Feel ............................................................................272 Standard Dialog Boxes ..................................................................................275 Using Dialog Boxes ......................................................................................279 Sliders ............................................................................................................282 Scroll Panes ..................................................................................................284 Toolbars ........................................................................................................285 Progress Bars ................................................................................................288 Menus ............................................................................................................290 Tabbed Panes ................................................................................................294 Summary ............................................................................................................295 Q&A ....................................................................................................................295

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.