in 21 Days
Java
™
6
Sams Teach Yourself
Rogers Cadenhead
Laura Lemay
800 East 96th Street, Indianapolis, Indiana 46240
Sams Teach Yourself Java
™
6 in 21 Days
Copyright © 2007 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmit-
ted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written per-
mission from the publisher. No patent liability is assumed with respect to the use of the information
contained herein. Although every precaution has been taken in the preparation of this book, the pub-
lisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for
damages resulting from the use of the information contained herein.
International Standard Book Number-10: 0-672-32943-3
International Standard Book Number-13: 978-0-672-32943-2
Printed in the United States of America
First Printing: May 2007
10090807 4321
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been appro-
priately 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 any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no war-
ranty or fitness is implied. The information provided is on an “as is” basis. The author(s) and the
publisher shall have neither liability nor responsibility to any person or entity with respect to any loss
or damages arising from the information contained in this book or from the use of the CD or pro-
grams 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
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearsoned.com
Library of Congress Cataloging-in-Publication Data
Cadenhead, Rogers.
Sams teach yourself Java 6 in 21 days / Rogers Cadenhead, Laura Lemay.
p. cm.
Includes index.
ISBN 978-0-672-32943-2 (pbk. w/cd)
1. Java (Computer program language) I. Lemay, Laura. II. Title.
QA76.73.J38C333 2007
005.13'3--dc22
2007014568
Acquisitions Editor
Mark Taber
Development Editor
Songlin Qiu
Managing Editor
Patrick Kanouse
Project Editor
Mandie Frank
Copy Editor
Karen Annett
Indexer
WordWise Publishing
Services, LLC
Proofreader
Language Logistics,
LLC
Technical Editor
Adam DeFields
Publishing
Coordinator
Vanessa Evans
Multimedia
Developer
Dan Scherf
Designer
Gary Adair
Page Layout
Bronkella Publishing,
LLC
Contents at a Glance
Introduction 1
WEEK I: The Java Language
1 Getting Started with Java 9
2 The ABCs of Programming 35
3 Working with Objects 63
4 Lists, Logic, and Loops 89
5 Creating Classes and Methods 115
6 Packages, Interfaces, and Other Class Features 145
7 Exceptions, Assertions, and Threads 183
WEEK II: The Java Class Library
8 Data Structures 219
9 Working with Swing 247
10 Building a Swing Interface 273
11 Arranging Components on a User Interface 303
12 Responding to User Input 333
13 Using Color, Fonts, and Graphics 357
14 Developing Swing Applications 381
WEEK III: Java Programming
15 Working with Input and Output 405
16 Serializing and Examining Objects 433
17 Communicating Across the Internet 453
18 Accessing Databases with JDBC 485
19 Reading and Writing RSS Feeds 511
20 XML Web Services 535
21 Writing Java Servlets and Java Server Pages 555
Appendixes
A Using the Java Development Kit 601
B Programming with the Java Development Kit 625
C This Book’s Web Site 649
Index 651
Bonus Material on the Companion CD-ROM
CD1 Choosing Java
CD2 Writing Java Applets
CD3 Regular Expressions
CD4 Where to Go from Here: Java Resources
Table of Contents
Introduction 1
How This Book Is Organized ..................................................................................2
Who Should Read This Book
..................................................................................4
Conventions Used in This Book
..............................................................................5
WEEK I: The Java Language
DAY 1: Getting Started with Java 9
The Java Language ..............................................................................................10
History of the Language
..............................................................................10
Introduction to Java
....................................................................................11
Selecting a Development Tool
......................................................................11
Object-Oriented Programming
................................................................................12
Objects and Classes
..............................................................................................14
Attributes and Behavior
........................................................................................17
Attributes of a Class of Objects
....................................................................17
Behavior of a Class of Objects
......................................................................18
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
..................................................................................................30
Summary
............................................................................................................31
Q&A
................................................................................................................31
Quiz
..................................................................................................................32
Questions
..................................................................................................32
Answers
....................................................................................................32
Certification Practice
..................................................................................32
Exercises
............................................................................................................33