Java™: A Beginner’s Guide, - Pdf 15

Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:ii
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:50 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:i
Java

:
A Beginner’s Guide,
Third Edition
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:50 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:ii
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:50 AM
Color profile: Generic CMYK printer profile
Composite Default screen
This page intentionally left blank.
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:iii
Java


every occurrence of a trademarked name, we use names in an editorial fashion only, and to the
benefit of the trademark owner, with no intention of infringement of the trademark. Where such
designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are
available at special quantity discounts to use as premiums and sales promotions, or for use in
corporate training programs. For more information, please contact George Hoare, Special Sales, at
or (212) 904-4069.

TERMS OF USE

This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its
licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as
permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work,
you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works
based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it
without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and
personal use; any other use of the work is strictly prohibited. Your right to use the work may be
terminated if you fail to comply with these terms.

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO
GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR
COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,
INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA
HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its
licensors do not warrant or guarantee that the functions contained in the work will meet your
requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its
licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of
cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for
the content of any information accessed through the work. Under no circumstances shall McGraw-

Reference, and C#: The Complete Reference, and he is the
co-author of The Art of Java. Schildt holds both graduate and
undergraduate degrees from the University of Illinois. He can
be reached at his consulting office at (217) 586-4683. His Web
site is www.HerbSchildt.com.
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:v
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:50 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Copyright © 2005 The McGraw-Hill Companies. Click here for terms of use.
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / 1
Blind Folio FM:vii
Contents at a Glance
1 Java Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Introducing Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3 Program Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4 Introducing Classes, Objects, and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
5 More Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
6 A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
7 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
8 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
9 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
10 Using I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
12 Enumerations, Autoboxing, and Static Import . . . . . . . . . . . . . . . . . . . . . . . . . . 447
13 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
14 Applets, Events, and Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Obtaining the Java Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The First Sample Program Line by Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:50 AM
Color profile: Generic CMYK printer profile
Composite Default screen
For more information about this title, click here
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:x
Handling Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
A Second Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Another Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Project 1-1 Converting Gallons to Liters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Two Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Create Blocks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Semicolons and Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Indentation Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Casting Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Project 2-2 Display a Truth Table for the Logical Operators . . . . . . . . . . . . . . . . . . . . . 65
x Java: A Beginner’s Guide
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:51 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Contents xi
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xi
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
Type Conversion in Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Spacing and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Module 2 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3
Program Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Input Characters from the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The if-else-if Ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Nested switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Project 3-1 Start Building a Java Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Some Variations on the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Missing Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Loops with No Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Declaring Loop Control Variables Inside the for Loop . . . . . . . . . . . . . . . . . . . . . 91

