M urach m u rach's p ro g ra m m in g 4™ EDITION JOEL MURACH A CORE JAVA BOOK that teaches you the Java essentials at a professional pace, using the time-saving features ofNetBeans AN OBJECT-ORIENTED BOOK that shows you how ro use business classes, inheritance, and interfaces the way they’re used in the real world A GUI PROGRAMMING BOOK that show's you how to develop graphical user interfaces that handle events and validate data A DATA ACCESS BOOK that shows you how to develop 3-tier, object-oriented, database applications the way the pros do it TRAINING & REFERENCE murach's programming 4th EDITION Joel Murach M ike M urach & A ssociates, Inc. 4340 N. Knoll Ave. · Fresno, CA 93722 www.murach.com · [email protected] Author: Joel Murach Editor: Anne Boehm Cover design: Zylka Design Production: Maria Pedroza David Books for Java programmers Murach’s Java Programming Murach’s Java Servlets and JSP Books for database programmers Murach’s Oracle SQL and PL/SQL Murach’s MySQL Murach’s SQL Server for Developers Books for web developers Murach ’s HTML5 and CSS3 Murach’s JavaScript and DOM Scripting Murach’s PHP and MySQL Books for .NET programmers Murach’s C#2010 Murach’s ASP.NET 4 Web Programming with C#2010 Murach’s ADO.NET 4 Database Programming with C# 2010 Murach’s Visual Basic 2010 Murach’s ASP.NET 4 Web Programming with VB 2010 Murach’s ADO.NET 4 Database Programming with VB 2010 Books for IBM mainframe programmers Murach’s OS/390 and z/OS JCL Murach’s Mainframe COBOL Murach’s CICSforthe COBOL Programmer DB2 for the COBOL Programmer, Part 1 © 2011, Mike Murach & Associates, Inc. All rights reserved. Printed in the United States of America 10 987654321 ISBN: 978-1-890774-65-3 Contents Introduction XV Section 1 Essential Java skills Chapter 1 How to get started with Java and NetBeans 3 Chapter 2 Introduction to Java programming 35 Chapter 3 How to work with data 79 Chapter 4 How to code control statements 111 Chapter 5 How to validate input data 145 Chapter 6 How to test and debug an application 167 Section 2 Object-oriented programming with Java Chapter 7 How to define and use classes 185 Chapter 8 How to work with inheritance 237 Chapter 9 How to work with interfaces 275 Chapter 10 Other object-oriented programming skills 311 Section 3 More Java skills Chapter 11 How to work with arrays 339 Chapter 12 How to work with collections and generics 363 Chapter 13 How to work with dates and strings 405 Chapter 14 How to handle exceptions 431 Section 4 GUI programming with Swing Chapter 15 How to develop a form 459 Chapter 16 How to work with controls and handle events 501 Chapter 17 How to develop and deploy applets 537 Section 5 Data access programming with Java Chapter 18 How to work with text and binary files 559 Chapter 19 How to work with XML 613 Chapter 20 How to work with a Derby database 645 Chapter 21 How to use JDBC to work with a database 677 Section 6 Advanced Java skills Chapter 22 How to work with threads 717 Chapter 23 How to deploy an application 751 Appendixes Appendix A How to set up your PC for this book 771 Appendix B How to set up your Mac for this book 111 E x p a n d e d c o n t e n t s Section 1 Essential Java skills Chapter 1 How to get started with Java and NetBeans Introduction to Java.............................................................................4 Toolkits and platforms.....................................................................................................4 How Java compares to C++ and C#.................................................................................4 Applications, applets, and servlets..................................................................................6 The code for the console version of the Future Value application..................................8 How Java compiles and interprets code....................................................................... 10 Introduction to Java IDEs............................................................................................. 12 How to use NetBeans to work with existing projects...................14 Introduction to Java projects and the NetBeans IDE................................................... 14 How to open, close, and delete a project..................................................................... 16 How to compile and run a project................................................................................. 16 How to use the Output window with a console application........................................ 18 How to work with two or more projects........................................................................20 How to use NetBeans to develop new projects............................22 How to create a new project..........................................................................................22 How to set the Java version for a project.......................................................................24 How to work with Java source code and files................................................................26 How to use the code completion feature........................................................................28 How to detect and correct syntax errors........................................................................30 Chapter 2 Introduction to Java programming Basic coding skills............................................................................36 How to code statements.................................................................................................36 How to code comments.................................................................................................36 How to create identifiers.................................................................................................38 How to declare a class and a main method....................................................................40 How to work with numeric variables...............................................42 How to declare and initialize variables..........................................................................42 How to code assignment statements..............................................................................44 How to code arithmetic expressions..............................................................................44 How to work with string variables...................................................46 How to create a String object.........................................................................................46 How to join and append strings.....................................................................................46 How to include special characters in strings..................................................................48 How to use Java classes, objects, and methods............................50 How to create objects and call methods........................................................................50 How to import Java classes............................................................................................52 How to use the API documentation to research Java classes........................................54 How to use the console for input and output................................56 How to use the System.out object to print output to the console.................................56 How to use the Scanner class to read input from the console......................................58 Examples that get input from the console.....................................................................60 How to code simple control statements........................................62 How to compare numeric variables...............................................................................62 How to compare string variables...................................................................................62 How to code if/else statements......................................................................................64 How to code while statements.......................................................................................66 Expanded contents V Two illustrative applications.............................................................68 The Invoice application..................................................................................................68 The Test Score application..............................................................................................70 How to test and debug an application.............................................72 How to test an application..............................................................................................72 How to debug an application..........................................................................................72 Chapter 3 How to work with data Basic skills for working with data....................................................80 The eight primitive data types........................................................................................80 How to declare and initialize variables........................................................................82 How to declare and initialize constants.......................................................................84 How to code assignment statements and arithmetic expressions..................................86 How to use the shortcut assignment operators...............................................................88 How to work with the order of precedence....................................................................90 How to work with casting...............................................................................................92 How to use Java classes for working with data types...................94 How to use the NumberFormat class...........................................................................94 How to use the Math class...........................................................................................96 How to use the Integer and Double classes.................................................................98 The formatted Invoice application..................................................100 The code for the application....................................................................................... 100 A bug in the arithmetic............................................................................................... 102 How to debug the application..................................................................................... 102 How to use the BigDecimal class...................................................104 The constructors and methods.................................................................................... 104 How to use BigDecimal arithmetic............................................................................. 106 Chapter 4 How to code control statements How to code Boolean expressions.................................................112 How to compare primitive data types......................................................................... 112 How to compare strings.............................................................................................. 114 How to use the logical operators................................................................................ 116 How to code if/else and switch statements...................................118 How to code if/else statements................................................................................... 118 How to code switch statements................................................................................... 120 An enhanced version of the Invoice application......................................................... 124 How to code loops............................................................................126 How to code while and do-while loops...................................................................... 126 How to code for loops................................................................................................. 128 The Future Value application...................................................................................... 130 How to code nested loops........................................................................................... 132 How to code break and continue statements................................134 How to code break statements.................................................................................... 134 How to code continue statements.................................................................................136 How to code and call static methods.............................................138 How to code static methods........................................................................................ 138 How to call static methods.......................................................................................... 138 The Future Value application with a static method..................................................... 140 Expanded contents Chapter 5 How to validate input data How to handle exceptions...............................................................146 How exceptions work...................................................................................................146 How to catch exceptions............................................................................................. 148 The Future Value application with exception handling...............................................150 How to validate data.........................................................................152 How to prevent exceptions from being thrown.......................................................... 152 How to validate a single entry................................................................................... 154 How to use generic methods to validate an entry...................................................... 156 The Future Value application with data validation........................158 The console................................................................................................................. 158 The code.......................................................................................................................160 Chapter 6 How to test and debug an application Basic skills for testing and debugging..........................................168 Typical test phases...................................................................................................... 168 The three types of errors............................................................................................. 168 Common Java errors.....................................................................................................170 A simple way to trace code execution....................................................................... 172 How to use NetBeans to debug an application.............................174 How to set and remove breakpoints........................................................................... 174 How to step through code........................................................................................... 176 How to inspect variables...............................................................................................176 How to inspect the stack trace.................................................................................... 178 Section 2 Object-oriented programming with Java Chapter 7 How to define and use classes An introduction to classes..............................................................186 How classes can be used to structure an application................................................. 186 How encapsulation works.............................................................................................188 The relationship between a class and its objects.........................................................190 How to code a class that defines an object...................................192 How to use NetBeans to create a new class............................................................... 192 The code for the Product class.................................................................................... 194 How to code instance variables.................................................................................. 196 How to code constructors........................................................................................... 198 How to code methods..................................................................................................200 How to overload methods............................................................................................202 How to use the this keyword........................................................................................204 How to use NetBeans to work with classes.................................................................206 How to create and use an object....................................................208 How to create an object...............................................................................................208 How to call the methods of an object..........................................................................210 How primitive types and reference types are passed to a method..............................212 A ProductDB class that creates a Product object........................................................214 A ProductApp class that uses a Product object...........................................................216 How to code and use static fields and methods...........................218 How to code static fields and methods........................................................................218 How to call static fields and methods..........................................................................220 How to code a static initialization block......................................................................222 When to use static fields and methods.........................................................................222 Expanded contents The Line Item application................................................................224 The console...................................................................................................................224 The class diagrams.......................................................................................................224 The code for the classes................................................................................................226 Chapter 8 How to work with inheritance An introduction to inheritance........................................................238 How inheritance works.................................................................................................238 How the Java API uses inheritance..............................................................................240 How the Object class works.........................................................................................242 How to use inheritance in your applications................................................................244 Basic skills for working with inheritance.......................................246 How to create a superclass...........................................................................................246 How to create a subclass...............................................................................................248 How polymorphism works...........................................................................................250 The Product application..................................................................252 The console...................................................................................................................252 The ProductApp class..................................................................................................254 The Product, Book, and Software classes....................................................................256 The ProductDB class....................................................................................................256 More skills for working with inheritance........................................260 How to get information about an object’s type............................................................260 How to cast objects......................................................................................................262 How to compare objects..............................................................................................264 How to work with the abstract and finalk eywords.......................266 How to work with the abstract keyword......................................................................266 How to work with the final keyword.........................................................................268 Chapter 9 How to work with interfaces An introduction to interfaces..........................................................276 A simple interface.........................................................................................................276 Interfaces compared to abstract classes.......................................................................278 Some interfaces of the Java API..................................................................................280 How to work with interfaces............................................................282 How to code an interface...........................................................................................282 How to implement an interface....................................................................................284 How to inherit a class and implement an interface......................................................286 How to use an interface as a parameter.....................................................................288 How to use inheritance with interfaces......................................................................290 How to use NetBeans to work with interfaces...........................................................292 A Product Maintenance application that uses interfaces.............294 The class diagram.........................................................................................................294 The console...................................................................................................................296 The DAOFactory class..................................................................................................298 The ProductTextFile class............................................................................................298 The ProductMaintApp class........................................................................................300 How to implement the Cloneable interface...................................304 A Product class that implements the Cloneable interface...........................................304 A Lineltem class that implements the Cloneable interface........................................306 Viiî Expanded contents Chapter 10 Other object-oriented programming skills How to work with packages.............................................................312 An introduction to packages.........................................................................................312 How to use NetBeans to work with packages..............................................................314 How to use NetBeans to work with libraries................................................................316 How to use javadoc to document a package.................................318 How to add javadoc comments to a class.....................................................................318 How to use HTML and javadoc tags in javadoc comments.........................................320 How to use NetBeans to generate documentation.......................................................322 How to view the documentation for a package............................................................322 How to code classes that are closely related................................324 How to code more than one class per file.....................................................................324 An introduction to nested classes.................................................................................326 How to work with enumerations.....................................................328 How to declare an enumeration....................................................................................328 How to use an enumeration..........................................................................................328 How to enhance an enumeration...................................................................................330 How to work with static imports...................................................................................330 Section 3 More Java skills Chapter 11 How to work with arrays Basic skills for working with arrays...............................................340 How to create an array..................................................................................................340 How to assign values to the elements of an array........................................................342 How to use for loops with arrays..................................................................................344 How to use enhanced for loops with arrays..................................................................346 More skills for working with arrays................................................348 The methods of the Arrays class...................................................................................348 Code examples that work with the Arrays class..........................................................350 How to implement the Comparable interface..............................................................352 How to create a reference to an array...........................................................................354 How to copy an array....................................................................................................354 How to work with two-dimensional arrays....................................356 How to work with rectangular arrays...........................................................................356 How to work with jagged arrays...................................................................................358 Chapter 12 How to work with collections and generics An introduction to Java collections...............................................364 A comparison of arrays and collections.......................................................................364 An overview of the Java collection framework............................................................366 An introduction to generics..........................................................................................368 How to use the ArrayList class.......................................................370 The ArrayList class.......................................................................................................370 Code examples that work with array lists.....................................................................372 An Invoice application that uses an array list...............................374 An overview of the Invoice application........................................................................374 The code for the Invoice class......................................................................................376 The code for the InvoiceApp class...............................................................................378 Expanded contents ix How to use the LinkedList class.....................................................380 The LinkedList class.....................................................................................................380 Code examples that work with linked lists...................................................................382 A class that uses a linked list to implement a generic queue.......................................384 An enhanced version of the Invoice application...........................386 An overview of the enhanced Invoice application.......................................................386 The code for the InvoiceApp class...............................................................................388 How to work with maps...................................................................392 The HashMap and TreeMap classes.............................................................................392 Code examples that work with hash maps and tree maps...........................................394 How to work with legacy collections..............................................396 An introduction to legacy collection classes................................................................396 How to use an untyped collection.................................................................................398 How to use wrapper classes with untyped collections.................................................400 Chapter 13 How to work with dates and strings How to work with dates and times.................................................406 How to use the GregorianCalendar class to set dates and times..................................406 How to use the Calendar and GregorianCalendar fields and methods........................408 How to use the Date class.............................................................................................410 How to use the DateFormat class to format dates and times.......................................412 A DateUtils class that provides methods for handling dates.......................................414 An Invoice class that includes an invoice date.............................................................416 How to work with the String class..................................................418 Constructors of the String class....................................................................................418 Code examples that create strings.................................................................................418 Methods of the String class..........................................................................................420 Code examples that work with strings.........................................................................422 How to work with the StringBuilder class.....................................424 Constructors and methods of the StringBuilder class..................................................424 Code examples that work with the StringBuilder class...............................................426 Chapter 14 How to handle exceptions An introduction to exceptions........................................................432 The exception hierarchy...............................................................................................432 How exceptions are propagated....................................................................................434 How to work with exceptions..........................................................436 How to use the try statement........................................................................................436 How to use the try-with-resources statement...............................................................438 How to use the methods of an exception.....................................................................440 How to use a multi-catch block....................................................................................442 How to use the throws clause.......................................................................................444 How to use the throw statement....................................................................................446 How to work with custom exception classes................................448 How to create your own exception class.......................................................................448 How to use exception chaining.....................................................................................450 How to work with assertions...........................................................452 How to code assert statements......................................................................................452 How to enable and disable assertions..........................................................................452
Description: