Sams Teach Yourself Java In 21 Days (Covering Java 7 And Android)

Transcription

Rogers CadenheadSams Teach YourselfJava(Covers Java 7 and Android)in 21 Days800 East 96th Street, Indianapolis, Indiana 46240

Sams Teach Yourself Java in 21 Days(Covering Java 7 and Android)Copyright 2013 by Pearson Education, Inc.All rights reserved. No part of this book shall be reproduced, stored in a retrieval system,or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed withrespect to the use of the information contained herein. Although every precaution has beentaken in the preparation of this book, the publisher and author assume no responsibility forerrors or omissions. Nor is any liability assumed for damages resulting from the use of theinformation contained herein.ISBN-13: 978-0-672-33574-7ISBN-10: 0-672-33574-3Library of Congress Cataloging-in-Publication Data:Cadenhead, Rogers.Sams teach yourself Java in 21 days : covering Java 7 and Android /Rogers Cadenhead.—6th ed.p. cm.ISBN 978-0-672-33574-7 (pbk.)1. Java (Computer program language) 2. Android (Electronic resource)I. Title.QA76.73.J38C315 2013005.13'3--dc232012022262Editor-in-ChiefMark TaubAcquisitions EditorMark TaberDevelopment EditorSonglin QiuManaging EditorKristy HartProject EditorAnne GoebelCopy EditorGayle JohnsonIndexerTim WrightProofreaderChrissy White,Language Logistics,LLCTechnical EditorBoris MinkinEditorial AssistantVanessa EvansPrinted in the United States of AmericaCover DesignerAnne JonesFirst Printing August 2012CompositorNonie RatcliffTrademarksAll terms mentioned in this book that are known to be trademarks or service marks havebeen appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of anytrademark or service mark.Warning and DisclaimerEvery effort has been made to make this book as complete and as accurate as possible, butno warranty or fitness is implied. The information provided is on an “as is” basis. Theauthor and the publisher shall have neither liability nor responsibility to any person orentity with respect to any loss or damages arising from the information contained in thisbook or from the use of the programs accompanying it.Bulk SalesSams Publishing offers excellent discounts on this book when ordered in quantity for bulkpurchases or special sales. For more information, please contactU.S. Corporate and Government or sales outside of the U.S., please contactInternational Salesinternational@pearsoned.com

Contents at a GlanceIntroduction1WEEK 1: The Java Language1 Getting Started with Java92 The ABCs of Programming373 Working with Objects654 Lists, Logic, and Loops915 Creating Classes and Methods1196 Packages, Interfaces, and Other Class Features1497 Exceptions and Threads187WEEK 2: The Java Class Library8 Data Structures2219 Working with Swing24910 Building a Swing Interface27711 Arranging Components on a User Interface30512 Responding to User Input33913 Creating Java2D Graphics36714 Developing Swing Applications391WEEK 3: Java Programming15 Working with Input and Output41916 Serializing and Examining Objects44717 Communicating Across the Internet46718 Accessing Databases with JDBC 4.1 and Derby49919 Reading and Writing RSS Feeds52120 XML Web Services54521 Writing Android Apps for Java565AppendixesA Using the NetBeans Integrated Development Environment595B This Book’s Website603C Setting Up an Android Development Environment605D Using the Java Development Kit613E Programming with the Java Development Kit635Quiz Answers

Table of ContentsIntroduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .WEEK 1: The Java Language1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Getting Started with JavaThe Java Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14Attributes and Behavior. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19Behavior of a Class of ObjectsCreating a Class .17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Attributes of a Class of ObjectsRunning the Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25Organizing Classes and Class Behavior .Creating a Class Hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31PackagesInheritance in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32Q&A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34Questions .34Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35Certification Practice .Exercises .210. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Object-Oriented Programming .Inheritance910Selecting a Development ToolObjects and Classes .7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .History of the LanguageIntroduction to Java1The ABCs of ProgrammingStatements and Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38Creating Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40Variables and Data Types .

Table of ContentsVariable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Assigning Values to Variables.43. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46ConstantsComments .Literals .Number Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47Boolean Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50Character Literals .String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51Expressions and OperatorsArithmeticMore About Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5456Logical Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Operator Precedence .String Arithmetic53. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Incrementing and Decrementing .57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61Q&A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62Questions .62Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63Certification Practice .Exercises .341. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with Objects .Creating New ObjectsUsing new. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66How Objects Are Constructed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70Getting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70Setting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70Class Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73A Note on Memory ManagementUsing Class and Instance VariablesCalling Methods .Formatting Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Class Methods .75. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76Nesting Method Calls .v

