Tải bản đầy đủ (.pdf) (545 trang)

Essential skills java 2 a biginner guide

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.52 MB, 545 trang )

AM
FL
Y
TE
Team-Fly®


Java 2:


A Beginner’s Guide
Second Edition


This page intentionally left blank.


Java 2:


A Beginner’s Guide
Second Edition
Herbert Schildt

McGraw-Hill/Osborne
New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto



Copyright © 2003 by The McGraw-Hill Companies, Inc.]. All rights reserved. Manufactured in the United States of America. Except
as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or
by any means, or stored in a database or retrieval system, without the prior written permission of the publisher.
0-07-223041-X
The material in this eBook also appears in the print version of this title: 0-07-222588-2

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after 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-Hill and/or its licensors be liable for any
indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even
if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
DOI: 10.1036/007223041X



About the Author
Herbert Schildt is the world’s leading programming author.
He is an authority on the C, C++, Java, and C# languages, and is a
master Windows programmer. His programming books have sold
more than 3 million copies worldwide and have been translated
into all major foreign languages. He is the author of numerous
bestsellers, including Java 2: The Complete Reference, Java 2:
A Beginner’s Guide, Java 2 Programmer’s Reference, C++: The
Complete Reference, C: The Complete Reference, and C#: The
Complete Reference. Schildt holds a master’s degree in computer
science from the University of Illinois. He can be reached at his
consulting office at (217) 586-4683.


This page intentionally left blank.


For more information about this title, click here.

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
7 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
8 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
9 Exception Handling

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

10 Using I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
12 Applets, Events, and Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435

Copyright 2003 by The McGraw-Hill Companies, Inc. Click here for Terms of Use.

vii


viii

Java 2: A Beginner’s Guide

A Answers to Mastery Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
B Using Java’s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511



For more information about this title, click here.

Contents
PREFACE

...........................................................

xix

1 Java Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Origins of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Java Relates to C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Java Relates to C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Contribution to the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java Applets and Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Obtaining the Java Software Developer’s Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The First Sample Program Line by Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


1

Copyright 2003 by The McGraw-Hill Companies, Inc. Click here for Terms of Use.

ix

2
3
4
5
5
5
6
6
7
8
9
10
10
12
12
13
13
14
17


Java 2: A Beginner’s Guide

17

20
22
23
23
25
27
29
29
30
32
32
33
34

2 Introducing Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Data Types Are Important . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Boolean Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 2-1 How Far Away Is the Lightning? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Hexadecimal and Octal Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Character Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Closer Look at Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dynamic Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Scope and Lifetime of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Shorthand Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Type Conversion in Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Casting Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 2-2 Display a Truth Table for the Logical Operators . . . . . . . . . . . . . . . . . . . .

35

AM
FL
Y

A Second Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Another Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 1-1 Converting Gallons to Liters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create Blocks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Semicolons and Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Indentation Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 1-2 Improving the Gallons-to-Liters Converter . . . . . . . . . . . . . . . . . . . . . . . .
The Java Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Identifiers in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

The Java Class Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 1 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

TE

x

Team-Fly®

36
36
37
38
40
41
43
44
44
45
45
47
47
48
49
52
52
54
55
57
58

60
61
62
64
65


Contents

Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Type Conversion in Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Spacing and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 2 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66
66
68
69

3 Program Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Input Characters from the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The if-else-if Ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Nested switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 3-1 Start Building a Java Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Some Variations on the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Missing Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Loops with No Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring Loop Control Variables Inside the for Loop . . . . . . . . . . . . . . . . . . . . .
The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 3-2 Improve the Java Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use break to Exit a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use break as a Form of goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 3-3 Finish the Java Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 3 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

71

4 Introducing Classes, Objects, and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The General Form of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Objects Are Created . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reference Variables and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Method to the Vehicle Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Returning from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Parameterized Method to Vehicle . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 4-1 Creating a Help Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72
74

75
76
78
82
83
86
87
88
90
91
92
94
97
100
102
106
109
112
113

115
116
116
117
121
121
122
123
125
126

129
130
133

xi


xii

