www.elsolucionario.net
www.elsolucionario.net
www.elsolucionario.net
Java :
The Complete Reference,
Seventh Edition
www.elsolucionario.net
™
www.elsolucionario.net
Herbert Schildt is a leading authority on the
Java, C, C++, and C# languages, and is a master
Windows programmer. His programming books
have sold more than 3.5 million copies worldwide
and have been translated into all major foreign
languages. He is the author of the best-selling
The Art of Java, Java: A Beginner’s Guide, and
Swing: A Beginner’s Guide. Among his other
bestsellers are C++: The Complete Reference, C++:
A Beginner’s Guide, C#: The Complete Reference, and
C#: A Beginner’s Guide. 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.
www.elsolucionario.net
About the Author
www.elsolucionario.net
Java :
The Complete Reference,
Seventh Edition
Herbert Schildt
New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto
www.elsolucionario.net
™
www.elsolucionario.net
Copyright © 2007 by The McGraw-Hill Companies. All rights reserved. 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.
ISBN: 978-0-07-163177-8
MHID: 0-07-163177-1
The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-226385-5,
MHID: 0-07-226385-7.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training
programs. To contact a representative please e-mail us at
Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or
mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any
information and is not responsible for any errors or omissions or the results obtained from the use of such information.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGrawHill”) 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.
www.elsolucionario.net
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.
www.elsolucionario.net
Contents at a Glance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . .
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enumerations, Autoboxing, and Annotations (Metadata) . . . . . . . . .
I/O, Applets, and Other Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
15
33
57
77
105
125
157
183
205
223
255
285
315
Part II The Java Library
15
16
17
18
19
20
21
22
23
24
25
26
27
String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exploring java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util Part 1: The Collections Framework . . . . . . . . . . . . . . . . . . . .
java.util Part 2: More Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . . .
Input/Output: Exploring java.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing the AWT: Working with Windows, Graphics, and Text . .
Using AWT Controls, Layout Managers, and Menus . . . . . . . . . . . .
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
NIO, Regular Expressions, and Other Packages . . . . . . . . . . . . . . . . .
359
385
437
503
555
599
617
637
663
701
755
787
813
v
www.elsolucionario.net
Part I The Java Language
www.elsolucionario.net
Java: The Complete Reference
Part III Software Development Using Java
28
29
30
31
Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exploring Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
847
859
879
907
Part IV Applying Java
32 Financial Applets and Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33 Creating a Download Manager in Java . . . . . . . . . . . . . . . . . . . . . . . . .
A Using Java’s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . .
931
965
991
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
997
www.elsolucionario.net
vi
www.elsolucionario.net
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxix
1 The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Lineage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Birth of Modern Programming: C . . . . . . . . . . . . . . . . . . .
C++: The Next Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Stage Is Set for Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Creation of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The C# Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Java Changed the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Servlets: Java on the Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multithreaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Architecture-Neutral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interpreted and High Performance . . . . . . . . . . . . . . . . . . . . . .
Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java SE 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Culture of Innovation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3
4
5
6
6
8
8
8
9
9
9
10
10
11
11
11
12
12
12
12
13
13
14
14
2 An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Three OOP Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Closer Look at the First Sample Program . . . . . . . . . . . . . . .
15
15
15
16
16
21
21
22
22
vii
www.elsolucionario.net
Part I The Java Language
www.elsolucionario.net
Java: The Complete Reference
A Second Short Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Blocks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Lexical Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Java Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Java Class Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
26
26
27
29
30
30
30
31
31
31
31
32
3 Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java Is a Strongly Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
short . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Closer Look at Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dynamic Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Scope and Lifetime of Variables . . . . . . . . . . . . . . . . . . . . .
Type Conversion and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Automatic Conversions . . . . . . . . . . . . . . . . . . . . . . . . . .
Casting Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Automatic Type Promotion in Expressions . . . . . . . . . . . . . . . . . . . . .
The Type Promotion Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . .
33
33
33
34
35
35
35
35
36
36
36
37
38
39
39
40
40
40
40
41
41
42
42
45
45
45
47
47
48
48
51
55
www.elsolucionario.net
viii
www.elsolucionario.net
A Few Words About Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Note to C/C++ Programmers About Pointers . . . . . . . . . . . . . . . . .
55
56
4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Basic Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . .
The Modulus Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arithmetic Compound Assignment Operators . . . . . . . . . . . .
Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . .
The Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Unsigned Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bitwise Operator Compound Assignments . . . . . . . . . . . . . . .
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Boolean Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . .
The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
57
58
59
59
60
62
63
65
66
68
69
70
71
72
73
73
74
74
5 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The For-Each Version of the for Loop . . . . . . . . . . . . . . . . . . . .
Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77
77
77
80
84
84
86
88
92
97
98
98
102
103
6 Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The General Form of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Closer Look at new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Assigning Object Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Method to the Box Class . . . . . . . . . . . . . . . . . . . . . .
105
105
105
106
109
109
111
111
112
ix
www.elsolucionario.net
Contents
www.elsolucionario.net
Java: The Complete Reference
Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding a Method That Takes Parameters . . . . . . . . . . . . . . . .
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Instance Variable Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The finalize( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
114
115
117
119
120
121
121
121
122
7 A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . .
Overloading Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Objects as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Closer Look at Argument Passing . . . . . . . . . . . . . . . . . . . . . . . . . . .
Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Nested and Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . .
Exploring the String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Varargs: Variable-Length Arguments . . . . . . . . . . . . . . . . . . . . . . . . . .
Overloading Vararg Methods . . . . . . . . . . . . . . . . . . . . . . . . . . .
Varargs and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
125
125
128
130
132
134
135
138
141
143
143
145
148
150
151
154
155
8 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inheritance Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Member Access and Inheritance . . . . . . . . . . . . . . . . . . . . . . . .
A More Practical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Superclass Variable Can Reference a Subclass Object . . . . .
Using super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using super to Call Superclass Constructors . . . . . . . . . . . . . .
A Second Use for super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Multilevel Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
When Constructors Are Called . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dynamic Method Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Overridden Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applying Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using final with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using final to Prevent Overriding . . . . . . . . . . . . . . . . . . . . . . .
Using final to Prevent Inheritance . . . . . . . . . . . . . . . . . . . . . . .
The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
157
157
159
160
162
163
163
166
167
170
171
174
175
176
177
180
180
181
181
www.elsolucionario.net
x
www.elsolucionario.net
9 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding Packages and CLASSPATH . . . . . . . . . . . . . . . . . . . . .
A Short Package Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Access Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Access Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Importing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Nested Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applying Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variables in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interfaces Can Be Extended . . . . . . . . . . . . . . . . . . . . . . . . . . . .
183
183
184
184
185
186
187
190
192
193
194
196
197
200
202
10 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exception-Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exception Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Uncaught Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using try and catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying a Description of an Exception . . . . . . . . . . . . . . . .
Multiple catch Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Nested try Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Java’s Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Your Own Exception Subclasses . . . . . . . . . . . . . . . . . . . . . .
Chained Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
205
205
206
206
207
209
209
211
213
214
216
217
219
221
222
11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Java Thread Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Thread Class and the Runnable Interface . . . . . . . . . . . . .
The Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Choosing an Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Multiple Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using isAlive( ) and join( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
223
224
224
225
225
226
226
228
228
230
232
232
233
236
xi
www.elsolucionario.net
Contents
www.elsolucionario.net
Java: The Complete Reference
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Synchronized Methods . . . . . . . . . . . . . . . . . . . . . . . . . .
The synchronized Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interthread Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Suspending, Resuming, and Stopping Threads . . . . . . . . . . . . . . . . . .
Suspending, Resuming, and Stopping Threads
Using Java 1.1 and Earlier . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Modern Way of Suspending, Resuming,
and Stopping Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
238
239
241
242
247
249
12 Enumerations, Autoboxing, and Annotations (Metadata) . . . . . . . .
Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enumeration Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The values( ) and valueOf( ) Methods . . . . . . . . . . . . . . . . . . . .
Java Enumerations Are Class Types . . . . . . . . . . . . . . . . . . . . .
Enumerations Inherit Enum . . . . . . . . . . . . . . . . . . . . . . . . . . .
Another Enumeration Example . . . . . . . . . . . . . . . . . . . . . . . . .
Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Autoboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Autoboxing and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Autoboxing/Unboxing Occurs in Expressions . . . . . . . . . . . .
Autoboxing/Unboxing Boolean and Character Values . . . . .
Autoboxing/Unboxing Helps Prevent Errors . . . . . . . . . . . . .
A Word of Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Annotations (Metadata) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Annotation Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Specifying a Retention Policy . . . . . . . . . . . . . . . . . . . . . . . . . . .
Obtaining Annotations at Run Time by Use of Reflection . . .
The AnnotatedElement Interface . . . . . . . . . . . . . . . . . . . . . . . .
Using Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Marker Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Single-Member Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Built-In Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Some Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
255
255
255
258
259
261
263
264
266
267
268
270
271
271
272
272
273
273
278
279
280
281
282
284
13 I/O, Applets, and Other Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
I/O Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Byte Streams and Character Streams . . . . . . . . . . . . . . . . . . . . .
The Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Console Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
285
285
286
286
288
288
289
290
249
251
254
www.elsolucionario.net
xii
www.elsolucionario.net
Writing Console Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The PrintWriter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applet Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The transient and volatile Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strictfp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Native Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problems with Native Methods . . . . . . . . . . . . . . . . . . . . . . . . .
Using assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Assertion Enabling and Disabling Options . . . . . . . . . . . . . . .
Static Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Invoking Overloaded Constructors Through this( ) . . . . . . . . . . . . . .
292
292
293
296
299
300
302
302
306
306
309
309
312
14 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Generics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Generics Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generics Work Only with Objects . . . . . . . . . . . . . . . . . . . . . . .
Generic Types Differ Based on Their Type Arguments . . . . .
How Generics Improve Type Safety . . . . . . . . . . . . . . . . . . . . .
A Generic Class with Two Type Parameters . . . . . . . . . . . . . . . . . . . .
The General Form of a Generic Class . . . . . . . . . . . . . . . . . . . . . . . . . .
Bounded Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Wildcard Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bounded Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Generic Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Raw Types and Legacy Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a Generic Superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Generic Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Run-Time Type Comparisons Within a Generic Hierarchy . . .
Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overriding Methods in a Generic Class . . . . . . . . . . . . . . . . . .
Erasure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bridge Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ambiguity Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Some Generic Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Type Parameters Can’t Be Instantiated . . . . . . . . . . . . . . . . . . .
Restrictions on Static Members . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Array Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Exception Restriction . . . . . . . . . . . . . . . . . . . . . . . . . .
Final Thoughts on Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
315
316
316
320
320
320
322
324
324
327
329
334
336
337
339
342
342
344
345
348
348
349
351
353
354
354
354
355
356
356
xiii
www.elsolucionario.net
Contents
www.elsolucionario.net
Java: The Complete Reference
Part II The Java Library
15 String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The String Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Special String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Concatenation with Other Data Types . . . . . . . . . . . . .
String Conversion and toString( ) . . . . . . . . . . . . . . . . . . . . . . .
Character Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
charAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
getChars( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
getBytes( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
toCharArray( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
String Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
equals( ) and equalsIgnoreCase( ) . . . . . . . . . . . . . . . . . . . . . . .
regionMatches( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
startsWith( ) and endsWith( ) . . . . . . . . . . . . . . . . . . . . . . . . . . .
equals( ) Versus == . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
compareTo( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Searching Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
substring( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
concat( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
trim( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Conversion Using valueOf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Changing the Case of Characters Within a String . . . . . . . . . . . . . . . .
Additional String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
StringBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
StringBuffer Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
length( ) and capacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ensureCapacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
setLength( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
charAt( ) and setCharAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
getChars( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
append( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
insert( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
reverse( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
delete( ) and deleteCharAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . .
replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
substring( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Additional StringBuffer Methods . . . . . . . . . . . . . . . . . . . . . . .
StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
359
359
362
362
362
362
363
364
365
365
365
366
366
366
366
367
368
368
369
370
372
372
373
373
373
374
375
376
377
377
378
378
378
379
379
380
381
381
382
382
383
383
384
www.elsolucionario.net
xiv
www.elsolucionario.net
Contents
385
386
386
386
390
398
401
402
403
403
404
405
406
407
409
410
411
412
412
413
415
418
418
418
419
419
420
422
422
422
422
422
424
429
429
431
431
431
431
432
433
433
434
www.elsolucionario.net
16 Exploring java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Primitive Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Double and Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Byte, Short, Integer, and Long . . . . . . . . . . . . . . . . . . . . . . . . . .
Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recent Additions to Character for Unicode Code
Point Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Executing Other Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ProcessBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using currentTimeMillis( ) to Time Program Execution . . . . .
Using arraycopy( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Environment Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using clone( ) and the Cloneable Interface . . . . . . . . . . . . . . . . . . . . . .
Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ClassLoader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Transcendental Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exponential Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rounding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Miscellaneous Math Methods . . . . . . . . . . . . . . . . . . . . . . . . . . .
StrictMath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thread, ThreadGroup, and Runnable . . . . . . . . . . . . . . . . . . . . . . . . . .
The Runnable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ThreadGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ThreadLocal and InheritableThreadLocal . . . . . . . . . . . . . . . . . . . . . .
Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
RuntimePermission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Throwable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SecurityManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
StackTraceElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The CharSequence Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Comparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Appendable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xv
www.elsolucionario.net
Java: The Complete Reference
The Iterable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Readable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The java.lang Subpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.lang.annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.lang.instrument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.lang.management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.lang.ref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.lang.reflect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
434
434
435
435
435
435
435
436
17 java.util Part 1: The Collections Framework . . . . . . . . . . . . . . . . . . .
Collections Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recent Changes to Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generics Fundamentally Change the Collections
Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Autoboxing Facilitates the Use of Primitive Types . . . . . . . . .
The For-Each Style for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Collection Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Collection Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The List Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Set Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The SortedSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The NavigableSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Queue Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Deque Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Collection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The LinkedList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The HashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The LinkedHashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TreeSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The PriorityQueue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ArrayDeque Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The EnumSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Accessing a Collection via an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . .
Using an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The For-Each Alternative to Iterators . . . . . . . . . . . . . . . . . . . .
Storing User-Defined Classes in Collections . . . . . . . . . . . . . . . . . . . .
The RandomAccess Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Map Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The NavigableMap Interface . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Map Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Collection Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
437
438
439
439
439
440
440
441
441
443
444
444
445
446
448
448
451
453
454
455
456
457
458
458
459
461
462
463
464
464
466
468
472
473
475
www.elsolucionario.net
xvi
www.elsolucionario.net
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Generic Collections? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Legacy Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Enumeration Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Hashtable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using store( ) and load( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parting Thoughts on Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
480
484
487
487
487
491
493
494
497
500
501
18 java.util Part 2: More Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . .
StringTokenizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BitSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
GregorianCalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
TimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SimpleTimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Observer Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Observer Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Timer and TimerTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Currency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Formatter Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Formatter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Strings and Characters . . . . . . . . . . . . . . . . . . . . . .
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formatting Time and Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The %n and %% Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Specifying a Minimum Field Width . . . . . . . . . . . . . . . . . . . . .
Specifying Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Space, +, 0, and ( Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Comma Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The # Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Uppercase Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using an Argument Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Java printf( ) Connection . . . . . . . . . . . . . . . . . . . . . . . . . . .
503
503
505
507
509
512
513
514
515
516
518
519
519
522
524
525
526
526
526
529
529
530
532
533
534
535
535
536
537
537
537
538
539
xvii
www.elsolucionario.net
Contents
www.elsolucionario.net
Java: The Complete Reference
Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Scanner Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scanning Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Some Scanner Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Other Scanner Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ResourceBundle, ListResourceBundle,
and PropertyResourceBundle Classes . . . . . . . . . . . . . . . . . . . . . . .
Miscellaneous Utility Classes and Interfaces . . . . . . . . . . . . . . . . . . . .
The java.util Subpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.concurrent, java.util.concurrent.atomic, and
java.util.concurrent.locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.jar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.prefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.spi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.zip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
540
540
541
544
547
548
19 Input/Output: Exploring java.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Java I/O Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . .
File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using FilenameFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The listFiles( ) Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Closeable and Flushable Interfaces . . . . . . . . . . . . . . . . . . . . . . . .
The Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
InputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
OutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FileInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FileOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ByteArrayInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ByteArrayOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filtered Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Buffered Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SequenceInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PrintStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DataOutputStream and DataInputStream . . . . . . . . . . . . . . . .
RandomAccessFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FileReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FileWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
555
555
556
559
560
561
561
561
562
562
562
562
564
565
567
568
569
569
573
574
576
578
578
579
579
579
579
549
553
554
554
554
554
554
554
554
554
www.elsolucionario.net
xviii
www.elsolucionario.net
CharArrayReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CharArrayWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BufferedReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BufferedWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PushbackReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PrintWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Console Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Stream I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Improving wc( ) Using a StreamTokenizer . . . . . . . . . . . . . . . .
Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Serializable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Externalizable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ObjectOutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ObjectOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ObjectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ObjectInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Serialization Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stream Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
582
582
583
585
585
586
587
589
590
592
593
593
593
593
595
595
595
598
20 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Networking Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Networking Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . .
InetAddress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Factory Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inet4Address and Inet6Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
TCP/IP Client Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
URLConnection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
HttpURLConnection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The URI Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
TCP/IP Server Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Datagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DatagramSocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DatagramPacket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Datagram Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
599
599
600
601
601
602
603
603
605
607
610
612
612
612
613
613
614
615
21 The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two Types of Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applet Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applet Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Applet Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applet Initialization and Termination . . . . . . . . . . . . . . . . . . . .
Overriding update( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
617
617
617
618
620
621
622
623
xix
www.elsolucionario.net
Contents
www.elsolucionario.net
Java: The Complete Reference
Simple Applet Display Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Requesting Repainting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Banner Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Status Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The HTML APPLET Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Parameters to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Improving the Banner Applet . . . . . . . . . . . . . . . . . . . . . . . . . .
getDocumentBase( ) and getCodeBase( ) . . . . . . . . . . . . . . . . . . . . . . .
AppletContext and showDocument( ) . . . . . . . . . . . . . . . . . . . . . . . . .
The AudioClip Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The AppletStub Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Outputting to the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
623
625
626
628
629
630
631
633
634
635
635
636
22 Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two Event Handling Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ActionEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The AdjustmentEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ComponentEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ContainerEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The FocusEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The InputEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ItemEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The KeyEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MouseEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MouseWheelEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TextEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The WindowEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sources of Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Event Listener Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ActionListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . .
The AdjustmentListener Interface . . . . . . . . . . . . . . . . . . . . . . .
The ComponentListener Interface . . . . . . . . . . . . . . . . . . . . . . .
The ContainerListener Interface . . . . . . . . . . . . . . . . . . . . . . . . .
The FocusListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ItemListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The KeyListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MouseListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . .
The MouseMotionListener Interface . . . . . . . . . . . . . . . . . . . . .
The MouseWheelListener Interface . . . . . . . . . . . . . . . . . . . . . .
The TextListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The WindowFocusListener Interface . . . . . . . . . . . . . . . . . . . . .
637
637
638
638
638
639
639
640
641
642
642
643
643
644
645
646
647
648
648
649
650
650
651
651
651
651
651
651
652
652
652
652
652
www.elsolucionario.net
xx
www.elsolucionario.net
The WindowListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adapter Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Anonymous Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
653
653
653
656
659
660
662
23 Introducing the AWT: Working with Windows, Graphics,
and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AWT Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Window Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Frame Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting the Window’s Dimensions . . . . . . . . . . . . . . . . . . . . . . .
Hiding and Showing a Window . . . . . . . . . . . . . . . . . . . . . . . . .
Setting a Window’s Title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Closing a Frame Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a Frame Window in an Applet . . . . . . . . . . . . . . . . . . . . . . . .
Handling Events in a Frame Window . . . . . . . . . . . . . . . . . . . .
Creating a Windowed Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying Information Within a Window . . . . . . . . . . . . . . . . . . . . . .
Working with Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Drawing Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Drawing Ellipses and Circles . . . . . . . . . . . . . . . . . . . . . . . . . . .
Drawing Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Drawing Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sizing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Color Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting the Current Graphics Color . . . . . . . . . . . . . . . . . . . . . .
A Color Demonstration Applet . . . . . . . . . . . . . . . . . . . . . . . . .
Setting the Paint Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Determining the Available Fonts . . . . . . . . . . . . . . . . . . . . . . . .
Creating and Selecting a Font . . . . . . . . . . . . . . . . . . . . . . . . . . .
Obtaining Font Information . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Managing Text Output Using FontMetrics . . . . . . . . . . . . . . . . . . . . . .
Displaying Multiple Lines of Text . . . . . . . . . . . . . . . . . . . . . . .
663
664
666
666
666
667
667
667
667
667
668
668
668
668
668
670
674
676
676
677
677
678
679
680
681
682
683
684
684
685
686
687
689
690
691
693
xxi
www.elsolucionario.net
Contents
www.elsolucionario.net
Java: The Complete Reference
Centering Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multiline Text Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
694
695
24 Using AWT Controls, Layout Managers, and Menus . . . . . . . . . . . .
Control Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding and Removing Controls . . . . . . . . . . . . . . . . . . . . . . . .
Responding to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The HeadlessException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applying Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CheckboxGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Choice Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Choice Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Managing Scroll Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Scroll Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a TextField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling a TextField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a TextArea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FlowLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BorderLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Insets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
GridLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CardLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
GridBagLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Menu Bars and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FileDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Events by Extending AWT Components . . . . . . . . . . . . . .
Extending Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending Checkbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending a Check Box Group . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending Scrollbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
701
701
702
702
702
702
704
704
707
707
709
711
711
713
714
716
717
719
720
721
723
724
725
727
728
730
732
737
742
747
748
749
750
751
752
752
753
25 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Image Fundamentals: Creating, Loading, and Displaying . . . . . . . .
Creating an Image Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
755
755
756
756
www.elsolucionario.net
xxii
www.elsolucionario.net
Loading an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ImageObserver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Double Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MediaTracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ImageProducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MemoryImageSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ImageConsumer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PixelGrabber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CropImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
RGBImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cell Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Additional Imaging Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
756
757
758
759
762
765
766
767
767
770
770
772
783
786
26 The Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Concurrent API Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.concurrent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.concurrent.atomic . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
java.util.concurrent.locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Synchronization Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CountDownLatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CyclicBarrier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exchanger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using an Executor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Simple Executor Example . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Callable and Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TimeUnit Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Concurrent Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Atomic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Concurrency Utilities Versus Java’s Traditional Approach . . . .
787
788
788
789
789
789
789
795
796
799
801
802
804
806
808
808
811
812
27 NIO, Regular Expressions, and Other Packages . . . . . . . . . . . . . . . .
The Core Java API Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
NIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
NIO Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Charsets and Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the NIO System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Is NIO the Future of I/O Handling? . . . . . . . . . . . . . . . . . . . . .
Regular Expression Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Matcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Regular Expression Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Demonstrating Pattern Matching . . . . . . . . . . . . . . . . . . . . . . .
813
813
815
815
818
819
825
825
825
826
827
827
xxiii
www.elsolucionario.net
Contents