Sams Teach Yourself
in 21 Days
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 transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,without written permission 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 publisher 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. InternationalStandard 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 10 09 08 07 4 3 2 1
WordWise Publishing Services, LLCTrademarks
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 of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
Language Logistics, LLC
Every effort has been made to make this book as complete and as accurate as possible, but no warranty 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 orprograms accompanying it.
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 firstname.lastname@example.org For sales outside of the U.S., pleasecontact International Sales email@example.com
Bronkella Publishing, LLC
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.J38C3332007 005.13'3--dc22 2007014568
Contents at a Glance
WEEK I: The Java Language 1 Getting Started with Java 2 The ABCs of Programming 3 Working with Objects 4 Lists, Logic, and Loops 5 Creating Classes and Methods 6 Packages, Interfaces, and Other Class Features 7 Exceptions, Assertions, and Threads WEEK II: The Java Class Library 8 Data Structures 9 Working with Swing 10 Building aSwing Interface 11 Arranging Components on a User Interface 12 Responding to User Input 13 Using Color, Fonts, and Graphics 14 Developing Swing Applications WEEK III: Java Programming 15 Working with Input and Output 16 Serializing and Examining Objects 17 Communicating Across the Internet 18 Accessing Databases with JDBC 19 Reading and Writing RSS Feeds 20 XML Web Services 21 Writing JavaServlets and Java Server Pages
9 35 63 89 115 145 183
219 247 273 303 333 357 381
405 433 453 485 511 535 555
A Using the Java Development Kit B Programming with the Java Development Kit C This Book’s Web Site
601 625 649 651
Bonus Material on the Companion CD-ROM
CD1 Choosing Java CD2 Writing Java Applets CD3 Regular Expressions CD4 Where to Go from Here:...