Java 2: A Beginner’s Guide

Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Constructor to the Vehicle Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The new Operator Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Garbage Collection and Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The finalize( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 4-2 Demonstrate Finalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 4 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

139
140
141
142
143
144
145
147
149


5 More Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 5-1 Sorting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Irregular Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays of Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Assigning Array References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the length Member . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 5-2 A Queue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operating on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Strings Are Immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise AND, OR, XOR, and NOT Operators . . . . . . . . . . . . . . . . . . . . . . . .
The Shift Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bitwise Shorthand Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 5-3 A ShowBits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 5 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

151


6 A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling Access to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Access Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 6-1 Improving the Queue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

195

152
152
156
158
158
160
161
161
163
164
165
168
172
172
173
176
176
178
180
180
185
187
188

191
193
196
196
202


Contents

Pass Objects to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Arguments Are Passed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 6-2 Overloading the Queue Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Static Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 6-3 The Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Nested and Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 6 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

203
205
208
210
216
219
222
224

227
229
232
236

7 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inheritance Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Member Access and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructors and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using super to Call Superclass Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using super to Access Superclass Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 7-1 Extending the Vehicle Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Multilevel Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
When Are Constructors Called? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Superclass References and Subclass Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overridden Methods Support Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Overridden Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applying Method Overriding to TwoDShape . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
final Prevents Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
final Prevents Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using final with Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 7 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

239

8 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding Packages and CLASSPATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Short Package Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

289

240
243
246
248
254
255
258
261
262
268
271
273
273
278
283
283
283
284
286
287
290
290
292

292

xiii


xiv

Java 2: A Beginner’s Guide

Packages and Member Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Package Access Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Importing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Class Library Is Contained in Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Interface References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 8-1 Creating a Queue Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variables in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interfaces Can Be Extended . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 8 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

294
295
297
299
302
303
304
308

310
316
317
318

9 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Exception Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exception Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using try and catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Exception Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Consequences of an Uncaught Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exceptions Enable You to Handle Errors Gracefully . . . . . . . . . . . . . . . . . . . . . .
Using Multiple catch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Catching Subclass Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Try Blocks Can Be Nested . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Throwing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rethrowing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Closer Look at Throwable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Exception Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 9-1 Adding Exceptions to the Queue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 9 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

321

10 Using I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s I/O Is Built upon Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Byte Streams and Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

The Byte Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Character Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

353

322
322
323
324
327
328
330
331
332
334
334
336
338
340
342
344
347
350
354
354
355
355
355



Contents

Using the Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Console Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing Console Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading and Writing Files Using Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inputting from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading and Writing Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 10-1 A File Comparison Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Random Access Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Java’s Character-Based Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Console Input Using Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Console Output Using Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File I/O Using Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a FileWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a FileReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Java’s Type Wrappers to Convert Numeric Strings . . . . . . . . . . . . . . . . . . . . . . .
Project 10-2 Creating a Disk-Based Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 10 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

358
358
360
361
362
364
366
370

372
375
376
379
381
381
382
384
387
394

11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multithreading Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Thread Class and Runnable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Some Simple Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 11-1 Extending Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Multiple Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Determining When a Thread Ends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Synchronized Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The synchronized Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thread Communication Using notify( ), wait( ), and notifyAll( ) . . . . . . . . . . . . . . . . . .
An Example That Uses wait( ) and notify( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Suspending, Resuming, and Stopping Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 11-2 Using the Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 11 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

395

396
397
398
401
403
406
409
412
416
416
419
422
423
428
432
434

12 Applets, Events, and Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Applet Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Applet Organization and Essential Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440

xv


xvi

Java 2: A Beginner’s Guide

The Applet Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Complete Applet Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Applet Initialization and Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Requesting Repainting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The update( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project 12-1 A Simple Banner Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Status Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Parameters to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Listener Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Mouse Event Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More Java Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The transient and volatile Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strictfp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Native Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 12 Mastery Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

440
441
442
443

444
445
449
450
452
454
454
454
455
455
455
456
458
458
459
462
462
463
463
463
465
465
466

A Answers to Mastery Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 1: Java Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 2: Introducing Data Types and Operators. . . . . . . . . . . . . . . . . . . . . . . . . .
Module 3: Program Control Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 4: Introducing Classes, Objects, and Methods . . . . . . . . . . . . . . . . . . . . . .
Module 5: More Data Types and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Module 6: A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . .
Module 7: Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 8: Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 9: Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 10: Using I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 11: Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Module 12: Applets, Events, and Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . .

467
468
470
472
475
476
479
484
486
487
490
494
496


Contents

B Using Java’s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The javadoc Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@deprecated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
{@docRoot} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

@exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
{@inheritDoc} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
{@link} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
{@linkplain} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@param . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@see . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@serial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@serialData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@serialField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@since . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
{@value} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The General Form of a Documentation Comment . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What javadoc Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Example that Uses Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Index

503
504
505
505
505
506
506
506
506
506

507
507
507
507
508
508
508
508
508
509
509
509

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

xvii


This page intentionally left blank.


Preface
I

n the space of a few short years, Java went from relative obscurity to being the most important
language of the Internet. The impact of Java cannot be understated. It transformed the Web into
a highly interactive environment, setting a new standard in computer language design in the
process. The innovative aspects of Java have already changed the course of programming well
into the foreseeable future. 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 no
previous programming experience. The book starts with the basics, such as how to compile and
run a Java program. It then discusses every keyword in the Java language. It concludes with
some of Java’s most advanced features, such as multithreaded programming and creating applets.
By the time you finish, you will have a firm grasp of the essentials of Java programming.
It is important to state at the outset that this book is just a starting point. Java is more than
just the elements that define the language. Java also includes extensive libraries and tools that
aid in the development of programs. Furthermore, Java provides a sophisticated set of libraries
that handle the browser user interface. To be a top-notch Java programmer implies mastery of
these areas, too. After completing this book you will have the knowledge to pursue any and all
other aspects of Java.

Copyright 2003 by The McGraw-Hill Companies, Inc. Click here for Terms of Use.

xix


Java 2: A Beginner’s Guide

How This Book Is Organized
This book presents an evenly paced tutorial in which each section builds upon the previous
one. It contains 12 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
Mastery Check

AM
FL

Y

Each module begins with a set of critical skills that you will be learning. The location of each
skill is indicated within the module.

Each module concludes with a Mastery Check, a list of questions that lets you test your
knowledge. The answers are in Appendix A.

Progress Checks

TE

xx

At the end of each major section is a “Progress Check” that tests your understanding of the key
points that were presented. The answers to these questions are at the bottom of the page.

Ask the Expert
Sprinkled throughout the book are “Ask the Expert” boxes. These contain additional
information or interesting commentary about a topic. They use a question-and-answer format.

Projects
Each module contains one or more projects that show you how to apply what you are learning.
These are real-world examples that you can use as starting points for your own programs.

No Previous Programming
Experience Required
This book assumes no previous programming experience. Thus, if you have never programmed
before, you can use this book. Of course, in this day and age, most readers will have at least a
little prior programming experience. For many, this previous experience will be in C++. As you

will learn, C++ and Java are related. Therefore, if you already know C++, then you will be able

Team-Fly®


Preface

to learn Java very easily. Since many readers will have some C++ experience, similarities
between C++ and Java are pointed out from time to time throughout the book.

Required Software
To compile and run the programs in this book you will need the latest Java Software
Developers Kit (SDK) from Sun, which at the time of this writing is Java 2, version 1.4.
Instructions for obtaining the Java SDK are given in Module 1.

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.

xxi


xxii

Java 2: A Beginner’s Guide

For Further Study
Java 2: 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 2: The Complete Reference



Java 2 Programmer’s Reference

To learn about C++, you will find these books especially helpful.


C++: The Complete Reference



Teach Yourself C++



C++ from the Ground Up



STL Programming from the Ground Up



C/C++ Annotated Archives

To learn about C#, we suggest the following Schildt books:



C#: A Beginner’s Guide



C#: The Complete Reference

If you want to learn more about the C language, then the following titles will be of interest.


C: The Complete Reference



Teach Yourself C

When you need solid answers, fast, turn to Herbert Schildt, the
recognized authority on programming.


Module

1

Java Fundamentals

CRITICAL SKILLS
1.1


Know the history and philosophy of Java

1.2

Understand Java’s contribution to the Internet
Understand the importance of bytecode
Know the Java buzzwords

1.5

Understand the foundational principles of object-oriented programming

1.6

Create, compile, and run a simple Java program

1.7

Use variables

1.8

Use the if and for control statements

1.9

Create blocks of code

1.10


Understand how statements are positioned, indented, and terminated

1.11

Know the Java keywords

1.12

Understand the rules for Java identifiers

Copyright 2003 by The McGraw-Hill Companies, Inc. Click here for Terms of Use.

1


2

Module 1:

Java Fundamentals

T

he rise of the Internet and the World Wide Web have fundamentally reshaped computing.
Only a few short years ago, the cyber landscape was dominated by stand-alone PCs. Today,
nearly all PCs are connected to the Internet. The Internet, itself, was transformed—originally
offering a convenient way to share files and information, today it is a vast, distributed computing
universe. These changes have been as rapid as they have been profound, and in their wake,
they gave rise to a new way to program: Java.
Java is the preeminent language of the Internet, but it is more than that. Java has

revolutionized programming, changing the way that we think about both the form and
the function of a program. To be a professional programmer today implies the ability to
program in Java—it has become that important. In the course of this book, you will learn
the skills needed to master it.
The purpose of this module is to introduce you to Java, including its history, its design
philosophy, and several of its most important features. By far, the hardest thing about learning a
programming language is the fact that no element exists in isolation. Instead, the components of
the language work in conjunction with each other. This interrelatedness is especially pronounced
in Java. In fact, it is difficult to discuss one aspect of Java without involving others. To help
overcome this problem, this module provides a brief overview of several Java features,
including the general form of a Java program, some basic control structures, and operators.
It does not go into too many details but, rather, concentrates on the general concepts common
to any Java program.

CRITICAL SKILL

1.1

The Origins of Java
Computer language innovation is driven forward by two factors: improvements in the art of
programming and changes in the computing environment. Java is no exception. Building upon
the rich legacy inherited from C and C++, Java adds refinements and features that reflect the
current state of the art in programming. Responding to the rise of the online environment, Java
offers features that streamline programming for a highly distributed architecture.
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and
Mike Sheridan at Sun Microsystems in 1991. This language was initially called “Oak” but
was renamed “Java” in 1995. Somewhat surprisingly, the original impetus for Java was not
the Internet! Instead, the primary motivation was the need for a platform-independent language
that could be used to create software to be embedded in various consumer electronic devices,
such as toasters, microwave ovens, and remote controls. As you can probably guess, many

different types of CPUs are used as controllers. The trouble was that most computer languages
are designed to be compiled for a specific target. For example, consider C++.
Although it is possible to compile a C++ program for just about any type of CPU, to do so
requires a full C++ compiler targeted for that CPU. The problem, however, is that compilers
are expensive and time-consuming to create. In an attempt to find a better solution, Gosling


×