Teach Yourself Java 6 in 21 Days [Cadenhead & Lemay 2007-06-04].pdf

(5567 KB) Pobierz
Rogers Cadenhead
Laura Lemay
Sams
Teach Yourself
Java 6
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 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
10 09 08 07
4 3 2 1
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
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.
Proofreader
Language Logistics,
LLC
Technical Editor
Adam DeFields
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.
Publishing
Coordinator
Vanessa Evans
Multimedia
Developer
Dan Scherf
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
Designer
Gary Adair
Page Layout
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.J38C333 2007
005.13'3--dc22
2007014568
Contents at a Glance
Introduction
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 a Swing 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 Java Servlets and Java Server Pages
1
9
35
63
89
115
145
183
219
247
273
303
333
357
381
405
433
453
485
511
535
555
Appendixes
A
Using the Java Development Kit
B
Programming with the Java Development Kit
C
This Book’s Web Site
601
625
649
651
Index
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
How This Book Is Organized
Who Should Read This Book
1
..................................................................................
2
..................................................................................
4
..............................................................................
5
Conventions Used in This Book
WEEK I:
The Java Language
DAY 1:
Getting Started with Java
9
The Java Language
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
History of the Language
Introduction to Java
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
Selecting a Development Tool
Object-Oriented Programming
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
Objects and Classes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
Attributes and Behavior
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
Attributes of a Class of Objects
Behavior of a Class of Objects
Running the Program
Inheritance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
Creating a Class
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
Organizing Classes and Class Behavior
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
26
Creating a Class Hierarchy
Inheritance in Action
Interfaces
Packages
Q&A
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
Single and Multiple Inheritance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
Quiz
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
Questions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
Answers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
Certification Practice
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
Exercises
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
Zgłoś jeśli naruszono regulamin