Composite Default screen
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xii
xii Java: A Beginner’s Guide
The new Operator Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Garbage Collection and Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
The finalize( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Project 4-2 Demonstrate Finalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Module 4 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
5
More Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Project 5-1 Sorting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Irregular Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Arrays of Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Initializing Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Assigning Array References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Using the length Member . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Project 5-2 A Queue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
The For-Each Style for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Iterating Over Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Applying the Enhanced for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Constructing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Understanding static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Static Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Project 6-3 The Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Introducing Nested and Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Varargs: Variable-Length Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Varargs Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Overloading Varargs Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Varargs and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Module 6 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
7
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Inheritance Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Member Access and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Constructors and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Using super to Call Superclass Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Using super to Access Superclass Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Project 7-1 Extending the Vehicle Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Creating a Multilevel Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
When Are Constructors Called? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Superclass References and Subclass Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Overridden Methods Support Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Why Overridden Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Applying Method Overriding to TwoDShape . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Using final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
final Prevents Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
final Prevents Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Using final with Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

The Exception Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Exception Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Using try and catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
A Simple Exception Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
The Consequences of an Uncaught Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Exceptions Enable You to Handle Errors Gracefully . . . . . . . . . . . . . . . . . . . . . . 340
Using Multiple catch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Catching Subclass Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Try Blocks Can Be Nested . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Throwing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Rethrowing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
A Closer Look at Throwable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Using finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Using throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Java’s Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Creating Exception Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Project 9-1 Adding Exceptions to the Queue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Module 9 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
10
Using I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Java’s I/O Is Built upon Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Byte Streams and Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
The Byte Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
The Character Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
The Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Using the Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Reading Console Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Writing Console Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Reading and Writing Files Using Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Inputting from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Using Synchronized Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
The synchronized Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Thread Communication Using notify( ), wait( ), and notifyAll( ) . . . . . . . . . . . . . . . . . . 434
An Example That Uses wait( ) and notify( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Suspending, Resuming, and Stopping Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Project 11-2 Using the Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Module 11 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
12
Enumerations, Autoboxing, and Static Import . . . . . . . . . . . . . . . . . . . . . . . 447
Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Enumeration Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Java Enumerations Are Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
The values( ) and valueOf( ) Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Constructors, Methods, Instance Variables, and Enumerations . . . . . . . . . . . . . . . . . . . 454
Two Important Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Enumerations Inherit Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Project 12-1 A Computer-Controlled Traffic Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Autoboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Autoboxing Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Autoboxing and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Autoboxing/Unboxing Occurs in Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
A Word of Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Static Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Contents xv
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:51 AM
Color profile: Generic CMYK printer profile
Composite Default screen

Module 13 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
14
Applets, Events, and Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Applet Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Applet Organization and Essential Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
The Applet Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
A Complete Applet Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Applet Initialization and Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Requesting Repainting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
The update( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Project 14-1 A Simple Banner Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Using the Status Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Passing Parameters to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
The Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Event Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 11:23:03 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Event Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Event Listener Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Using the Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
A Simple Mouse Event Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
More Java Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552

The General Form of a Documentation Comment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
What javadoc Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
An Example that Uses Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Contents xvii
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xvii
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:51 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xviii
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:51 AM
Color profile: Generic CMYK printer profile
Composite Default screen
This page intentionally left blank.
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xix
Preface
J
ava is the preeminent language of the Internet. Moreover, it is the universal language of
Web programmers around the world. To be a professional Web developer today implies
proficiency in Java. Therefore, if Internet-based programming is in your future, you have
chosen the right language to learn—and, this book will help you learn it.
The purpose of this book is to teach you the fundamentals of Java programming. It uses a
step-by-step approach complete with numerous examples, self-tests, and projects. It assumes

forefront of computer language design. One reason for this is the culture of innovation and
change that came to surround Java. As a result, Java has gone through several upgrades—some
relatively small, others more significant.
The first major update to Java was version 1.1. The features added by Java 1.1 were more
substantial than the small increase in the version number would have you think. For example,
Java 1.1 added many new library elements, redefined the way events are handled, and
reconfigured many features of the original 1.0 library.
The next major release of Java was Java 2, where the 2 indicates “second generation.” The
creation of Java 2 was a watershed event, marking the beginning of Java’s “modern age.”
The first release of Java 2 carried the version number 1.2. It may seem odd that the first release
of Java 2 used the 1.2 version number. The number originally referred to the internal version
number of the Java libraries, but then was generalized to refer to the entire release, itself. With
Java 2, Sun repackaged the Java product as J2SE (Java 2 Platform Standard Edition), and the
version numbers began to be applied to that product.
The next upgrade of Java was J2SE 1.3. This version of Java was the first major upgrade
to the original Java 2 release. For the most part it added to existing functionality and
“tightened up” the development environment. The release of J2SE 1.4 further enhanced Java.
This release contained several important new features, including chained exceptions, channel-
based I/O, and the assert keyword.
The latest release of Java is J2SE 5. As important as each of the preceding upgrades to
Java have been, none compares in scale, size, and scope to that of J2SE 5. It has fundamentally
reshaped the Java world!
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:52 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Preface xxi
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xxi

of the change. Instead, Sun elected to increase the version number to 5 as a way of emphasizing
that a major event was taking place. Thus, the current product is called J2SE 5, and the
developer’s kit is called JDK 5. However, in order to maintain consistency, Sun decided to
use 1.5 as its internal version number. Thus, 5 is the external version number and 1.5 is the
internal version number.
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:52 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xxii
xxii Java: A Beginner’s Guide
Because of Sun’s use of 1.5 as the internal version number, when you ask the compiler its
version, it will respond with 1.5 rather than 5. Also, the online documentation supplied by
Sun uses 1.5 to refer to features added by the J2SE 5. In general, whenever you see 1.5, it
simply means 5.
This book has been fully updated to include the new features added by J2SE 5. To handle
all of the new material, two entirely new modules where added to this edition. Module 12
discusses enumerations, autoboxing, static import, and metadata. Module 13 examines generics.
Descriptions of the “for-each” style for loop and variable-length arguments were integrated
into existing modules.
How This Book Is Organized
This book presents an evenly paced tutorial in which each section builds upon the previous
one. It contains 14 modules, each discussing an aspect of Java. This book is unique because it
includes several special elements that reinforce what you are learning.
Critical Skills
Each module begins with a set of critical skills that you will learn. The location of each skill
within the module is indicated.
Mastery Check

version 5 (J2SE 5). Instructions for obtaining the Java JDK are given in Module 1.
If you are using an earlier version of Java, such as J2SE 1.4, then you will still be able to
use this book, but you won’t be able to compile and run the programs that use the new features
added by J2SE 5.
Don’t Forget: Code on the Web
Remember, the source code for all of the examples and projects in this book is available free
of charge on the Web at www.osborne.com.
P:\010Comp\Begin8\189-0\fm.vp
Tuesday, March 01, 2005 10:22:52 AM
Color profile: Generic CMYK printer profile
Composite Default screen
TEAM LinG
Begin8 / Java: A Beginner’s Guide, 3rd Ed/ Schildt / 3189-0 / Front Matter
Blind Folio FM:xxiv
For Further Study
Java: A Beginner’s Guide is your gateway to the Herb Schildt series of programming books.
Here are some others that you will find of interest.
To learn more about Java programming, we recommend the following:

Java: The Complete Reference, J2SE 5 Edition

The Art of Java
To learn about C++, you will find these books especially helpful.

C++: The Complete Reference

Teach Yourself C++

C++ from the Ground Up


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status