viTable of ContentsReferences to Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81Casting Primitive Types.Casting Objects . . . . . . . . . . . . . . . . . . .83. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85Converting Primitive Types to Objects and Vice VersaComparing Object Values and ClassesComparing Objects .Determining the Class of an Object .Summary .Q&AQuiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88Questions .89. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90Certification Practice .Exercises .477. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Casting Objects and Primitive Types .Lists, Logic, and Loops .Arrays.91. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Array Objects .92. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93Declaring Array Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94Changing Array Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99Accessing Array Elements .Block StatementsIf Conditionals .Switch Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .For Loops101. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108The Ternary Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111While and Do LoopsWhile LoopsDo-While Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113Breaking Out of Loops .Labeled Loops .Quiz .114114. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116Questions116Summary .Q&A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117Certification PracticeExercises .

Table of Contents5Creating Classes and MethodsDefining Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Instance and Class Variables .120. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120121. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121Class Variables.Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124. . . . . . . . . . . . . . . . . . . . . . . . . . . .125. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127Variable Scope and Method DefinitionsPassing Arguments to Methods .Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130Creating Java ApplicationsHelper Classes . . . . . . . . . . . . . . . . . . . . . . . . .130. . . . . . . . . . . . . . . . . . . . . . . . . . .130Java Applications and Command-Line Arguments .Passing Arguments to Java Applications . . . . . . . . . . . . . . . . . . . . . . .131. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136Handling Arguments in Your Java Application .Creating Methods with the Same Name .Constructors .Basic Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138138. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140Overloading ConstructorsOverriding Methods .Creating Methods That Override Existing Methods .Overriding Constructors.140. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142143. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146Questions146Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148Certification PracticeExercises .6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Calling the Original Method .Quiz .137. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Calling Another Constructor .Q&A.120. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Defining Instance VariablesCreating Methods119Packages, Interfaces, and Other Class Features .Modifiers . . . . . . . . . . . . . . . . . . . .149. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Static Variables and Methods .150. . . . . . . . . . . . . . . . . . . . . . . . . .151. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .157Access Control for Methods and Variablesvii

viiiTable of ContentsFinal Classes, Methods, and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162Abstract Classes and Methods .Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165The import DeclarationClass Name ConflictsCreating Your Own Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Picking a Package Name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165Creating the Folder Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166Adding a Class to a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167Packages and Class Access Control .Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168The Problem of Single Inheritance .Interfaces and Classes .Implementing and Using Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169Implementing Multiple InterfacesOther Uses of Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170Creating and Extending InterfacesNew Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172Methods Inside InterfacesExtending Interfaces .Creating an Online Storefront . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181Q&A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183Questions183Inner ClassesSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185Certification PracticeExercises .7165Exceptions and Threads.Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188190

Table of ContentsManaging Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exception Consistency Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192195. . . . . . . . . . . . . . . . . . . . . . . . .198. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199Declaring Methods That Might Throw Exceptions .The throws Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . .200Passing on Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201throws and Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202Which Exceptions Should You Throw?. . . . . . . . . . . . . . . . . . . . . . . . . . . .203. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203Creating and Throwing Your Own ExceptionsThrowing ExceptionsCreating Your Own Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204Combining throws, try, and throw. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204When and When Not to Use Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205When to Use ExceptionsWhen Not to Use Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206Bad Style Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209Writing a Threaded ProgramA Threaded Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215Q&A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217Questions217Stopping a ThreadSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218Certification PracticeExercises .WEEK 2: The Java Class Library8191. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Protecting Code and Catching Exceptions .The finally Clause191Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221Moving Beyond Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222Java Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224Bit Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225Array Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228ix

xTable of ContentsLooping Through Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235StacksMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241Hash MapsGenerics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245Q&A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246Questions246Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247Certification PracticeExercises .9Working with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating an Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Developing a Framework .Creating a Component .254. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258259. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261Image Icons.Text Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262Text Areas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263Scrolling Panes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Check Boxes and Radio Buttons .265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271Combo Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273Q&A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274Questions274Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275Certification PracticeExercises .10251. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with ComponentsLists .250. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Adding Components to a Container .Labels249Building a Swing Interface .Swing Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .278. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283Standard Dialog BoxesUsing Dialog Boxes278

Table of ContentsSliders. . . . . . . . . . . . . . . . . . . . . . . . . . . .

Contents at a Glance Introduction 1 WEEK 1: The Java Language 1 Getting Started with Java 9 2 The ABCs of Programming 37 3 Working with Objects 65 4 Lists, Logic, and Loops 91 5 Creating Classes and Methods 119 6 Packages, Interfaces, and Other Class Features 149 7 Exceptions and Threads 187 WEEK 2: The Java Class Library 8 Data Structures 221 9 Working with Swing 249 10 Building a Swing .