Java A Beginner's Guide, 6th Edition (PDF)

Java_A_Beginners_Guide_6th_Edi

User Manual: Pdf

Open the PDF directly: View PDF PDF.
Page Count: 1153

DownloadJava A Beginner's Guide, 6th Edition (PDF)
Open PDF In BrowserView PDF
®

The
Complete
Reference

™

Java
Eighth Edition

About the Author
Herbert Schildt is a leading authority on the Java, C++, C, and C# languages.
His programming books have sold millions of copies worldwide and have been
translated into all major foreign languages. He is the author of numerous
books on Java, including Java: A Beginner’s Guide, Herb Schildt’s Java Programming
Cookbook, Swing: A Beginner’s Guide, and The Art of Java. Among his other
bestsellers are C++: The Complete Reference™, C#: The Complete Reference™, and
C: The Complete Reference™. Although interested in all facets of computing, his
primary focus is computer languages, including compilers, interpreters, and
robotic control languages. He also has an active interest in the standardization
of languages. 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.

About the Technical Editor
Dr. Danny Coward has been a contributor to the Java Platforms since 1997. He
was a founding member of the Java EE group while at Sun. He has served as a
member of the Java Community Process Executive Committee and has been a
leading contributor to all editions of the Java Platform—Java SE, Java ME, and
Java EE—and established the original JavaFX team.

®

The
Complete
Reference™

Java
Eighth Edition

Herbert Schildt

New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto

Copyright © 2011 by The McGraw-Hill Companies. All rights reserved. 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-160631-8
MHID: 0-07-160631-9
The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-160630-1,
MHID: 0-07-160630-0.
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. To contact a representative please e-mail us at bulksales@mcgraw-hill.com.
Trademarks: McGraw-Hill, the McGraw-Hill Publishing logo, Complete Reference™, and related trade dress are trademarks or
registered trademarks of The McGraw-Hill Companies and/or its affiliates in the United States and other countries and may not be used
without written permission. All other trademarks are the property of their respective owners. The McGraw-Hill Companies is not
associated with any product or vendor mentioned in this book.
Information has been obtained by Publisher from sources believed to be reliable. However, because of the possibility of human or
mechanical error by our sources, Publisher, or others, Publisher does not guarantee to the accuracy, adequacy, or completeness of
any information included in this work and is not responsible for any errors or omissions or the results obtained from the use of such
information.
Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any
information contained in this Work, and is not responsible for any errors or omissions.
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.

FREE SUBSCRIPTION
TO ORACLE MAGAZINE

GET
YOUR

Oracle Magazine is essential gear for today’s information technology professionals.
Stay informed and increase your productivity with every issue of Oracle Magazine.
Inside each free bimonthly issue you’ll get:

t 6
 QUPEBUFJOGPSNBUJPOPO0SBDMF%BUBCBTF 0SBDMF"QQMJDBUJPO4FSWFS 
8FCEFWFMPQNFOU FOUFSQSJTFHSJEDPNQVUJOH EBUBCBTFUFDIOPMPHZ 
BOECVTJOFTTUSFOET
t 5IJSEQBSUZOFXTBOEBOOPVODFNFOUT
t 5FDIOJDBMBSUJDMFTPO0SBDMFBOEQBSUOFSQSPEVDUT UFDIOPMPHJFT 
BOEPQFSBUJOHFOWJSPONFOUT
t %FWFMPQNFOUBOEBENJOJTUSBUJPOUJQT
t 3FBMXPSMEDVTUPNFSTUPSJFT

If there are other Oracle users at
your location who would like to
receive their own subscription to
Oracle Magazine, please photocopy this form and pass it along.

Three easy ways to subscribe:
1 Web

7JTJUPVS8FCTJUFBU oracle.com/oraclemagazine
:PVMMGJOEBTVCTDSJQUJPOGPSNUIFSF QMVTNVDINPSF
2 Fax

$PNQMFUFUIFRVFTUJPOOBJSFPOUIFCBDLPGUIJTDBSE
BOEGBYUIFRVFTUJPOOBJSFTJEFPOMZUP+1.847.763.9638
3 Mail

$PNQMFUFUIFRVFTUJPOOBJSFPOUIFCBDLPGUIJTDBSE
BOENBJMJUUP P.O. Box 1263, Skokie, IL 60076-8263

Copyright © 2008, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Want your own FREE subscription?
To receive a free subscription to Oracle Magazine, you must fill out the entire card, sign it, and date
it (incomplete cards cannot be processed or acknowledged). You can also fax your application to
+1.847.763.9638. Or subscribe at our Web site at oracle.com/oraclemagazine
Yes, please send me a FREE subscription Oracle Magazine.
From time to time, Oracle Publishing allows our partners
exclusive access to our e-mail addresses for special promotions and announcements. To be included in this program,
please check this circle. If you do not wish to be included, you
will only receive notices about your subscription via e-mail.
Oracle Publishing allows sharing of our postal mailing list with
selected third parties. If you prefer your mailing address not to
be included in this program, please check this circle.
If at any time you would like to be removed from either mailing list, please contact
Customer Service at +1.847.763.9635 or send an e-mail to oracle@halldata.com.
If you opt in to the sharing of information, Oracle may also provide you with
e-mail related to Oracle products, services, and events. If you want to completely
unsubscribe from any e-mail communication from Oracle, please send an e-mail to:
unsubscribe@oracle-mail.com with the following in the subject line: REMOVE [your
e-mail address]. For complete information on Oracle Publishing’s privacy practices,
please visit oracle.com/html/privacy/html

No.

x
signature (required)

date

name

title

company

e-mail address

street/p.o. box
city/state/zip or postal code

telephone

country

fax

Would you like to receive your free subscription in digital format instead of print if it becomes available?

Yes

No

YOU MUST ANSWER ALL 10 QUESTIONS BELOW.
1

08014004

2

WHAT IS THE PRIMARY BUSINESS ACTIVITY
OF YOUR FIRM AT THIS LOCATION? (check
one only)
o
o
o
o
o
o
o

01
02
03
04
05
06
07

o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o

08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
98

Aerospace and Defense Manufacturing
Application Service Provider
Automotive Manufacturing
Chemicals
Media and Entertainment
Construction/Engineering
Consumer Sector/Consumer Packaged
Goods
Education
Financial Services/Insurance
Health Care
High Technology Manufacturing, OEM
Industrial Manufacturing
Independent Software Vendor
Life Sciences (biotech, pharmaceuticals)
Natural Resources
Oil and Gas
Professional Services
Public Sector (government)
Research
Retail/Wholesale/Distribution
Systems Integrator, VAR/VAD
Telecommunications
Travel and Transportation
Utilities (electric, gas, sanitation, water)
Other Business and Services _________

3

4

WHICH OF THE FOLLOWING BEST DESCRIBES
YOUR PRIMARY JOB FUNCTION?
(check one only)
CORPORATE MANAGEMENT/STAFF
o 01 Executive Management (President, Chair,
CEO, CFO, Owner, Partner, Principal)
o 02 Finance/Administrative Management
(VP/Director/ Manager/Controller,
Purchasing, Administration)
o 03 Sales/Marketing Management
(VP/Director/Manager)
o 04 Computer Systems/Operations
Management
(CIO/VP/Director/Manager MIS/IS/IT, Ops)
IS/IT STAFF
o 05 Application Development/Programming
Management
o 06 Application Development/Programming
Staff
o 07 Consulting
o 08 DBA/Systems Administrator
o 09 Education/Training
o 10 Technical Support Director/Manager
o 11 Other Technical Management/Staff
o 98 Other

5

WHAT IS YOUR CURRENT PRIMARY OPERATING
PLATFORM (check all that apply)
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
98

99

o

Digital Equipment Corp UNIX/VAX/VMS
HP UNIX
IBM AIX
IBM UNIX
Linux (Red Hat)
Linux (SUSE)
Linux (Oracle Enterprise)
Linux (other)
Macintosh
MVS
Netware
Network Computing
SCO UNIX
Sun Solaris/SunOS
Windows
Other UNIX
Other
None of the Above

6

01
02
03
04
05
06
07

99

o

Hardware
Business Applications (ERP, CRM, etc.)
Application Development Tools
Database Products
Internet or Intranet Products
Other Software
Middleware Products
None of the Above

7

HARDWARE
o 15 Macintosh
o 16 Mainframe
o 17 Massively Parallel Processing

SERVICES
o 24 Consulting
o 25 Education/Training
o 26 Maintenance
o 27 Online Database
o 28 Support
o 29 Technology-Based Training
o 30 Other
99 o None of the Above

o
o

8

01
02
03
04
05
06

More than 25,000 Employees
10,001 to 25,000 Employees
5,001 to 10,000 Employees
1,001 to 5,000 Employees
101 to 1,000 Employees
Fewer than 100 Employees

DURING THE NEXT 12 MONTHS, HOW MUCH
DO YOU ANTICIPATE YOUR ORGANIZATION
WILL SPEND ON COMPUTER HARDWARE,
SOFTWARE, PERIPHERALS, AND SERVICES FOR
YOUR LOCATION? (check one only)
01
02
03
04
05
06

Less than $10,000
$10,000 to $49,999
$50,000 to $99,999
$100,000 to $499,999
$500,000 to $999,999
$1,000,000 and Over

WHAT IS YOUR COMPANY’S YEARLY SALES
REVENUE? (check one only)
o
o
o
o
o

9

18
19
20
21
22
23

WHAT IS YOUR COMPANY’S SIZE?
(check one only)

o
o
o
o
o
o

IN YOUR JOB, DO YOU USE OR PLAN TO PURCHASE ANY OF THE FOLLOWING PRODUCTS?
(check all that apply)
SOFTWARE
o 01 CAD/CAE/CAM
o 02 Collaboration Software
o 03 Communications
o 04 Database Management
o 05 File Management
o 06 Finance
o 07 Java
o 08 Multimedia Authoring
o 09 Networking
o 10 Programming
o 11 Project Management
o 12 Scientific and Engineering
o 13 Systems Management
o 14 Workflow

o
o
o
o
o
o

o
o
o
o
o
o

DO YOU EVALUATE, SPECIFY, RECOMMEND,
OR AUTHORIZE THE PURCHASE OF ANY OF
THE FOLLOWING? (check all that apply)
o
o
o
o
o
o
o

Minicomputer
Intel x86(32)
Intel x86(64)
Network Computer
Symmetric Multiprocessing
Workstation Services

o
o
o
o
o
o

01
02
03
04
05

$500, 000, 000 and above
$100, 000, 000 to $500, 000, 000
$50, 000, 000 to $100, 000, 000
$5, 000, 000 to $50, 000, 000
$1, 000, 000 to $5, 000, 000

WHAT LANGUAGES AND FRAMEWORKS DO
YOU USE? (check all that apply)
o
o
o
o

01
02
03
04

Ajax
C
C++
C#

o
o
o
o

13
14
15
16

Python
Ruby/Rails
Spring
Struts

10

05 Hibernate
06 J++/J#
07 Java
08 JSP
09 .NET
10 Perl
11 PHP
12 PL/SQL

o 17 SQL
o 18 Visual Basic
o 98 Other

WHAT ORACLE PRODUCTS ARE IN USE AT YOUR
SITE? (check all that apply)
ORACLE DATABASE
o 01 Oracle Database 11g
o 02 Oracle Database 10 g
o 03 Oracle9 i Database
o 04 Oracle Embedded Database
(Oracle Lite, Times Ten, Berkeley DB)
o 05 Other Oracle Database Release
ORACLE FUSION MIDDLEWARE
o 06 Oracle Application Server
o 07 Oracle Portal
o 08 Oracle Enterprise Manager
o 09 Oracle BPEL Process Manager
o 10 Oracle Identity Management
o 11 Oracle SOA Suite
o 12 Oracle Data Hubs
ORACLE DEVELOPMENT TOOLS
o 13 Oracle JDeveloper
o 14 Oracle Forms
o 15 Oracle Reports
o 16 Oracle Designer
o 17 Oracle Discoverer
o 18 Oracle BI Beans
o 19 Oracle Warehouse Builder
o 20 Oracle WebCenter
o 21 Oracle Application Express
ORACLE APPLICATIONS
o 22 Oracle E-Business Suite
o 23 PeopleSoft Enterprise
o 24 JD Edwards EnterpriseOne
o 25 JD Edwards World
o 26 Oracle Fusion
o 27 Hyperion
o 28 Siebel CRM
ORACLE SERVICES
o 28 Oracle E-Business Suite On Demand
o 29 Oracle Technology On Demand
o 30 Siebel CRM On Demand
o 31 Oracle Consulting
o 32 Oracle Education
o 33 Oracle Support
o 98 Other
99 o None of the Above

Contents at a Glance
Part I
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Part II
15
16
17
18
19
20
21
22
23
24
25
26
27
28

The Java Language
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
17
35
61
81
109
129
161
187
207
227
259
289
325

The Java Library
String Handling
Exploring java.lang
java.util Part 1: The Collections Framework
java.util Part 2: More Utility Classes
Input/Output: Exploring java.io
Exploring NIO
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
Regular Expressions and Other Packages

371
397
453
525
581
629
667
687
707
735
773
829
861
909

v

vi

Java: The Complete Reference, Eighth Edition

Part III
29
30
31
32

Part IV
33
34
Appendix

Software Development Using Java
Java Beans
Introducing Swing
Exploring Swing
Servlets

933
945
965
993

Applying Java
Financial Applets and Servlets
Creating a Download Manager in Java
Using Java’s Documentation Comments

1019
1053
1079

Index

1087

Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix

Part I
Chapter 1

Chapter 2

The Java Language
The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Java’s Lineage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Birth of Modern Programming: C . . . . . . . . . . . . . . . . . . . . . . . . 4
C++: The Next Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
The Stage Is Set for Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Creation of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The C# Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
How Java Changed the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Servlets: Java on the Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Multithreaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Architecture-Neutral. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Interpreted and High Performance . . . . . . . . . . . . . . . . . . . . . . . . . 12
Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Dynamic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The Evolution of Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Java SE 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
A Culture of Innovation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
An Overview of Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Two Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
The Three OOP Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

vii

viii

Java: The Complete Reference, Eighth Edition

Chapter 3

Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
A Closer Look at the First Sample Program. . . . . . . . . . . . . . . . . . . 24
A Second Short Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Two Control Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Using Blocks of Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Lexical Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Separators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The Java Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The Java Class Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Java Is a Strongly Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
short . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
long. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Floating-Point Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
float. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
double. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
A Closer Look at Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Integer Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
String Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Dynamic Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
The Scope and Lifetime of Variables . . . . . . . . . . . . . . . . . . . . . . . . 45
Type Conversion and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Java’s Automatic Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Casting Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Automatic Type Promotion in Expressions . . . . . . . . . . . . . . . . . . . . . . . 49
The Type Promotion Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Contents

Chapter 4

Chapter 5

Chapter 6

Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . . . . . . 58
A Few Words About Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
A Note to C/C++ Programmers About Pointers . . . . . . . . . . . . . . . . . . . 59
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Arithmetic Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The Basic Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The Modulus Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Arithmetic Compound Assignment Operators . . . . . . . . . . . . . . . . 63
Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
The Bitwise Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
The Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
The Right Shift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
The Unsigned Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Bitwise Operator Compound Assignments . . . . . . . . . . . . . . . . . . . 73
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Boolean Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The ? Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Operator Precedence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Control Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Java’s Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Iteration Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The For-Each Version of the for Loop . . . . . . . . . . . . . . . . . . . . . . . 97
Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Using break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Using continue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
Class Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
The General Form of a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
A Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Declaring Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
A Closer Look at new. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Assigning Object Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Introducing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Adding a Method to the Box Class . . . . . . . . . . . . . . . . . . . . . . . . . 116

ix

x

Java: The Complete Reference, Eighth Edition

Chapter 7

Chapter 8

Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Adding a Method That Takes Parameters . . . . . . . . . . . . . . . . . . . 119
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Instance Variable Hiding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
The finalize( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
A Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . .129
Overloading Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Overloading Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Using Objects as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
A Closer Look at Argument Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Introducing Access Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Understanding static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Introducing final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Arrays Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Introducing Nested and Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Exploring the String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Using Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Varargs: Variable-Length Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Overloading Vararg Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Varargs and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
Inheritance Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Member Access and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 163
A More Practical Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
A Superclass Variable Can Reference a Subclass Object . . . . . . . . 166
Using super. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Using super to Call Superclass Constructors . . . . . . . . . . . . . . . . . 167
A Second Use for super. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Creating a Multilevel Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
When Constructors Are Called . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Dynamic Method Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Why Overridden Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Applying Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Using final with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Using final to Prevent Overriding . . . . . . . . . . . . . . . . . . . . . . . . . 184
Using final to Prevent Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . 185
The Object Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Contents

Chapter 9

Chapter 10

Chapter 11

Packages and Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Defining a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Finding Packages and CLASSPATH . . . . . . . . . . . . . . . . . . . . . . . . 188
A Short Package Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Access Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
An Access Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Importing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Defining an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Implementing Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Nested Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Applying Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Variables in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Interfaces Can Be Extended . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207
Exception-Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Exception Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Uncaught Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Using try and catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Displaying a Description of an Exception . . . . . . . . . . . . . . . . . . . 212
Multiple catch Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Nested try Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
finally. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Java’s Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Creating Your Own Exception Subclasses . . . . . . . . . . . . . . . . . . . . . . . 221
Chained Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Three New JDK 7 Exception Features . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227
The Java Thread Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
The Thread Class and the Runnable Interface . . . . . . . . . . . . . . . 230
The Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Implementing Runnable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Extending Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Choosing an Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Creating Multiple Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Using isAlive( ) and join( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

xi

xii

Java: The Complete Reference, Eighth Edition

Chapter 12

Chapter 13

Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Using Synchronized Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
The synchronized Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Interthread Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Deadlock. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Suspending, Resuming, and Stopping Threads . . . . . . . . . . . . . . . . . . . 251
Suspending, Resuming, and Stopping Threads Using Java 1.1
and Earlier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The Modern Way of Suspending, Resuming, and
Stopping Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Obtaining A Thread’s State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Using Multithreading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Enumerations, Autoboxing, and Annotations (Metadata) . . . . . . . . . . .259
Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Enumeration Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
The values( ) and valueOf( ) Methods. . . . . . . . . . . . . . . . . . . . . . 262
Java Enumerations Are Class Types . . . . . . . . . . . . . . . . . . . . . . . . 263
Enumerations Inherit Enum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Another Enumeration Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
The Numeric Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Autoboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Autoboxing and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Autoboxing/Unboxing Occurs in Expressions . . . . . . . . . . . . . . . 272
Autoboxing/Unboxing Boolean and Character Values . . . . . . . . 274
Autoboxing/Unboxing Helps Prevent Errors . . . . . . . . . . . . . . . . 274
A Word of Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Annotations (Metadata) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Annotation Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Specifying a Retention Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Obtaining Annotations at Run Time by Use of Reflection . . . . . . 277
The AnnotatedElement Interface . . . . . . . . . . . . . . . . . . . . . . . . . 282
Using Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Marker Annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Single-Member Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
The Built-In Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Some Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
I/O, Applets, and Other Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289
I/O Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Byte Streams and Character Streams . . . . . . . . . . . . . . . . . . . . . . . 290
The Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Contents

Chapter 14

Reading Console Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Reading Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Writing Console Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
The PrintWriter Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Automatically Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Applet Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
The transient and volatile Modifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Using instanceof. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
strictfp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Native Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Problems with Native Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Using assert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Assertion Enabling and Disabling Options . . . . . . . . . . . . . . . . . . 319
Static Import. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Invoking Overloaded Constructors Through this( ) . . . . . . . . . . . . . . . 321
Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
What Are Generics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
A Simple Generics Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Generics Work Only with Objects. . . . . . . . . . . . . . . . . . . . . . . . . . 330
Generic Types Differ Based on Their Type Arguments. . . . . . . . . 330
How Generics Improve Type Safety . . . . . . . . . . . . . . . . . . . . . . . . 330
A Generic Class with Two Type Parameters . . . . . . . . . . . . . . . . . . . . . . 332
The General Form of a Generic Class. . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Bounded Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Using Wildcard Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Bounded Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Creating a Generic Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Generic Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Generic Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Raw Types and Legacy Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Generic Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Using a Generic Superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
A Generic Subclass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Run-Time Type Comparisons Within a Generic Hierarchy . . . . . 355
Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Overriding Methods in a Generic Class . . . . . . . . . . . . . . . . . . . . . 358
Type Inference with Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Erasure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Bridge Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Ambiguity Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Some Generic Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Type Parameters Can’t Be Instantiated . . . . . . . . . . . . . . . . . . . . . 365

xiii

xiv

Java: The Complete Reference, Eighth Edition

Restrictions on Static Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Generic Array Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Generic Exception Restriction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

Part II
Chapter 15

The Java Library
String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .371
The String Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Special String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
String Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
String Concatenation with Other Data Types . . . . . . . . . . . . . . . . 375
String Conversion and toString( ) . . . . . . . . . . . . . . . . . . . . . . . . . 376
Character Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
charAt( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
getChars( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
getBytes( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
toCharArray( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
String Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
equals( ) and equalsIgnoreCase( ). . . . . . . . . . . . . . . . . . . . . . . . . 378
regionMatches( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
startsWith( ) and endsWith( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
equals( ) Versus ==. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
compareTo( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Searching Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Modifying a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
substring( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
concat( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
trim( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Data Conversion Using valueOf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Changing the Case of Characters Within a String . . . . . . . . . . . . . . . . . 387
Additional String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
StringBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
StringBuffer Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
length( ) and capacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
ensureCapacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
setLength( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
charAt( ) and setCharAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
getChars( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
append( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
insert( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
reverse( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
delete( ) and deleteCharAt( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393

Contents

Chapter 16

replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
substring( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Additional StringBuffer Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 394
StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Exploring java.lang. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397
Primitive Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Double and Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Understanding isInfinite( ) and isNaN( ) . . . . . . . . . . . . . . . . . . . 402
Byte, Short, Integer, and Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Additions to Character for Unicode Code Point Support . . . . . . 414
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Executing Other Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
ProcessBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Using currentTimeMillis( ) to Time Program Execution. . . . . . . 425
Using arraycopy( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Environment Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Using clone( ) and the Cloneable Interface . . . . . . . . . . . . . . . . . . . . . 427
Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
ClassLoader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Trigonometric Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Exponential Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Rounding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Miscellaneous Math Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
StrictMath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Thread, ThreadGroup, and Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . 436
The Runnable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
ThreadGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
ThreadLocal and InheritableThreadLocal . . . . . . . . . . . . . . . . . . . . . . 444
Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
RuntimePermission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Throwable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
SecurityManager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
StackTraceElement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446

xv

xvi

Java: The Complete Reference, Eighth Edition

Chapter 17

Enum. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
ClassValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
The CharSequence Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
The Comparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
The Appendable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
The Iterable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
The Readable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
The AutoCloseable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
The Thread.UncaughtExceptionHandler Interface . . . . . . . . . . . . . . . 450
The java.lang Subpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
java.lang.annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
java.lang.instrument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
java.lang.invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
java.lang.management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
java.lang.ref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
java.lang.reflect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
java.util Part 1: The Collections Framework . . . . . . . . . . . . . . . . . . . . .453
Collections Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
JDK 5 Changed the Collections Framework. . . . . . . . . . . . . . . . . . . . . . 455
Generics Fundamentally Changed the Collections Framework . . 455
Autoboxing Facilitates the Use of Primitive Types . . . . . . . . . . . . 456
The For-Each Style for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
The Collection Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
The Collection Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
The List Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
The Set Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
The SortedSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
The NavigableSet Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
The Queue Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
The Deque Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
The Collection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
The LinkedList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
The HashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
The LinkedHashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
The TreeSet Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
The PriorityQueue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
The ArrayDeque Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
The EnumSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Accessing a Collection via an Iterator. . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Using an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
The For-Each Alternative to Iterators. . . . . . . . . . . . . . . . . . . . . . . 479
Storing User-Defined Classes in Collections. . . . . . . . . . . . . . . . . . . . . . 480
The RandomAccess Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

Contents

Chapter 18

Working with Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
The Map Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
The Map Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Using a Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
The Collection Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Why Generic Collections? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
The Legacy Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
The Enumeration Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Dictionary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Hashtable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Using store( ) and load( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Parting Thoughts on Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
java.util Part 2: More Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . . .525
StringTokenizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
BitSet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
GregorianCalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
TimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
SimpleTimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
The Observer Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
An Observer Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Timer and TimerTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Currency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
The Formatter Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
The Formatter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Formatting Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Formatting Strings and Characters. . . . . . . . . . . . . . . . . . . . . . . . . 551
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Formatting Time and Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
The %n and %% Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Specifying a Minimum Field Width . . . . . . . . . . . . . . . . . . . . . . . . 555
Specifying Precision. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Using the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
The Space, +, 0, and ( Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558

xvii

xviii

Java: The Complete Reference, Eighth Edition

Chapter 19

The Comma Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
The # Flag. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
The Uppercase Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Using an Argument Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Closing a Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
The Java printf( ) Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Scanner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
The Scanner Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Scanning Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Some Scanner Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Setting Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Other Scanner Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
The ResourceBundle, ListResourceBundle, and
PropertyResourceBundle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Miscellaneous Utility Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . 577
The java.util Subpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
java.util.concurrent, java.util.concurrent.atomic,
and java.util.concurrent.locks . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
java.util.jar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
java.util.logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
java.util.prefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
java.util.regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
java.util.spi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
java.util.zip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Input/Output: Exploring java.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .581
The I/O Classes and Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Using FilenameFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
The listFiles( ) Alternative. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Creating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
The AutoCloseable, Closeable, and Flushable Interfaces . . . . . . . . . . . 588
I/O Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Two Ways to Close a Stream. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
The Stream Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
The Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
InputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
OutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
FileInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
FileOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
ByteArrayInputStream. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
ByteArrayOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Filtered Byte Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Buffered Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
SequenceInputStream. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
PrintStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605

Contents

Chapter 20

Chapter 21

DataOutputStream and DataInputStream . . . . . . . . . . . . . . . . . . . 607
RandomAccessFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
The Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Writer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
FileReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
FileWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
CharArrayReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
CharArrayWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
BufferedReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
BufferedWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
PushbackReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
PrintWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
The Console Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Serializable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Externalizable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
ObjectOutput. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
ObjectOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
ObjectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
ObjectInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
A Serialization Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
Stream Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Exploring NIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .629
The NIO Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
NIO Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Channels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Charsets and Selectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Enhancements Added to NIO by JDK 7 . . . . . . . . . . . . . . . . . . . . . . . . . 634
The Path Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
The Files Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
The Paths Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
The File Attribute Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
The FileSystem, FileSystems, and FileStore Classes . . . . . . . . . . . . 640
Using the NIO System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
Use NIO for Channel-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . 640
Use NIO for Stream-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Use NIO for Path and File System Operations . . . . . . . . . . . . . . . 652
Pre-JDK 7 Channel-Based Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Read a File, Pre-JDK 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Write to a File, Pre-JDK 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Networking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .667
Networking Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
The Networking Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 668

xix

xx

Java: The Complete Reference, Eighth Edition

Chapter 22

Chapter 23

InetAddress. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Factory Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Instance Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Inet4Address and Inet6Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
TCP/IP Client Sockets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
URL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
URLConnection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
HttpURLConnection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
The URI Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
TCP/IP Server Sockets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Datagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
DatagramSocket. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
DatagramPacket. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
A Datagram Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .687
Two Types of Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Applet Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
The Applet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
Applet Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
An Applet Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Applet Initialization and Termination . . . . . . . . . . . . . . . . . . . . . . 692
Overriding update( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Simple Applet Display Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Requesting Repainting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
A Simple Banner Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Using the Status Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
The HTML APPLET Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
Passing Parameters to Applets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Improving the Banner Applet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
getDocumentBase( ) and getCodeBase( ) . . . . . . . . . . . . . . . . . . . . . . . 704
AppletContext and showDocument( ) . . . . . . . . . . . . . . . . . . . . . . . . . . 704
The AudioClip Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
The AppletStub Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
Outputting to the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
Event Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .707
Two Event Handling Mechanisms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
The Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
Event Sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
Event Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Event Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
The ActionEvent Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
The AdjustmentEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
The ComponentEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712

Contents

Chapter 24

The ContainerEvent Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
The FocusEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
The InputEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
The ItemEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
The KeyEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
The MouseEvent Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
The MouseWheelEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
The TextEvent Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
The WindowEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
Sources of Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Event Listener Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
The ActionListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
The AdjustmentListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . 721
The ComponentListener Interface . . . . . . . . . . . . . . . . . . . . . . . . 721
The ContainerListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 721
The FocusListener Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
The ItemListener Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
The KeyListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
The MouseListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
The MouseMotionListener Interface . . . . . . . . . . . . . . . . . . . . . . . 722
The MouseWheelListener Interface. . . . . . . . . . . . . . . . . . . . . . . . 722
The TextListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
The WindowFocusListener Interface . . . . . . . . . . . . . . . . . . . . . . . 723
The WindowListener Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Using the Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Handling Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
Adapter Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Inner Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
Anonymous Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
Introducing the AWT: Working with Windows, Graphics, and Text . . .735
AWT Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Window Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Working with Frame Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Setting the Window’s Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . 740
Hiding and Showing a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Setting a Window’s Title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Closing a Frame Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Creating a Frame Window in an Applet . . . . . . . . . . . . . . . . . . . . . . . . . 741
Handling Events in a Frame Window. . . . . . . . . . . . . . . . . . . . . . . 742

xxi

xxii

Java: The Complete Reference, Eighth Edition

Chapter 25

Creating a Windowed Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
Displaying Information Within a Window . . . . . . . . . . . . . . . . . . . . . . . 749
Working with Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Drawing Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
Drawing Ellipses and Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Drawing Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
Drawing Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Sizing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
Working with Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
Color Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
Setting the Current Graphics Color . . . . . . . . . . . . . . . . . . . . . . . . 756
A Color Demonstration Applet. . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
Setting the Paint Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
Working with Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Determining the Available Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Creating and Selecting a Font. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Obtaining Font Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Managing Text Output Using FontMetrics. . . . . . . . . . . . . . . . . . . . . . . 764
Displaying Multiple Lines of Text. . . . . . . . . . . . . . . . . . . . . . . . . . 766
Centering Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
Multiline Text Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
Using AWT Controls, Layout Managers, and Menus . . . . . . . . . . . . . . .773
Control Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
Adding and Removing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
Responding to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
The HeadlessException. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Using Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Handling Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Applying Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
Handling Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
CheckboxGroup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
Choice Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Handling Choice Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Using Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
Handling Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Managing Scroll Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
Handling Scroll Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
Using a TextField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792
Handling a TextField. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Using a TextArea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
Understanding Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
FlowLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
BorderLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
Using Insets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800

Contents

Chapter 26

Chapter 27

GridLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
CardLayout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
GridBagLayout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
Menu Bars and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Dialog Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
FileDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Handling Events by Extending AWT Components . . . . . . . . . . . . . . . . 822
Extending Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Extending Checkbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
Extending a Check Box Group. . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Extending Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Extending List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Extending Scrollbar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
A Word About Overriding paint( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .829
File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Image Fundamentals: Creating, Loading, and Displaying . . . . . . . . . . 830
Creating an Image Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Loading an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Displaying an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
ImageObserver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
Double Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
MediaTracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
ImageProducer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
MemoryImageSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
ImageConsumer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
PixelGrabber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
ImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
CropImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
RGBImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
Cell Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
Additional Imaging Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860
The Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .861
The Concurrent API Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
java.util.concurrent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
java.util.concurrent.atomic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
java.util.concurrent.locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Using Synchronization Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
CountDownLatch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
CyclicBarrier. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
Exchanger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
Phaser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875
Using an Executor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
A Simple Executor Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Using Callable and Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885

xxiii

xxiv

Java: The Complete Reference, Eighth Edition

Chapter 28

Part III
Chapter 29

The TimeUnit Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
The Concurrent Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889
Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889
Atomic Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892
Parallel Programming via the Fork/Join Framework . . . . . . . . . . . . . . 893
The Main Fork/Join Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894
The Divide-and-Conquer Strategy . . . . . . . . . . . . . . . . . . . . . . . . . 897
A Simple First Fork/Join Example . . . . . . . . . . . . . . . . . . . . . . . . . 898
Understanding the Impact of the Level of Parallelism . . . . . . . . . 900
An Example that Uses RecursiveTask. . . . . . . . . . . . . . . . . . . . 903
Executing a Task Asynchronously. . . . . . . . . . . . . . . . . . . . . . . . . . 905
Cancelling a Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Determining a Task’s Completion Status . . . . . . . . . . . . . . . . . . . . 906
Restarting a Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Things to Explore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Some Fork/Join Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908
The Concurrency Utilities Versus Java’s Traditional Approach . . . . . . 908
Regular Expressions and Other Packages . . . . . . . . . . . . . . . . . . . . . . .909
The Core Java API Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909
Regular Expression Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
Matcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912
Regular Expression Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
Demonstrating Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . 913
Two Pattern-Matching Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Exploring Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Remote Method Invocation (RMI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923
A Simple Client/Server Application Using RMI . . . . . . . . . . . . . . 923
Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
DateFormat Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
SimpleDateFormat Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929

Software Development Using Java
Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .933
What Is a Java Bean?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
Advantages of Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
Design Patterns for Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
Design Patterns for Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
Methods and Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
Using the BeanInfo Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
Bound and Constrained Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
Customizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937

Contents

Chapter 30

Chapter 31

Chapter 32

The Java Beans API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
Introspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
PropertyDescriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
EventSetDescriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
MethodDescriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
A Bean Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
Introducing Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .945
The Origins of Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
Swing Is Built on the AWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
Two Key Swing Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
Swing Components Are Lightweight . . . . . . . . . . . . . . . . . . . . . . . 946
Swing Supports a Pluggable Look and Feel . . . . . . . . . . . . . . . . . . 946
The MVC Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
Components and Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
The Top-Level Container Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
The Swing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
A Simple Swing Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
Create a Swing Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
Painting in Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
Painting Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
Compute the Paintable Area. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
A Paint Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
Exploring Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .965
JLabel and ImageIcon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
JTextField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
The Swing Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
JButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
JToggleButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
Check Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973
Radio Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
JTabbedPane. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
JList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
JComboBox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984
Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
JTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 990
Continuing Your Exploration of Swing. . . . . . . . . . . . . . . . . . . . . . . . . . 992
Servlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .993
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
The Life Cycle of a Servlet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
Servlet Development Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
Using Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995

xxv

xxvi

Java: The Complete Reference, Eighth Edition

A Simple Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996
Create and Compile the Servlet Source Code . . . . . . . . . . . . . . . . 997
Start Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
Start a Web Browser and Request the Servlet . . . . . . . . . . . . . . . . 998
The Servlet API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
The javax.servlet Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
The Servlet Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999
The ServletConfig Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
The ServletContext Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
The ServletRequest Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
The ServletResponse Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
The GenericServlet Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
The ServletInputStream Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
The ServletOutputStream Class . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
The Servlet Exception Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
Reading Servlet Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
The javax.servlet.http Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004
The HttpServletRequest Interface . . . . . . . . . . . . . . . . . . . . . . . . 1004
The HttpServletResponse Interface . . . . . . . . . . . . . . . . . . . . . . . 1005
The HttpSession Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006
The HttpSessionBindingListener Interface . . . . . . . . . . . . . . . . . 1006
The Cookie Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007
The HttpServlet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008
The HttpSessionEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
The HttpSessionBindingEvent Class . . . . . . . . . . . . . . . . . . . . . . 1010
Handling HTTP Requests and Responses . . . . . . . . . . . . . . . . . . . . . . 1010
Handling HTTP GET Requests . . . . . . . . . . . . . . . . . . . . . . . . . . 1010
Handling HTTP POST Requests . . . . . . . . . . . . . . . . . . . . . . . . . 1012
Using Cookies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013
Session Tracking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015

Part IV
Chapter 33

Applying Java
Financial Applets and Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1019
Finding the Payments for a Loan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1020
The RegPay Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024
The init( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024
The makeGUI( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024
The actionPerformed( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . 1027
The compute( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028
Finding the Future Value of an Investment . . . . . . . . . . . . . . . . . . . . . 1028
Finding the Initial Investment Required to Achieve
a Future Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Finding the Initial Investment Needed for a Desired Annuity . . . . . . 1036
Finding the Maximum Annuity for a Given Investment . . . . . . . . . . . 1040
Finding the Remaining Balance on a Loan . . . . . . . . . . . . . . . . . . . . . 1044

Contents

Chapter 34

Appendix

Creating Financial Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
Converting the RegPay Applet into a Servlet. . . . . . . . . . . . . . . . 1048
The RegPayS Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
Some Things to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
Creating a Download Manager in Java . . . . . . . . . . . . . . . . . . . . . . . . .1053
Understanding Internet Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
An Overview of the Download Manager. . . . . . . . . . . . . . . . . . . . . . . . 1054
The Download Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055
The Download Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
The Download Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
The download( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
The run( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
The stateChanged( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
Action and Accessor Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
The ProgressRenderer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
The DownloadsTableModel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
The addDownload( ) Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
The clearDownload( ) Method. . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
The getColumnClass( ) Method. . . . . . . . . . . . . . . . . . . . . . . . . . 1067
The getValueAt( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
The update( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
The DownloadManager Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
The DownloadManager Variables. . . . . . . . . . . . . . . . . . . . . . . . . 1073
The DownloadManager Constructor . . . . . . . . . . . . . . . . . . . . . . 1074
The verifyUrl( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
The tableSelectionChanged( ) Method . . . . . . . . . . . . . . . . . . . . 1075
The updateButtons( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075
Handling Action Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076
Compiling and Running the Download Manager . . . . . . . . . . . . . . . . 1076
Enhancing the Download Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
Using Java’s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . .1079
The javadoc Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1079
@author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1080
{@code} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1080
@deprecated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1080
{@docRoot}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
@exception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
{@inheritDoc}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
{@link}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
{@linkplain} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
{@literal} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
@param . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
@return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1082
@see . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1082
@serial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1082

xxvii

xxviii

Java: The Complete Reference, Eighth Edition

@serialData. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@serialField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@since. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
{@value} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The General Form of a Documentation Comment . . . . . . . . . . . . . . .
What javadoc Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Example that Uses Documentation Comments . . . . . . . . . . . . . . .

1082
1082
1082
1083
1083
1083
1083
1083
1084

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1087

Preface

J

ava is one of the world’s most important and widely used computer languages.
Furthermore, it has held that distinction for many years. Unlike some other computer
languages whose influence has waned with the passage of time, Java’s has grown
stronger. Java leapt to the forefront of Internet programming with its first release. Each
subsequent version has solidified that position. Today, it is still the first and best choice for
developing web-based applications. Java is also part of the smartphone revolution because
it is used for Android programming. Simply put: much of the modern world runs on Java
code. Java really is that important.
A key reason for Java’s success is its agility. Since its original 1.0 release, Java has
continually adapted to changes in the programming environment and to changes in the
way that programmers program. Most importantly, it has not just followed the trends, it has
helped create them. Java’s ability to accommodate the fast rate of change in the computing
world is a crucial part of why it has been and continues to be so successful.
Since this book was first published in 1996, it has gone through several editions, each
reflecting the ongoing evolution of Java. This is the Eighth edition, and it has been updated
for Java SE 7. As a result, it contains a substantial amount of new material. For example, it
includes coverage of the Project Coin language enhancements, the expanded features of
NIO (NIO.2), and the Fork/Join Framework. In general, discussions of the new features
are integrated into existing chapters, but because of the many additions to NIO, it is now
discussed in its own chapter. However, the overall structure of the book remains the same.
This means that if you are familiar with the previous edition, you will feel right at home
with this version.

A Book for All Programmers
This book is for all programmers, whether you are a novice or an experienced pro. The
beginner will find its carefully paced discussions and many examples especially helpful. Its
in-depth coverage of Java’s more advanced features and libraries will appeal to the pro. For
both, it offers a lasting resource and handy reference.

xxix

xxx

Java: The Complete Reference, Eighth Edition

What’s Inside
This book is a comprehensive guide to the Java language, describing its syntax, keywords,
and fundamental programming principles. Significant portions of the Java API library are
also examined. The book is divided into four parts, each focusing on a different aspect of
the Java programming environment.
Part I presents an in-depth tutorial of the Java language. It begins with the basics,
including such things as data types, operators, control statements, and classes. It then
moves on to inheritance, packages, interfaces, exception handling, and multithreading.
The final chapters in Part I describe annotations, enumerations, autoboxing, and generics.
I/O and applets are also introduced.
Part II examines key aspects of Java’s standard API library. Topics include strings, I/O,
networking, the standard utilities, the Collections Framework, applets, GUI-based controls,
imaging, and concurrency (including the new Fork/Join Framework).
Part III looks at three important Java technologies: Java Beans, servlets, and Swing.
Part IV contains two chapters that show examples of Java in action. The first chapter
develops several applets that perform various popular financial calculations, such as
computing the regular payment on a loan or the minimum investment needed to withdraw
a desired monthly annuity. This chapter also shows how to convert those applets into
servlets. The second chapter develops a download manager that oversees the downloading
of files. It includes the ability to start, stop, and resume a transfer. Both chapters are adapted
from my book The Art of Java, which I co-authored with James Holmes.

Don’t Forget: Code on the Web
Remember, the source code for all of the examples in this book is available free-of-charge
on the Web at www.oraclepressbooks.com.

Special Thanks
I want to give special thanks to Patrick Naughton, Joe O’Neil, James Holmes, and Danny
Coward.
Patrick Naughton was one of the creators of the Java language. He also helped write
the first edition of this book. For example, among many other contributions, much of the
material in Chapters 19, 21, and 26 was initially provided by Patrick. His insights, expertise,
and energy contributed greatly to the success of that book.
During the preparation of the second and third editions of this book, Joe O’Neil
provided initial drafts for the material now found in Chapters 28, 29, 31, and 32 of this
edition. Joe helped on several of my books and his input has always been top-notch.
James Holmes provided Chapter 34. James is an extraordinary programmer and author.
He was my co-author on The Art of Java and is the author of Struts: The Complete Reference™,
and a co-author of JSF: The Complete Reference™.
Danny Coward is the technical editor for this edition of the book. His advice, insights,
and suggestions were of great value and much appreciated.
HERBERT SCHILDT

For Further Study
Java: The Complete Reference™ 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:
Herb Schildt’s Java Programming Cookbook
Java: A Beginner’s Guide
Swing: A Beginner’s Guide
The Art Of Java
To learn about C++, you will find these books especially helpful:
C++: The Complete Reference™
Herb Schildt’s C++ Programming Cookbook
C++: A Beginner’s Guide
The Art of C++
C++ From the Ground Up
STL Programming From the Ground Up
To learn about C#, we suggest the following Schildt books:
C#: The Complete Reference™
C#: A Beginner’s Guide
To learn about the C language, the following title will be of interest:
C: The Complete Reference™

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

This page intentionally left blank

PART

I
CHAPTER 1
The History and Evolution
of Java

CHAPTER 2
An Overview of Java

CHAPTER 3
Data Types, Variables,
and Arrays

CHAPTER 4
Operators

CHAPTER 5
Control Statements

CHAPTER 6
Introducing Classes

CHAPTER 7
A Closer Look at Methods
and Classes

CHAPTER 8
Inheritance

CHAPTER 9
Packages and Interfaces

CHAPTER 10
Exception Handling

CHAPTER 11
Multithreaded Programming

The Java Language

CHAPTER 12
Enumerations, Autoboxing,
and Annotations (Metadata)

CHAPTER 13
I/O, Applets, and
Other Topics

CHAPTER 14
Generics

CHAPTER

32
1

The History and Evolution
of Java

To fully understand Java, one must understand the reasons behind its creation, the forces
that shaped it, and the legacy that it inherits. Like the successful computer languages that
came before, Java is a blend of the best elements of its rich heritage combined with the
innovative concepts required by its unique mission. While the remaining chapters of
this book describe the practical aspects of Java—including its syntax, key libraries, and
applications—this chapter explains how and why Java came about, what makes it so
important, and how it has evolved over the years.
Although Java has become inseparably linked with the online environment of the
Internet, it is important to remember that Java is first and foremost a programming
language. Computer language innovation and development occurs for two fundamental
reasons:
• To adapt to changing environments and uses
• To implement refinements and improvements in the art of programming
As you will see, the development of Java was driven by both elements in nearly equal
measure.

Java’s Lineage
Java is related to C++, which is a direct descendant of C. Much of the character of Java is
inherited from these two languages. From C, Java derives its syntax. Many of Java’s objectoriented features were influenced by C++. In fact, several of Java’s defining characteristics
come from—or are responses to—its predecessors. Moreover, the creation of Java was
deeply rooted in the process of refinement and adaptation that has been occurring in
computer programming languages for the past several decades. For these reasons, this
section reviews the sequence of events and forces that led to Java. As you will see, each
innovation in language design was driven by the need to solve a fundamental problem
that the preceding languages could not solve. Java is no exception.

3

4

PART I

The Java Language

The Birth of Modern Programming: C
The C language shook the computer world. Its impact should not be underestimated, because
it fundamentally changed the way programming was approached and thought about. The
creation of C was a direct result of the need for a structured, efficient, high-level language
that could replace assembly code when creating systems programs. As you probably know,
when a computer language is designed, trade-offs are often made, such as the following:
• Ease-of-use versus power
• Safety versus efficiency
• Rigidity versus extensibility
Prior to C, programmers usually had to choose between languages that optimized one
set of traits or the other. For example, although FORTRAN could be used to write fairly
efficient programs for scientific applications, it was not very good for system code. And
while BASIC was easy to learn, it wasn’t very powerful, and its lack of structure made its
usefulness questionable for large programs. Assembly language can be used to produce
highly efficient programs, but it is not easy to learn or use effectively. Further, debugging
assembly code can be quite difficult.
Another compounding problem was that early computer languages such as BASIC,
COBOL, and FORTRAN were not designed around structured principles. Instead, they
relied upon the GOTO as a primary means of program control. As a result, programs
written using these languages tended to produce “spaghetti code”—a mass of tangled
jumps and conditional branches that make a program virtually impossible to understand.
While languages like Pascal are structured, they were not designed for efficiency, and failed
to include certain features necessary to make them applicable to a wide range of programs.
(Specifically, given the standard dialects of Pascal available at the time, it was not practical
to consider using Pascal for systems-level code.)
So, just prior to the invention of C, no one language had reconciled the conflicting
attributes that had dogged earlier efforts. Yet the need for such a language was pressing. By
the early 1970s, the computer revolution was beginning to take hold, and the demand for
software was rapidly outpacing programmers’ ability to produce it. A great deal of effort was
being expended in academic circles in an attempt to create a better computer language.
But, and perhaps most importantly, a secondary force was beginning to be felt. Computer
hardware was finally becoming common enough that a critical mass was being reached. No
longer were computers kept behind locked doors. For the first time, programmers were
gaining virtually unlimited access to their machines. This allowed the freedom to experiment.
It also allowed programmers to begin to create their own tools. On the eve of C’s creation,
the stage was set for a quantum leap forward in computer languages.
Invented and first implemented by Dennis Ritchie on a DEC PDP-11 running the UNIX
operating system, C was the result of a development process that started with an older
language called BCPL, developed by Martin Richards. BCPL influenced a language called
B, invented by Ken Thompson, which led to the development of C in the 1970s. For many
years, the de facto standard for C was the one supplied with the UNIX operating system and
described in The C Programming Language by Brian Kernighan and Dennis Ritchie (PrenticeHall, 1978). C was formally standardized in December 1989, when the American National
Standards Institute (ANSI) standard for C was adopted.

The History and Evolution of Java

The creation of C is considered by many to have marked the beginning of the modern
age of computer languages. It successfully synthesized the conflicting attributes that had so
troubled earlier languages. The result was a powerful, efficient, structured language that
was relatively easy to learn. It also included one other, nearly intangible aspect: it was a
programmer’s language. Prior to the invention of C, computer languages were generally
designed either as academic exercises or by bureaucratic committees. C is different. It was
designed, implemented, and developed by real, working programmers, reflecting the way
that they approached the job of programming. Its features were honed, tested, thought
about, and rethought by the people who actually used the language. The result was a
language that programmers liked to use. Indeed, C quickly attracted many followers
who had a near-religious zeal for it. As such, it found wide and rapid acceptance in the
programmer community. In short, C is a language designed by and for programmers.
As you will see, Java inherited this legacy.

C++: The Next Step
During the late 1970s and early 1980s, C became the dominant computer programming
language, and it is still widely used today. Since C is a successful and useful language, you
might ask why a need for something else existed. The answer is complexity. Throughout the
history of programming, the increasing complexity of programs has driven the need for
better ways to manage that complexity. C++ is a response to that need. To better understand
why managing program complexity is fundamental to the creation of C++, consider the
following.
Approaches to programming have changed dramatically since the invention of the
computer. For example, when computers were first invented, programming was done by
manually toggling in the binary machine instructions by use of the front panel. As long as
programs were just a few hundred instructions long, this approach worked. As programs grew,
assembly language was invented so that a programmer could deal with larger, increasingly
complex programs by using symbolic representations of the machine instructions. As
programs continued to grow, high-level languages were introduced that gave the programmer
more tools with which to handle complexity.
The first widespread language was, of course, FORTRAN. While FORTRAN was an
impressive first step, it is hardly a language that encourages clear and easy-to-understand
programs. The 1960s gave birth to structured programming. This is the method of programming
championed by languages such as C. The use of structured languages enabled programmers
to write, for the first time, moderately complex programs fairly easily. However, even with
structured programming methods, once a project reaches a certain size, its complexity
exceeds what a programmer can manage. By the early 1980s, many projects were pushing
the structured approach past its limits. To solve this problem, a new way to program was
invented, called object-oriented programming (OOP). Object-oriented programming is discussed
in detail later in this book, but here is a brief definition: OOP is a programming methodology
that helps organize complex programs through the use of inheritance, encapsulation, and
polymorphism.
In the final analysis, although C is one of the world’s great programming languages,
there is a limit to its ability to handle complexity. Once the size of a program exceeds a
certain point, it becomes so complex that it is difficult to grasp as a totality. While the
precise size at which this occurs differs, depending upon both the nature of the program
and the programmer, there is always a threshold at which a program becomes unmanageable.

5

Part I

Chapter 1

6

PART I

The Java Language

C++ added features that enabled this threshold to be broken, allowing programmers to
comprehend and manage larger programs.
C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell Laboratories
in Murray Hill, New Jersey. Stroustrup initially called the new language “C with Classes.”
However, in 1983, the name was changed to C++. C++ extends C by adding object-oriented
features. Because C++ is built on the foundation of C, it includes all of C’s features, attributes,
and benefits. This is a crucial reason for the success of C++ as a language. The invention of
C++ was not an attempt to create a completely new programming language. Instead, it was
an enhancement to an already highly successful one.

The Stage Is Set for Java
By the end of the 1980s and the early 1990s, object-oriented programming using C++ took
hold. Indeed, for a brief moment it seemed as if programmers had finally found the perfect
language. Because C++ blended the high efficiency and stylistic elements of C with the
object-oriented paradigm, it was a language that could be used to create a wide range of
programs. However, just as in the past, forces were brewing that would, once again, drive
computer language evolution forward. Within a few years, the World Wide Web and the
Internet would reach critical mass. This event would precipitate another revolution in
programming.

The Creation of Java
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first working
version. This language was initially called “Oak,” but was renamed “Java” in 1995. Between
the initial implementation of Oak in the fall of 1992 and the public announcement of Java
in the spring of 1995, many more people contributed to the design and evolution of the
language. Bill Joy, Arthur van Hoff, Jonathan Payne, Frank Yellin, and Tim Lindholm were
key contributors to the maturing of the original prototype.
Somewhat surprisingly, the original impetus for Java was not the Internet! Instead, the
primary motivation was the need for a platform-independent (that is, architecture-neutral)
language that could be used to create software to be embedded in various consumer
electronic devices, such as microwave ovens and remote controls. As you can probably
guess, many different types of CPUs are used as controllers. The trouble with C and C++
(and most other languages) is that they are designed to be compiled for a specific target.
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 is that compilers are
expensive and time-consuming to create. An easier—and more cost-efficient—solution
was needed. In an attempt to find such a solution, Gosling and others began work on a
portable, platform-independent language that could be used to produce code that would
run on a variety of CPUs under differing environments. This effort ultimately led to the
creation of Java.
About the time that the details of Java were being worked out, a second, and ultimately
more important, factor was emerging that would play a crucial role in the future of Java.
This second force was, of course, the World Wide Web. Had the Web not taken shape at
about the same time that Java was being implemented, Java might have remained a useful
but obscure language for programming consumer electronics. However, with the emergence

The History and Evolution of Java

of the World Wide Web, Java was propelled to the forefront of computer language design,
because the Web, too, demanded portable programs.
Most programmers learn early in their careers that portable programs are as elusive as they
are desirable. While the quest for a way to create efficient, portable (platform-independent)
programs is nearly as old as the discipline of programming itself, it had taken a back seat
to other, more pressing problems. Further, because (at that time) much of the computer
world had divided itself into the three competing camps of Intel, Macintosh, and UNIX,
most programmers stayed within their fortified boundaries, and the urgent need for
portable code was reduced. However, with the advent of the Internet and the Web, the
old problem of portability returned with a vengeance. After all, the Internet consists of a
diverse, distributed universe populated with various types of computers, operating systems,
and CPUs. Even though many kinds of platforms are attached to the Internet, users would
like them all to be able to run the same program. What was once an irritating but lowpriority problem had become a high-profile necessity.
By 1993, it became obvious to members of the Java design team that the problems of
portability frequently encountered when creating code for embedded controllers are also
found when attempting to create code for the Internet. In fact, the same problem that Java
was initially designed to solve on a small scale could also be applied to the Internet on a
large scale. This realization caused the focus of Java to switch from consumer electronics
to Internet programming. So, while the desire for an architecture-neutral programming
language provided the initial spark, the Internet ultimately led to Java’s large-scale success.
As mentioned earlier, Java derives much of its character from C and C++. This is by intent.
The Java designers knew that using the familiar syntax of C and echoing the object-oriented
features of C++ would make their language appealing to the legions of experienced C/C++
programmers. In addition to the surface similarities, Java shares some of the other attributes
that helped make C and C++ successful. First, Java was designed, tested, and refined by real,
working programmers. It is a language grounded in the needs and experiences of the
people who devised it. Thus, Java is a programmer’s language. Second, Java is cohesive and
logically consistent. Third, except for those constraints imposed by the Internet environment,
Java gives you, the programmer, full control. If you program well, your programs reflect it.
If you program poorly, your programs reflect that, too. Put differently, Java is not a language
with training wheels. It is a language for professional programmers.
Because of the similarities between Java and C++, it is tempting to think of Java as
simply the “Internet version of C++.” However, to do so would be a large mistake. Java has
significant practical and philosophical differences. While it is true that Java was influenced
by C++, it is not an enhanced version of C++. For example, Java is neither upwardly nor
downwardly compatible with C++. Of course, the similarities with C++ are significant, and if
you are a C++ programmer, then you will feel right at home with Java. One other point: Java
was not designed to replace C++. Java was designed to solve a certain set of problems. C++
was designed to solve a different set of problems. Both will coexist for many years to come.
As mentioned at the start of this chapter, computer languages evolve for two reasons:
to adapt to changes in environment and to implement advances in the art of programming.
The environmental change that prompted Java was the need for platform-independent
programs destined for distribution on the Internet. However, Java also embodies changes
in the way that people approach the writing of programs. For example, Java enhanced
and refined the object-oriented paradigm used by C++, added integrated support for
multithreading, and provided a library that simplified Internet access. In the final analysis,

7

Part I

Chapter 1

8

PART I

The Java Language

though, it was not the individual features of Java that made it so remarkable. Rather, it was
the language as a whole. Java was the perfect response to the demands of the then newly
emerging, highly distributed computing universe. Java was to Internet programming what
C was to system programming: a revolutionary force that changed the world.

The C# Connection
The reach and power of Java continues to be felt in the world of computer language
development. Many of its innovative features, constructs, and concepts have become part
of the baseline for any new language. The success of Java is simply too important to ignore.
Perhaps the most important example of Java’s influence is C#. Created by Microsoft to
support the .NET Framework, C# is closely related to Java. For example, both share the
same general syntax, support distributed programming, and utilize the same object model.
There are, of course, differences between Java and C#, but the overall “look and feel” of
these languages is very similar. This “cross-pollination” from Java to C# is the strongest
testimonial to date that Java redefined the way we think about and use a computer language.

How Java Changed the Internet
The Internet helped catapult Java to the forefront of programming, and Java, in turn, had
a profound effect on the Internet. In addition to simplifying web programming in general,
Java innovated a new type of networked program called the applet that changed the way
the online world thought about content. Java also addressed some of the thorniest issues
associated with the Internet: portability and security. Let’s look more closely at each of these.

Java Applets
An applet is a special kind of Java program that is designed to be transmitted over the Internet
and automatically executed by a Java-compatible web browser. Furthermore, an applet is
downloaded on demand, without further interaction with the user. If the user clicks a link
that contains an applet, the applet will be automatically downloaded and run in the browser.
Applets are intended to be small programs. They are typically used to display data provided
by the server, handle user input, or provide simple functions, such as a loan calculator, that
execute locally, rather than on the server. In essence, the applet allows some functionality to
be moved from the server to the client.
The creation of the applet changed Internet programming because it expanded the
universe of objects that can move about freely in cyberspace. In general, there are two very
broad categories of objects that are transmitted between the server and the client: passive
information and dynamic, active programs. For example, when you read your e-mail, you
are viewing passive data. Even when you download a program, the program’s code is still
only passive data until you execute it. By contrast, the applet is a dynamic, self-executing
program. Such a program is an active agent on the client computer, yet it is initiated by
the server.
As desirable as dynamic, networked programs are, they also present serious problems
in the areas of security and portability. Obviously, a program that downloads and executes
automatically on the client computer must be prevented from doing harm. It must also be
able to run in a variety of different environments and under different operating systems.
As you will see, Java solved these problems in an effective and elegant way. Let’s look a bit
more closely at each.

Chapter 1

The History and Evolution of Java

9

As you are likely aware, every time you download a “normal” program, you are taking a risk,
because the code you are downloading might contain a virus, Trojan horse, or other harmful
code. At the core of the problem is the fact that malicious code can cause its damage because
it has gained unauthorized access to system resources. For example, a virus program might
gather private information, such as credit card numbers, bank account balances, and
passwords, by searching the contents of your computer’s local file system. In order for Java to
enable applets to be downloaded and executed on the client computer safely, it was necessary
to prevent an applet from launching such an attack.
Java achieved this protection by confining an applet to the Java execution environment
and not allowing it access to other parts of the computer. (You will see how this is
accomplished shortly.) The ability to download applets with confidence that no harm will
be done and that no security will be breached is considered by many to be the single most
innovative aspect of Java.

Portability
Portability is a major aspect of the Internet because there are many different types of
computers and operating systems connected to it. If a Java program were to be run on
virtually any computer connected to the Internet, there needed to be some way to enable
that program to execute on different systems. For example, in the case of an applet, the
same applet must be able to be downloaded and executed by the wide variety of CPUs,
operating systems, and browsers connected to the Internet. It is not practical to have
different versions of the applet for different computers. The same code must work on all
computers. Therefore, some means of generating portable executable code was needed. As
you will soon see, the same mechanism that helps ensure security also helps create portability.

Java’s Magic: The Bytecode
The key that allows Java to solve both the security and the portability problems just described
is that the output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is
a highly optimized set of instructions designed to be executed by the Java run-time system,
which is called the Java Virtual Machine (JVM). In essence, the original JVM was designed as
an interpreter for bytecode. This may come as a bit of a surprise since many modern languages
are designed to be compiled into executable code because of performance concerns.
However, the fact that a Java program is executed by the JVM helps solve the major
problems associated with web-based programs. Here is why.
Translating a Java program into bytecode makes it much easier to run a program in
a wide variety of environments because only the JVM needs to be implemented for each
platform. Once the run-time package exists for a given system, any Java program can run
on it. Remember, although the details of the JVM will differ from platform to platform, all
understand the same Java bytecode. If a Java program were compiled to native code, then
different versions of the same program would have to exist for each type of CPU connected
to the Internet. This is, of course, not a feasible solution. Thus, the execution of bytecode
by the JVM is the easiest way to create truly portable programs.
The fact that a Java program is executed by the JVM also helps to make it secure.
Because the JVM is in control, it can contain the program and prevent it from generating

Part I

Security

10

PART I

The Java Language

side effects outside of the system. As you will see, safety is also enhanced by certain
restrictions that exist in the Java language.
In general, when a program is compiled to an intermediate form and then interpreted
by a virtual machine, it runs slower than it would run if compiled to executable code.
However, with Java, the differential between the two is not so great. Because bytecode has
been highly optimized, the use of bytecode enables the JVM to execute programs much
faster than you might expect.
Although Java was designed as an interpreted language, there is nothing about Java that
prevents on-the-fly compilation of bytecode into native code in order to boost performance.
For this reason, the HotSpot technology was introduced not long after Java’s initial release.
HotSpot provides a Just-In-Time (JIT) compiler for bytecode. When a JIT compiler is part
of the JVM, selected portions of bytecode are compiled into executable code in real time,
on a piece-by-piece, demand basis. It is important to understand that it is not practical to
compile an entire Java program into executable code all at once, because Java performs
various run-time checks that can be done only at run time. Instead, a JIT compiler compiles
code as it is needed, during execution. Furthermore, not all sequences of bytecode are
compiled—only those that will benefit from compilation. The remaining code is simply
interpreted. However, the just-in-time approach still yields a significant performance boost.
Even when dynamic compilation is applied to bytecode, the portability and safety features
still apply, because the JVM is still in charge of the execution environment.

Servlets: Java on the Server Side
As useful as applets can be, they are just one half of the client/server equation. Not long
after the initial release of Java, it became obvious that Java would also be useful on the
server side. The result was the servlet. A servlet is a small program that executes on the
server. Just as applets dynamically extend the functionality of a web browser, servlets
dynamically extend the functionality of a web server. Thus, with the advent of the servlet,
Java spanned both sides of the client/server connection.
Servlets are used to create dynamically generated content that is then served to the
client. For example, an online store might use a servlet to look up the price for an item in a
database. The price information is then used to dynamically generate a web page that is sent
to the browser. Although dynamically generated content is available through mechanisms
such as CGI (Common Gateway Interface), the servlet offers several advantages, including
increased performance.
Because servlets (like all Java programs) are compiled into bytecode and executed by
the JVM, they are highly portable. Thus, the same servlet can be used in a variety of
different server environments. The only requirements are that the server support the JVM
and a servlet container.

The Java Buzzwords
No discussion of Java’s history is complete without a look at the Java buzzwords. Although
the fundamental forces that necessitated the invention of Java are portability and security,
other factors also played an important role in molding the final form of the language. The
key considerations were summed up by the Java team in the following list of buzzwords:
• Simple
• Secure

Chapter 1

The History and Evolution of Java

11

• Portable
• Robust
• Multithreaded
• Architecture-neutral
• Interpreted
• High performance
• Distributed
• Dynamic
Two of these buzzwords have already been discussed: secure and portable. Let’s examine
what each of the others implies.

Simple
Java was designed to be easy for the professional programmer to learn and use effectively.
Assuming that you have some programming experience, you will not find Java hard to master.
If you already understand the basic concepts of object-oriented programming, learning Java
will be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will
require very little effort. Because Java inherits the C/C++ syntax and many of the objectoriented features of C++, most programmers have little trouble learning Java.

Object-Oriented
Although influenced by its predecessors, Java was not designed to be source-code compatible
with any other language. This allowed the Java team the freedom to design with a blank
slate. One outcome of this was a clean, usable, pragmatic approach to objects. Borrowing
liberally from many seminal object-software environments of the last few decades, Java
manages to strike a balance between the purist’s “everything is an object” paradigm and
the pragmatist’s “stay out of my way” model. The object model in Java is simple and easy to
extend, while primitive types, such as integers, are kept as high-performance nonobjects.

Robust
The multiplatformed environment of the Web places extraordinary demands on a
program, because the program must execute reliably in a variety of systems. Thus, the
ability to create robust programs was given a high priority in the design of Java. To gain
reliability, Java restricts you in a few key areas to force you to find your mistakes early in
program development. At the same time, Java frees you from having to worry about many
of the most common causes of programming errors. Because Java is a strictly typed
language, it checks your code at compile time. However, it also checks your code at run
time. Many hard-to-track-down bugs that often turn up in hard-to-reproduce run-time
situations are simply impossible to create in Java. Knowing that what you have written
will behave in a predictable way under diverse conditions is a key feature of Java.
To better understand how Java is robust, consider two of the main reasons for program
failure: memory management mistakes and mishandled exceptional conditions (that is,
run-time errors). Memory management can be a difficult, tedious task in traditional

Part I

• Object-oriented

12

PART I

The Java Language

programming environments. For example, in C/C++, the programmer must manually allocate
and free all dynamic memory. This sometimes leads to problems, because programmers will
either forget to free memory that has been previously allocated or, worse, try to free some
memory that another part of their code is still using. Java virtually eliminates these problems
by managing memory allocation and deallocation for you. (In fact, deallocation is completely
automatic, because Java provides garbage collection for unused objects.) Exceptional
conditions in traditional environments often arise in situations such as division by zero or
“file not found,” and they must be managed with clumsy and hard-to-read constructs. Java
helps in this area by providing object-oriented exception handling. In a well-written Java
program, all run-time errors can—and should—be managed by your program.

Multithreaded
Java was designed to meet the real-world requirement of creating interactive, networked
programs. To accomplish this, Java supports multithreaded programming, which allows you
to write programs that do many things simultaneously. The Java run-time system comes with
an elegant yet sophisticated solution for multiprocess synchronization that enables you to
construct smoothly running interactive systems. Java’s easy-to-use approach to multithreading
allows you to think about the specific behavior of your program, not the multitasking
subsystem.

Architecture-Neutral
A central issue for the Java designers was that of code longevity and portability. At the time
of Java’s creation, one of the main problems facing programmers was that no guarantee
existed that if you wrote a program today, it would run tomorrow—even on the same
machine. Operating system upgrades, processor upgrades, and changes in core system
resources can all combine to make a program malfunction. The Java designers made
several hard decisions in the Java language and the Java Virtual Machine in an attempt to
alter this situation. Their goal was “write once; run anywhere, any time, forever.” To a great
extent, this goal was accomplished.

Interpreted and High Performance
As described earlier, Java enables the creation of cross-platform programs by compiling into
an intermediate representation called Java bytecode. This code can be executed on any
system that implements the Java Virtual Machine. Most previous attempts at cross-platform
solutions have done so at the expense of performance. As explained earlier, the Java
bytecode was carefully designed so that it would be easy to translate directly into native
machine code for very high performance by using a just-in-time compiler. Java run-time
systems that provide this feature lose none of the benefits of the platform-independent code.

Distributed
Java is designed for the distributed environment of the Internet because it handles TCP/IP
protocols. In fact, accessing a resource using a URL is not much different from accessing a
file. Java also supports Remote Method Invocation (RMI). This feature enables a program to
invoke methods across a network.

Chapter 1

The History and Evolution of Java

13

Java programs carry with them substantial amounts of run-time type information that is used
to verify and resolve accesses to objects at run time. This makes it possible to dynamically link
code in a safe and expedient manner. This is crucial to the robustness of the Java environment,
in which small fragments of bytecode may be dynamically updated on a running system.

The Evolution of Java
The initial release of Java was nothing short of revolutionary, but it did not mark the end of
Java’s era of rapid innovation. Unlike most other software systems that usually settle into a
pattern of small, incremental improvements, Java continued to evolve at an explosive pace.
Soon after the release of Java 1.0, the designers of Java had already created Java 1.1. The
features added by Java 1.1 were more significant and substantial than the increase in the
minor revision number would have you think. Java 1.1 added many new library elements,
redefined the way events are handled, and reconfigured many features of the 1.0 library. It
also deprecated (rendered obsolete) several features originally defined by Java 1.0. Thus,
Java 1.1 both added to and subtracted from attributes of its original specification.
The next major release of Java was Java 2, where the “2” indicates “second generation.”
The creation of Java 2 was a watershed event, marking the beginning of Java’s “modern
age.” The first release of Java 2 carried the version number 1.2. It may seem odd that the
first release of Java 2 used the 1.2 version number. The reason is that it originally referred
to the internal version number of the Java libraries, but then was generalized to refer to
the entire release. With Java 2, Sun repackaged the Java product as J2SE (Java 2 Platform
Standard Edition), and the version numbers began to be applied to that product.
Java 2 added support for a number of new features, such as Swing and the Collections
Framework, and it enhanced the Java Virtual Machine and various programming tools. Java 2
also contained a few deprecations. The most important affected the Thread class in which
the methods suspend( ), resume( ), and stop( ) were deprecated.
J2SE 1.3 was the first major upgrade to the original Java 2 release. For the most part,
it added to existing functionality and “tightened up” the development environment. In
general, programs written for version 1.2 and those written for version 1.3 are source-code
compatible. Although version 1.3 contained a smaller set of changes than the preceding
three major releases, it was nevertheless important.
The release of J2SE 1.4 further enhanced Java. This release contained several important
upgrades, enhancements, and additions. For example, it added the new keyword assert,
chained exceptions, and a channel-based I/O subsystem. It also made changes to the
Collections Framework and the networking classes. In addition, numerous small changes
were made throughout. Despite the significant number of new features, version 1.4
maintained nearly 100 percent source-code compatibility with prior versions.
The next release of Java was J2SE 5, and it was revolutionary. Unlike most of the previous
Java upgrades, which offered important, but measured improvements, J2SE 5 fundamentally
expanded the scope, power, and range of the language. To grasp the magnitude of the
changes that J2SE 5 made to Java, consider the following list of its major new features:
• Generics
• Annotations

Part I

Dynamic

14

PART I

The Java Language

• Autoboxing and auto-unboxing
• Enumerations
• Enhanced, for-each style for loop
• Variable-length arguments (varargs)
• Static import
• Formatted I/O
• Concurrency utilities
This is not a list of minor tweaks or incremental upgrades. Each item in the list represented
a significant addition to the Java language. Some, such as generics, the enhanced for, and
varargs, introduced new syntax elements. Others, such as autoboxing and auto-unboxing,
altered the semantics of the language. Annotations added an entirely new dimension to
programming. In all cases, the impact of these additions went beyond their direct effects.
They changed the very character of Java itself.
The importance of these new features is reflected in the use of the version number “5.”
The next version number for Java would normally have been 1.5. However, the new features
were so significant that a shift from 1.4 to 1.5 just didn’t seem to express the magnitude of
the change. Instead, Sun elected to increase the version number to 5 as a way of emphasizing
that a major event was taking place. Thus, it was named J2SE 5, and the Developer’s Kit was
called JDK 5. However, in order to maintain consistency, Sun decided to use 1.5 as its
internal version number, which is also referred to as the developer version number. The
“5” in J2SE 5 is called the product version number.
The next release of Java was called Java SE 6. Sun once again decided to change the
name of the Java platform. First, notice that the “2” was dropped. Thus, the platform was
now named Java SE, and the official product name was Java Platform, Standard Edition 6.
The Java Developer’s Kit was called JDK 6. As with J2SE 5, the 6 in Java SE 6 is the product
version number. The internal, developer version number is 1.6.
Java SE 6 built on the base of J2SE 5, adding incremental improvements. Java SE 6 added
no major features to the Java language proper, but it did enhance the API libraries, added
several new packages, and offered improvements to the runtime. It also went through several
updates during its (in Java terms) long life cycle, with several upgrades added along the way.
In general, Java SE 6 served to further solidify the advances made by J2SE 5.

Java SE 7
The newest release of Java is called Java SE 7, with the Java Developer’s Kit being called JDK 7,
and an internal version number of 1.7. Java SE 7 is the first major release of Java since Sun
Microsystems was acquired by Oracle (a process that began in April 2009 and that was
completed in January 2010). Java SE 7 contains many new features, including significant
additions to the language and the API libraries. Upgrades to the Java run-time system that
support non-Java languages are also included, but it is the language and library additions
that are of most interest to Java programmers.

The History and Evolution of Java

15

The new language features were developed as part of Project Coin. The purpose of
Project Coin was to identify a number of small changes to the Java language that would be
incorporated into JDK 7. Although these new features are collectively referred to as “small,”
the effects of these changes are quite large in terms of the code they impact. In fact, for
many programmers, these changes may well be the most important new features in Java
SE 7. Here is a list of the new language features:
• A String can now control a switch statement.
• Binary integer literals.
• Underscores in numeric literals.
• An expanded try statement, called try-with-resources, that supports automatic resource
management. (For example, streams can now be closed automatically when they are
no longer needed.)
• Type inference (via the diamond operator) when constructing a generic instance.
• Enhanced exception handling in which two or more exceptions can be caught by a
single catch (multi-catch) and better type checking for exceptions that are rethrown.
• Although not a syntax change, the compiler warnings associated with some types of
varargs methods have been improved, and you have more control over the warnings.
As you can see, even though the Project Coin features were considered small changes to
the language, their benefits will be much larger than the qualifier “small” would suggest. In
particular, the try-with-resources statement will profoundly affect the way that stream-based
code is written. Also, the ability to now use a String to control a switch statement is a
long-desired improvement that will simplify coding in many situations.
Java SE 7 makes several additions to the Java API library. Two of the most important are
the enhancements to the NIO Framework and the addition of the Fork/Join Framework.
NIO (which originally stood for New I/O) was added to Java in version 1.4. However, the
changes proposed for Java SE 7 fundamentally expand its capabilities. So significant are
the changes, that the term NIO.2 is often used.
The Fork/Join Framework provides important support for parallel programming. Parallel
programming is the name commonly given to the techniques that make effective use of
computers that contain more than one processor, including multicore systems. The
advantage that multicore environments offer is the prospect of significantly increased
program performance. The Fork/Join Framework addresses parallel programming by
• Simplifying the creation and use of tasks that can execute concurrently
• Automatically making use of multiple processors
Therefore, by using the Fork/Join Framework, you can easily create scaleable
applications that automatically take advantage of the processors available in the execution
environment. Of course, not all algorithms lend themselves to parallelization, but for those
that do, a significant improvement in execution speed can be obtained.

Part I

Chapter 1

16

PART I

The Java Language

The material in this book has been updated to reflect Java SE 7, with many new
features, updates, and additions indicated throughout.

A Culture of Innovation
Since the beginning, Java has been at the center of a culture of innovation. Its original release
redefined programming for the Internet. The Java Virtual Machine (JVM) and bytecode
changed the way we think about security and portability. The applet (and then the servlet)
made the Web come alive. The Java Community Process (JCP) redefined the way that new
ideas are assimilated into the language. Because Java is used for Android programming, Java
is part of the smartphone revolution. The world of Java has never stood still for very long.
Java SE 7 is the latest release in Java’s ongoing, dynamic history.

CHAPTER

2

An Overview of Java

As in all other computer languages, the elements of Java do not exist in isolation. Rather,
they work together to form the language as a whole. However, this interrelatedness can
make it difficult to describe one aspect of Java without involving several others. Often a
discussion of one feature implies prior knowledge of another. For this reason, this chapter
presents a quick overview of several key features of Java. The material described here will
give you a foothold that will allow you to write and understand simple programs. Most of
the topics discussed will be examined in greater detail in the remaining chapters of Part I.

Object-Oriented Programming
Object-oriented programming (OOP) is at the core of Java. In fact, all Java programs are to
at least some extent object-oriented. OOP is so integral to Java that it is best to understand
its basic principles before you begin writing even simple Java programs. Therefore, this
chapter begins with a discussion of the theoretical aspects of OOP.

Two Paradigms
All computer programs consist of two elements: code and data. Furthermore, a program
can be conceptually organized around its code or around its data. That is, some programs
are written around “what is happening” and others are written around “who is being
affected.” These are the two paradigms that govern how a program is constructed. The first
way is called the process-oriented model. This approach characterizes a program as a series of
linear steps (that is, code). The process-oriented model can be thought of as code acting on
data. Procedural languages such as C employ this model to considerable success. However,
as mentioned in Chapter 1, problems with this approach appear as programs grow larger
and more complex.
To manage increasing complexity, the second approach, called object-oriented programming,
was conceived. Object-oriented programming organizes a program around its data (that is,
objects) and a set of well-defined interfaces to that data. An object-oriented program can
be characterized as data controlling access to code. As you will see, by switching the controlling
entity to data, you can achieve several organizational benefits.

17

18

PART I

The Java Language

Abstraction
An essential element of object-oriented programming is abstraction. Humans manage
complexity through abstraction. For example, people do not think of a car as a set of tens
of thousands of individual parts. They think of it as a well-defined object with its own
unique behavior. This abstraction allows people to use a car to drive to the grocery store
without being overwhelmed by the complexity of the parts that form the car. They can
ignore the details of how the engine, transmission, and braking systems work. Instead,
they are free to utilize the object as a whole.
A powerful way to manage abstraction is through the use of hierarchical classifications.
This allows you to layer the semantics of complex systems, breaking them into more
manageable pieces. From the outside, the car is a single object. Once inside, you see that
the car consists of several subsystems: steering, brakes, sound system, seat belts, heating,
cellular phone, and so on. In turn, each of these subsystems is made up of more specialized
units. For instance, the sound system consists of a radio, a CD player, and/or a tape player.
The point is that you manage the complexity of the car (or any other complex system)
through the use of hierarchical abstractions.
Hierarchical abstractions of complex systems can also be applied to computer programs.
The data from a traditional process-oriented program can be transformed by abstraction
into its component objects. A sequence of process steps can become a collection of messages
between these objects. Thus, each of these objects describes its own unique behavior. You
can treat these objects as concrete entities that respond to messages telling them to do
something. This is the essence of object-oriented programming.
Object-oriented concepts form the heart of Java just as they form the basis for human
understanding. It is important that you understand how these concepts translate into
programs. As you will see, object-oriented programming is a powerful and natural paradigm
for creating programs that survive the inevitable changes accompanying the life cycle of any
major software project, including conception, growth, and aging. For example, once you
have well-defined objects and clean, reliable interfaces to those objects, you can gracefully
decommission or replace parts of an older system without fear.

The Three OOP Principles
All object-oriented programming languages provide mechanisms that help you implement
the object-oriented model. They are encapsulation, inheritance, and polymorphism. Let’s
take a look at these concepts now.

Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse. One way to think about encapsulation
is as a protective wrapper that prevents the code and data from being arbitrarily accessed by
other code defined outside the wrapper. Access to the code and data inside the wrapper is
tightly controlled through a well-defined interface. To relate this to the real world, consider
the automatic transmission on an automobile. It encapsulates hundreds of bits of information
about your engine, such as how much you are accelerating, the pitch of the surface you are
on, and the position of the shift lever. You, as the user, have only one method of affecting
this complex encapsulation: by moving the gear-shift lever. You can’t affect the transmission
by using the turn signal or windshield wipers, for example. Thus, the gear-shift lever is a
well-defined (indeed, unique) interface to the transmission. Further, what occurs inside the

An Overview of Java

19

transmission does not affect objects outside the transmission. For example, shifting gears
does not turn on the headlights! Because an automatic transmission is encapsulated, dozens
of car manufacturers can implement one in any way they please. However, from the driver’s
point of view, they all work the same. This same idea can be applied to programming. The
power of encapsulated code is that everyone knows how to access it and thus can use it
regardless of the implementation details—and without fear of unexpected side effects.
In Java, the basis of encapsulation is the class. Although the class will be examined in
great detail later in this book, the following brief discussion will be helpful now. A class defines
the structure and behavior (data and code) that will be shared by a set of objects. Each object
of a given class contains the structure and behavior defined by the class, as if it were stamped
out by a mold in the shape of the class. For this reason, objects are sometimes referred to as
instances of a class. Thus, a class is a logical construct; an object has physical reality.
When you create a class, you will specify the code and data that constitute that class.
Collectively, these elements are called members of the class. Specifically, the data defined by
the class are referred to as member variables or instance variables. The code that operates on
that data is referred to as member methods or just methods. (If you are familiar with C/C++, it
may help to know that what a Java programmer calls a method, a C/C++ programmer calls a
function.) In properly written Java programs, the methods define how the member variables
can be used. This means that the behavior and interface of a class are defined by the methods
that operate on its instance data.
Since the purpose of a class is to encapsulate complexity, there are mechanisms for
hiding the complexity of the implementation inside the class. Each method or variable in a
class may be marked private or public. The public interface of a class represents everything
that external users of the class need to know, or may know. The private methods and data
can only be accessed by code that is a member of the class. Therefore, any other code that
is not a member of the class cannot access a private method or variable. Since the private
members of a class may only be accessed by other parts of your program through the class’
public methods, you can ensure that no improper actions take place. Of course, this means
that the public interface should be carefully designed not to expose too much of the inner
workings of a class (see Figure 2-1).

Inheritance
Inheritance is the process by which one object acquires the properties of another object. This
is important because it supports the concept of hierarchical classification. As mentioned
earlier, most knowledge is made manageable by hierarchical (that is, top-down) classifications.
For example, a Golden Retriever is part of the classification dog, which in turn is part of the
mammal class, which is under the larger class animal. Without the use of hierarchies, each
object would need to define all of its characteristics explicitly. However, by use of inheritance,
an object need only define those qualities that make it unique within its class. It can inherit
its general attributes from its parent. Thus, it is the inheritance mechanism that makes it
possible for one object to be a specific instance of a more general case. Let’s take a closer
look at this process.
Most people naturally view the world as made up of objects that are related to each
other in a hierarchical way, such as animals, mammals, and dogs. If you wanted to describe
animals in an abstract way, you would say they have some attributes, such as size, intelligence,
and type of skeletal system. Animals also have certain behavioral aspects; they eat, breathe,
and sleep. This description of attributes and behavior is the class definition for animals.

Part I

Chapter 2

20

PART I

The Java Language

Figure 2-1 Encapsulation: public methods can be used to protect private data.
If you wanted to describe a more specific class of animals, such as mammals, they would
have more specific attributes, such as type of teeth and mammary glands. This is known as a
subclass of animals, where animals are referred to as mammals’ superclass.
Since mammals are simply more precisely specified animals, they inherit all of the
attributes from animals. A deeply inherited subclass inherits all of the attributes from each
of its ancestors in the class hierarchy.
Inheritance interacts with encapsulation as well. If a given class encapsulates some
attributes, then any subclass will have the same attributes plus any that it adds as part of its
specialization (see Figure 2-2). This is a key concept that lets object-oriented programs grow
in complexity linearly rather than geometrically. A new subclass inherits all of the attributes
of all of its ancestors. It does not have unpredictable interactions with the majority of the
rest of the code in the system.

An Overview of Java

21

Part I

Chapter 2

Figure 2-2 Labrador inherits the encapsulation of all its superclasses.

Polymorphism
Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface to
be used for a general class of actions. The specific action is determined by the exact nature
of the situation. Consider a stack (which is a last-in, first-out list). You might have a program
that requires three types of stacks. One stack is used for integer values, one for floatingpoint values, and one for characters. The algorithm that implements each stack is the same,
even though the data being stored differs. In a non–object-oriented language, you would be
required to create three different sets of stack routines, with each set using different names.
However, because of polymorphism, in Java you can specify a general set of stack routines
that all share the same names.

22

PART I

The Java Language

More generally, the concept of polymorphism is often expressed by the phrase “one
interface, multiple methods.” This means that it is possible to design a generic interface to a
group of related activities. This helps reduce complexity by allowing the same interface to
be used to specify a general class of action. It is the compiler’s job to select the specific action
(that is, method) as it applies to each situation. You, the programmer, do not need to make
this selection manually. You need only remember and utilize the general interface.
Extending the dog analogy, a dog’s sense of smell is polymorphic. If the dog smells a
cat, it will bark and run after it. If the dog smells its food, it will salivate and run to its bowl.
The same sense of smell is at work in both situations. The difference is what is being smelled,
that is, the type of data being operated upon by the dog’s nose! This same general concept
can be implemented in Java as it applies to methods within a Java program.

Polymorphism, Encapsulation, and Inheritance Work Together
When properly applied, polymorphism, encapsulation, and inheritance combine to produce
a programming environment that supports the development of far more robust and scaleable
programs than does the process-oriented model. A well-designed hierarchy of classes is the
basis for reusing the code in which you have invested time and effort developing and testing.
Encapsulation allows you to migrate your implementations over time without breaking the
code that depends on the public interface of your classes. Polymorphism allows you to create
clean, sensible, readable, and resilient code.
Of the two real-world examples, the automobile more completely illustrates the power
of object-oriented design. Dogs are fun to think about from an inheritance standpoint, but
cars are more like programs. All drivers rely on inheritance to drive different types (subclasses)
of vehicles. Whether the vehicle is a school bus, a Mercedes sedan, a Porsche, or the family
minivan, drivers can all more or less find and operate the steering wheel, the brakes, and
the accelerator. After a bit of gear grinding, most people can even manage the difference
between a stick shift and an automatic, because they fundamentally understand their
common superclass, the transmission.
People interface with encapsulated features on cars all the time. The brake and gas
pedals hide an incredible array of complexity with an interface so simple you can operate
them with your feet! The implementation of the engine, the style of brakes, and the size of
the tires have no effect on how you interface with the class definition of the pedals.
The final attribute, polymorphism, is clearly reflected in the ability of car manufacturers
to offer a wide array of options on basically the same vehicle. For example, you can get an
antilock braking system or traditional brakes, power or rack-and-pinion steering, and 4-, 6-,
or 8-cylinder engines. Either way, you will still press the brake pedal to stop, turn the steering
wheel to change direction, and press the accelerator when you want to move. The same
interface can be used to control a number of different implementations.
As you can see, it is through the application of encapsulation, inheritance, and
polymorphism that the individual parts are transformed into the object known as a car.
The same is also true of computer programs. By the application of object-oriented
principles, the various parts of a complex program can be brought together to form a
cohesive, robust, maintainable whole.
As mentioned at the start of this section, every Java program is object-oriented. Or, put
more precisely, every Java program involves encapsulation, inheritance, and polymorphism.
Although the short example programs shown in the rest of this chapter and in the next few
chapters may not seem to exhibit all of these features, they are nevertheless present. As you

Chapter 2

An Overview of Java

23

A First Simple Program
Now that the basic object-oriented underpinning of Java has been discussed, let’s look at
some actual Java programs. Let’s start by compiling and running the short sample program
shown here. As you will see, this involves a little more work than you might imagine.
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}

NOTE The descriptions that follow use the standard Java SE 7 Development Kit (JDK 7), which is available
from Oracle. If you are using a different Java development environment, then you may need to follow a
different procedure for compiling and executing Java programs. In this case, consult your compiler’s
documentation for details.

Entering the Program
For most computer languages, the name of the file that holds the source code to a program
is immaterial. However, this is not the case with Java. The first thing that you must learn
about Java is that the name you give to a source file is very important. For this example,
the name of the source file should be Example.java. Let’s see why.
In Java, a source file is officially called a compilation unit. It is a text file that contains
(among other things) one or more class definitions. (For now, we will be using source files
that contain only one class.) The Java compiler requires that a source file use the .java
filename extension.
As you can see by looking at the program, the name of the class defined by the program
is also Example. This is not a coincidence. In Java, all code must reside inside a class. By
convention, the name of the main class should match the name of the file that holds the
program. You should also make sure that the capitalization of the filename matches the
class name. The reason for this is that Java is case-sensitive. At this point, the convention
that filenames correspond to class names may seem arbitrary. However, this convention
makes it easier to maintain and organize your programs.

Compiling the Program
To compile the Example program, execute the compiler, javac, specifying the name of the
source file on the command line, as shown here:
C:\>javac Example.java

The javac compiler creates a file called Example.class that contains the bytecode version of
the program. As discussed earlier, the Java bytecode is the intermediate representation of

Part I

will see, many of the features supplied by Java are part of its built-in class libraries, which do
make extensive use of encapsulation, inheritance, and polymorphism.

24

PART I

The Java Language

your program that contains instructions the Java Virtual Machine will execute. Thus, the
output of javac is not code that can be directly executed.
To actually run the program, you must use the Java application launcher called java. To
do so, pass the class name Example as a command-line argument, as shown here:
C:\>java Example

When the program is run, the following output is displayed:
This is a simple Java program.

When Java source code is compiled, each individual class is put into its own output file
named after the class and using the .class extension. This is why it is a good idea to give
your Java source files the same name as the class they contain—the name of the source file
will match the name of the .class file. When you execute java as just shown, you are actually
specifying the name of the class that you want to execute. It will automatically search for a
file by that name that has the .class extension. If it finds the file, it will execute the code
contained in the specified class.

A Closer Look at the First Sample Program
Although Example.java is quite short, it includes several key features that are common to
all Java programs. Let’s closely examine each part of the program.
The program begins with the following lines:
/*
This is a simple Java program.
Call this file "Example.java".
*/

This is a comment. Like most other programming languages, Java lets you enter a remark
into a program’s source file. The contents of a comment are ignored by the compiler.
Instead, a comment describes or explains the operation of the program to anyone who is
reading its source code. In this case, the comment describes the program and reminds you
that the source file should be called Example.java. Of course, in real applications, comments
generally explain how some part of the program works or what a specific feature does.
Java supports three styles of comments. The one shown at the top of the program is
called a multiline comment. This type of comment must begin with /* and end with */.
Anything between these two comment symbols is ignored by the compiler. As the name
suggests, a multiline comment may be several lines long.
The next line of code in the program is shown here:
class Example {

This line uses the keyword class to declare that a new class is being defined. Example
is an identifier that is the name of the class. The entire class definition, including all of its
members, will be between the opening curly brace ({) and the closing curly brace (}). For
the moment, don’t worry too much about the details of a class except to note that in Java,
all program activity occurs within one. This is one reason why all Java programs are (at least
a little bit) object-oriented.

Chapter 2

An Overview of Java

25

// Your program begins with a call to main().

This is the second type of comment supported by Java. A single-line comment begins with
a // and ends at the end of the line. As a general rule, programmers use multiline comments
for longer remarks and single-line comments for brief, line-by-line descriptions. The third
type of comment, a documentation comment, will be discussed in the “Comments” section
later in this chapter.
The next line of code is shown here:
public static void main(String args[ ]) {

This line begins the main( ) method. As the comment preceding it suggests, this is the line
at which the program will begin executing. All Java applications begin execution by calling
main( ). The full meaning of each part of this line cannot be given now, since it involves a
detailed understanding of Java’s approach to encapsulation. However, since most of the
examples in the first part of this book will use this line of code, let’s take a brief look at
each part now.
The public keyword is an access modifier, which allows the programmer to control the
visibility of class members. When a class member is preceded by public, then that member
may be accessed by code outside the class in which it is declared. (The opposite of public is
private, which prevents a member from being used by code defined outside of its class.) In
this case, main( ) must be declared as public, since it must be called by code outside of its
class when the program is started. The keyword static allows main( ) to be called without
having to instantiate a particular instance of the class. This is necessary since main( ) is
called by the Java Virtual Machine before any objects are made. The keyword void simply
tells the compiler that main( ) does not return a value. As you will see, methods may also
return values. If all this seems a bit confusing, don’t worry. All of these concepts will be
discussed in detail in subsequent chapters.
As stated, main( ) is the method called when a Java application begins. Keep in mind
that Java is case-sensitive. Thus, Main is different from main. It is important to understand
that the Java compiler will compile classes that do not contain a main( ) method. But java
has no way to run these classes. So, if you had typed Main instead of main, the compiler
would still compile your program. However, java would report an error because it would be
unable to find the main( ) method.
Any information that you need to pass to a method is received by variables specified
within the set of parentheses that follow the name of the method. These variables are called
parameters. If there are no parameters required for a given method, you still need to include
the empty parentheses. In main( ), there is only one parameter, albeit a complicated one.
String args[ ] declares a parameter named args, which is an array of instances of the class
String. (Arrays are collections of similar objects.) Objects of type String store character
strings. In this case, args receives any command-line arguments present when the program
is executed. This program does not make use of this information, but other programs
shown later in this book will.
The last character on the line is the {. This signals the start of main( )’s body. All of the
code that comprises a method will occur between the method’s opening curly brace and its
closing curly brace.

Part I

The next line in the program is the single-line comment, shown here:

26

PART I

The Java Language

One other point: main( ) is simply a starting place for your program. A complex
program will have dozens of classes, only one of which will need to have a main( ) method
to get things started. Furthermore, in some cases, you won’t need main( ) at all. For example,
when creating applets—Java programs that are embedded in web browsers—you won’t use
main( ) since the web browser uses a different means of starting the execution of applets.
The next line of code is shown here. Notice that it occurs inside main( ).
System.out.println("This is a simple Java program.");

This line outputs the string "This is a simple Java program." followed by a new line on the
screen. Output is actually accomplished by the built-in println( ) method. In this case, println( )
displays the string which is passed to it. As you will see, println( ) can be used to display other
types of information, too. The line begins with System.out. While too complicated to explain
in detail at this time, briefly, System is a predefined class that provides access to the system,
and out is the output stream that is connected to the console.
As you have probably guessed, console output (and input) is not used frequently in most
real-world Java applications. Since most modern computing environments are windowed and
graphical in nature, console I/O is used mostly for simple utility programs, demonstration
programs, and server-side code. Later in this book, you will learn other ways to generate
output using Java. But for now, we will continue to use the console I/O methods.
Notice that the println( ) statement ends with a semicolon. All statements in Java end
with a semicolon. The reason that the other lines in the program do not end in a semicolon
is that they are not, technically, statements.
The first } in the program ends main( ), and the last } ends the Example class definition.

A Second Short Program
Perhaps no other concept is more fundamental to a programming language than that of a
variable. As you probably know, a variable is a named memory location that may be assigned
a value by your program. The value of a variable may be changed during the execution of
the program. The next program shows how a variable is declared and how it is assigned a
value. The program also illustrates some new aspects of console output. As the comments
at the top of the program state, you should call this file Example2.java.
/*
Here is another short example.
Call this file "Example2.java".
*/
class Example2 {
public static void main(String args []) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");

Chapter 2

An Overview of Java

27

System.out.println(num);
}

When you run this program, you will see the following output:
This is num: 100
The value of num * 2 is 200

Let’s take a close look at why this output is generated. The first new line in the program
is shown here:
int num; // this declares a variable called num

This line declares an integer variable called num. Java (like most other languages) requires
that variables be declared before they are used.
Following is the general form of a variable declaration:
type var-name;
Here, type specifies the type of variable being declared, and var-name is the name of the
variable. If you want to declare more than one variable of the specified type, you may use a
comma-separated list of variable names. Java defines several data types, including integer,
character, and floating-point. The keyword int specifies an integer type.
In the program, the line
num = 100; // this assigns num the value 100

assigns to num the value 100. In Java, the assignment operator is a single equal sign.
The next line of code outputs the value of num preceded by the string "This is num:".
System.out.println("This is num: " + num);

In this statement, the plus sign causes the value of num to be appended to the string that
precedes it, and then the resulting string is output. (Actually, num is first converted from an
integer into its string equivalent and then concatenated with the string that precedes it. This
process is described in detail later in this book.) This approach can be generalized. Using
the + operator, you can join together as many items as you want within a single println( )
statement.
The next line of code assigns num the value of num times 2. Like most other languages,
Java uses the * operator to indicate multiplication. After this line executes, num will contain
the value 200.
Here are the next two lines in the program:
System.out.print ("The value of num * 2 is ");
System.out.println (num);

Several new things are occurring here. First, the built-in method print( ) is used to
display the string "The value of num * 2 is ". This string is not followed by a newline. This
means that when the next output is generated, it will start on the same line. The print( )
method is just like println( ), except that it does not output a newline character after each
call. Now look at the call to println( ). Notice that num is used by itself. Both print( ) and
println( ) can be used to output values of any of Java’s built-in types.

Part I

}

28

PART I

The Java Language

Two Control Statements
Although Chapter 5 will look closely at control statements, two are briefly introduced here
so that they can be used in example programs in Chapters 3 and 4. They will also help
illustrate an important aspect of Java: blocks of code.

The if Statement
The Java if statement works much like the IF statement in any other language. Further, it is
syntactically identical to the if statements in C, C++, and C#. Its simplest form is shown here:
if(condition) statement;
Here, condition is a Boolean expression. If condition is true, then the statement is executed.
If condition is false, then the statement is bypassed. Here is an example:
if(num < 100) System.out.println("num is less than 100");

In this case, if num contains a value that is less than 100, the conditional expression is
true, and println( ) will execute. If num contains a value greater than or equal to 100, then
the println( ) method is bypassed.
As you will see in Chapter 4, Java defines a full complement of relational operators
which may be used in a conditional expression. Here are a few:

Operator

Meaning

<

Less than

>

Greater than

==

Equal to

Notice that the test for equality is the double equal sign.
Here is a program that illustrates the if statement:
/*
Demonstrate the if.
Call this file "IfSample.java".
*/
class IfSample {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y) System.out.println("x is less than y");
x = x * 2;
if(x == y) System.out.println("x now equal to y");

Chapter 2

An Overview of Java

29

// this won't display anything
if(x == y) System.out.println("you won't see this");
}
}

The output generated by this program is shown here:
x is less than y
x now equal to y
x now greater than y

Notice one other thing in this program. The line
int x, y;

declares two variables, x and y, by use of a comma-separated list.

The for Loop
As you may know from your previous programming experience, loop statements are an
important part of nearly any programming language. Java is no exception. In fact, as you
will see in Chapter 5, Java supplies a powerful assortment of loop constructs. Perhaps the
most versatile is the for loop. The simplest form of the for loop is shown here:
for(initialization; condition; iteration) statement;
In its most common form, the initialization portion of the loop sets a loop control
variable to an initial value. The condition is a Boolean expression that tests the loop control
variable. If the outcome of that test is true, the for loop continues to iterate. If it is false,
the loop terminates. The iteration expression determines how the loop control variable is
changed each time the loop iterates. Here is a short program that illustrates the for loop:
/*
Demonstrate the for loop.
Call this file "ForTest.java".
*/
class ForTest {
public static void main(String args[]) {
int x;
for(x = 0; x<10; x = x+1)
System.out.println("This is x: " + x);
}
}

This program generates the following output:
This
This
This
This

is
is
is
is

x:
x:
x:
x:

0
1
2
3

Part I

x = x * 2;
if(x > y) System.out.println("x now greater than y");

30

PART I

The Java Language

This
This
This
This
This
This

is
is
is
is
is
is

x:
x:
x:
x:
x:
x:

4
5
6
7
8
9

In this example, x is the loop control variable. It is initialized to zero in the initialization
portion of the for. At the start of each iteration (including the first one), the conditional
test x < 10 is performed. If the outcome of this test is true, the println( ) statement is
executed, and then the iteration portion of the loop is executed. This process continues
until the conditional test is false.
As a point of interest, in professionally written Java programs you will almost never see
the iteration portion of the loop written as shown in the preceding program. That is, you
will seldom see statements like this:
x = x + 1;

The reason is that Java includes a special increment operator which performs this operation
more efficiently. The increment operator is ++. (That is, two plus signs back to back.) The
increment operator increases its operand by one. By use of the increment operator, the
preceding statement can be written like this:
x++;

Thus, the for in the preceding program will usually be written like this:
for(x = 0; x<10; x++)

You might want to try this. As you will see, the loop still runs exactly the same as it did
before.
Java also provides a decrement operator, which is specified as – –. This operator
decreases its operand by one.

Using Blocks of Code
Java allows two or more statements to be grouped into blocks of code, also called code blocks.
This is done by enclosing the statements between opening and closing curly braces. Once a
block of code has been created, it becomes a logical unit that can be used any place that a
single statement can. For example, a block can be a target for Java’s if and for statements.
Consider this if statement:
if(x < y) { // begin a block
x = y;
y = 0;
} // end of block

Here, if x is less than y, then both statements inside the block will be executed. Thus, the
two statements inside the block form a logical unit, and one statement cannot execute
without the other also executing. The key point here is that whenever you need to logically
link two or more statements, you do so by creating a block.

Chapter 2

An Overview of Java

31

/*
Demonstrate a block of code.
Call this file "BlockTest.java"
*/
class BlockTest {
public static void main(String args[]) {
int x, y;
y = 20;
// the target of this loop is a block
for(x = 0; x<10; x++) {
System.out.println("This is x: " + x);
System.out.println("This is y: " + y);
y = y - 2;
}
}
}

The output generated by this program is shown here:
This
This
This
This
This
This
This
This
This
This
This
This
This
This
This
This
This
This
This
This

is
is
is
is
is
is
is
is
is
is
is
is
is
is
is
is
is
is
is
is

x:
y:
x:
y:
x:
y:
x:
y:
x:
y:
x:
y:
x:
y:
x:
y:
x:
y:
x:
y:

0
20
1
18
2
16
3
14
4
12
5
10
6
8
7
6
8
4
9
2

In this case, the target of the for loop is a block of code and not just a single statement.
Thus, each time the loop iterates, the three statements inside the block will be executed.
This fact is, of course, evidenced by the output generated by the program.
As you will see later in this book, blocks of code have additional properties and uses.
However, the main reason for their existence is to create logically inseparable units of code.

Part I

Let’s look at another example. The following program uses a block of code as the target
of a for loop.

32

PART I

The Java Language

Lexical Issues
Now that you have seen several short Java programs, it is time to more formally describe the
atomic elements of Java. Java programs are a collection of whitespace, identifiers, literals,
comments, operators, separators, and keywords. The operators are described in the next
chapter. The others are described next.

Whitespace
Java is a free-form language. This means that you do not need to follow any special
indentation rules. For instance, the Example program could have been written all on one
line or in any other strange way you felt like typing it, as long as there was at least one
whitespace character between each token that was not already delineated by an operator
or separator. In Java, whitespace is a space, tab, or newline.

Identifiers
Identifiers are used to name things, such as classes, variables, and methods. An identifier
may be any descriptive sequence of uppercase and lowercase letters, numbers, or the
underscore and dollar-sign characters. (The dollar-sign character is not intended for
general use.) They must not begin with a number, lest they be confused with a numeric
literal. Again, Java is case-sensitive, so VALUE is a different identifier than Value. Some
examples of valid identifiers are
AvgTemp

count

a4

$test

this_is_ok

Invalid identifier names include these:
2count

high-temp

Not/ok

Literals
A constant value in Java is created by using a literal representation of it. For example, here
are some literals:
100

98.6

‘X’

“This is a test”

Left to right, the first literal specifies an integer, the next is a floating-point value, the third
is a character constant, and the last is a string. A literal can be used anywhere a value of its
type is allowed.

Comments
As mentioned, there are three types of comments defined by Java. You have already seen
two: single-line and multiline. The third type is called a documentation comment. This type
of comment is used to produce an HTML file that documents your program. The
documentation comment begins with a /** and ends with a */. Documentation comments
are explained in the Appendix.

Chapter 2

An Overview of Java

33

In Java, there are a few characters that are used as separators. The most commonly used
separator in Java is the semicolon. As you have seen, it is used to terminate statements. The
separators are shown in the following table:

Symbol

Name

Purpose

()

Parentheses

Used to contain lists of parameters in method definition and
invocation. Also used for defining precedence in expressions,
containing expressions in control statements, and surrounding
cast types.

{}

Braces

Used to contain the values of automatically initialized arrays.
Also used to define a block of code, for classes, methods, and
local scopes.

[]

Brackets

Used to declare array types. Also used when dereferencing array
values.

;

Semicolon

Terminates statements.

,

Comma

Separates consecutive identifiers in a variable declaration. Also
used to chain statements together inside a for statement.

.

Period

Used to separate package names from subpackages and classes. Also
used to separate a variable or method from a reference variable.

The Java Keywords
There are 50 keywords currently defined in the Java language (see Table 2-1). These
keywords, combined with the syntax of the operators and separators, form the foundation
of the Java language. These keywords cannot be used as identifiers. Thus, they cannot be
used as names for a variable, class, or method.
The keywords const and goto are reserved but not used. In the early days of Java, several
other keywords were reserved for possible future use. However, the current specification for
Java defines only the keywords shown in Table 2-1.

abstract

continue

for

new

switch

assert

default

goto

package

synchronized

boolean

do

if

private

this

break

double

implements

protected

throw

byte

else

import

public

throws

case

enum

instanceof

return

transient

catch

extends

int

short

try

char

final

interface

static

void

class

finally

long

strictfp

volatile

const

float

native

super

while

Table 2-1 Java Keywords

Part I

Separators

34

PART I

The Java Language

In addition to the keywords, Java reserves the following: true, false, and null. These are
values defined by Java. You may not use these words for the names of variables, classes, and
so on.

The Java Class Libraries
The sample programs shown in this chapter make use of two of Java’s built-in methods:
println( ) and print( ). As mentioned, these methods are members of the System class,
which is a class predefined by Java that is automatically included in your programs. In the
larger view, the Java environment relies on several built-in class libraries that contain many
built-in methods that provide support for such things as I/O, string handling, networking,
and graphics. The standard classes also provide support for windowed output. Thus, Java as
a totality is a combination of the Java language itself, plus its standard classes. As you will
see, the class libraries provide much of the functionality that comes with Java. Indeed, part
of becoming a Java programmer is learning to use the standard Java classes. Throughout
Part I of this book, various elements of the standard library classes and methods are
described as needed. In Part II, the class libraries are described in detail.

CHAPTER

3

Data Types, Variables,
and Arrays

This chapter examines three of Java’s most fundamental elements: data types, variables, and
arrays. As with all modern programming languages, Java supports several types of data. You
may use these types to declare variables and to create arrays. As you will see, Java’s approach
to these items is clean, efficient, and cohesive.

Java Is a Strongly Typed Language
It is important to state at the outset that Java is a strongly typed language. Indeed, part
of Java’s safety and robustness comes from this fact. Let’s see what this means. First, every
variable has a type, every expression has a type, and every type is strictly defined. Second,
all assignments, whether explicit or via parameter passing in method calls, are checked for
type compatibility. There are no automatic coercions or conversions of conflicting types as
in some languages. The Java compiler checks all expressions and parameters to ensure that
the types are compatible. Any type mismatches are errors that must be corrected before the
compiler will finish compiling the class.

The Primitive Types
Java defines eight primitive types of data: byte, short, int, long, char, float, double, and
boolean. The primitive types are also commonly referred to as simple types, and both
terms will be used in this book. These can be put in four groups:
• Integers This group includes byte, short, int, and long, which are for whole-valued
signed numbers.
• Floating-point numbers This group includes float and double, which represent
numbers with fractional precision.
• Characters This group includes char, which represents symbols in a character set,
like letters and numbers.
• Boolean This group includes boolean, which is a special type for representing
true/false values.

35

36

PART I

The Java Language

You can use these types as-is, or to construct arrays or your own class types. Thus, they
form the basis for all other types of data that you can create.
The primitive types represent single values—not complex objects. Although Java is
otherwise completely object-oriented, the primitive types are not. They are analogous to
the simple types found in most other non–object-oriented languages. The reason for this
is efficiency. Making the primitive types into objects would have degraded performance
too much.
The primitive types are defined to have an explicit range and mathematical behavior.
Languages such as C and C++ allow the size of an integer to vary based upon the dictates
of the execution environment. However, Java is different. Because of Java’s portability
requirement, all data types have a strictly defined range. For example, an int is always 32 bits,
regardless of the particular platform. This allows programs to be written that are guaranteed
to run without porting on any machine architecture. While strictly specifying the size of an
integer may cause a small loss of performance in some environments, it is necessary in
order to achieve portability.
Let’s look at each type of data in turn.

Integers
Java defines four integer types: byte, short, int, and long. All of these are signed, positive
and negative values. Java does not support unsigned, positive-only integers. Many other
computer languages support both signed and unsigned integers. However, Java’s designers
felt that unsigned integers were unnecessary. Specifically, they felt that the concept of
unsigned was used mostly to specify the behavior of the high-order bit, which defines the sign
of an integer value. As you will see in Chapter 4, Java manages the meaning of the highorder bit differently, by adding a special “unsigned right shift” operator. Thus, the need for
an unsigned integer type was eliminated.
The width of an integer type should not be thought of as the amount of storage it
consumes, but rather as the behavior it defines for variables and expressions of that type.
The Java run-time environment is free to use whatever size it wants, as long as the types
behave as you declared them. The width and ranges of these integer types vary widely, as
shown in this table:

Name

Width

Range

long

64

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

int

32

–2,147,483,648 to 2,147,483,647

short

16

–32,768 to 32,767

byte

8

–128 to 127

Let’s look at each type of integer.

byte
The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to
127. Variables of type byte are especially useful when you’re working with a stream of data
from a network or file. They are also useful when you’re working with raw binary data that
may not be directly compatible with Java’s other built-in types.

Chapter 3

Data Types, Variables, and Arrays

37

byte b, c;

short
short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the leastused Java type. Here are some examples of short variable declarations:
short s;
short t;

int
The most commonly used integer type is int. It is a signed 32-bit type that has a range
from –2,147,483,648 to 2,147,483,647. In addition to other uses, variables of type int are
commonly employed to control loops and to index arrays. Although you might think that
using a byte or short would be more efficient than using an int in situations in which the
larger range of an int is not needed, this may not be the case. The reason is that when byte
and short values are used in an expression they are promoted to int when the expression is
evaluated. (Type promotion is described later in this chapter.) Therefore, int is often the
best choice when an integer is needed.

long
long is a signed 64-bit type and is useful for those occasions where an int type is not large
enough to hold the desired value. The range of a long is quite large. This makes it useful
when big, whole numbers are needed. For example, here is a program that computes the
number of miles that light will travel in a specified number of days:
// Compute distance light travels using long variables.
class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}

Part I

Byte variables are declared by use of the byte keyword. For example, the following
declares two byte variables called b and c:

38

PART I

The Java Language

This program generates the following output:
In 1000 days light will travel about 16070400000000 miles.

Clearly, the result could not have been held in an int variable.

Floating-Point Types
Floating-point numbers, also known as real numbers, are used when evaluating expressions
that require fractional precision. For example, calculations such as square root, or
transcendentals such as sine and cosine, result in a value whose precision requires a floatingpoint type. Java implements the standard (IEEE–754) set of floating-point types and
operators. There are two kinds of floating-point types, float and double, which represent
single- and double-precision numbers, respectively. Their width and ranges are shown here:

Name

Width in Bits

Approximate Range

double

64

4.9e–324 to 1.8e+308

float

32

1.4e–045 to 3.4e+038

Each of these floating-point types is examined next.

float
The type float specifies a single-precision value that uses 32 bits of storage. Single precision is
faster on some processors and takes half as much space as double precision, but will become
imprecise when the values are either very large or very small. Variables of type float are
useful when you need a fractional component, but don’t require a large degree of precision.
For example, float can be useful when representing dollars and cents.
Here are some example float variable declarations:
float hightemp, lowtemp;

double
Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double
precision is actually faster than single precision on some modern processors that have been
optimized for high-speed mathematical calculations. All transcendental math functions,
such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy
over many iterative calculations, or are manipulating large-valued numbers, double is the
best choice.
Here is a short program that uses double variables to compute the area of a circle:
// Compute the area of a circle.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately

Chapter 3

Data Types, Variables, and Arrays

39

System.out.println("Area of circle is " + a);
}
}

Characters
In Java, the data type used to store characters is char. However, C/C++ programmers
beware: char in Java is not the same as char in C or C++. In C/C++, char is 8 bits wide. This
is not the case in Java. Instead, Java uses Unicode to represent characters. Unicode defines a
fully international character set that can represent all of the characters found in all human
languages. It is a unification of dozens of character sets, such as Latin, Greek, Arabic,
Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits.
Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative
chars. The standard set of characters known as ASCII still ranges from 0 to 127 as always,
and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255. Since Java is
designed to allow programs to be written for worldwide use, it makes sense that it would use
Unicode to represent characters. Of course, the use of Unicode is somewhat inefficient for
languages such as English, German, Spanish, or French, whose characters can easily be
contained within 8 bits. But such is the price that must be paid for global portability.
NOTE More information about Unicode can be found at http://www.unicode.org.
Here is a program that demonstrates char variables:
// Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}

This program displays the following output:
ch1 and ch2: X Y

Notice that ch1 is assigned the value 88, which is the ASCII (and Unicode) value that
corresponds to the letter X. As mentioned, the ASCII character set occupies the first 127
values in the Unicode character set. For this reason, all the “old tricks” that you may have
used with characters in other languages will work in Java, too.
Although char is designed to hold Unicode characters, it can also be used as an integer
type on which you can perform arithmetic operations. For example, you can add two

Part I

a = pi * r * r; // compute area

40

PART I

The Java Language

characters together, or increment the value of a character variable. Consider the following
program:
// char variables behave like integers.
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}

The output generated by this program is shown here:
ch1 contains X
ch1 is now Y

In the program, ch1 is first given the value X. Next, ch1 is incremented. This results in ch1
containing Y, the next character in the ASCII (and Unicode) sequence.
NOTE In the formal specification for Java, char is referred to as an integral type, which means that it is
in the same general category as int, short, long, and byte. However, because its principal use is for
representing Unicode characters, char is commonly considered to be in a category of its own.

Booleans
Java has a primitive type, called boolean, for logical values. It can have only one of two
possible values, true or false. This is the type returned by all relational operators, as in the
case of a < b. boolean is also the type required by the conditional expressions that govern the
control statements such as if and for.
Here is a program that demonstrates the boolean type:
// Demonstrate boolean values.
class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");

Chapter 3

Data Types, Variables, and Arrays

41

}
}

The output generated by this program is shown here:
b is
b is
This
10 >

false
true
is executed.
9 is true

There are three interesting things to notice about this program. First, as you can see,
when a boolean value is output by println( ), "true" or "false" is displayed. Second, the value
of a boolean variable is sufficient, by itself, to control the if statement. There is no need to
write an if statement like this:
if(b == true) …

Third, the outcome of a relational operator, such as <, is a boolean value. This is why the
expression 10>9 displays the value "true." Further, the extra set of parentheses around 10>9
is necessary because the + operator has a higher precedence than the >.

A Closer Look at Literals
Literals were mentioned briefly in Chapter 2. Now that the built-in types have been formally
described, let’s take a closer look at them.

Integer Literals
Integers are probably the most commonly used type in the typical program. Any whole
number value is an integer literal. Examples are 1, 2, 3, and 42. These are all decimal values,
meaning they are describing a base 10 number. There are two other bases which can be used
in integer literals, octal (base eight) and hexadecimal (base 16). Octal values are denoted in
Java by a leading zero. Normal decimal numbers cannot have a leading zero. Thus, the
seemingly valid value 09 will produce an error from the compiler, since 9 is outside of octal’s
0 to 7 range. A more common base for numbers used by programmers is hexadecimal,
which matches cleanly with modulo 8 word sizes, such as 8, 16, 32, and 64 bits. You signify a
hexadecimal constant with a leading zero-x, (0x or 0X). The range of a hexadecimal digit is
0 to 15, so A through F (or a through f ) are substituted for 10 through 15.
Integer literals create an int value, which in Java is a 32-bit integer value. Since Java is
strongly typed, you might be wondering how it is possible to assign an integer literal to one
of Java’s other integer types, such as byte or long, without causing a type mismatch error.
Fortunately, such situations are easily handled. When a literal value is assigned to a byte or
short variable, no error is generated if the literal value is within the range of the target type.
An integer literal can always be assigned to a long variable. However, to specify a long
literal, you will need to explicitly tell the compiler that the literal value is of type long. You
do this by appending an upper- or lowercase L to the literal. For example, 0x7ffffffffffffffL
or 9223372036854775807L is the largest long. An integer can also be assigned to a char as
long as it is within range.

Part I

// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));

42

PART I

The Java Language

Beginning with JDK 7, you can also specify integer literals using binary. To do so, prefix
the value with 0b or 0B. For example, this specifies the decimal value 10 using a binary
literal:
int x = 0b1010;

Among other uses, the addition of binary literals makes it easier to enter values used as
bitmasks. In such a case, the decimal (or hexadecimal) representation of the value does not
visually convey its meaning relative to its use. The binary literal does.
Also beginning with JDK 7, you can embed one or more underscores in an integer
literal. Doing so makes it easier to read large integer literals. When the literal is compiled,
the underscores are discarded. For example, given
int x = 123_456_789;

the value given to x will be 123,456,789. The underscores will be ignored. Underscores can
only be used to separate digits. They cannot come at the beginning or the end of a literal. It
is, however, permissible for more than one underscore to be used between two digits. For
example, this is valid:
int x = 123___456___789;

The use of underscores in an integer literal is especially useful when encoding such
things as telephone numbers, customer ID numbers, part numbers, and so on. They are
also useful for providing visual groupings when specifying binary literals. For example,
binary values are often visually grouped in four-digits units, as shown here:
int x = 0b1101_0101_0001_1010;

Floating-Point Literals
Floating-point numbers represent decimal values with a fractional component. They can be
expressed in either standard or scientific notation. Standard notation consists of a whole
number component followed by a decimal point followed by a fractional component. For
example, 2.0, 3.14159, and 0.6667 represent valid standard-notation floating-point
numbers. Scientific notation uses a standard-notation, floating-point number plus a suffix that
specifies a power of 10 by which the number is to be multiplied. The exponent is indicated
by an E or e followed by a decimal number, which can be positive or negative. Examples
include 6.022E23, 314159E–05, and 2e+100.
Floating-point literals in Java default to double precision. To specify a float literal, you
must append an F or f to the constant. You can also explicitly specify a double literal by
appending a D or d. Doing so is, of course, redundant. The default double type consumes
64 bits of storage, while the smaller float type requires only 32 bits.
Hexadecimal floating-point literals are also supported, but they are rarely used. They
must be in a form similar to scientific notation, but a P or p, rather than an E or e, is used.
For example, 0x12.2P2 is a valid floating-point literal. The value following the P, called the
binary exponent, indicates the power-of-two by which the number is multiplied. Therefore,
0x12.2P2 represents 72.5.

Data Types, Variables, and Arrays

43

Beginning with JDK 7, you can embed one or more underscores in a floating-point
literal. This feature works the same as it does for integer literals, which were just described.
Its purpose is to make it easier to read large floating-point literals. When the literal is
compiled, the underscores are discarded. For example, given
double num = 9_423_497_862.0;

the value given to num will be 9,423,497,862.0. The underscores will be ignored. As is the
case with integer literals, underscores can only be used to separate digits. They cannot
come at the beginning or the end of a literal. It is, however, permissible for more than one
underscore to be used between two digits. It is also permissible to use underscores in the
fractional portion of the number. For example,
double num = 9_423_497.1_0_9;

is legal. In this case, the fractional part is .109.

Boolean Literals
Boolean literals are simple. There are only two logical values that a boolean value can have,
true and false. The values of true and false do not convert into any numerical representation.
The true literal in Java does not equal 1, nor does the false literal equal 0. In Java, the
Boolean literals can only be assigned to variables declared as boolean or used in expressions
with Boolean operators.

Character Literals
Characters in Java are indices into the Unicode character set. They are 16-bit values that
can be converted into integers and manipulated with the integer operators, such as the
addition and subtraction operators. A literal character is represented inside a pair of single
quotes. All of the visible ASCII characters can be directly entered inside the quotes, such as
'a', 'z', and '@'. For characters that are impossible to enter directly, there are several escape
sequences that allow you to enter the character you need, such as ' \" for the single-quote
character itself and ' \n' for the newline character. There is also a mechanism for directly
entering the value of a character in octal or hexadecimal. For octal notation, use the
backslash followed by the three-digit number. For example, ' \141' is the letter 'a'. For
hexadecimal, you enter a backslash-u ( \u), then exactly four hexadecimal digits. For
example, ' \u0061' is the ISO-Latin-1 'a' because the top byte is zero. ' \ua432 ' is a Japanese
Katakana character. Table 3-1 shows the character escape sequences.

String Literals
String literals in Java are specified like they are in most other languages—by enclosing a
sequence of characters between a pair of double quotes. Examples of string literals are
"Hello World"
"two\nlines"
" \"This is in quotes\""

Part I

Chapter 3

44

PART I

The Java Language

Escape Sequence

Description

\ddd

Octal character (ddd)

\uxxxx

Hexadecimal Unicode character (xxxx)

\'

Single quote

\"

Double quote

\\

Backslash

\r

Carriage return

\n

New line (also known as line feed)

\f

Form feed

\t

Tab

\b

Backspace

Table 3-1 Character Escape Sequences
The escape sequences and octal/hexadecimal notations that were defined for character
literals work the same way inside of string literals. One important thing to note about Java
strings is that they must begin and end on the same line. There is no line-continuation
escape sequence as there is in some other languages.
NOTE As you may know, in some other languages, including C/C++, strings are implemented as arrays of
characters. However, this is not the case in Java. Strings are actually object types. As you will see later
in this book, because Java implements strings as objects, Java includes extensive string-handling
capabilities that are both powerful and easy to use.

Variables
The variable is the basic unit of storage in a Java program. A variable is defined by the
combination of an identifier, a type, and an optional initializer. In addition, all variables have
a scope, which defines their visibility, and a lifetime. These elements are examined next.

Declaring a Variable
In Java, all variables must be declared before they can be used. The basic form of a variable
declaration is shown here:
type identifier [ = value ][, identifier [= value ] …];
The type is one of Java’s atomic types, or the name of a class or interface. (Class and
interface types are discussed later in Part I of this book.) The identifier is the name of the
variable. You can initialize the variable by specifying an equal sign and a value. Keep in
mind that the initialization expression must result in a value of the same (or compatible)
type as that specified for the variable. To declare more than one variable of the specified type,
use a comma-separated list.
Here are several examples of variable declarations of various types. Note that some
include an initialization.

int a, b, c;
int d = 3, e, f = 5;
byte z = 22;
double pi = 3.14159;
char x = 'x';

//
//
//
//
//
//

Data Types, Variables, and Arrays

45

declares three ints, a, b, and c.
declares three more ints, initializing
d and f.
initializes z.
declares an approximation of pi.
the variable x has the value 'x'.

The identifiers that you choose have nothing intrinsic in their names that indicates
their type. Java allows any properly formed identifier to have any declared type.

Dynamic Initialization
Although the preceding examples have used only constants as initializers, Java allows
variables to be initialized dynamically, using any expression valid at the time the variable
is declared.
For example, here is a short program that computes the length of the hypotenuse of a
right triangle given the lengths of its two opposing sides:
// Demonstrate dynamic initialization.
class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}

Here, three local variables—a, b, and c—are declared. The first two, a and b, are initialized
by constants. However, c is initialized dynamically to the length of the hypotenuse (using
the Pythagorean theorem). The program uses another of Java’s built-in methods, sqrt( ),
which is a member of the Math class, to compute the square root of its argument. The key
point here is that the initialization expression may use any element valid at the time of the
initialization, including calls to methods, other variables, or literals.

The Scope and Lifetime of Variables
So far, all of the variables used have been declared at the start of the main( ) method.
However, Java allows variables to be declared within any block. As explained in Chapter 2,
a block is begun with an opening curly brace and ended by a closing curly brace. A block
defines a scope. Thus, each time you start a new block, you are creating a new scope. A scope
determines what objects are visible to other parts of your program. It also determines the
lifetime of those objects.
Many other computer languages define two general categories of scopes: global and
local. However, these traditional scopes do not fit well with Java’s strict, object-oriented
model. While it is possible to create what amounts to being a global scope, it is by far the
exception, not the rule. In Java, the two major scopes are those defined by a class and those
defined by a method. Even this distinction is somewhat artificial. However, since the class
scope has several unique properties and attributes that do not apply to the scope defined

Part I

Chapter 3

46

PART I

The Java Language

by a method, this distinction makes some sense. Because of the differences, a discussion of
class scope (and variables declared within it) is deferred until Chapter 6, when classes are
described. For now, we will only examine the scopes defined by or within a method.
The scope defined by a method begins with its opening curly brace. However, if that
method has parameters, they too are included within the method’s scope. Although this
book will look more closely at parameters in Chapter 6, for the sake of this discussion, they
work the same as any other method variable.
As a general rule, variables declared inside a scope are not visible (that is, accessible)
to code that is defined outside that scope. Thus, when you declare a variable within a
scope, you are localizing that variable and protecting it from unauthorized access and/or
modification. Indeed, the scope rules provide the foundation for encapsulation.
Scopes can be nested. For example, each time you create a block of code, you are
creating a new, nested scope. When this occurs, the outer scope encloses the inner scope.
This means that objects declared in the outer scope will be visible to code within the inner
scope. However, the reverse is not true. Objects declared within the inner scope will not be
visible outside it.
To understand the effect of nested scopes, consider the following program:
// Demonstrate block scope.
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}

As the comments indicate, the variable x is declared at the start of main( )’s scope and is
accessible to all subsequent code within main( ). Within the if block, y is declared. Since a
block defines a scope, y is only visible to other code within its block. This is why outside of
its block, the line y = 100; is commented out. If you remove the leading comment symbol,
a compile-time error will occur, because y is not visible outside of its block. Within the if
block, x can be used because code within a block (that is, a nested scope) has access to
variables declared by an enclosing scope.
Within a block, variables can be declared at any point, but are valid only after they are
declared. Thus, if you define a variable at the start of a method, it is available to all of the
code within that method. Conversely, if you declare a variable at the end of a block, it is
effectively useless, because no code will have access to it. For example, this fragment is
invalid because count cannot be used prior to its declaration:

Data Types, Variables, and Arrays

// This fragment is wrong!
count = 100; // oops! cannot use count before it is declared!
int count;

Here is another important point to remember: variables are created when their scope is
entered, and destroyed when their scope is left. This means that a variable will not hold its
value once it has gone out of scope. Therefore, variables declared within a method will not
hold their values between calls to that method. Also, a variable declared within a block will
lose its value when the block is left. Thus, the lifetime of a variable is confined to its scope.
If a variable declaration includes an initializer, then that variable will be reinitialized
each time the block in which it is declared is entered. For example, consider the next
program:
// Demonstrate lifetime of a variable.
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
System.out.println("y is: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}

The output generated by this program is shown here:
y
y
y
y
y
y

is: -1
is now: 100
is: -1
is now: 100
is: -1
is now: 100

As you can see, y is reinitialized to –1 each time the inner for loop is entered. Even though
it is subsequently assigned the value 100, this value is lost.
One last point: Although blocks can be nested, you cannot declare a variable to have
the same name as one in an outer scope. For example, the following program is illegal:
// This program will not compile
class ScopeErr {
public static void main(String args[]) {
int bar = 1;
{
// creates a new scope
int bar = 2; // Compile-time error – bar already defined!
}
}
}

47

Part I

Chapter 3

48

PART I

The Java Language

Type Conversion and Casting
If you have previous programming experience, then you already know that it is fairly common
to assign a value of one type to a variable of another type. If the two types are compatible,
then Java will perform the conversion automatically. For example, it is always possible to
assign an int value to a long variable. However, not all types are compatible, and thus, not
all type conversions are implicitly allowed. For instance, there is no automatic conversion
defined from double to byte. Fortunately, it is still possible to obtain a conversion between
incompatible types. To do so, you must use a cast, which performs an explicit conversion
between incompatible types. Let’s look at both automatic type conversions and casting.

Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an automatic type conversion
will take place if the following two conditions are met:
• The two types are compatible.
• The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place. For example, the
int type is always large enough to hold all valid byte values, so no explicit cast statement is
required.
For widening conversions, the numeric types, including integer and floating-point types,
are compatible with each other. However, there are no automatic conversions from the
numeric types to char or boolean. Also, char and boolean are not compatible with each other.
As mentioned earlier, Java also performs an automatic type conversion when storing a
literal integer constant into variables of type byte, short, long, or char.

Casting Incompatible Types
Although the automatic type conversions are helpful, they will not fulfill all needs. For
example, what if you want to assign an int value to a byte variable? This conversion will not
be performed automatically, because a byte is smaller than an int. This kind of conversion
is sometimes called a narrowing conversion, since you are explicitly making the value narrower
so that it will fit into the target type.
To create a conversion between two incompatible types, you must use a cast. A cast is
simply an explicit type conversion. It has this general form:
(target-type) value
Here, target-type specifies the desired type to convert the specified value to. For example, the
following fragment casts an int to a byte. If the integer’s value is larger than the range of a
byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.
int a;
byte b;
// …
b = (byte) a;

Data Types, Variables, and Arrays

49

A different type of conversion will occur when a floating-point value is assigned to an
integer type: truncation. As you know, integers do not have fractional components. Thus,
when a floating-point value is assigned to an integer type, the fractional component is lost.
For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1.
The 0.23 will have been truncated. Of course, if the size of the whole number component is
too large to fit into the target integer type, then that value will be reduced modulo the
target type’s range.
The following program demonstrates some type conversions that require casts:
// Demonstrate casts.
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}

This program generates the following output:
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67

Let’s look at each conversion. When the value 257 is cast into a byte variable, the result is the
remainder of the division of 257 by 256 (the range of a byte), which is 1 in this case. When
the d is converted to an int, its fractional component is lost. When d is converted to a byte, its
fractional component is lost, and the value is reduced modulo 256, which in this case is 67.

Automatic Type Promotion in Expressions
In addition to assignments, there is another place where certain type conversions may
occur: in expressions. To see why, consider the following. In an expression, the precision

Part I

Chapter 3

50

PART I

The Java Language

required of an intermediate value will sometimes exceed the range of either operand. For
example, examine the following expression:
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;

The result of the intermediate term a * b easily exceeds the range of either of its byte
operands. To handle this kind of problem, Java automatically promotes each byte, short,
or char operand to int when evaluating an expression. This means that the subexpression
a*b is performed using integers—not bytes. Thus, 2,000, the result of the intermediate
expression, 50 * 40, is legal even though a and b are both specified as type byte.
As useful as the automatic promotions are, they can cause confusing compile-time
errors. For example, this seemingly correct code causes a problem:
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a byte!

The code is attempting to store 50 * 2, a perfectly valid byte value, back into a byte
variable. However, because the operands were automatically promoted to int when the
expression was evaluated, the result has also been promoted to int. Thus, the result of the
expression is now of type int, which cannot be assigned to a byte without the use of a cast.
This is true even if, as in this particular case, the value being assigned would still fit in the
target type.
In cases where you understand the consequences of overflow, you should use an explicit
cast, such as
byte b = 50;
b = (byte)(b * 2);

which yields the correct value of 100.

The Type Promotion Rules
Java defines several type promotion rules that apply to expressions. They are as follows: First,
all byte, short, and char values are promoted to int, as just described. Then, if one operand
is a long, the whole expression is promoted to long. If one operand is a float, the entire
expression is promoted to float. If any of the operands are double, the result is double.
The following program demonstrates how each value in the expression gets promoted
to match the second argument to each binary operator:
class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);

Chapter 3

Data Types, Variables, and Arrays

51

}
}

Let’s look closely at the type promotions that occur in this line from the program:
double result = (f * b) + (i / c) - (d * s);

In the first subexpression, f * b, b is promoted to a float and the result of the subexpression
is float. Next, in the subexpression i/c, c is promoted to int, and the result is of type int.
Then, in d * s, the value of s is promoted to double, and the type of the subexpression is
double. Finally, these three intermediate values, float, int, and double, are considered. The
outcome of float plus an int is a float. Then the resultant float minus the last double is
promoted to double, which is the type for the final result of the expression.

Arrays
An array is a group of like-typed variables that are referred to by a common name. Arrays of
any type can be created and may have one or more dimensions. A specific element in an
array is accessed by its index. Arrays offer a convenient means of grouping related
information.
NOTE If you are familiar with C/C++, be careful. Arrays in Java work differently than they do in those
languages.

One-Dimensional Arrays
A one-dimensional array is, essentially, a list of like-typed variables. To create an array, you first
must create an array variable of the desired type. The general form of a one-dimensional
array declaration is
type var-name[ ];
Here, type declares the element type (also called the base type) of the array. The element type
determines the data type of each element that comprises the array. Thus, the element
type for the array determines what type of data the array will hold. For example, the
following declares an array named month_days with the type “array of int”:
int month_days[];

Although this declaration establishes the fact that month_days is an array variable, no
array actually exists. In fact, the value of month_days is set to null, which represents an array
with no value. To link month_days with an actual, physical array of integers, you must allocate
one using new and assign it to month_days. new is a special operator that allocates memory.
You will look more closely at new in a later chapter, but you need to use it now to
allocate memory for arrays. The general form of new as it applies to one-dimensional
arrays appears as follows:
array-var = new type [size];

Part I

System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);

52

PART I

The Java Language

Here, type specifies the type of data being allocated, size specifies the number of elements in
the array, and array-var is the array variable that is linked to the array. That is, to use new to
allocate an array, you must specify the type and number of elements to allocate. The elements
in the array allocated by new will automatically be initialized to zero (for numeric types), false
(for boolean), or null (for reference types, which are described in a later chapter). This
example allocates a 12-element array of integers and links them to month_days:
month_days = new int[12];

After this statement executes, month_days will refer to an array of 12 integers. Further, all
elements in the array will be initialized to zero.
Let’s review: Obtaining an array is a two-step process. First, you must declare a variable
of the desired array type. Second, you must allocate the memory that will hold the array,
using new, and assign it to the array variable. Thus, in Java all arrays are dynamically
allocated. If the concept of dynamic allocation is unfamiliar to you, don’t worry. It will
be described at length later in this book.
Once you have allocated an array, you can access a specific element in the array by
specifying its index within square brackets. All array indexes start at zero. For example,
this statement assigns the value 28 to the second element of month_days:
month_days[1] = 28;

The next line displays the value stored at index 3:
System.out.println(month_days[3]);

Putting together all the pieces, here is a program that creates an array of the number of
days in each month:
// Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}

Data Types, Variables, and Arrays

When you run this program, it prints the number of days in April. As mentioned, Java array
indexes start with zero, so the number of days in April is month_days[3] or 30.
It is possible to combine the declaration of the array variable with the allocation of the
array itself, as shown here:
int month_days[] = new int[12];

This is the way that you will normally see it done in professionally written Java programs.
Arrays can be initialized when they are declared. The process is much the same as that
used to initialize the simple types. An array initializer is a list of comma-separated expressions
surrounded by curly braces. The commas separate the values of the array elements. The
array will automatically be created large enough to hold the number of elements you specify
in the array initializer. There is no need to use new. For example, to store the number of
days in each month, the following code creates an initialized array of integers:
// An improved version of the previous program.
class AutoArray {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
System.out.println("April has " + month_days[3] + " days.");
}
}

When you run this program, you see the same output as that generated by the previous
version.
Java strictly checks to make sure you do not accidentally try to store or reference values
outside of the range of the array. The Java run-time system will check to be sure that all
array indexes are in the correct range. For example, the run-time system will check the
value of each index into month_days to make sure that it is between 0 and 11 inclusive. If
you try to access elements outside the range of the array (negative numbers or numbers
greater than the length of the array), you will cause a run-time error.
Here is one more example that uses a one-dimensional array. It finds the average of a
set of numbers.
// Average an array of values.
class Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}

53

Part I

Chapter 3

54

PART I

The Java Language

Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look
and act like regular multidimensional arrays. However, as you will see, there are a couple
of subtle differences. To declare a multidimensional array variable, specify each additional
index using another set of square brackets. For example, the following declares a twodimensional array variable called twoD:
int twoD[][] = new int[4][5];

This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as
an array of arrays of int. Conceptually, this array will look like the one shown in Figure 3-1.
The following program numbers each element in the array from left to right, top to
bottom, and then displays these values:
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}

This program generates the following output:
0 1 2
5 6 7
10 11
15 16

3 4
8 9
12 13 14
17 18 19

When you allocate memory for a multidimensional array, you need only specify the
memory for the first (leftmost) dimension. You can allocate the remaining dimensions
separately. For example, this following code allocates memory for the first dimension of
twoD when it is declared. It allocates the second dimension manually.
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];

Data Types, Variables, and Arrays

55

Part I

Chapter 3

Figure 3-1 A conceptual view of a 4 by 5, two-dimensional array
While there is no advantage to individually allocating the second dimension arrays in
this situation, there may be in others. For example, when you allocate dimensions manually,
you do not need to allocate the same number of elements for each dimension. As stated
earlier, since multidimensional arrays are actually arrays of arrays, the length of each array
is under your control. For example, the following program creates a two-dimensional array
in which the sizes of the second dimension are unequal:
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j>, shifts all of the bits in a value to the right a specified number of
times. Its general form is shown here:
value >> num
Here, num specifies the number of positions to right-shift the value in value. That is, the >>
moves all of the bits in the specified value to the right the number of bit positions specified
by num.

Chapter 4

Operators

71

int a = 32;
a = a >> 2; // a now contains 8

When a value has bits that are “shifted off,” those bits are lost. For example, the next
code fragment shifts the value 35 to the right two positions, which causes the two low-order
bits to be lost, resulting again in a being set to 8:
int a = 35;
a = a >> 2; // a contains 8

Looking at the same operation in binary shows more clearly how this happens:
00100011 35
>> 2
00001000 8
Each time you shift a value to the right, it divides that value by two—and discards any
remainder. You can take advantage of this for high-performance integer division by 2. Of
course, you must be sure that you are not shifting any bits off the right end.
When you are shifting right, the top (leftmost) bits exposed by the right shift are filled
in with the previous contents of the top bit. This is called sign extension and serves to preserve
the sign of negative numbers when you shift them right. For example, –8 >> 1 is –4, which,
in binary, is
11111000
>> 1
11111100

–8
–4

It is interesting to note that if you shift –1 right, the result always remains –1, since sign
extension keeps bringing in more ones in the high-order bits.
Sometimes it is not desirable to sign-extend values when you are shifting them to the
right. For example, the following program converts a byte value to its hexadecimal string
representation. Notice that the shifted value is masked by ANDing it with 0x0f to discard
any sign-extended bits so that the value can be used as an index into the array of
hexadecimal characters.
// Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}

Part I

The following code fragment shifts the value 32 to the right by two positions, resulting
in a being set to 8:

72

PART I

The Java Language

Here is the output of this program:
b = 0xf1

The Unsigned Right Shift
As you have just seen, the >> operator automatically fills the high-order bit with its previous
contents each time a shift occurs. This preserves the sign of the value. However, sometimes
this is undesirable. For example, if you are shifting something that does not represent a
numeric value, you may not want sign extension to take place. This situation is common
when you are working with pixel-based values and graphics. In these cases, you will
generally want to shift a zero into the high-order bit no matter what its initial value was.
This is known as an unsigned shift. To accomplish this, you will use Java’s unsigned, shiftright operator, >>>, which always shifts zeros into the high-order bit.
The following code fragment demonstrates the >>>. Here, a is set to –1, which sets all
32 bits to 1 in binary. This value is then shifted right 24 bits, filling the top 24 bits with
zeros, ignoring normal sign extension. This sets a to 255.
int a = -1;
a = a >>> 24;

Here is the same operation in binary form to further illustrate what is happening:
11111111 11111111 11111111 11111111 –1 in binary as an int
>>>24
00000000 00000000 00000000 11111111 255 in binary as an int
The >>> operator is often not as useful as you might like, since it is only meaningful
for 32- and 64-bit values. Remember, smaller values are automatically promoted to int in
expressions. This means that sign-extension occurs and that the shift will take place on a
32-bit rather than on an 8- or 16-bit value. That is, one might expect an unsigned right shift
on a byte value to zero-fill beginning at bit 7. But this is not the case, since it is a 32-bit value
that is actually being shifted. The following program demonstrates this effect:
// Unsigned shifting a byte value.
class ByteUShift {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
byte c = (byte) (b >> 4);
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);
System.out.println("
b = 0x"
+ hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
System.out.println("
b >> 4 = 0x"
+ hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
System.out.println("
b >>> 4 = 0x"
+ hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);

Chapter 4

Operators

73

}
}

The following output of this program shows how the >>> operator appears to do nothing
when dealing with bytes. The variable b is set to an arbitrary negative byte value for this
demonstration. Then c is assigned the byte value of b shifted right by four, which is 0xff
because of the expected sign extension. Then d is assigned the byte value of b unsigned
shifted right by four, which you might have expected to be 0x0f, but is actually 0xff because
of the sign extension that happened when b was promoted to int before the shift. The last
expression sets e to the byte value of b masked to 8 bits using the AND operator, then shifted
right by four, which produces the expected value of 0x0f. Notice that the unsigned shift right
operator was not used for d, since the state of the sign bit after the AND was known.
b = 0xf1
b >> 4 = 0xff
b >>> 4 = 0xff
(b & 0xff) >> 4 = 0x0f

Bitwise Operator Compound Assignments
All of the binary bitwise operators have a compound form similar to that of the algebraic
operators, which combines the assignment with the bitwise operation. For example, the
following two statements, which shift the value in a right by four bits, are equivalent:
a = a >> 4;
a >>= 4;

Likewise, the following two statements, which result in a being assigned the bitwise
expression a OR b, are equivalent:
a = a | b;
a |= b;

The following program creates a few integer variables and then uses compound bitwise
operator assignments to manipulate the variables:
class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}

Part I

System.out.println("(b & 0xff) >> 4 = 0x"
+ hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);

74

PART I

The Java Language

The output of this program is shown here:
a = 3
b = 1
c = 6

Relational Operators
The relational operators determine the relationship that one operand has to the other.
Specifically, they determine equality and ordering. The relational operators are shown here:

Operator

Result

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

The outcome of these operations is a boolean value. The relational operators are most
frequently used in the expressions that control the if statement and the various loop
statements.
Any type in Java, including integers, floating-point numbers, characters, and Booleans
can be compared using the equality test, ==, and the inequality test, !=. Notice that in Java
equality is denoted with two equal signs, not one. (Remember: a single equal sign is the
assignment operator.) Only numeric types can be compared using the ordering operators.
That is, only integer, floating-point, and character operands may be compared to see which
is greater or less than the other.
As stated, the result produced by a relational operator is a boolean value. For example,
the following code fragment is perfectly valid:
int a = 4;
int b = 1;
boolean c = a < b;

In this case, the result of a>

>>>

<<

>

>=

<

==

!=

&
^
|
&&
||
?:
=

op=

Lowest
Table 4-1 The Precedence of the Java Operators

!

+ (unary)

<=

instanceof

– (unary)

(type-cast)

Chapter 4

Operators

79

Parentheses raise the precedence of the operations that are inside them. This is often
necessary to obtain the result you desire. For example, consider the following expression:
a >> b + 3

This expression first adds 3 to b and then shifts a right by that result. That is, this
expression can be rewritten using redundant parentheses like this:
a >> (b + 3)

However, if you want to first shift a right by b positions and then add 3 to that result,
you will need to parenthesize the expression like this:
(a >> b) + 3

In addition to altering the normal precedence of an operator, parentheses can
sometimes be used to help clarify the meaning of an expression. For anyone reading your
code, a complicated expression can be difficult to understand. Adding redundant but
clarifying parentheses to complex expressions can help prevent confusion later. For
example, which of the following expressions is easier to read?
a | 4 + c >> b & 7
(a | (((4 + c) >> b) & 7))

One other point: parentheses (redundant or not) do not degrade the performance
of your program. Therefore, adding parentheses to reduce ambiguity does not negatively
affect your program.

Part I

Using Parentheses

This page intentionally left blank

CHAPTER

5

Control Statements

A programming language uses control statements to cause the flow of execution to advance
and branch based on changes to the state of a program. Java’s program control statements
can be put into the following categories: selection, iteration, and jump. Selection statements
allow your program to choose different paths of execution based upon the outcome of an
expression or the state of a variable. Iteration statements enable program execution to
repeat one or more statements (that is, iteration statements form loops). Jump statements
allow your program to execute in a nonlinear fashion. All of Java’s control statements are
examined here.

Java’s Selection Statements
Java supports two selection statements: if and switch. These statements allow you to control
the flow of your program’s execution based upon conditions known only during run time.
You will be pleasantly surprised by the power and flexibility contained in these two statements.

if
The if statement was introduced in Chapter 2. It is examined in detail here. The if statement
is Java’s conditional branch statement. It can be used to route program execution through
two different paths. Here is the general form of the if statement:
if (condition) statement1;
else statement2;
Here, each statement may be a single statement or a compound statement enclosed in curly
braces (that is, a block). The condition is any expression that returns a boolean value. The
else clause is optional.
The if works like this: If the condition is true, then statement1 is executed. Otherwise,
statement2 (if it exists) is executed. In no case will both statements be executed. For example,
consider the following:
int a, b;
//...
if(a < b) a = 0;
else b = 0;

81

82

PART I

The Java Language

Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero. In no case are they
both set to zero.
Most often, the expression used to control the if will involve the relational operators.
However, this is not technically necessary. It is possible to control the if using a single boolean
variable, as shown in this code fragment:
boolean dataAvailable;
//...
if (dataAvailable)
ProcessData();
else
waitForMoreData();

Remember, only one statement can appear directly after the if or the else. If you want to
include more statements, you’ll need to create a block, as in this fragment:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else
waitForMoreData();

Here, both statements within the if block will execute if bytesAvailable is greater than zero.
Some programmers find it convenient to include the curly braces when using the if, even
when there is only one statement in each clause. This makes it easy to add another statement
at a later date, and you don’t have to worry about forgetting the braces. In fact, forgetting to
define a block when one is needed is a common cause of errors. For example, consider the
following code fragment:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else
waitForMoreData();
bytesAvailable = n;

It seems clear that the statement bytesAvailable = n; was intended to be executed inside the
else clause, because of the indentation level. However, as you recall, whitespace is insignificant
to Java, and there is no way for the compiler to know what was intended. This code will
compile without complaint, but it will behave incorrectly when run. The preceding example
is fixed in the code that follows:
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else {

Chapter 5

Control Statements

83

}

Nested ifs
A nested if is an if statement that is the target of another if or else. Nested ifs are very
common in programming. When you nest ifs, the main thing to remember is that an else
statement always refers to the nearest if statement that is within the same block as the else
and that is not already associated with an else. Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c;
// associated with this else
}
else a = d;
// this else refers to if(i == 10)

As the comments indicate, the final else is not associated with if(j<20) because it is not in
the same block (even though it is the nearest if without an else). Rather, the final else is
associated with if(i==10). The inner else refers to if(k>100) because it is the closest if within
the same block.

The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the if-elseif ladder. It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;
The if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest of
the ladder is bypassed. If none of the conditions is true, then the final else statement will be
executed. The final else acts as a default condition; that is, if all other conditional tests fail,
then the last else statement is performed. If there is no final else and all other conditions
are false, then no action will take place.
Here is a program that uses an if-else-if ladder to determine which season a particular
month is in.
// Demonstrate if-else-if statements.
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;

Part I

waitForMoreData();
bytesAvailable = n;

84

PART I

The Java Language

if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}

Here is the output produced by the program:
April is in the Spring.

You might want to experiment with this program before moving on. As you will find, no
matter what value you give month, one and only one assignment statement within the ladder
will be executed.

switch
The switch statement is Java’s multiway branch statement. It provides an easy way to
dispatch execution to different parts of your code based on the value of an expression. As
such, it often provides a better alternative than a large series of if-else-if statements. Here is
the general form of a switch statement:
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
case valueN :
// statement sequence
break;
default:
// default statement sequence
}
For versions of Java prior to JDK 7, expression must be of type byte, short, int, char, or an
enumeration. (Enumerations are described in Chapter 12.) Beginning with JDK 7, expression

Control Statements

can also be of type String. Each value specified in the case statements must be a unique
constant expression (such as a literal value). Duplicate case values are not allowed. The type
of each value must be compatible with the type of expression.
The switch statement works like this: The value of the expression is compared with each of
the values in the case statements. If a match is found, the code sequence following that case
statement is executed. If none of the constants matches the value of the expression, then the
default statement is executed. However, the default statement is optional. If no case matches
and no default is present, then no further action is taken.
The break statement is used inside the switch to terminate a statement sequence. When a
break statement is encountered, execution branches to the first line of code that follows the
entire switch statement. This has the effect of “jumping out” of the switch.
Here is a simple example that uses a switch statement:
// A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}

The output produced by this program is shown here:
i
i
i
i
i
i

is
is
is
is
is
is

zero.
one.
two.
three.
greater than 3.
greater than 3.

As you can see, each time through the loop, the statements associated with the case
constant that matches i are executed. All others are bypassed. After i is greater than 3,
no case statements match, so the default statement is executed.

85

Part I

Chapter 5

86

PART I

The Java Language

The break statement is optional. If you omit the break, execution will continue on into the
next case. It is sometimes desirable to have multiple cases without break statements between
them. For example, consider the following program:
// In a switch, break statements are optional.
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println("i is less than 10");
break;
default:
System.out.println("i is 10 or more");
}
}
}

This program generates the following output:
i
i
i
i
i
i
i
i
i
i
i
i

is
is
is
is
is
is
is
is
is
is
is
is

less than 5
less than 5
less than 5
less than 5
less than 5
less than 10
less than 10
less than 10
less than 10
less than 10
10 or more
10 or more

As you can see, execution falls through each case until a break statement (or the end of the
switch) is reached.
While the preceding example is, of course, contrived for the sake of illustration, omitting
the break statement has many practical applications in real programs. To sample its more
realistic usage, consider the following rewrite of the season example shown earlier. This version
uses a switch to provide a more efficient implementation.
// An improved version of the season program.
class Switch {
public static void main(String args[]) {
int month = 4;

Chapter 5

Control Statements

87

switch (month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("April is in the " + season + ".");
}
}

As mentioned, beginning with JDK 7, you can use a string to control a switch statement.
For example,
// Use a string to control a switch statement.
class StringSwitch {
public static void main(String args[]) {
String str = "two";
switch(str) {
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
case "three":
System.out.println("three");
break;
default:
System.out.println("no match");
break;
}
}
}

Part I

String season;

88

PART I

The Java Language

As you would expect, the output from the program is
two

The string contained in str (which is "two" in this program) is tested against the case
constants. When a match is found (as it is in the second case), the code sequence associated
with that sequence is executed.
Being able to use strings in a switch statement streamlines many situations. For example,
using a string-based switch is an improvement over using the equivalent sequence of if/else
statements. However, switching on strings is more expensive than switching on integers.
Therefore, it is best to switch on strings only in cases in which the controlling data is already
in string form. In other words, don’t use strings in a switch unnecessarily.

Nested switch Statements
You can use a switch as part of the statement sequence of an outer switch. This is called a
nested switch. Since a switch statement defines its own block, no conflicts arise between the
case constants in the inner switch and those in the outer switch. For example, the following
fragment is perfectly valid:
switch(count) {
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is one");
break;
}
break;
case 2: // ...

Here, the case 1: statement in the inner switch does not conflict with the case 1: statement
in the outer switch. The count variable is compared only with the list of cases at the outer
level. If count is 1, then target is compared with the inner list cases.
In summary, there are three important features of the switch statement to note:
• The switch differs from the if in that switch can only test for equality, whereas if can
evaluate any type of Boolean expression. That is, the switch looks only for a match
between the value of the expression and one of its case constants.
• No two case constants in the same switch can have identical values. Of course, a
switch statement and an enclosing outer switch can have case constants in common.
• A switch statement is usually more efficient than a set of nested ifs.
The last point is particularly interesting because it gives insight into how the Java compiler
works. When it compiles a switch statement, the Java compiler will inspect each of the case
constants and create a “jump table” that it will use for selecting the path of execution
depending on the value of the expression. Therefore, if you need to select among a large

Control Statements

89

group of values, a switch statement will run much faster than the equivalent logic coded using
a sequence of if-elses. The compiler can do this because it knows that the case constants are
all the same type and simply must be compared for equality with the switch expression. The
compiler has no such knowledge of a long list of if expressions.

Iteration Statements
Java’s iteration statements are for, while, and do-while. These statements create what we
commonly call loops. As you probably know, a loop repeatedly executes the same set of
instructions until a termination condition is met. As you will see, Java has a loop to fit any
programming need.

while
The while loop is Java’s most fundamental loop statement. It repeats a statement or block
while its controlling expression is true. Here is its general form:
while(condition) {
// body of loop
}
The condition can be any Boolean expression. The body of the loop will be executed as long
as the conditional expression is true. When condition becomes false, control passes to the
next line of code immediately following the loop. The curly braces are unnecessary if only a
single statement is being repeated.
Here is a while loop that counts down from 10, printing exactly ten lines of "tick":
// Demonstrate the while loop.
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--;
}
}
}

When you run this program, it will “tick” ten times:
tick
tick
tick
tick
tick
tick
tick
tick
tick
tick

10
9
8
7
6
5
4
3
2
1

Part I

Chapter 5

90

PART I

The Java Language

Since the while loop evaluates its conditional expression at the top of the loop, the body of
the loop will not execute even once if the condition is false to begin with. For example, in the
following fragment, the call to println() is never executed:
int a = 10, b = 20;
while(a > b)
System.out.println("This will not be displayed");

The body of the while (or any other of Java’s loops) can be empty. This is because a null
statement (one that consists only of a semicolon) is syntactically valid in Java. For example,
consider the following program:
// The target of a loop can be empty.
class NoBody {
public static void main(String args[]) {
int i, j;
i = 100;
j = 200;
// find midpoint between i and j
while(++i < --j); // no body in this loop
System.out.println("Midpoint is " + i);
}
}

This program finds the midpoint between i and j. It generates the following output:
Midpoint is 150

Here is how this while loop works. The value of i is incremented, and the value of j is
decremented. These values are then compared with one another. If the new value of i is still
less than the new value of j, then the loop repeats. If i is equal to or greater than j, the loop
stops. Upon exit from the loop, i will hold a value that is midway between the original values of
i and j. (Of course, this procedure only works when i is less than j to begin with.) As you can
see, there is no need for a loop body; all of the action occurs within the conditional expression,
itself. In professionally written Java code, short loops are frequently coded without bodies
when the controlling expression can handle all of the details itself.

do-while
As you just saw, if the conditional expression controlling a while loop is initially false, then
the body of the loop will not be executed at all. However, sometimes it is desirable to
execute the body of a loop at least once, even if the conditional expression is false to begin
with. In other words, there are times when you would like to test the termination expression
at the end of the loop rather than at the beginning. Fortunately, Java supplies a loop that
does just that: the do-while. The do-while loop always executes its body at least once,
because its conditional expression is at the bottom of the loop. Its general form is

Control Statements

do {
// body of loop
} while (condition);
Each iteration of the do-while loop first executes the body of the loop and then evaluates
the conditional expression. If this expression is true, the loop will repeat. Otherwise, the
loop terminates. As with all of Java’s loops, condition must be a Boolean expression.
Here is a reworked version of the “tick” program that demonstrates the do-while loop. It
generates the same output as before.
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
}
}

The loop in the preceding program, while technically correct, can be written more
efficiently as follows:
do {
System.out.println("tick " + n);
} while(--n > 0);

In this example, the expression (– –n > 0) combines the decrement of n and the test for zero
into one expression. Here is how it works. First, the – –n statement executes, decrementing
n and returning the new value of n. This value is then compared with zero. If it is greater
than zero, the loop continues; otherwise, it terminates.
The do-while loop is especially useful when you process a menu selection, because you will
usually want the body of a menu loop to execute at least once. Consider the following program,
which implements a very simple help system for Java’s selection and iteration statements:
// Using a do-while to process a menu selection
class Menu {
public static void main(String args[])
throws java.io.IOException {
char choice;
do {
System.out.println("Help on: ");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\n");
System.out.println("Choose one:");

91

Part I

Chapter 5

92

PART I

The Java Language

choice = (char) System.in.read();
} while( choice < '1' || choice > '5');
System.out.println("\n");
switch(choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println("
statement sequence");
System.out.println("
break;");
System.out.println(" //...");
System.out.println("}");
break;
case '3':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '4':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
case '5':
System.out.println("The for:\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
}
}

Here is a sample run produced by this program:
Help on:
1. if
2. switch
3. while
4. do-while
5. for
Choose one:
4
The do-while:
do {
statement;
} while (condition);

Control Statements

93

In the program, the do-while loop is used to verify that the user has entered a valid choice.
If not, then the user is reprompted. Since the menu must be displayed at least once, the
do-while is the perfect loop to accomplish this.
A few other points about this example: Notice that characters are read from the keyboard
by calling System.in.read( ). This is one of Java’s console input functions. Although Java’s
console I/O methods won’t be discussed in detail until Chapter 13, System.in.read( ) is used
here to obtain the user’s choice. It reads characters from standard input (returned as integers,
which is why the return value was cast to char). By default, standard input is line buffered, so
you must press enter before any characters that you type will be sent to your program.
Java’s console input can be a bit awkward to work with. Further, most real-world Java
programs will be graphical and window-based. For these reasons, not much use of console
input has been made in this book. However, it is useful in this context. One other point
to consider: Because System.in.read( ) is being used, the program must specify the
throws java.io.IOException clause. This line is necessary to handle input errors. It is
part of Java’s exception handling features, which are discussed in Chapter 10.

for
You were introduced to a simple form of the for loop in Chapter 2. As you will see, it is a
powerful and versatile construct.
Beginning with JDK 5, there are two forms of the for loop. The first is the traditional form
that has been in use since the original version of Java. The second is the new “for-each” form.
Both types of for loops are discussed here, beginning with the traditional form.
Here is the general form of the traditional for statement:
for(initialization; condition; iteration) {
// body
}
If only one statement is being repeated, there is no need for the curly braces.
The for loop operates as follows. When the loop first starts, the initialization portion of the
loop is executed. Generally, this is an expression that sets the value of the loop control variable,
which acts as a counter that controls the loop. It is important to understand that the initialization
expression is executed only once. Next, condition is evaluated. This must be a Boolean expression.
It usually tests the loop control variable against a target value. If this expression is true, then the
body of the loop is executed. If it is false, the loop terminates. Next, the iteration portion of the
loop is executed. This is usually an expression that increments or decrements the loop control
variable. The loop then iterates, first evaluating the conditional expression, then executing the
body of the loop, and then executing the iteration expression with each pass. This process
repeats until the controlling expression is false.
Here is a version of the “tick” program that uses a for loop:
// Demonstrate the for loop.
class ForTick {
public static void main(String args[]) {
int n;
for(n=10; n>0; n--)

Part I

Chapter 5

94

PART I

The Java Language

System.out.println("tick " + n);
}
}

Declaring Loop Control Variables Inside the for Loop
Often the variable that controls a for loop is needed only for the purposes of the loop and
is not used elsewhere. When this is the case, it is possible to declare the variable inside the
initialization portion of the for. For example, here is the preceding program recoded so
that the loop control variable n is declared as an int inside the for:
// Declare a loop control variable inside the for.
class ForTick {
public static void main(String args[]) {
// here, n is declared inside of the for loop
for(int n=10; n>0; n--)
System.out.println("tick " + n);
}
}

When you declare a variable inside a for loop, there is one important point to remember:
the scope of that variable ends when the for statement does. (That is, the scope of the variable
is limited to the for loop.) Outside the for loop, the variable will cease to exist. If you need to
use the loop control variable elsewhere in your program, you will not be able to declare it
inside the for loop.
When the loop control variable will not be needed elsewhere, most Java programmers
declare it inside the for. For example, here is a simple program that tests for prime numbers.
Notice that the loop control variable, i, is declared inside the for since it is not needed
elsewhere.
// Test for primes.
class FindPrime {
public static void main(String args[]) {
int num;
boolean isPrime;
num = 14;
if(num < 2) isPrime = false;
else isPrime = true;
for(int i=2; i <= num/i; i++) {
if((num % i) == 0) {
isPrime = false;
break;
}
}
if(isPrime) System.out.println("Prime");
else System.out.println("Not Prime");
}
}

Chapter 5

Control Statements

95

There will be times when you will want to include more than one statement in the
initialization and iteration portions of the for loop. For example, consider the loop in the
following program:
class Sample {
public static void main(String args[]) {
int a, b;
b = 4;
for(a=1; a i) {
System.out.println();
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}

The continue statement in this example terminates the loop counting j and continues with
the next iteration of the loop counting i. Here is the output of this program:
0
0
0
0
0
0
0
0
0
0

1
2
3
4
5
6
7
8
9

4
6 9
8 12 16
10 15 20
12 18 24
14 21 28
16 24 32
18 27 36

25
30
35
40
45

36
42 49
48 56 64
54 63 72 81

Good uses of continue are rare. One reason is that Java provides a rich set of loop
statements which fit most applications. However, for those special circumstances in which
early iteration is needed, the continue statement provides a structured way to accomplish it.

Part I

Chapter 5

108

PART I

The Java Language

return
The last control statement is return. The return statement is used to explicitly return from a
method. That is, it causes program control to transfer back to the caller of the method. As
such, it is categorized as a jump statement. Although a full discussion of return must wait
until methods are discussed in Chapter 6, a brief look at return is presented here.
At any time in a method the return statement can be used to cause execution to branch
back to the caller of the method. Thus, the return statement immediately terminates the
method in which it is executed. The following example illustrates this point. Here, return
causes execution to return to the Java run-time system, since it is the run-time system that calls
main( ):
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}

The output from this program is shown here:
Before the return.

As you can see, the final println( ) statement is not executed. As soon as return is executed,
control passes back to the caller.
One last point: In the preceding program, the if(t) statement is necessary. Without it, the
Java compiler would flag an “unreachable code” error because the compiler would know that
the last println( ) statement would never be executed. To prevent this error, the if statement is
used here to trick the compiler for the sake of this demonstration.

CHAPTER

6

Introducing Classes

The class is at the core of Java. It is the logical construct upon which the entire Java language
is built because it defines the shape and nature of an object. As such, the class forms the
basis for object-oriented programming in Java. Any concept you wish to implement in a Java
program must be encapsulated within a class.
Because the class is so fundamental to Java, this and the next few chapters will be devoted
to it. Here, you will be introduced to the basic elements of a class and learn how a class can be
used to create objects. You will also learn about methods, constructors, and the this keyword.

Class Fundamentals
Classes have been used since the beginning of this book. However, until now, only the most
rudimentary form of a class has been shown. The classes created in the preceding chapters
primarily exist simply to encapsulate the main( ) method, which has been used to demonstrate
the basics of the Java syntax. As you will see, classes are substantially more powerful than the
limited ones presented so far.
Perhaps the most important thing to understand about a class is that it defines a new
data type. Once defined, this new type can be used to create objects of that type. Thus, a
class is a template for an object, and an object is an instance of a class. Because an object is an
instance of a class, you will often see the two words object and instance used interchangeably.

The General Form of a Class
When you define a class, you declare its exact form and nature. You do this by specifying
the data that it contains and the code that operates on that data. While very simple classes
may contain only code or only data, most real-world classes contain both. As you will see, a
class’ code defines the interface to its data.
A class is declared by use of the class keyword. The classes that have been used up to
this point are actually very limited examples of its complete form. Classes can (and usually
do) get much more complex. A simplified general form of a class definition is shown here:
class classname {
type instance-variable1;

109

110

PART I

The Java Language

type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
The data, or variables, defined within a class are called instance variables. The code is
contained within methods. Collectively, the methods and variables defined within a class
are called members of the class. In most classes, the instance variables are acted upon and
accessed by the methods defined for that class. Thus, as a general rule, it is the methods
that determine how a class’ data can be used.
Variables defined within a class are called instance variables because each instance of
the class (that is, each object of the class) contains its own copy of these variables. Thus, the
data for one object is separate and unique from the data for another. We will come back to
this point shortly, but it is an important concept to learn early.
All methods have the same general form as main( ), which we have been using thus far.
However, most methods will not be specified as static or public. Notice that the general
form of a class does not specify a main( ) method. Java classes do not need to have a main( )
method. You only specify one if that class is the starting point for your program. Further,
some kinds of Java applications, such as applets, don’t require a main( ) method at all.
NOTE C++ programmers will notice that the class declaration and the implementation of the methods
are stored in the same place and not defined separately. This sometimes makes for very large .java
files, since any class must be entirely defined in a single source file. This design feature was built into
Java because it was felt that in the long run, having specification, declaration, and implementation all
in one place makes for code that is easier to maintain.

A Simple Class
Let’s begin our study of the class with a simple example. Here is a class called Box that
defines three instance variables: width, height, and depth. Currently, Box does not contain
any methods (but some will be added soon).
class Box {
double width;
double height;
double depth;
}

Introducing Classes

111

As stated, a class defines a new type of data. In this case, the new data type is called Box. You
will use this name to declare objects of type Box. It is important to remember that a class
declaration only creates a template; it does not create an actual object. Thus, the preceding
code does not cause any objects of type Box to come into existence.
To actually create a Box object, you will use a statement like the following:
Box mybox = new Box(); // create a Box object called mybox

After this statement executes, mybox will be an instance of Box. Thus, it will have “physical”
reality. For the moment, don’t worry about the details of this statement.
As mentioned earlier, each time you create an instance of a class, you are creating an
object that contains its own copy of each instance variable defined by the class. Thus, every
Box object will contain its own copies of the instance variables width, height, and depth. To
access these variables, you will use the dot (.) operator. The dot operator links the name of
the object with the name of an instance variable. For example, to assign the width variable
of mybox the value 100, you would use the following statement:
mybox.width = 100;

This statement tells the compiler to assign the copy of width that is contained within the
mybox object the value of 100. In general, you use the dot operator to access both the
instance variables and the methods within an object. One other point: Although commonly
referred to as the dot operator, the formal specification for Java categorizes the . as a separator.
However, since the use of the term “dot operator” is widespread, it is used in this book.
Here is a complete program that uses the Box class:
/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

Part I

Chapter 6

112

PART I

The Java Language

You should call the file that contains this program BoxDemo.java, because the main( )
method is in the class called BoxDemo, not the class called Box. When you compile this
program, you will find that two .class files have been created, one for Box and one for
BoxDemo. The Java compiler automatically puts each class into its own .class file. It is not
necessary for both the Box and the BoxDemo class to actually be in the same source file.
You could put each class in its own file, called Box.java and BoxDemo.java, respectively.
To run this program, you must execute BoxDemo.class. When you do, you will see the
following output:
Volume is 3000.0

As stated earlier, each object has its own copies of the instance variables. This means
that if you have two Box objects, each has its own copy of depth, width, and height. It is
important to understand that changes to the instance variables of one object have no
effect on the instance variables of another. For example, the following program declares
two Box objects:
// This program declares two Box objects.
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// compute volume of first box
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}

Chapter 6

Introducing Classes

113

Volume is 3000.0
Volume is 162.0

As you can see, mybox1’s data is completely separate from the data contained in mybox2.

Declaring Objects
As just explained, when you create a class, you are creating a new data type. You can use this
type to declare objects of that type. However, obtaining objects of a class is a two-step process.
First, you must declare a variable of the class type. This variable does not define an object.
Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual,
physical copy of the object and assign it to that variable. You can do this using the new
operator. The new operator dynamically allocates (that is, allocates at run time) memory
for an object and returns a reference to it. This reference is, more or less, the address in
memory of the object allocated by new. This reference is then stored in the variable. Thus,
in Java, all class objects must be dynamically allocated. Let’s look at the details of this
procedure.
In the preceding sample programs, a line similar to the following is used to declare an
object of type Box:
Box mybox = new Box();

This statement combines the two steps just described. It can be rewritten like this to show
each step more clearly:
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object

The first line declares mybox as a reference to an object of type Box. After this line
executes, mybox contains the value null, which indicates that it does not yet point to an
actual object. Any attempt to use mybox at this point will result in a compile-time error. The
next line allocates an actual object and assigns a reference to it to mybox. After the second
line executes, you can use mybox as if it were a Box object. But in reality, mybox simply
holds the memory address of the actual Box object. The effect of these two lines of code
is depicted in Figure 6-1.
NOTE Those readers familiar with C/C++ have probably noticed that object references appear to be
similar to pointers. This suspicion is, essentially, correct. An object reference is similar to a memory
pointer. The main difference—and the key to Java’s safety—is that you cannot manipulate references
as you can actual pointers. Thus, you cannot cause an object reference to point to an arbitrary
memory location or manipulate it like an integer.

A Closer Look at new
As just explained, the new operator dynamically allocates memory for an object. It has this
general form:
class-var = new classname ( );

Part I

The output produced by this program is shown here:

114

PART I

The Java Language

Figure 6-1 Declaring an object of type Box
Here, class-var is a variable of the class type being created. The classname is the name of
the class that is being instantiated. The class name followed by parentheses specifies the
constructor for the class. A constructor defines what occurs when an object of a class is
created. Constructors are an important part of all classes and have many significant
attributes. Most real-world classes explicitly define their own constructors within their
class definition. However, if no explicit constructor is specified, then Java will automatically
supply a default constructor. This is the case with Box. For now, we will use the default
constructor. Soon, you will see how to define your own constructors.
At this point, you might be wondering why you do not need to use new for such things
as integers or characters. The answer is that Java’s primitive types are not implemented as
objects. Rather, they are implemented as “normal” variables. This is done in the interest of
efficiency. As you will see, objects have many features and attributes that require Java to
treat them differently than it treats the primitive types. By not applying the same overhead
to the primitive types that applies to objects, Java can implement the primitive types more
efficiently. Later, you will see object versions of the primitive types that are available for your
use in those situations in which complete objects of these types are needed.
It is important to understand that new allocates memory for an object during run time.
The advantage of this approach is that your program can create as many or as few objects as
it needs during the execution of your program. However, since memory is finite, it is possible
that new will not be able to allocate memory for an object because insufficient memory
exists. If this happens, a run-time exception will occur. (You will learn how to handle
exceptions in Chapter 10.) For the sample programs in this book, you won’t need to worry
about running out of memory, but you will need to consider this possibility in real-world
programs that you write.
Let’s once again review the distinction between a class and an object. A class creates a
new data type that can be used to create objects. That is, a class creates a logical framework
that defines the relationship between its members. When you declare an object of a class,
you are creating an instance of that class. Thus, a class is a logical construct. An object has
physical reality. (That is, an object occupies space in memory.) It is important to keep this
distinction clearly in mind.

Chapter 6

Introducing Classes

115

Object reference variables act differently than you might expect when an assignment takes
place. For example, what do you think the following fragment does?
Box b1 = new Box();
Box b2 = b1;

You might think that b2 is being assigned a reference to a copy of the object referred to by
b1. That is, you might think that b1 and b2 refer to separate and distinct objects. However,
this would be wrong. Instead, after this fragment executes, b1 and b2 will both refer to the
same object. The assignment of b1 to b2 did not allocate any memory or copy any part of
the original object. It simply makes b2 refer to the same object as does b1. Thus, any
changes made to the object through b2 will affect the object to which b1 is referring, since
they are the same object.
This situation is depicted here:

Although b1 and b2 both refer to the same object, they are not linked in any other way.
For example, a subsequent assignment to b1 will simply unhook b1 from the original object
without affecting the object or affecting b2. For example:
Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;

Here, b1 has been set to null, but b2 still points to the original object.
REMEMBER When you assign one object reference variable to another object reference variable, you are
not creating a copy of the object, you are only making a copy of the reference.

Introducing Methods
As mentioned at the beginning of this chapter, classes usually consist of two things: instance
variables and methods. The topic of methods is a large one because Java gives them so much
power and flexibility. In fact, much of the next chapter is devoted to methods. However,
there are some fundamentals that you need to learn now so that you can begin to add
methods to your classes.

Part I

Assigning Object Reference Variables

116

PART I

The Java Language

This is the general form of a method:
type name(parameter-list) {
// body of method
}
Here, type specifies the type of data returned by the method. This can be any valid type,
including class types that you create. If the method does not return a value, its return type
must be void. The name of the method is specified by name. This can be any legal identifier
other than those already used by other items within the current scope. The parameter-list is a
sequence of type and identifier pairs separated by commas. Parameters are essentially
variables that receive the value of the arguments passed to the method when it is called.
If the method has no parameters, then the parameter list will be empty.
Methods that have a return type other than void return a value to the calling routine
using the following form of the return statement:
return value;
Here, value is the value returned.
In the next few sections, you will see how to create various types of methods, including
those that take parameters and those that return values.

Adding a Method to the Box Class
Although it is perfectly fine to create a class that contains only data, it rarely happens. Most
of the time, you will use methods to access the instance variables defined by the class. In
fact, methods define the interface to most classes. This allows the class implementor to
hide the specific layout of internal data structures behind cleaner method abstractions. In
addition to defining methods that provide access to data, you can also define methods that
are used internally by the class itself.
Let’s begin by adding a method to the Box class. It may have occurred to you while
looking at the preceding programs that the computation of a box’s volume was something
that was best handled by the Box class rather than the BoxDemo class. After all, since the
volume of a box is dependent upon the size of the box, it makes sense to have the Box class
compute it. To do this, you must add a method to Box, as shown here:
// This program includes a method inside the box class.
class Box {
double width;
double height;
double depth;
// display volume of a box
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3 {
public static void main(String args[]) {

Chapter 6

Introducing Classes

117

// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume();
}
}

This program generates the following output, which is the same as the previous version.
Volume is 3000.0
Volume is 162.0

Look closely at the following two lines of code:
mybox1.volume();
mybox2.volume();

The first line here invokes the volume( ) method on mybox1. That is, it calls volume( )
relative to the mybox1 object, using the object’s name followed by the dot operator. Thus,
the call to mybox1.volume( ) displays the volume of the box defined by mybox1, and the
call to mybox2.volume( ) displays the volume of the box defined by mybox2. Each time
volume( ) is invoked, it displays the volume for the specified box.
If you are unfamiliar with the concept of calling a method, the following discussion will
help clear things up. When mybox1.volume( ) is executed, the Java run-time system transfers
control to the code defined inside volume( ). After the statements inside volume( ) have
executed, control is returned to the calling routine, and execution resumes with the line of
code following the call. In the most general sense, a method is Java’s way of implementing
subroutines.
There is something very important to notice inside the volume( ) method: the instance
variables width, height, and depth are referred to directly, without preceding them with an
object name or the dot operator. When a method uses an instance variable that is defined
by its class, it does so directly, without explicit reference to an object and without use of the
dot operator. This is easy to understand if you think about it. A method is always invoked
relative to some object of its class. Once this invocation has occurred, the object is known.
Thus, within a method, there is no need to specify the object a second time. This means
that width, height, and depth inside volume( ) implicitly refer to the copies of those
variables found in the object that invokes volume( ).

Part I

Box mybox1 = new Box();
Box mybox2 = new Box();

118

PART I

The Java Language

Let’s review: When an instance variable is accessed by code that is not part of the class
in which that instance variable is defined, it must be done through an object, by use of the
dot operator. However, when an instance variable is accessed by code that is part of the
same class as the instance variable, that variable can be referred to directly. The same thing
applies to methods.

Returning a Value
While the implementation of volume( ) does move the computation of a box’s volume
inside the Box class where it belongs, it is not the best way to do it. For example, what if
another part of your program wanted to know the volume of a box, but not display its
value? A better way to implement volume( ) is to have it compute the volume of the box
and return the result to the caller. The following example, an improved version of the
preceding program, does just that:
// Now, volume() returns the volume of a box.
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}

Introducing Classes

119

As you can see, when volume( ) is called, it is put on the right side of an assignment
statement. On the left is a variable, in this case vol, that will receive the value returned by
volume( ). Thus, after
vol = mybox1.volume();

executes, the value of mybox1.volume( ) is 3,000 and this value then is stored in vol.
There are two important things to understand about returning values:
• The type of data returned by a method must be compatible with the return type
specified by the method. For example, if the return type of some method is
boolean, you could not return an integer.
• The variable receiving the value returned by a method (such as vol, in this case)
must also be compatible with the return type specified for the method.
One more point: The preceding program can be written a bit more efficiently because
there is actually no need for the vol variable. The call to volume( ) could have been used in
the println( ) statement directly, as shown here:
System.out.println("Volume is" + mybox1.volume());

In this case, when println( ) is executed, mybox1.volume( ) will be called automatically and
its value will be passed to println( ).

Adding a Method That Takes Parameters
While some methods don’t need parameters, most do. Parameters allow a method to be
generalized. That is, a parameterized method can operate on a variety of data and/or be
used in a number of slightly different situations. To illustrate this point, let’s use a very
simple example. Here is a method that returns the square of the number 10:
int square()
{
return 10 * 10;
}

While this method does, indeed, return the value of 10 squared, its use is very limited.
However, if you modify the method so that it takes a parameter, as shown next, then you
can make square( ) much more useful.
int square(int i)
{
return i * i;
}

Now, square( ) will return the square of whatever value it is called with. That is, square( ) is
now a general-purpose method that can compute the square of any integer value, rather
than just 10.
Here is an example:
int x, y;
x = square(5); // x equals 25
x = square(9); // x equals 81

Part I

Chapter 6

120

PART I

The Java Language

y = 2;
x = square(y); // x equals 4

In the first call to square( ), the value 5 will be passed into parameter i. In the second call, i
will receive the value 9. The third invocation passes the value of y, which is 2 in this example.
As these examples show, square( ) is able to return the square of whatever data it is passed.
It is important to keep the two terms parameter and argument straight. A parameter is a
variable defined by a method that receives a value when the method is called. For example,
in square( ), i is a parameter. An argument is a value that is passed to a method when it is
invoked. For example, square(100) passes 100 as an argument. Inside square( ), the
parameter i receives that value.
You can use a parameterized method to improve the Box class. In the preceding
examples, the dimensions of each box had to be set separately by use of a sequence of
statements, such as:
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;

While this code works, it is troubling for two reasons. First, it is clumsy and error prone.
For example, it would be easy to forget to set a dimension. Second, in well-designed Java
programs, instance variables should be accessed only through methods defined by their
class. In the future, you can change the behavior of a method, but you can’t change the
behavior of an exposed instance variable.
Thus, a better approach to setting the dimensions of a box is to create a method that
takes the dimensions of a box in its parameters and sets each instance variable
appropriately. This concept is implemented by the following program:
// This program uses a parameterized method.
class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
// sets dimensions of box
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
class BoxDemo5 {

Introducing Classes

121

public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}

As you can see, the setDim( ) method is used to set the dimensions of each box. For
example, when
mybox1.setDim(10, 20, 15);

is executed, 10 is copied into parameter w, 20 is copied into h, and 15 is copied into d.
Inside setDim( ) the values of w, h, and d are then assigned to width, height, and depth,
respectively.
For many readers, the concepts presented in the preceding sections will be familiar.
However, if such things as method calls, arguments, and parameters are new to you, then
you might want to take some time to experiment before moving on. The concepts of the
method invocation, parameters, and return values are fundamental to Java programming.

Constructors
It can be tedious to initialize all of the variables in a class each time an instance is created.
Even when you add convenience functions like setDim( ), it would be simpler and more
concise to have all of the setup done at the time the object is first created. Because the
requirement for initialization is so common, Java allows objects to initialize themselves
when they are created. This automatic initialization is performed through the use of a
constructor.
A constructor initializes an object immediately upon creation. It has the same name as
the class in which it resides and is syntactically similar to a method. Once defined, the
constructor is automatically called immediately after the object is created, before the new
operator completes. Constructors look a little strange because they have no return type,
not even void. This is because the implicit return type of a class’ constructor is the class type
itself. It is the constructor’s job to initialize the internal state of an object so that the code
creating an instance will have a fully initialized, usable object immediately.
You can rework the Box example so that the dimensions of a box are automatically
initialized when an object is constructed. To do so, replace setDim( ) with a constructor.

Part I

Chapter 6

122

PART I

The Java Language

Let’s begin by defining a simple constructor that simply sets the dimensions of each box to
the same values. This version is shown here:
/* Here, Box uses a constructor to initialize the
dimensions of a box.
*/
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo6 {
public static void main(String args[]) {
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}

When this program is run, it generates the following results:
Constructing Box
Constructing Box
Volume is 1000.0
Volume is 1000.0

As you can see, both mybox1 and mybox2 were initialized by the Box( ) constructor
when they were created. Since the constructor gives all boxes the same dimensions, 10 by
10 by 10, both mybox1 and mybox2 will have the same volume. The println( ) statement

Introducing Classes

123

inside Box( ) is for the sake of illustration only. Most constructors will not display anything.
They will simply initialize an object.
Before moving on, let’s reexamine the new operator. As you know, when you allocate an
object, you use the following general form:
class-var = new classname ( );
Now you can understand why the parentheses are needed after the class name. What is
actually happening is that the constructor for the class is being called. Thus, in the line
Box mybox1 = new Box();

new Box( ) is calling the Box( ) constructor. When you do not explicitly define a constructor
for a class, then Java creates a default constructor for the class. This is why the preceding
line of code worked in earlier versions of Box that did not define a constructor. The default
constructor automatically initializes all instance variables to zero. The default constructor is
often sufficient for simple classes, but it usually won’t do for more sophisticated ones. Once
you define your own constructor, the default constructor is no longer used.

Parameterized Constructors
While the Box( ) constructor in the preceding example does initialize a Box object, it is not
very useful—all boxes have the same dimensions. What is needed is a way to construct Box
objects of various dimensions. The easy solution is to add parameters to the constructor. As
you can probably guess, this makes it much more useful. For example, the following version
of Box defines a parameterized constructor that sets the dimensions of a box as specified by
those parameters. Pay special attention to how Box objects are created.
/* Here, Box uses a parameterized constructor to
initialize the dimensions of a box.
*/
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo7 {
public static void main(String args[]) {

Part I

Chapter 6

124

PART I

The Java Language

// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}

The output from this program is shown here:
Volume is 3000.0
Volume is 162.0

As you can see, each object is initialized as specified in the parameters to its constructor.
For example, in the following line,
Box mybox1 = new Box(10, 20, 15);

the values 10, 20, and 15 are passed to the Box( ) constructor when new creates the object.
Thus, mybox1’s copy of width, height, and depth will contain the values 10, 20, and 15,
respectively.

The this Keyword
Sometimes a method will need to refer to the object that invoked it. To allow this, Java
defines the this keyword. this can be used inside any method to refer to the current object.
That is, this is always a reference to the object on which the method was invoked. You can
use this anywhere a reference to an object of the current class’ type is permitted.
To better understand what this refers to, consider the following version of Box( ):
// A redundant use of this.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}

This version of Box( ) operates exactly like the earlier version. The use of this is redundant,
but perfectly correct. Inside Box( ), this will always refer to the invoking object. While it is
redundant in this case, this is useful in other contexts, one of which is explained in the next
section.

Chapter 6

Introducing Classes

125

As you know, it is illegal in Java to declare two local variables with the same name inside the
same or enclosing scopes. Interestingly, you can have local variables, including formal
parameters to methods, which overlap with the names of the class’ instance variables. However,
when a local variable has the same name as an instance variable, the local variable hides the
instance variable. This is why width, height, and depth were not used as the names of the
parameters to the Box( ) constructor inside the Box class. If they had been, then width, for
example, would have referred to the formal parameter, hiding the instance variable width.
While it is usually easier to simply use different names, there is another way around this
situation. Because this lets you refer directly to the object, you can use it to resolve any
namespace collisions that might occur between instance variables and local variables. For
example, here is another version of Box( ), which uses width, height, and depth for parameter
names and then uses this to access the instance variables by the same name:
// Use this to resolve name-space collisions.
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}

A word of caution: The use of this in such a context can sometimes be confusing, and
some programmers are careful not to use local variables and formal parameter names that
hide instance variables. Of course, other programmers believe the contrary—that it is a
good convention to use the same names for clarity, and use this to overcome the instance
variable hiding. It is a matter of taste which approach you adopt.

Garbage Collection
Since objects are dynamically allocated by using the new operator, you might be wondering
how such objects are destroyed and their memory released for later reallocation. In some
languages, such as C++, dynamically allocated objects must be manually released by use of a
delete operator. Java takes a different approach; it handles deallocation for you automatically.
The technique that accomplishes this is called garbage collection. It works like this: when no
references to an object exist, that object is assumed to be no longer needed, and the memory
occupied by the object can be reclaimed. There is no explicit need to destroy objects as in
C++. Garbage collection only occurs sporadically (if at all) during the execution of your
program. It will not occur simply because one or more objects exist that are no longer
used. Furthermore, different Java run-time implementations will take varying approaches to
garbage collection, but for the most part, you should not have to think about it while writing
your programs.

The finalize( ) Method
Sometimes an object will need to perform some action when it is destroyed. For example,
if an object is holding some non-Java resource such as a file handle or character font, then
you might want to make sure these resources are freed before an object is destroyed. To

Part I

Instance Variable Hiding

126

PART I

The Java Language

handle such situations, Java provides a mechanism called finalization. By using finalization,
you can define specific actions that will occur when an object is just about to be reclaimed
by the garbage collector.
To add a finalizer to a class, you simply define the finalize( ) method. The Java run time
calls that method whenever it is about to recycle an object of that class. Inside the finalize( )
method, you will specify those actions that must be performed before an object is destroyed.
The garbage collector runs periodically, checking for objects that are no longer referenced
by any running state or indirectly through other referenced objects. Right before an asset is
freed, the Java run time calls the finalize( ) method on the object.
The finalize( ) method has this general form:
protected void finalize( )
{
// finalization code here
}
Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined
outside its class. This and the other access modifiers are explained in Chapter 7.
It is important to understand that finalize( ) is only called just prior to garbage collection.
It is not called when an object goes out-of-scope, for example. This means that you cannot
know when—or even if—finalize( ) will be executed. Therefore, your program should
provide other means of releasing system resources, etc., used by the object. It must not
rely on finalize( ) for normal program operation.
NOTE If you are familiar with C++, then you know that C++ allows you to define a destructor for a class,
which is called when an object goes out-of-scope. Java does not support this idea or provide for
destructors. The finalize( ) method only approximates the function of a destructor. As you get more
experienced with Java, you will see that the need for destructor functions is minimal because of
Java’s garbage collection subsystem.

A Stack Class
While the Box class is useful to illustrate the essential elements of a class, it is of little
practical value. To show the real power of classes, this chapter will conclude with a more
sophisticated example. As you recall from the discussion of object-oriented programming
(OOP) presented in Chapter 2, one of OOP’s most important benefits is the encapsulation
of data and the code that manipulates that data. As you have seen, the class is the mechanism
by which encapsulation is achieved in Java. By creating a class, you are creating a new data
type that defines both the nature of the data being manipulated and the routines used to
manipulate it. Further, the methods define a consistent and controlled interface to the
class’ data. Thus, you can use the class through its methods without having to worry about
the details of its implementation or how the data is actually managed within the class. In a
sense, a class is like a “data engine.” No knowledge of what goes on inside the engine is
required to use the engine through its controls. In fact, since the details are hidden, its
inner workings can be changed as needed. As long as your code uses the class through
its methods, internal details can change without causing side effects outside the class.
To see a practical application of the preceding discussion, let’s develop one of the
archetypal examples of encapsulation: the stack. A stack stores data using first-in, last-out

Introducing Classes

127

ordering. That is, a stack is like a stack of plates on a table—the first plate put down on the
table is the last plate to be used. Stacks are controlled through two operations traditionally
called push and pop. To put an item on top of the stack, you will use push. To take an item
off the stack, you will use pop. As you will see, it is easy to encapsulate the entire stack
mechanism.
Here is a class called Stack that implements a stack for up to ten integers:
// This class defines an integer stack that can hold 10 values
class Stack {
int stck[] = new int[10];
int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}

As you can see, the Stack class defines two data items and three methods. The stack of
integers is held by the array stck. This array is indexed by the variable tos, which always
contains the index of the top of the stack. The Stack( ) constructor initializes tos to –1,
which indicates an empty stack. The method push( ) puts an item on the stack. To retrieve
an item, call pop( ). Since access to the stack is through push( ) and pop( ), the fact that the
stack is held in an array is actually not relevant to using the stack. For example, the stack
could be held in a more complicated data structure, such as a linked list, yet the interface
defined by push( ) and pop( ) would remain the same.
The class TestStack, shown here, demonstrates the Stack class. It creates two integer
stacks, pushes some values onto each, and then pops them off.
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();

Part I

Chapter 6

128

PART I

The Java Language

// push some numbers onto the stack
for(int i=0; i<10; i++) mystack1.push(i);
for(int i=10; i<20; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}

This program generates the following output:
Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2:
19
18
17
16
15
14
13
12
11
10

As you can see, the contents of each stack are separate.
One last point about the Stack class. As it is currently implemented, it is possible for the
array that holds the stack, stck, to be altered by code outside of the Stack class. This leaves
Stack open to misuse or mischief. In the next chapter, you will see how to remedy this
situation.

CHAPTER

7

A Closer Look at Methods
and Classes

This chapter continues the discussion of methods and classes begun in the preceding
chapter. It examines several topics relating to methods, including overloading, parameter
passing, and recursion. The chapter then returns to the class, discussing access control, the
use of the keyword static, and one of Java’s most important built-in classes: String.

Overloading Methods
In Java it is possible to define two or more methods within the same class that share the
same name, as long as their parameter declarations are different. When this is the case,
the methods are said to be overloaded, and the process is referred to as method overloading.
Method overloading is one of the ways that Java supports polymorphism. If you have never
used a language that allows the overloading of methods, then the concept may seem
strange at first. But as you will see, method overloading is one of Java’s most exciting and
useful features.
When an overloaded method is invoked, Java uses the type and/or number of arguments
as its guide to determine which version of the overloaded method to actually call. Thus,
overloaded methods must differ in the type and/or number of their parameters. While
overloaded methods may have different return types, the return type alone is insufficient to
distinguish two versions of a method. When Java encounters a call to an overloaded method,
it simply executes the version of the method whose parameters match the arguments used in
the call.
Here is a simple example that illustrates method overloading:
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}

129

130

PART I

The Java Language

// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// Overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}

This program generates the following output:
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625

As you can see, test( ) is overloaded four times. The first version takes no parameters,
the second takes one integer parameter, the third takes two integer parameters, and the
fourth takes one double parameter. The fact that the fourth version of test( ) also returns a
value is of no consequence relative to overloading, since return types do not play a role in
overload resolution.
When an overloaded method is called, Java looks for a match between the arguments
used to call the method and the method’s parameters. However, this match need not always
be exact. In some cases, Java’s automatic type conversions can play a role in overload
resolution. For example, consider the following program:
// Automatic type conversions apply to overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);

Chapter 7

A Closer Look at Methods and Classes

131

// Overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: " + a);
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)
}
}

This program generates the following output:
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2

As you can see, this version of OverloadDemo does not define test(int). Therefore,
when test( ) is called with an integer argument inside Overload, no matching method is
found. However, Java can automatically convert an integer into a double, and this conversion
can be used to resolve the call. Therefore, after test(int) is not found, Java elevates i to double
and then calls test(double). Of course, if test(int) had been defined, it would have been
called instead. Java will employ its automatic type conversions only if no exact match is found.
Method overloading supports polymorphism because it is one way that Java implements
the “one interface, multiple methods” paradigm. To understand how, consider the
following. In languages that do not support method overloading, each method must be
given a unique name. However, frequently you will want to implement essentially the same
method for different types of data. Consider the absolute value function. In languages that
do not support overloading, there are usually three or more versions of this function, each
with a slightly different name. For instance, in C, the function abs( ) returns the absolute
value of an integer, labs( ) returns the absolute value of a long integer, and fabs( ) returns
the absolute value of a floating-point value. Since C does not support overloading, each
function has to have its own name, even though all three functions do essentially the same
thing. This makes the situation more complex, conceptually, than it actually is. Although
the underlying concept of each function is the same, you still have three names to
remember. This situation does not occur in Java, because each absolute value method can
use the same name. Indeed, Java’s standard class library includes an absolute value method,
called abs( ). This method is overloaded by Java’s Math class to handle all numeric types.
Java determines which version of abs( ) to call based upon the type of argument.

Part I

}

132

PART I

The Java Language

The value of overloading is that it allows related methods to be accessed by use of a
common name. Thus, the name abs represents the general action that is being performed. It
is left to the compiler to choose the right specific version for a particular circumstance. You,
the programmer, need only remember the general operation being performed. Through
the application of polymorphism, several names have been reduced to one. Although this
example is fairly simple, if you expand the concept, you can see how overloading can help
you manage greater complexity.
When you overload a method, each version of that method can perform any activity you
desire. There is no rule stating that overloaded methods must relate to one another. However,
from a stylistic point of view, method overloading implies a relationship. Thus, while you
can use the same name to overload unrelated methods, you should not. For example, you
could use the name sqr to create methods that return the square of an integer and the
square root of a floating-point value. But these two operations are fundamentally different.
Applying method overloading in this manner defeats its original purpose. In practice, you
should only overload closely related operations.

Overloading Constructors
In addition to overloading normal methods, you can also overload constructor methods. In
fact, for most real-world classes that you create, overloaded constructors will be the norm,
not the exception. To understand why, let’s return to the Box class developed in the
preceding chapter. Following is the latest version of Box:
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}

As you can see, the Box( ) constructor requires three parameters. This means that all
declarations of Box objects must pass three arguments to the Box( ) constructor. For
example, the following statement is currently invalid:
Box ob = new Box();

Since Box( ) requires three arguments, it’s an error to call it without them. This raises
some important questions. What if you simply wanted a box and did not care (or know)
what its initial dimensions were? Or, what if you want to be able to initialize a cube by
specifying only one value that would be used for all three dimensions? As the Box class is
currently written, these other options are not available to you.

A Closer Look at Methods and Classes

133

Fortunately, the solution to these problems is quite easy: simply overload the Box
constructor so that it handles the situations just described. Here is a program that contains
an improved version of Box that does just that:
/* Here, Box defines three constructors to initialize
the dimensions of a box various ways.
*/
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor
Box() {
width = -1;
height = -1;
depth = -1;
}

used when no dimensions specified
// use -1 to indicate
// an uninitialized
// box

// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);

Part I

Chapter 7

134

PART I

The Java Language

// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}

The output produced by this program is shown here:
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0

As you can see, the proper overloaded constructor is called based upon the parameters
specified when new is executed.

Using Objects as Parameters
So far, we have only been using simple types as parameters to methods. However, it is both
correct and common to pass objects to methods. For example, consider the following short
program:
// Objects may be passed to methods.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// return true if o is equal to the invoking object
boolean equals(Test o) {
if(o.a == a && o.b == b) return true;
else return false;
}
}
class PassOb {
public static void main(String args[]) {
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(ob3));
}
}

This program generates the following output:
ob1 == ob2: true
ob1 == ob3: false

A Closer Look at Methods and Classes

135

As you can see, the equals( ) method inside Test compares two objects for equality and
returns the result. That is, it compares the invoking object with the one that it is passed. If
they contain the same values, then the method returns true. Otherwise, it returns false.
Notice that the parameter o in equals( ) specifies Test as its type. Although Test is a class
type created by the program, it is used in just the same way as Java’s built-in types.
One of the most common uses of object parameters involves constructors. Frequently,
you will want to construct a new object so that it is initially the same as some existing object.
To do this, you must define a constructor that takes an object of its class as a parameter. For
example, the following version of Box allows one object to initialize another:
// Here, Box allows one object to initialize another.
class Box {
double width;
double height;
double depth;
// Notice this constructor. It takes an object of type Box.
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor
Box() {
width = -1;
height = -1;
depth = -1;
}

used when no dimensions specified
// use -1 to indicate
// an uninitialized
// box

// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons2 {
public static void main(String args[]) {
// create boxes using the various constructors

Part I

Chapter 7

136

PART I

The Java Language

Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
Box myclone = new Box(mybox1); // create copy of mybox1
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of cube is " + vol);
// get volume of clone
vol = myclone.volume();
System.out.println("Volume of clone is " + vol);
}
}

As you will see when you begin to create your own classes, providing many forms of
constructors is usually required to allow objects to be constructed in a convenient and
efficient manner.

A Closer Look at Argument Passing
In general, there are two ways that a computer language can pass an argument to a subroutine.
The first way is call-by-value. This approach copies the value of an argument into the formal
parameter of the subroutine. Therefore, changes made to the parameter of the subroutine
have no effect on the argument. The second way an argument can be passed is call-by-reference.
In this approach, a reference to an argument (not the value of the argument) is passed to
the parameter. Inside the subroutine, this reference is used to access the actual argument
specified in the call. This means that changes made to the parameter will affect the
argument used to call the subroutine. As you will see, although Java uses call-by-value
to pass all arguments, the precise effect differs between whether a primitive type or a
reference type is passed.
When you pass a primitive type to a method, it is passed by value. Thus, a copy of the
argument is made, and what occurs to the parameter that receives the argument has no
effect outside the method. For example, consider the following program:
// Primitive types are passed by value.
class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}
}

A Closer Look at Methods and Classes

137

class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " +
a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}

The output from this program is shown here:
a and b before call: 15 20
a and b after call: 15 20

As you can see, the operations that occur inside meth( ) have no effect on the values of a
and b used in the call; their values here did not change to 30 and 10.
When you pass an object to a method, the situation changes dramatically, because
objects are passed by what is effectively call-by-reference. Keep in mind that when you
create a variable of a class type, you are only creating a reference to an object. Thus, when
you pass this reference to a method, the parameter that receives it will refer to the same
object as that referred to by the argument. This effectively means that objects act as if they
are passed to methods by use of call-by-reference. Changes to the object inside the method
do affect the object used as an argument. For example, consider the following program:
// Objects are passed through their references.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class PassObjRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);

Part I

Chapter 7

138

PART I

The Java Language

System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}

This program generates the following output:
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10

As you can see, in this case, the actions inside meth( ) have affected the object used as an
argument.
REMEMBER When an object reference is passed to a method, the reference itself is passed by use of
call-by-value. However, since the value being passed refers to an object, the copy of that value will
still refer to the same object that its corresponding argument does.

Returning Objects
A method can return any type of data, including class types that you create. For example, in
the following program, the incrByTen( ) method returns an object in which the value of a is
ten greater than it is in the invoking object.
// Returning an object.
class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}
}
class RetOb {
public static void main(String args[]) {
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);

A Closer Look at Methods and Classes

139

ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "
+ ob2.a);
}
}

The output generated by this program is shown here:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22

As you can see, each time incrByTen( ) is invoked, a new object is created, and a reference
to it is returned to the calling routine.
The preceding program makes another important point: Since all objects are
dynamically allocated using new, you don’t need to worry about an object going out-ofscope because the method in which it was created terminates. The object will continue to
exist as long as there is a reference to it somewhere in your program. When there are no
references to it, the object will be reclaimed the next time garbage collection takes place.

Recursion
Java supports recursion. Recursion is the process of defining something in terms of itself. As
it relates to Java programming, recursion is the attribute that allows a method to call itself.
A method that calls itself is said to be recursive.
The classic example of recursion is the computation of the factorial of a number. The
factorial of a number N is the product of all the whole numbers between 1 and N. For
example, 3 factorial is 1 × 2 × 3 ×, or 6. Here is how a factorial can be computed by use
of a recursive method:
// A simple example of recursion.
class Factorial {
// this is a recursive method
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}

Part I

Chapter 7

140

PART I

The Java Language

The output from this program is shown here:
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120

If you are unfamiliar with recursive methods, then the operation of fact( ) may seem
a bit confusing. Here is how it works. When fact( ) is called with an argument of 1, the
function returns 1; otherwise, it returns the product of fact(n–1)*n. To evaluate this
expression, fact( ) is called with n–1. This process repeats until n equals 1 and the calls
to the method begin returning.
To better understand how the fact( ) method works, let’s go through a short example.
When you compute the factorial of 3, the first call to fact( ) will cause a second call to be
made with an argument of 2. This invocation will cause fact( ) to be called a third time with
an argument of 1. This call will return 1, which is then multiplied by 2 (the value of n in the
second invocation). This result (which is 2) is then returned to the original invocation of
fact( ) and multiplied by 3 (the original value of n ). This yields the answer, 6. You might
find it interesting to insert println( ) statements into fact( ), which will show at what level
each call is and what the intermediate answers are.
When a method calls itself, new local variables and parameters are allocated storage on
the stack, and the method code is executed with these new variables from the start. As each
recursive call returns, the old local variables and parameters are removed from the stack,
and execution resumes at the point of the call inside the method. Recursive methods could
be said to “telescope” out and back.
Recursive versions of many routines may execute a bit more slowly than the iterative
equivalent because of the added overhead of the additional function calls. Many recursive
calls to a method could cause a stack overrun. Because storage for parameters and local
variables is on the stack and each new call creates a new copy of these variables, it is possible
that the stack could be exhausted. If this occurs, the Java run-time system will cause an
exception. However, you probably will not have to worry about this unless a recursive
routine runs wild.
The main advantage to recursive methods is that they can be used to create clearer and
simpler versions of several algorithms than can their iterative relatives. For example, the
QuickSort sorting algorithm is quite difficult to implement in an iterative way. Also, some
types of AI-related algorithms are most easily implemented using recursive solutions.
When writing recursive methods, you must have an if statement somewhere to force the
method to return without the recursive call being executed. If you don’t do this, once you
call the method, it will never return. This is a very common error in working with recursion.
Use println( ) statements liberally during development so that you can watch what is going
on and abort execution if you see that you have made a mistake.
Here is one more example of recursion. The recursive method printArray( ) prints the
first i elements in the array values.
// Another example that uses recursion.
class RecTest {
int values[];

A Closer Look at Methods and Classes

141

RecTest(int i) {
values = new int[i];
}
// display array -- recursively
void printArray(int i) {
if(i==0) return;
else printArray(i-1);
System.out.println("[" + (i-1) + "] " + values[i-1]);
}
}
class Recursion2 {
public static void main(String args[]) {
RecTest ob = new RecTest(10);
int i;
for(i=0; i<10; i++) ob.values[i] = i;
ob.printArray(10);
}
}

This program generates the following output:
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]

0
1
2
3
4
5
6
7
8
9

Introducing Access Control
As you know, encapsulation links data with the code that manipulates it. However,
encapsulation provides another important attribute: access control. Through encapsulation,
you can control what parts of a program can access the members of a class. By controlling
access, you can prevent misuse. For example, allowing access to data only through a welldefined set of methods, you can prevent the misuse of that data. Thus, when correctly
implemented, a class creates a “black box” which may be used, but the inner workings of
which are not open to tampering. However, the classes that were presented earlier do not
completely meet this goal. For example, consider the Stack class shown at the end of
Chapter 6. While it is true that the methods push( ) and pop( ) do provide a controlled
interface to the stack, this interface is not enforced. That is, it is possible for another part of
the program to bypass these methods and access the stack directly. Of course, in the wrong
hands, this could lead to trouble. In this section, you will be introduced to the mechanism
by which you can precisely control access to the various members of a class.

Part I

Chapter 7

142

PART I

The Java Language

How a member can be accessed is determined by the access modifier attached to its
declaration. Java supplies a rich set of access modifiers. Some aspects of access control are
related mostly to inheritance or packages. (A package is, essentially, a grouping of classes.)
These parts of Java’s access control mechanism will be discussed later. Here, let’s begin by
examining access control as it applies to a single class. Once you understand the
fundamentals of access control, the rest will be easy.
Java’s access modifiers are public, private, and protected. Java also defines a default
access level. protected applies only when inheritance is involved. The other access modifiers
are described next.
Let’s begin by defining public and private. When a member of a class is modified by
public, then that member can be accessed by any other code. When a member of a class is
specified as private, then that member can only be accessed by other members of its class.
Now you can understand why main( ) has always been preceded by the public modifier. It
is called by code that is outside the program—that is, by the Java run-time system. When
no access modifier is used, then by default the member of a class is public within its own
package, but cannot be accessed outside of its package. (Packages are discussed in the
following chapter.)
In the classes developed so far, all members of a class have used the default access
mode, which is essentially public. However, this is not what you will typically want to be the
case. Usually, you will want to restrict access to the data members of a class—allowing access
only through methods. Also, there will be times when you will want to define methods that
are private to a class.
An access modifier precedes the rest of a member’s type specification. That is, it must
begin a member’s declaration statement. Here is an example:
public int i;
private double j;
private int myMethod(int a, char b) { //...

To understand the effects of public and private access, consider the following program:
/* This program demonstrates the difference between
public and private.
*/
class Test {
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) { // set c's value
c = i;
}
int getc() { // get c's value
return c;
}
}

A Closer Look at Methods and Classes

143

class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
// These are OK, a and b may be accessed directly
ob.a = 10;
ob.b = 20;
// This is not OK and will cause an error
ob.c = 100; // Error!

//

// You must access c through its methods
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}

As you can see, inside the Test class, a uses default access, which for this example is
the same as specifying public. b is explicitly specified as public. Member c is given private
access. This means that it cannot be accessed by code outside of its class. So, inside the
AccessTest class, c cannot be used directly. It must be accessed through its public methods:
setc( ) and getc( ). If you were to remove the comment symbol from the beginning of the
following line,
// ob.c = 100; // Error!

then you would not be able to compile this program because of the access violation.
To see how access control can be applied to a more practical example, consider the
following improved version of the Stack class shown at the end of Chapter 6.
// This class defines an integer stack that can hold 10 values.
class Stack {
/* Now, both stck and tos are private. This means
that they cannot be accidentally or maliciously
altered in a way that would be harmful to the stack.
*/
private int stck[] = new int[10];
private int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}

Part I

Chapter 7

144

PART I

The Java Language

// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}

As you can see, now both stck, which holds the stack, and tos, which is the index of the
top of the stack, are specified as private. This means that they cannot be accessed or altered
except through push( ) and pop( ). Making tos private, for example, prevents other parts of
your program from inadvertently setting it to a value that is beyond the end of the stck array.
The following program demonstrates the improved Stack class. Try removing the
commented-out lines to prove to yourself that the stck and tos members are, indeed,
inaccessible.
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
// push some numbers onto the stack
for(int i=0; i<10; i++) mystack1.push(i);
for(int i=10; i<20; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
// these statements are not legal
// mystack1.tos = -2;
// mystack2.stck[3] = 100;
}
}

Although methods will usually provide access to the data defined by a class, this does
not always have to be the case. It is perfectly proper to allow an instance variable to be
public when there is good reason to do so. For example, most of the simple classes in this
book were created with little concern about controlling access to instance variables for the
sake of simplicity. However, in most real-world classes, you will need to allow operations on
data only through methods. The next chapter will return to the topic of access control. As
you will see, it is particularly important when inheritance is involved.

Chapter 7

A Closer Look at Methods and Classes

145

There will be times when you will want to define a class member that will be used
independently of any object of that class. Normally, a class member must be accessed only
in conjunction with an object of its class. However, it is possible to create a member that can
be used by itself, without reference to a specific instance. To create such a member, precede
its declaration with the keyword static. When a member is declared static, it can be accessed
before any objects of its class are created, and without reference to any object. You can declare
both methods and variables to be static. The most common example of a static member is
main( ). main( ) is declared as static because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects of
its class are declared, no copy of a static variable is made. Instead, all instances of the class
share the same static variable.
Methods declared as static have several restrictions:
• They can only directly call other static methods.
• They can only directly access static data.
• They cannot refer to this or super in any way. (The keyword super relates to
inheritance and is described in the next chapter.)
If you need to do computation in order to initialize your static variables, you can
declare a static block that gets executed exactly once, when the class is first loaded. The
following example shows a class that has a static method, some static variables, and a static
initialization block:
// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x)
System.out.println("x
System.out.println("a
System.out.println("b
}

{
= " + x);
= " + a);
= " + b);

static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is
set to 3, then the static block executes, which prints a message and then initializes b to a*4
or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( )
statements refer to the two static variables a and b, as well as to the local variable x.

Part I

Understanding static

146

PART I

The Java Language

Here is the output of the program:
Static block initialized.
x = 42
a = 3
b = 12

Outside of the class in which they are defined, static methods and variables can be
used independently of any object. To do so, you need only specify the name of their class
followed by the dot operator. For example, if you wish to call a static method from outside
its class, you can do so using the following general form:
classname.method( )
Here, classname is the name of the class in which the static method is declared. As you
can see, this format is similar to that used to call non-static methods through objectreference variables. A static variable can be accessed in the same way—by use of the dot
operator on the name of the class. This is how Java implements a controlled version of
global methods and global variables.
Here is an example. Inside main( ), the static method callme( ) and the static variable b
are accessed through their class name StaticDemo.
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}

Here is the output of this program:
a = 42
b = 99

Introducing final
A field can be declared as final. Doing so prevents its contents from being modified,
making it, essentially, a constant. This means that you must initialize a final field when
it is declared. You can do this in one of two ways: First, you can give it a value when it is
declared. Second, you can assign it a value within a constructor. The first approach is the
most common. Here is an example:

final
final
final
final
final

int
int
int
int
int

A Closer Look at Methods and Classes

147

FILE_NEW = 1;
FILE_OPEN = 2;
FILE_SAVE = 3;
FILE_SAVEAS = 4;
FILE_QUIT = 5;

Subsequent parts of your program can now use FILE_OPEN, etc., as if they were constants,
without fear that a value has been changed. It is a common coding convention to choose all
uppercase identifiers for final fields, as this example shows.
In addition to fields, both method parameters and local variables can be declared final.
Declaring a parameter final prevents it from being changed within the method. Declaring a
local variable final prevents it from being assigned a value more than once.
The keyword final can also be applied to methods, but its meaning is substantially
different than when it is applied to variables. This additional usage of final is described
in the next chapter, when inheritance is described.

Arrays Revisited
Arrays were introduced earlier in this book, before classes had been discussed. Now that
you know about classes, an important point can be made about arrays: they are implemented
as objects. Because of this, there is a special array attribute that you will want to take
advantage of. Specifically, the size of an array—that is, the number of elements that an array
can hold—is found in its length instance variable. All arrays have this variable, and it will
always hold the size of the array. Here is a program that demonstrates this property:
// This program demonstrates the length array member.
class Length {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
int a3[] = {4, 3, 2, 1};
System.out.println("length of a1 is " + a1.length);
System.out.println("length of a2 is " + a2.length);
System.out.println("length of a3 is " + a3.length);
}
}

This program displays the following output:
length of a1 is 10
length of a2 is 8
length of a3 is 4

As you can see, the size of each array is displayed. Keep in mind that the value of length
has nothing to do with the number of elements that are actually in use. It only reflects the
number of elements that the array is designed to hold.
You can put the length member to good use in many situations. For example, here is
an improved version of the Stack class. As you might recall, the earlier versions of this class

Part I

Chapter 7

148

PART I

The Java Language

always created a ten-element stack. The following version lets you create stacks of any size.
The value of stck.length is used to prevent the stack from overflowing.
// Improved Stack class that uses the length array member.
class Stack {
private int stck[];
private int tos;
// allocate and initialize stack
Stack(int size) {
stck = new int[size];
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==stck.length-1) // use length member
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
class TestStack2 {
public static void main(String args[]) {
Stack mystack1 = new Stack(5);
Stack mystack2 = new Stack(8);
// push some numbers onto the stack
for(int i=0; i<5; i++) mystack1.push(i);
for(int i=0; i<8; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<5; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
}

A Closer Look at Methods and Classes

149

Notice that the program creates two stacks: one five elements deep and the other eight
elements deep. As you can see, the fact that arrays maintain their own length information
makes it easy to create stacks of any size.

Introducing Nested and Inner Classes
It is possible to define a class within another class; such classes are known as nested classes.
The scope of a nested class is bounded by the scope of its enclosing class. Thus, if class B is
defined within class A, then B does not exist independently of A. A nested class has access
to the members, including private members, of the class in which it is nested. However, the
enclosing class does not have access to the members of the nested class. A nested class that
is declared directly within its enclosing class scope is a member of its enclosing class. It is
also possible to declare a nested class that is local to a block.
There are two types of nested classes: static and non-static. A static nested class is one
that has the static modifier applied. Because it is static, it must access the non-static members
of its enclosing class through an object. That is, it cannot refer to non-static members of its
enclosing class directly. Because of this restriction, static nested classes are seldom used.
The most important type of nested class is the inner class. An inner class is a non-static
nested class. It has access to all of the variables and methods of its outer class and may refer
to them directly in the same way that other non-static members of the outer class do.
The following program illustrates how to define and use an inner class. The class named
Outer has one instance variable named outer_x, one instance method named test( ), and
defines one inner class called Inner.
// Demonstrate an inner class.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}

Part I

Chapter 7

150

PART I

The Java Language

Output from this application is shown here:
display: outer_x = 100

In the program, an inner class named Inner is defined within the scope of class Outer.
Therefore, any code in class Inner can directly access the variable outer_x. An instance
method named display( ) is defined inside Inner. This method displays outer_x on the
standard output stream. The main( ) method of InnerClassDemo creates an instance of
class Outer and invokes its test( ) method. That method creates an instance of class Inner
and the display( ) method is called.
It is important to realize that an instance of Inner can be created only within the scope
of class Outer. The Java compiler generates an error message if any code outside of class
Outer attempts to instantiate class Inner. In general, an inner class instance must be
created by an enclosing scope.
As explained, an inner class has access to all of the members of its enclosing class, but
the reverse is not true. Members of the inner class are known only within the scope of the
inner class and may not be used by the outer class. For example,
// This program will not compile.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
int y = 10; // y is local to Inner
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
void showy() {
System.out.println(y); // error, y not known here!
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}

Here, y is declared as an instance variable of Inner. Thus, it is not known outside of that
class and it cannot be used by showy( ).

A Closer Look at Methods and Classes

151

Although we have been focusing on inner classes declared as members within an outer
class scope, it is possible to define inner classes within any block scope. For example, you
can define a nested class within the block defined by a method or even within the body of
a for loop, as this next program shows:
// Define an inner class within a for loop.
class Outer {
int outer_x = 100;
void test() {
for(int i=0; i<10; i++) {
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
Inner inner = new Inner();
inner.display();
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}

The output from this version of the program is shown here:
display:
display:
display:
display:
display:
display:
display:
display:
display:
display:

outer_x
outer_x
outer_x
outer_x
outer_x
outer_x
outer_x
outer_x
outer_x
outer_x

=
=
=
=
=
=
=
=
=
=

100
100
100
100
100
100
100
100
100
100

While nested classes are not applicable to all situations, they are particularly helpful
when handling events. We will return to the topic of nested classes in Chapter 22. There
you will see how inner classes can be used to simplify the code needed to handle certain
types of events. You will also learn about anonymous inner classes, which are inner classes that
don’t have a name.
One final point: Nested classes were not allowed by the original 1.0 specification for
Java. They were added by Java 1.1.

Part I

Chapter 7

152

PART I

The Java Language

Exploring the String Class
Although the String class will be examined in depth in Part II of this book, a short
exploration of it is warranted now, because we will be using strings in some of the example
programs shown toward the end of Part I. String is probably the most commonly used class
in Java’s class library. The obvious reason for this is that strings are a very important part of
programming.
The first thing to understand about strings is that every string you create is actually an
object of type String. Even string constants are actually String objects. For example, in the
statement
System.out.println("This is a String, too");

the string "This is a String, too" is a String object.
The second thing to understand about strings is that objects of type String are immutable;
once a String object is created, its contents cannot be altered. While this may seem like a
serious restriction, it is not, for two reasons:
• If you need to change a string, you can always create a new one that contains the
modifications.
• Java defines a peer class of String, called StringBuffer, which allows strings to
be altered, so all of the normal string manipulations are still available in Java.
(StringBuffer is described in Part II of this book.)
Strings can be constructed in a variety of ways. The easiest is to use a statement like this:
String myString = "this is a test";

Once you have created a String object, you can use it anywhere that a string is allowed.
For example, this statement displays myString:
System.out.println(myString);

Java defines one operator for String objects: +. It is used to concatenate two strings. For
example, this statement
String myString = "I" + " like " + "Java.";

results in myString containing "I like Java."
The following program demonstrates the preceding concepts:
// Demonstrating Strings.
class StringDemo {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;
System.out.println(strOb1);
System.out.println(strOb2);

Chapter 7

A Closer Look at Methods and Classes

153

System.out.println(strOb3);
}

The output produced by this program is shown here:
First String
Second String
First String and Second String

The String class contains several methods that you can use. Here are a few. You can test
two strings for equality by using equals( ). You can obtain the length of a string by calling
the length( ) method. You can obtain the character at a specified index within a string by
calling charAt( ). The general forms of these three methods are shown here:
boolean equals(secondStr)
int length( )
char charAt(index)
Here is a program that demonstrates these methods:
// Demonstrating some String methods.
class StringDemo2 {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
System.out.println("Length of strOb1: " +
strOb1.length());
System.out.println("Char at index 3 in strOb1: " +
strOb1.charAt(3));
if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}
}

This program generates the following output:
Length of strOb1: 12
Char at index 3 in strOb1: s
strOb1 != strOb2
strOb1 == strOb3

Part I

}

154

PART I

The Java Language

Of course, you can have arrays of strings, just like you can have arrays of any other type
of object. For example:
// Demonstrate String arrays.
class StringDemo3 {
public static void main(String args[]) {
String str[] = { "one", "two", "three" };
for(int i=0; i
ml = new LinkedList
(); // Add elements to the linked list. ml.add(new Address("J.W. West", "11 Oak Ave", "Urbana", "IL", "61801")); ml.add(new Address("Ralph Baker", "1142 Maple Lane", "Mahomet", "IL", "61853")); ml.add(new Address("Tom Carlton", "867 Elm St", "Champaign", "IL", "61820")); // Display the mailing list. for(Address element : ml) System.out.println(element + "\n"); System.out.println(); } } The output from the program is shown here: J.W. West 11 Oak Ave Urbana IL 61801 Ralph Baker 1142 Maple Lane Mahomet IL 61853 Tom Carlton 867 Elm St Champaign IL 61820 Aside from storing a user-defined class in a collection, another important thing to notice about the preceding program is that it is quite short. When you consider that it sets up a linked list that can store, retrieve, and process mailing addresses in about 50 lines of code, the power of the Collections Framework begins to become apparent. As most readers know, if all of this functionality had to be coded manually, the program would be several times longer. Collections offer off-the-shelf solutions to a wide variety of programming problems. You should use them whenever the situation presents itself. Part II } 482 PART II The Java Library The RandomAccess Interface The RandomAccess interface contains no members. However, by implementing this interface, a collection signals that it supports efficient random access to its elements. Although a collection might support random access, it might not do so efficiently. By checking for the RandomAccess interface, client code can determine at run time whether a collection is suitable for certain types of random access operations—especially as they apply to large collections. (You can use instanceof to determine if a class implements an interface.) RandomAccess is implemented by ArrayList and by the legacy Vector class, among others. Working with Maps A map is an object that stores associations between keys and values, or key/value pairs. Given a key, you can find its value. Both keys and values are objects. The keys must be unique, but the values may be duplicated. Some maps can accept a null key and null values, others cannot. There is one key point about maps that is important to mention at the outset: they don’t implement the Iterable interface. This means that you cannot cycle through a map using a for-each style for loop. Furthermore, you can’t obtain an iterator to a map. However, as you will soon see, you can obtain a collection-view of a map, which does allow the use of either the for loop or an iterator. The Map Interfaces Because the map interfaces define the character and nature of maps, this discussion of maps begins with them. The following interfaces support maps: Interface Description Map Maps unique keys to values. Map.Entry Describes an element (a key/value pair) in a map. This is an inner class of Map. NavigableMap Extends SortedMap to handle the retrieval of entries based on closest-match searches. SortedMap Extends Map so that the keys are maintained in ascending order. Each interface is examined next, in turn. The Map Interface The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. Map is generic and is declared as shown here: interface Map Here, K specifies the type of keys, and V specifies the type of values. java.util Part 1: The Collections Framework 483 The methods declared by Map are summarized in Table 17-10. Several methods throw a ClassCastException when an object is incompatible with the elements in a map. A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the map. An UnsupportedOperationException is thrown when an attempt is made to change an unmodifiable map. An IllegalArgumentException is thrown if an invalid argument is used. Maps revolve around two basic operations: get( ) and put( ). To put a value into a map, use put( ), specifying the key and the value. To obtain a value, call get( ), passing the key as an argument. The value is returned. As mentioned earlier, although part of the Collections Framework, maps are not, themselves, collections because they do not implement the Collection interface. However, you can obtain a collection-view of a map. To do this, you can use the entrySet( ) method. It Method Description void clear( ) Removes all key/value pairs from the invoking map. boolean containsKey(Object k) Returns true if the invoking map contains k as a key. Otherwise, returns false. boolean containsValue(Object v) Returns true if the map contains v as a value. Otherwise, returns false. Set> entrySet( ) Returns a Set that contains the entries in the map. The set contains objects of type Map.Entry. Thus, this method provides a set-view of the invoking map. boolean equals(Object obj) Returns true if obj is a Map and contains the same entries. Otherwise, returns false. V get(Object k) Returns the value associated with the key k. Returns null if the key is not found. int hashCode( ) Returns the hash code for the invoking map. boolean isEmpty( ) Returns true if the invoking map is empty. Otherwise, returns false. Set keySet( ) Returns a Set that contains the keys in the invoking map. This method provides a set-view of the keys in the invoking map. V put(K k, V v) Puts an entry in the invoking map, overwriting any previous value associated with the key. The key and value are k and v, respectively. Returns null if the key did not already exist. Otherwise, the previous value linked to the key is returned. void putAll(Map m) Puts all the entries from m into this map. V remove(Object k) Removes the entry whose key equals k. int size( ) Returns the number of key/value pairs in the map. Collection values( ) Returns a collection containing the values in the map. This method provides a collection-view of the values in the map. Table 17-10 The Methods Defined by Map Part II Chapter 17 484 PART II The Java Library returns a Set that contains the elements in the map. To obtain a collection-view of the keys, use keySet( ). To get a collection-view of the values, use values( ). For all three collectionviews, the collection is backed by the map. Changing one affects the other. Collection-views are the means by which maps are integrated into the larger Collections Framework. The SortedMap Interface The SortedMap interface extends Map. It ensures that the entries are maintained in ascending order based on the keys. SortedMap is generic and is declared as shown here: interface SortedMap Here, K specifies the type of keys, and V specifies the type of values. The methods declared by SortedMap are summarized in Table 17-11. Several methods throw a NoSuchElementException when no items are in the invoking map. A ClassCastException is thrown when an object is incompatible with the elements in a map. A NullPointerException is thrown if an attempt is made to use a null object when null is not allowed in the map. An IllegalArgumentException is thrown if an invalid argument is used. Sorted maps allow very efficient manipulations of submaps (in other words, subsets of a map). To obtain a submap, use headMap( ), tailMap( ), or subMap( ). The submap returned by these methods is backed by the invoking map. Changing one changes the other. To get the first key in the set, call firstKey( ). To get the last key, use lastKey( ). The NavigableMap Interface The NavigableMap interface extends SortedMap and declares the behavior of a map that supports the retrieval of entries based on the closest match to a given key or keys. NavigableMap is a generic interface that has this declaration: interface NavigableMap Here, K specifies the type of the keys, and V specifies the type of the values associated with the keys. In addition to the methods that it inherits from SortedMap, NavigableMap adds Method Description Comparator comparator( ) Returns the invoking sorted map’s comparator. If natural ordering is used for the invoking map, null is returned. K firstKey( ) Returns the first key in the invoking map. SortedMap headMap(K end) Returns a sorted map for those map entries with keys that are less than end. K lastKey( ) Returns the last key in the invoking map. SortedMap subMap(K start, K end) Returns a map containing those entries with keys that are greater than or equal to start and less than end. SortedMap tailMap(K start) Returns a map containing those entries with keys that are greater than or equal to start. Table 17-11 The Methods Defined by SortedMap Chapter 17 java.util Part 1: The Collections Framework 485 Method Description Map.Entry ceilingEntry(K obj) Searches the map for the smallest key k such that k >= obj. If such a key is found, its entry is returned. Otherwise, null is returned. K ceilingKey(K obj) Searches the map for the smallest key k such that k >= obj. If such a key is found, it is returned. Otherwise, null is returned. NavigableSet descendingKeySet( ) Returns a NavigableSet that contains the keys in the invoking map in reverse order. Thus, it returns a reverse set-view of the keys. The resulting set is backed by the map. NavigableMap descendingMap( ) Returns a NavigableMap that is the reverse of the invoking map. The resulting map is backed by the invoking map. Map.Entry firstEntry( ) Returns the first entry in the map. This is the entry with the least key. Map.Entry floorEntry(K obj) Searches the map for the largest key k such that k <= obj. If such a key is found, its entry is returned. Otherwise, null is returned. K floorKey(K obj) Searches the map for the largest key k such that k <= obj. If such a key is found, it is returned. Otherwise, null is returned. NavigableMap headMap(K upperBound, boolean incl) Returns a NavigableMap that includes all entries from the invoking map that have keys that are less than upperBound. If incl is true, then an element equal to upperBound is included. The resulting map is backed by the invoking map. Map.Entry higherEntry(K obj) Searches the set for the largest key k such that k > obj. If such a key is found, its entry is returned. Otherwise, null is returned. K higherKey(K obj) Searches the set for the largest key k such that k > obj. If such a key is found, it is returned. Otherwise, null is returned. Map.Entry lastEntry( ) Returns the last entry in the map. This is the entry with the largest key. Map.Entry lowerEntry(K obj) Searches the set for the largest key k such that k < obj. If such a key is found, its entry is returned. Otherwise, null is returned. Table 17-12 The Methods defined by NavigableMap Part II those summarized in Table 17-12. Several methods throw a ClassCastException when an object is incompatible with the keys in the map. A NullPointerException is thrown if an attempt is made to use a null object and null keys are not allowed in the set. An IllegalArgumentException is thrown if an invalid argument is used. 486 PART II The Java Library Method Description K lowerKey(K obj) Searches the set for the largest key k such that k < obj. If such a key is found, it is returned. Otherwise, null is returned. NavigableSet navigableKeySet( ) Returns a NavigableSet that contains the keys in the invoking map. The resulting set is backed by the invoking map. Map.Entry pollFirstEntry( ) Returns the first entry, removing the entry in the process. Because the map is sorted, this is the entry with the least key value. null is returned if the map is empty. Map.Entry pollLastEntry( ) Returns the last entry, removing the entry in the process. Because the map is sorted, this is the entry with the greatest key value. null is returned if the map is empty. NavigableMap subMap(K lowerBound, boolean lowIncl, K upperBound boolean highIncl) Returns a NavigableMap that includes all entries from the invoking map that have keys that are greater than lowerBound and less than upperBound. If lowIncl is true, then an element equal to lowerBound is included. If highIncl is true, then an element equal to highIncl is included. The resulting map is backed by the invoking map. NavigableMap tailMap(K lowerBound, boolean incl) Returns a NavigableMap that includes all entries from the invoking map that have keys that are greater than lowerBound. If incl is true, then an element equal to lowerBound is included. The resulting map is backed by the invoking map. Table 17-12 The Methods defined by NavigableMap (continued) The Map.Entry Interface The Map.Entry interface enables you to work with a map entry. Recall that the entrySet( ) method declared by the Map interface returns a Set containing the map entries. Each of these set elements is a Map.Entry object. Map.Entry is generic and is declared like this: interface Map.Entry Here, K specifies the type of keys, and V specifies the type of values. Table 17-13 summarizes the methods declared by Map.Entry. Various exceptions are possible. java.util Part 1: The Collections Framework Method Description boolean equals(Object obj) Returns true if obj is a Map.Entry whose key and value are equal to that of the invoking object. K getKey( ) Returns the key for this map entry. V getValue( ) Returns the value for this map entry. int hashCode( ) Returns the hash code for this map entry. V setValue(V v) Sets the value for this map entry to v. A ClassCastException is thrown if v is not the correct type for the map. An IllegalArgumentException is thrown if there is a problem with v. A NullPointerException is thrown if v is null and the map does not permit null keys. An UnsupportedOperationException is thrown if the map cannot be changed. 487 Table 17-13 The Methods Defined by Map.Entry The Map Classes Several classes provide implementations of the map interfaces. The classes that can be used for maps are summarized here: Class Description AbstractMap Implements most of the Map interface. EnumMap Extends AbstractMap for use with enum keys. HashMap Extends AbstractMap to use a hash table. TreeMap Extends AbstractMap to use a tree. WeakHashMap Extends AbstractMap to use a hash table with weak keys. LinkedHashMap Extends HashMap to allow insertion-order iterations. IdentityHashMap Extends AbstractMap and uses reference equality when comparing documents. Notice that AbstractMap is a superclass for all concrete map implementations. WeakHashMap implements a map that uses “weak keys,” which allows an element in a map to be garbage-collected when its key is otherwise unused. This class is not discussed further here. The other map classes are described next. Part II Chapter 17 488 PART II The Java Library The HashMap Class The HashMap class extends AbstractMap and implements the Map interface. It uses a hash table to store the map. This allows the execution time of get( ) and put( ) to remain constant even for large sets. HashMap is a generic class that has this declaration: class HashMap Here, K specifies the type of keys, and V specifies the type of values. The following constructors are defined: HashMap( ) HashMap(Map m) HashMap(int capacity) HashMap(int capacity, float fillRatio) The first form constructs a default hash map. The second form initializes the hash map by using the elements of m. The third form initializes the capacity of the hash map to capacity. The fourth form initializes both the capacity and fill ratio of the hash map by using its arguments. The meaning of capacity and fill ratio is the same as for HashSet, described earlier. The default capacity is 16. The default fill ratio is 0.75. HashMap implements Map and extends AbstractMap. It does not add any methods of its own. You should note that a hash map does not guarantee the order of its elements. Therefore, the order in which elements are added to a hash map is not necessarily the order in which they are read by an iterator. The following program illustrates HashMap. It maps names to account balances. Notice how a set-view is obtained and used. import java.util.*; class HashMapDemo { public static void main(String args[]) { // Create a hash map. HashMap hm = new HashMap(); // Put elements to the map hm.put("John Doe", new Double(3434.34)); hm.put("Tom Smith", new Double(123.22)); hm.put("Jane Baker", new Double(1378.00)); hm.put("Tod Hall", new Double(99.22)); hm.put("Ralph Smith", new Double(-19.08)); // Get a set of the entries. Set> set = hm.entrySet(); // Display the set. for(Map.Entry me : set) { System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); Chapter 17 java.util Part 1: The Collections Framework 489 // Deposit 1000 into John Doe's account. double balance = hm.get("John Doe"); hm.put("John Doe", balance + 1000); System.out.println("John Doe's new balance: " + hm.get("John Doe")); } } Ralph Smith: -19.08 Tom Smith: 123.22 John Doe: 3434.34 Tod Hall: 99.22 Jane Baker: 1378.0 John Doe's new balance: 4434.34 The program begins by creating a hash map and then adds the mapping of names to balances. Next, the contents of the map are displayed by using a set-view, obtained by calling entrySet( ). The keys and values are displayed by calling the getKey( ) and getValue( ) methods that are defined by Map.Entry. Pay close attention to how the deposit is made into John Doe’s account. The put( ) method automatically replaces any preexisting value that is associated with the specified key with the new value. Thus, after John Doe’s account is updated, the hash map will still contain just one "John Doe" account. The TreeMap Class The TreeMap class extends AbstractMap and implements the NavigableMap interface. It creates maps stored in a tree structure. A TreeMap provides an efficient means of storing key/value pairs in sorted order and allows rapid retrieval. You should note that, unlike a hash map, a tree map guarantees that its elements will be sorted in ascending key order. TreeMap is a generic class that has this declaration: class TreeMap Here, K specifies the type of keys, and V specifies the type of values. The following TreeMap constructors are defined: TreeMap( ) TreeMap(Comparator comp) TreeMap(Map m) TreeMap(SortedMap sm) The first form constructs an empty tree map that will be sorted by using the natural order of its keys. The second form constructs an empty tree-based map that will be sorted by using the Comparator comp. (Comparators are discussed later in this chapter.) The third form initializes a tree map with the entries from m, which will be sorted by using the natural order of the keys. The fourth form initializes a tree map with the entries from sm, which will be sorted in the same order as sm. TreeMap has no map methods beyond those specified by the NavigableMap interface and the AbstractMap class. Part II Output from this program is shown here (the precise order may vary): 490 PART II The Java Library The following program reworks the preceding example so that it uses TreeMap: import java.util.*; class TreeMapDemo { public static void main(String args[]) { // Create a tree map. TreeMap tm = new TreeMap(); // Put elements to the map. tm.put("John Doe", new Double(3434.34)); tm.put("Tom Smith", new Double(123.22)); tm.put("Jane Baker", new Double(1378.00)); tm.put("Tod Hall", new Double(99.22)); tm.put("Ralph Smith", new Double(-19.08)); // Get a set of the entries. Set> set = tm.entrySet(); // Display the elements. for(Map.Entry me : set) { System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); // Deposit 1000 into John Doe's account. double balance = tm.get("John Doe"); tm.put("John Doe", balance + 1000); System.out.println("John Doe's new balance: " + tm.get("John Doe")); } } The following is the output from this program: Jane Baker: 1378.0 John Doe: 3434.34 Ralph Smith: -19.08 Todd Hall: 99.22 Tom Smith: 123.22 John Doe's current balance: 4434.34 Notice that TreeMap sorts the keys. However, in this case, they are sorted by first name instead of last name. You can alter this behavior by specifying a comparator when the map is created, as described shortly. The LinkedHashMap Class LinkedHashMap extends HashMap. It maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. Chapter 17 java.util Part 1: The Collections Framework 491 That is, when iterating through a collection-view of a LinkedHashMap, the elements will be returned in the order in which they were inserted. You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed. LinkedHashMap is a generic class that has this declaration: class LinkedHashMap LinkedHashMap( ) LinkedHashMap(Map m) LinkedHashMap(int capacity) LinkedHashMap(int capacity, float fillRatio) LinkedHashMap(int capacity, float fillRatio, boolean Order) The first form constructs a default LinkedHashMap. The second form initializes the LinkedHashMap with the elements from m. The third form initializes the capacity. The fourth form initializes both capacity and fill ratio. The meaning of capacity and fill ratio are the same as for HashMap. The default capacity is 16. The default ratio is 0.75. The last form allows you to specify whether the elements will be stored in the linked list by insertion order, or by order of last access. If Order is true, then access order is used. If Order is false, then insertion order is used. LinkedHashMap adds only one method to those defined by HashMap. This method is removeEldestEntry( ), and it is shown here: protected boolean removeEldestEntry(Map.Entry e) This method is called by put( ) and putAll( ). The oldest entry is passed in e. By default, this method returns false and does nothing. However, if you override this method, then you can have the LinkedHashMap remove the oldest entry in the map. To do this, have your override return true. To keep the oldest entry, return false. The IdentityHashMap Class IdentityHashMap extends AbstractMap and implements the Map interface. It is similar to HashMap except that it uses reference equality when comparing elements. IdentityHashMap is a generic class that has this declaration: class IdentityHashMap Here, K specifies the type of key, and V specifies the type of value. The API documentation explicitly states that IdentityHashMap is not for general use. The EnumMap Class EnumMap extends AbstractMap and implements Map. It is specifically for use with keys of an enum type. It is a generic class that has this declaration: class EnumMap, V> Here, K specifies the type of key, and V specifies the type of value. Notice that K must extend Enum, which enforces the requirement that the keys must be of an enum type. Part II Here, K specifies the type of keys, and V specifies the type of values. LinkedHashMap defines the following constructors: 492 PART II The Java Library EnumMap defines the following constructors: EnumMap(Class kType) EnumMap(Map m) EnumMap(EnumMap em) The first constructor creates an empty EnumMap of type kType. The second creates an EnumMap map that contains the same entries as m. The third creates an EnumMap initialized with the values in em. EnumMap defines no methods of its own. Comparators Both TreeSet and TreeMap store elements in sorted order. However, it is the comparator that defines precisely what “sorted order” means. By default, these classes store their elements by using what Java refers to as “natural ordering,” which is usually the ordering that you would expect (A before B, 1 before 2, and so forth). If you want to order elements a different way, then specify a Comparator when you construct the set or map. Doing so gives you the ability to govern precisely how elements are stored within sorted collections and maps. Comparator is a generic interface that has this declaration: interface Comparator Here, T specifies the type of objects being compared. The Comparator interface defines two methods: compare( ) and equals( ). The compare( ) method, shown here, compares two elements for order: int compare(T obj1, T obj2) obj1 and obj2 are the objects to be compared. Normally, this method returns zero if the objects are equal. It returns a positive value if obj1 is greater than obj2. Otherwise, a negative value is returned. The method can throw a ClassCastException if the types of the objects are not compatible for comparison. By implementing compare( ), you can alter the way that objects are ordered. For example, to sort in reverse order, you can create a comparator that reverses the outcome of a comparison. The equals( ) method, shown here, tests whether an object equals the invoking comparator: boolean equals(object obj) Here, obj is the object to be tested for equality. The method returns true if obj and the invoking object are both Comparator objects and use the same ordering. Otherwise, it returns false. Overriding equals( ) is not necessary, and most simple comparators will not do so. Using a Comparator The following is an example that demonstrates the power of a custom comparator. It implements the compare( ) method for strings that operates in reverse of normal. Thus, it causes a tree set to be sorted in reverse order. Chapter 17 java.util Part 1: The Collections Framework 493 // Use a custom comparator. import java.util.*; // A reverse comparator for strings. class MyComp implements Comparator { public int compare(String a, String b) { String aStr, bStr; // Reverse the comparison. return bStr.compareTo(aStr); } // No need to override equals. } class CompDemo { public static void main(String args[]) { // Create a tree set. TreeSet ts = new TreeSet(new MyComp()); // Add elements to the tree set. ts.add("C"); ts.add("A"); ts.add("B"); ts.add("E"); ts.add("F"); ts.add("D"); // Display the elements. for(String element : ts) System.out.print(element + " "); System.out.println(); } } As the following output shows, the tree is now sorted in reverse order: F E D C B A Look closely at the MyComp class, which implements Comparator by implementing compare( ). (As explained earlier, overriding equals( ) is neither necessary nor common.) Inside compare( ), the String method compareTo( ) compares the two strings. However, bStr—not aStr—invokes compareTo( ). This causes the outcome of the comparison to be reversed. For a more practical example, the following program is an updated version of the TreeMap program shown earlier that stores account balances. In the previous version, the accounts were sorted by name, but the sorting began with the first name. The following Part II aStr = a; bStr = b; 494 PART II The Java Library program sorts the accounts by last name. To do so, it uses a comparator that compares the last name of each account. This results in the map being sorted by last name. // Use a comparator to sort accounts by last name. import java.util.*; // Compare last whole words in two strings. class TComp implements Comparator { public int compare(String a, String b) { int i, j, k; String aStr, bStr; aStr = a; bStr = b; // Find index of beginning of last name. i = aStr.lastIndexOf(' '); j = bStr.lastIndexOf(' '); k = aStr.substring(i).compareTo(bStr.substring(j)); if(k==0) // last names match, check entire name return aStr.compareTo(bStr); else return k; } // No need to override equals. } class TreeMapDemo2 { public static void main(String args[]) { // Create a tree map. TreeMap tm = new TreeMap(new TComp()); // Put elements to the map. tm.put("John Doe", new Double(3434.34)); tm.put("Tom Smith", new Double(123.22)); tm.put("Jane Baker", new Double(1378.00)); tm.put("Tod Hall", new Double(99.22)); tm.put("Ralph Smith", new Double(-19.08)); // Get a set of the entries. Set> set = tm.entrySet(); // Display the elements. for(Map.Entry me : set) { System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); Chapter 17 java.util Part 1: The Collections Framework 495 // Deposit 1000 into John Doe's account. double balance = tm.get("John Doe"); tm.put("John Doe", balance + 1000); System.out.println("John Doe's new balance: " + tm.get("John Doe")); } } Here is the output; notice that the accounts are now sorted by last name: Part II Jane Baker: 1378.0 John Doe: 3434.34 Todd Hall: 99.22 Ralph Smith: -19.08 Tom Smith: 123.22 John Doe's new balance: 4434.34 The comparator class TComp compares two strings that hold first and last names. It does so by first comparing last names. To do this, it finds the index of the last space in each string and then compares the substrings of each element that begin at that point. In cases where last names are equivalent, the first names are then compared. This yields a tree map that is sorted by last name, and within last name by first name. You can see this because Ralph Smith comes before Tom Smith in the output. The Collection Algorithms The Collections Framework defines several algorithms that can be applied to collections and maps. These algorithms are defined as static methods within the Collections class. They are summarized in Table 17-14. As explained earlier, beginning with JDK 5 all of the algorithms were retrofitted for generics. Method Description static boolean addAll(Collection c, T... elements) Inserts the elements specified by elements into the collection specified by c. Returns true if the elements were added and false otherwise. static Queue asLifoQueue(Deque c) Returns a last-in, first-out view of c. static int binarySearch(List list, T value, Comparator c) Searches for value in list ordered according to c. Returns the position of value in list, or a negative value if value is not found. Table 17-14 The Algorithms Defined by Collections 496 PART II The Java Library Method Description static int binarySearch(List> list, T value) Searches for value in list. The list must be sorted. Returns the position of value in list, or a negative value if value is not found. static Collection checkedCollection(Collection c, Class t) Returns a run-time type-safe view of a collection. An attempt to insert an incompatible element will cause a ClassCastException. static List checkedList(List c, Class t) Returns a run-time type-safe view of a List. An attempt to insert an incompatible element will cause a ClassCastException. static Map checkedMap(Map c, Class keyT, Class valueT) Returns a run-time type-safe view of a Map. An attempt to insert an incompatible element will cause a ClassCastException. static List checkedSet(Set c, Class t) Returns a run-time type-safe view of a Set. An attempt to insert an incompatible element will cause a ClassCastException. static SortedMap checkedSortedMap(SortedMap c, Class keyT, Class valueT) Returns a run-time type-safe view of a SortedMap. An attempt to insert an incompatible element will cause a ClassCastException. static SortedSet checkedSortedSet(SortedSet c, Class t) Returns a run-time type-safe view of a SortedSet. An attempt to insert an incompatible element will cause a ClassCastException. static void copy(List list1, List list2) Copies the elements of list2 to list1. static boolean disjoint(Collection a, Collection b) Compares the elements in a to elements in b. Returns true if the two collections contain no common elements (i.e., the collections contain disjoint sets of elements). Otherwise, returns false. static Enumeration emptyEnumeration( ) Returns an empty enumeration, which is an enumeration with no elements. (Added by JDK 7.) static Iterator emptyIterator( ) Returns an empty iterator, which is an iterator with no elements. (Added by JDK 7.) Table 17-14 The Algorithms Defined by Collections (continued) java.util Part 1: The Collections Framework Method Description static List emptyList( ) Returns an immutable, empty List object of the inferred type. static ListIterator emptyListIterator( ) Returns an empty list iterator, which is a list iterator that has no elements. (Added by JDK 7.) static Map emptyMap( ) Returns an immutable, empty Map object of the inferred type. static Set emptySet( ) Returns an immutable, empty Set object of the inferred type. static Enumeration enumeration(Collection c) Returns an enumeration over c. (See “The Enumeration Interface,” later in this chapter.) static void fill(List list, T obj) Assigns obj to each element of list. static int frequency(Collection c, object obj) Counts the number of occurrences of obj in c and returns the result. static int indexOfSubList(List list, List subList) Searches list for the first occurrence of subList. Returns the index of the first match, or –1 if no match is found. static int lastIndexOfSubList(List list, List subList) Searches list for the last occurrence of subList. Returns the index of the last match, or –1 if no match is found. static ArrayList list(Enumeration enum) Returns an ArrayList that contains the elements of enum. static T max(Collection c, Comparator comp) Returns the maximum element in c as determined by comp. static > T max(Collection c) Returns the maximum element in c as determined by natural ordering. The collection need not be sorted. static T min(Collection c, Comparator comp) Returns the minimum element in c as determined by comp. The collection need not be sorted. static > T min(Collection c) Returns the minimum element in c as determined by natural ordering. static List nCopies(int num, T obj) Returns num copies of obj contained in an immutable list. num must be greater than or equal to zero. static Set newSetFromMap(Map m) Creates and returns a set backed by the map specified by m, which must be empty at the time this method is called. Table 17-14 The Algorithms Defined by Collections (continued) 497 Part II Chapter 17 498 PART II The Java Library Method Description static boolean replaceAll(List list, T old, T new) Replaces all occurrences of old with new in list. Returns true if at least one replacement occurred. Returns false, otherwise. static void reverse(List list) Reverses the sequence in list. static Comparator reverseOrder(Comparator comp) Returns a reverse comparator based on the one passed in comp. That is, the returned comparator reverses the outcome of a comparison that uses comp. static Comparator reverseOrder( ) Returns a reverse comparator, which is a comparator that reverses the outcome of a comparison between two elements. static void rotate(List list, int n) Rotates list by n places to the right. To rotate left, use a negative value for n. static void shuffle(List list, Random r) Shuffles (i.e., randomizes) the elements in list by using r as a source of random numbers. static void shuffle(List list) Shuffles (i.e., randomizes) the elements in list. static Set singleton(T obj) Returns obj as an immutable set. This is an easy way to convert a single object into a set. static List singletonList(T obj) Returns obj as an immutable list. This is an easy way to convert a single object into a list. static Map singletonMap(K k, V v) Returns the key/value pair k/v as an immutable map. This is an easy way to convert a single key/value pair into a map. static void sort(List list, Comparator comp) Sorts the elements of list as determined by comp. static > void sort(List list) Sorts the elements of list as determined by their natural ordering. static void swap(List list, int idx1, int idx2) Exchanges the elements in list at the indices specified by idx1 and idx2. static Collection synchronizedCollection(Collection c) Returns a thread-safe collection backed by c. static List synchronizedList(List list) Returns a thread-safe list backed by list. static Map synchronizedMap(Map m) Returns a thread-safe map backed by m. static Set synchronizedSet(Set s) Returns a thread-safe set backed by s. Table 17-14 The Algorithms Defined by Collections (continued) java.util Part 1: The Collections Framework Method Description static SortedMap synchronizedSortedMap(SortedMap sm) Returns a thread-safe sorted map backed by sm. static SortedSet synchronizedSortedSet(SortedSet ss) Returns a thread-safe sorted set backed by ss. static Collection unmodifiableCollection( Collection c) Returns an unmodifiable collection backed by c. static List unmodifiableList(List list) Returns an unmodifiable list backed by list. static Map unmodifiableMap(Map m) Returns an unmodifiable map backed by m. static Set unmodifiableSet(Set s) Returns an unmodifiable set backed by s. static SortedMap unmodifiableSortedMap(SortedMap sm) Returns an unmodifiable sorted map backed by sm. static SortedSet unmodifiableSortedSet(SortedSet ss) Returns an unmodifiable sorted set backed by ss. 499 Table 17-14 The Algorithms Defined by Collections (continued) Several of the methods can throw a ClassCastException, which occurs when an attempt is made to compare incompatible types, or an UnsupportedOperationException, which occurs when an attempt is made to modify an unmodifiable collection. Other exceptions are possible, depending on the method. One thing to pay special attention to is the set of checked methods, such as checkedCollection( ), which returns what the API documentation refers to as a “dynamically typesafe view” of a collection. This view is a reference to the collection that monitors insertions into the collection for type compatibility at run time. An attempt to insert an incompatible element will cause a ClassCastException. Using such a view is especially helpful during debugging because it ensures that the collection always contains valid elements. Related methods include checkedSet( ), checkedList( ), checkedMap( ), and so on. They obtain a type-safe view for the indicated collection. Notice that several methods, such as synchronizedList( ) and synchronizedSet( ), are used to obtain synchronized (thread-safe) copies of the various collections. As a general rule, the standard collections implementations are not synchronized. You must use the synchronization algorithms to provide synchronization. One other point: iterators to synchronized collections must be used within synchronized blocks. The set of methods that begins with unmodifiable returns views of the various collections that cannot be modified. These will be useful when you want to grant some process read—but not write—capabilities on a collection. Collections defines three static variables: EMPTY_SET, EMPTY_LIST, and EMPTY_MAP. All are immutable. Part II Chapter 17 500 PART II The Java Library The following program demonstrates some of the algorithms. It creates and initializes a linked list. The reverseOrder( ) method returns a Comparator that reverses the comparison of Integer objects. The list elements are sorted according to this comparator and then are displayed. Next, the list is randomized by calling shuffle( ), and then its minimum and maximum values are displayed. // Demonstrate various algorithms. import java.util.*; class AlgorithmsDemo { public static void main(String args[]) { // Create and initialize linked list. LinkedList ll = new LinkedList(); ll.add(-8); ll.add(20); ll.add(-20); ll.add(8); // Create a reverse order comparator. Comparator r = Collections.reverseOrder(); // Sort list by using the comparator. Collections.sort(ll, r); System.out.print("List sorted in reverse: "); for(int i : ll) System.out.print(i+ " "); System.out.println(); // Shuffle list. Collections.shuffle(ll); // Display randomized list. System.out.print("List shuffled: "); for(int i : ll) System.out.print(i + " "); System.out.println(); System.out.println("Minimum: " + Collections.min(ll)); System.out.println("Maximum: " + Collections.max(ll)); } } Output from this program is shown here: List sorted in reverse: 20 8 -8 -20 List shuffled: 20 -20 8 -8 Minimum: -20 Maximum: 20 Notice that min( ) and max( ) operate on the list after it has been shuffled. Neither requires a sorted list for its operation. Chapter 17 java.util Part 1: The Collections Framework 501 Arrays The Arrays class provides various methods that are useful when working with arrays. These methods help bridge the gap between collections and arrays. Each method defined by Arrays is examined in this section. The asList( ) method returns a List that is backed by a specified array. In other words, both the list and the array refer to the same location. It has the following signature: Here, array is the array that contains the data. The binarySearch( ) method uses a binary search to find a specified value. This method must be applied to sorted arrays. Here are some of its forms. (Additional forms let you search a subrange): static int binarySearch(byte array[ ], byte value) static int binarySearch(char array[ ], char value) static int binarySearch(double array[ ], double value) static int binarySearch(float array[ ], float value) static int binarySearch(int array[ ], int value) static int binarySearch(long array[ ], long value) static int binarySearch(short array[ ], short value) static int binarySearch(Object array[ ], Object value) static int binarySearch(T[ ] array, T value, Comparator c) Here, array is the array to be searched, and value is the value to be located. The last two forms throw a ClassCastException if array contains elements that cannot be compared (for example, Double and StringBuffer) or if value is not compatible with the types in array. In the last form, the Comparator c is used to determine the order of the elements in array. In all cases, if value exists in array, the index of the element is returned. Otherwise, a negative value is returned. The copyOf( ) method returns a copy of an array and has the following forms: static boolean[ ] copyOf(boolean[ ] source, int len) static byte[ ] copyOf(byte[ ] source, int len) static char[ ] copyOf(char[ ] source, int len) static double[ ] copyOf(double[ ] source, int len) static float[ ] copyOf(float[ ] source, int len) static int[ ] copyOf(int[ ] source, int len) static long[ ] copyOf(long[ ] source, int len) static short[ ] copyOf(short[ ] source, int len) static T[ ] copyOf(T[ ] source, int len) static T[ ] copyOf(U[ ] source, int len, Class resultT) The original array is specified by source, and the length of the copy is specified by len. If the copy is longer than source, then the copy is padded with zeros (for numeric arrays), nulls (for object arrays), or false (for boolean arrays). If the copy is shorter than source, then the copy is truncated. In the last form, the type of resultT becomes the type of the array returned. If len is negative, a NegativeArraySizeException is thrown. If source is null, a NullPointerException is thrown. If resultT is incompatible with the type of source, an ArrayStoreException is thrown. Part II static List asList(T... array) 502 PART II The Java Library The copyOfRange( ) method returns a copy of a range within an array and has the following forms: static boolean[ ] copyOfRange(boolean[ ] source, int start, int end) static byte[ ] copyOfRange(byte[ ] source, int start, int end) static char[ ] copyOfRange(char[ ] source, int start, int end) static double[ ] copyOfRange(double[ ] source, int start, int end) static float[ ] copyOfRange(float[ ] source, int start, int end) static int[ ] copyOfRange(int[ ] source, int start, int end) static long[ ] copyOfRange(long[ ] source, int start, int end) static short[ ] copyOfRange(short[ ] source, int start, int end) static T[ ] copyOfRange(T[ ] source, int start, int end) static T[ ] copyOfRange(U[ ] source, int start, int end, Class resultT) The original array is specified by source. The range to copy is specified by the indices passed via start and end. The range runs from start to end – 1. If the range is longer than source, then the copy is padded with zeros (for numeric arrays), nulls (for object arrays), or false (for boolean arrays). In the last form, the type of resultT becomes the type of the array returned. If start is negative or greater than the length of source, an ArrayIndexOutOfBoundsException is thrown. If start is greater than end, an IllegalArgumentException is thrown. If source is null, a NullPointerException is thrown. If resultT is incompatible with the type of source, an ArrayStoreException is thrown. The equals( ) method returns true if two arrays are equivalent. Otherwise, it returns false. The equals( ) method has the following forms: static boolean equals(boolean array1[ ], boolean array2 [ ]) static boolean equals(byte array1[ ], byte array2 [ ]) static boolean equals(char array1[ ], char array2 [ ]) static boolean equals(double array1[ ], double array2 [ ]) static boolean equals(float array1[ ], float array2 [ ]) static boolean equals(int array1[ ], int array2 [ ]) static boolean equals(long array1[ ], long array2 [ ]) static boolean equals(short array1[ ], short array2 [ ]) static boolean equals(Object array1[ ], Object array2 [ ]) Here, array1 and array2 are the two arrays that are compared for equality. The deepEquals( ) method can be used to determine if two arrays, which might contain nested arrays, are equal. It has this declaration: static boolean deepEquals(Object[ ] a, Object[ ] b) It returns true if the arrays passed in a and b contain the same elements. If a and b contain nested arrays, then the contents of those nested arrays are also checked. It returns false if the arrays, or any nested arrays, differ. Chapter 17 java.util Part 1: The Collections Framework 503 static void fill(boolean array[ ], boolean value) static void fill(byte array[ ], byte value) static void fill(char array[ ], char value) static void fill(double array[ ], double value) static void fill(float array[ ], float value) static void fill(int array[ ], int value) static void fill(long array[ ], long value) static void fill(short array[ ], short value) static void fill(Object array[ ], Object value) Here, value is assigned to all elements in array. The second version of the fill( ) method assigns a value to a subset of an array. Its forms are shown here: static void fill(boolean array[ ], int start, int end, boolean value) static void fill(byte array[ ], int start, int end, byte value) static void fill(char array[ ], int start, int end, char value) static void fill(double array[ ], int start, int end, double value) static void fill(float array[ ], int start, int end, float value) static void fill(int array[ ], int start, int end, int value) static void fill(long array[ ], int start, int end, long value) static void fill(short array[ ], int start, int end, short value) static void fill(Object array[ ], int start, int end, Object value) Here, value is assigned to the elements in array from position start to position end–1. These methods may all throw an IllegalArgumentException if start is greater than end, or an ArrayIndexOutOfBoundsException if start or end is out of bounds. An ArrayStoreException is possible with the Object versions. The sort( ) method sorts an array so that it is arranged in ascending order. The sort( ) method has two versions. The first version, shown here, sorts the entire array: static void sort(byte array[ ]) static void sort(char array[ ]) static void sort(double array[ ]) static void sort(float array[ ]) static void sort(int array[ ]) static void sort(long array[ ]) static void sort(short array[ ]) static void sort(Object array[ ]) static void sort(T array[ ], Comparator c) Here, array is the array to be sorted. In the last form, c is a Comparator that is used to order the elements of array. The last two forms can throw a ClassCastException if elements of the array being sorted are not comparable. Part II The fill( ) method assigns a value to all elements in an array. In other words, it fills an array with a specified value. The fill( ) method has two versions. The first version, which has the following forms, fills an entire array: 504 PART II The Java Library The second version of sort( ) enables you to specify a range within an array that you want to sort. Its forms are shown here: static void sort(byte array[ ], int start, int end) static void sort(char array[ ], int start, int end) static void sort(double array[ ], int start, int end) static void sort(float array[ ], int start, int end) static void sort(int array[ ], int start, int end) static void sort(long array[ ], int start, int end) static void sort(short array[ ], int start, int end) static void sort(Object array[ ], int start, int end) static void sort(T array[ ], int start, int end, Comparator c) Here, the range beginning at start and running through end–1 within array will be sorted. In the last form, c is a Comparator that is used to order the elements of array. All of these methods can throw an IllegalArgumentException if start is greater than end, or an ArrayIndexOutOfBoundsException if start or end is out of bounds. The last two forms can also throw a ClassCastException if elements of the array being sorted are not comparable. Arrays also provides toString( ) and hashCode( ) for the various types of arrays. In addition, deepToString( ) and deepHashCode( ) are provided, which operate effectively on arrays that contain nested arrays. The following program illustrates how to use some of the methods of the Arrays class: // Demonstrate Arrays import java.util.*; class ArraysDemo { public static void main(String args[]) { // Allocate and initialize array. int array[] = new int[10]; for(int i = 0; i < 10; i++) array[i] = -3 * i; // Display, sort, and display the array. System.out.print("Original contents: "); display(array); Arrays.sort(array); System.out.print("Sorted: "); display(array); // Fill and display the array. Arrays.fill(array, 2, 6, -1); System.out.print("After fill(): "); display(array); // Sort and display the array. Arrays.sort(array); System.out.print("After sorting again: "); display(array); // Binary search for -9. System.out.print("The value -9 is at location "); Chapter 17 java.util Part 1: The Collections Framework 505 int index = Arrays.binarySearch(array, -9); System.out.println(index); } static void display(int array[]) { for(int i: array) System.out.print(i + " "); } The following is the output from this program: Original contents: 0 -3 -6 -9 -12 -15 -18 -21 -24 -27 Sorted: -27 -24 -21 -18 -15 -12 -9 -6 -3 0 After fill(): -27 -24 -1 -1 -1 -1 -9 -6 -3 0 After sorting again: -27 -24 -9 -6 -3 -1 -1 -1 -1 0 The value -9 is at location 2 Why Generic Collections? As mentioned at the start of this chapter, the entire Collections Framework was refitted for generics when JDK 5 was released. Furthermore, the Collections Framework is arguably the single most important use of generics in the Java API. The reason for this is that generics add type safety to the Collections Framework. Before moving on, it is worth taking some time to examine in detail the significance of this improvement. It will also show why older pre-generics collection-based code should be updated. Let’s begin with an example that uses pre-generics code. The following program stores a list of strings in an ArrayList and then displays the contents of the list: // Pre-generics example that uses a collection. import java.util.*; class OldStyle { public static void main(String args[]) { ArrayList list = new ArrayList(); // These lines store strings, but any type of object // can be stored. In old-style code, there is no // convenient way to restrict the type of objects stored // in a collection list.add("one"); list.add("two"); list.add("three"); list.add("four"); Iterator itr = list.iterator(); while(itr.hasNext()) { // To retrieve an element, an explicit type cast is needed // because the collection stores only Object. Part II System.out.println(); } 506 PART II The Java Library String str = (String) itr.next(); // explicit cast needed here. System.out.println(str + " is " + str.length() + " chars long."); } } } Prior to generics, all collections stored references of type Object. This allowed any type of reference to be stored in the collection. The preceding program uses this feature to store references to objects of type String in list, but any type of reference could have been stored. Unfortunately, the fact that a pre-generics collection stored Object references could easily lead to errors. First, it required that you, rather than the compiler, ensure that only objects of the proper type be stored in a specific collection. For example, in the preceding example, list is clearly intended to store Strings, but there is nothing that actually prevents another type of reference from being added to the collection. For example, the compiler will find nothing wrong with this line of code: list.add(new Integer(100)); Because list stores Object references, it can store a reference to Integer as well as it can store a reference to String. However, if you intended list to hold only strings, then the preceding statement would corrupt the collection. Again, the compiler had no way to know that the preceding statement is invalid. The second problem with pre-generics collections is that when you retrieve a reference from the collection, you must manually cast that reference into the proper type. This is why the preceding program casts the reference returned by next( ) into String. Prior to generics, collections simply stored Object references. Thus, the cast was necessary when retrieving objects from a collection. Aside from the inconvenience of always having to cast a retrieved reference into its proper type, this lack of type safety often led to a rather serious, but surprisingly easy-tocreate, error. Because Object can be cast into any type of object, it was possible to cast a reference obtained from a collection into the wrong type. For example, if the following statement were added to the preceding example, it would still compile without error, but generate a run-time exception when executed: Integer i = (Integer) itr.next(); Recall that the preceding example stored only references to instances of type String in list. Thus, when this statement attempts to cast a String into an Integer, an invalid cast exception results! Because this happens at run time, this is a very serious error. The addition of generics fundamentally improves the usability and safety of collections because it • Ensures that only references to objects of the proper type can actually be stored in a collection. Thus, a collection will always contain references of a known type. • Eliminates the need to cast a reference retrieved from a collection. Instead, a reference retrieved from a collection is automatically cast into the proper type. This prevents run-time errors due to invalid casts and avoids an entire category of errors. Chapter 17 java.util Part 1: The Collections Framework 507 These two improvements are made possible because each collection class has been given a type parameter that specifies the type of the collection. For example, ArrayList is now declared like this: class ArrayList Here, E is the type of element stored in the collection. Therefore, the following declares an ArrayList for objects of type String: Now, only references of type String can be added to list. The Iterator and ListIterator interfaces are now also generic. This means that the type parameter must agree with the type of the collection for which the iterator is obtained. Furthermore, this type compatibility is enforced at compile time. The following program shows the modern, generic form of the preceding program: // Modern, generics version. import java.util.*; class NewStyle { public static void main(String args[]) { // Now, list holds references of type String. ArrayList list = new ArrayList(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); // Notice that Iterator is also generic. Iterator itr = list.iterator(); // The following statement will now cause a compile-time error. Iterator itr = list.iterator(); // Error! // while(itr.hasNext()) { String str = itr.next(); // no cast needed // Now, the following line is a compile-time, // rather than run-time, error. Integer i = itr.next(); // this won't compile // System.out.println(str + " is " + str.length() + " chars long."); } } } Now, list can hold only references to objects of type String. Furthermore, as the following line shows, there is no need to cast the return value of next( ) into String: String str = itr.next(); // no cast needed The cast is performed automatically. Part II ArrayList list = new ArrayList(); 508 PART II The Java Library Because of support for raw types, older, pre-generics collection code will continue to compile and run. However, all new code should use generics, and you should update older code as soon as time permits. The addition of generics to the Collections Framework was a fundamental improvement that should be utilized wherever possible. The Legacy Classes and Interfaces As explained at the start of this chapter, early versions of java.util did not include the Collections Framework. Instead, it defined several classes and an interface that provided an ad hoc method of storing objects. When collections were added (by J2SE 1.2), several of the original classes were reengineered to support the collection interfaces. Thus, they are now technically part of the Collections Framework. However, where a modern collection duplicates the functionality of a legacy class, you will usually want to use the newer collection class. In general, the legacy classes are supported because there is still code that uses them. One other point: none of the modern collection classes described in this chapter are synchronized, but all the legacy classes are synchronized. This distinction may be important in some situations. Of course, you can easily synchronize collections by using one of the algorithms provided by Collections. The legacy classes defined by java.util are shown here: Dictionary Hashtable Properties Stack Vector There is one legacy interface called Enumeration. The following sections examine Enumeration and each of the legacy classes, in turn. The Enumeration Interface The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. This legacy interface has been superseded by Iterator. Although not deprecated, Enumeration is considered obsolete for new code. However, it is used by several methods defined by the legacy classes (such as Vector and Properties), is used by several other API classes, and is currently in widespread use in application code. Because it is still in use, it was retrofitted for generics by JDK 5. It has this declaration: interface Enumeration where E specifies the type of element being enumerated. Enumeration specifies the following two methods: boolean hasMoreElements( ) E nextElement( ) When implemented, hasMoreElements( ) must return true while there are still more elements to extract, and false when all the elements have been enumerated. nextElement( ) returns the next object in the enumeration. That is, each call to nextElement( ) obtains the Chapter 17 java.util Part 1: The Collections Framework 509 next object in the enumeration. It throws NoSuchElementException when the enumeration is complete. Vector implements a dynamic array. It is similar to ArrayList, but with two differences: Vector is synchronized, and it contains many legacy methods that duplicate the functionality of methods defined by the Collections Framework. With the advent of collections, Vector was reengineered to extend AbstractList and to implement the List interface. With the release of JDK 5, it was retrofitted for generics and reengineered to implement Iterable. This means that Vector is fully compatible with collections, and a Vector can have its contents iterated by the enhanced for loop. Vector is declared like this: class Vector Here, E specifies the type of element that will be stored. Here are the Vector constructors: Vector( ) Vector(int size) Vector(int size, int incr) Vector(Collection c) The first form creates a default vector, which has an initial size of 10. The second form creates a vector whose initial capacity is specified by size. The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward. The fourth form creates a vector that contains the elements of collection c. All vectors start with an initial capacity. After this initial capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for that object plus extra room for additional objects. By allocating more than just the required memory, the vector reduces the number of allocations that must take place as the vector grows. This reduction is important, because allocations are costly in terms of time. The amount of extra space allocated during each reallocation is determined by the increment that you specify when you create the vector. If you don’t specify an increment, the vector’s size is doubled by each allocation cycle. Vector defines these protected data members: int capacityIncrement; int elementCount; Object[ ] elementData; The increment value is stored in capacityIncrement. The number of elements currently in the vector is stored in elementCount. The array that holds the vector is stored in elementData. In addition to the collections methods defined by List, Vector defines several legacy methods, which are summarized in Table 17-15. Part II Vector 510 PART II The Java Library Method Description void addElement(E element) The object specified by element is added to the vector. int capacity( ) Returns the capacity of the vector. Object clone( ) Returns a duplicate of the invoking vector. boolean contains(Object element) Returns true if element is contained by the vector, and returns false if it is not. void copyInto(Object array[ ]) The elements contained in the invoking vector are copied into the array specified by array. E elementAt(int index) Returns the element at the location specified by index. Enumeration elements( ) Returns an enumeration of the elements in the vector. void ensureCapacity(int size) Sets the minimum capacity of the vector to size. E firstElement( ) Returns the first element in the vector. int indexOf(Object element) Returns the index of the first occurrence of element. If the object is not in the vector, –1 is returned. int indexOf(Object element, int start) Returns the index of the first occurrence of element at or after start. If the object is not in that portion of the vector, –1 is returned. void insertElementAt(E element, int index) Adds element to the vector at the location specified by index. boolean isEmpty( ) Returns true if the vector is empty, and returns false if it contains one or more elements. E lastElement( ) Returns the last element in the vector. int lastIndexOf(Object element) Returns the index of the last occurrence of element. If the object is not in the vector, –1 is returned. int lastIndexOf(Object element, int start) Returns the index of the last occurrence of element before start. If the object is not in that portion of the vector, –1 is returned. void removeAllElements( ) Empties the vector. After this method executes, the size of the vector is zero. boolean removeElement(Object element) Removes element from the vector. If more than one instance of the specified object exists in the vector, then it is the first one that is removed. Returns true if successful and false if the object is not found. void removeElementAt(int index) Removes the element at the location specified by index. void setElementAt(E element, int index) The location specified by index is assigned element. void setSize(int size) Sets the number of elements in the vector to size. If the new size is less than the old size, elements are lost. If the new size is larger than the old size, null elements are added. Table 17-15 The Legacy Methods Defined by Vector Chapter 17 java.util Part 1: The Collections Framework Method Description int size( ) Returns the number of elements currently in the vector. String toString( ) Returns the string equivalent of the vector. void trimToSize( ) Sets the vector’s capacity equal to the number of elements that it currently holds. 511 Because Vector implements List, you can use a vector just like you use an ArrayList instance. You can also manipulate one using its legacy methods. For example, after you instantiate a Vector, you can add an element to it by calling addElement( ). To obtain the element at a specific location, call elementAt( ). To obtain the first element in the vector, call firstElement( ). To retrieve the last element, call lastElement( ). You can obtain the index of an element by using indexOf( ) and lastIndexOf( ). To remove an element, call removeElement( ) or removeElementAt( ). The following program uses a vector to store various types of numeric objects. It demonstrates several of the legacy methods defined by Vector. It also demonstrates the Enumeration interface. // Demonstrate various Vector operations. import java.util.*; class VectorDemo { public static void main(String args[]) { // initial size is 3, increment is 2 Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(1); v.addElement(2); v.addElement(3); v.addElement(4); System.out.println("Capacity after four additions: " + v.capacity()); v.addElement(5); System.out.println("Current capacity: " + v.capacity()); v.addElement(6); v.addElement(7); System.out.println("Current capacity: " + v.capacity()); Part II Table 17-15 The Legacy Methods Defined by Vector (continued) 512 PART II The Java Library v.addElement(9); v.addElement(10); System.out.println("Current capacity: " + v.capacity()); v.addElement(11); v.addElement(12); System.out.println("First element: " + v.firstElement()); System.out.println("Last element: " + v.lastElement()); if(v.contains(3)) System.out.println("Vector contains 3."); // Enumerate the elements in the vector. Enumeration vEnum = v.elements(); System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements()) System.out.print(vEnum.nextElement() + " "); System.out.println(); } } The output from this program is shown here: Initial size: 0 Initial capacity: 3 Capacity after four additions: 5 Current capacity: 5 Current capacity: 7 Current capacity: 9 First element: 1 Last element: 12 Vector contains 3. Elements in vector: 1 2 3 4 5 6 7 9 10 11 12 Instead of relying on an enumeration to cycle through the objects (as the preceding program does), you can use an iterator. For example, the following iterator-based code can be substituted into the program: // Use an iterator to display contents. Iterator vItr = v.iterator(); System.out.println("\nElements in vector:"); while(vItr.hasNext()) Chapter 17 java.util Part 1: The Collections Framework 513 System.out.print(vItr.next() + " "); System.out.println(); You can also use a for-each for loop to cycle through a Vector, as the following version of the preceding code shows: System.out.println(); Because the Enumeration interface is not recommended for new code, you will usually use an iterator or a for-each for loop to enumerate the contents of a vector. Of course, much legacy code exists that employs Enumeration. Fortunately, enumerations and iterators work in nearly the same manner. Stack Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. With the release of JDK 5, Stack was retrofitted for generics and is declared as shown here: class Stack Here, E specifies the type of element stored in the stack. Stack includes all the methods defined by Vector and adds several of its own, shown in Table 17-16. To put an object on the top of the stack, call push( ). To remove and return the top element, call pop( ). You can use peek( ) to return, but not remove, the top object. An EmptyStackException is thrown if you call pop( ) or peek( ) when the invoking stack is empty. The empty( ) method returns true if nothing is on the stack. The search( ) method Method Description boolean empty( ) Returns true if the stack is empty, and returns false if the stack contains elements. E peek( ) Returns the element on the top of the stack, but does not remove it. E pop( ) Returns the element on the top of the stack, removing it in the process. E push(E element) Pushes element onto the stack. element is also returned. int search(Object element) Searches for element in the stack. If found, its offset from the top of the stack is returned. Otherwise, –1 is returned. Table 17-16 The Methods Defined by Stack Part II // Use an enhanced for loop to display contents System.out.println("\nElements in vector:"); for(int i : v) System.out.print(i + " "); 514 PART II The Java Library determines whether an object exists on the stack and returns the number of pops that are required to bring it to the top of the stack. Here is an example that creates a stack, pushes several Integer objects onto it, and then pops them off again: // Demonstrate the Stack class. import java.util.*; class StackDemo { static void showpush(Stack st, int a) { st.push(a); System.out.println("push(" + a + ")"); System.out.println("stack: " + st); } static void showpop(Stack st) { System.out.print("pop -> "); Integer a = st.pop(); System.out.println(a); System.out.println("stack: " + st); } public static void main(String args[]) { Stack st = new Stack(); System.out.println("stack: " + st); showpush(st, 42); showpush(st, 66); showpush(st, 99); showpop(st); showpop(st); showpop(st); try { showpop(st); } catch (EmptyStackException e) { System.out.println("empty stack"); } } } The following is the output produced by the program; notice how the exception handler for EmptyStackException is caught so that you can gracefully handle a stack underflow: stack: [ ] push(42) stack: [42] push(66) stack: [42, 66] push(99) stack: [42, 66, 99] pop -> 99 Chapter 17 stack: pop -> stack: pop -> stack: pop -> java.util Part 1: The Collections Framework 515 [42, 66] 66 [42] 42 [ ] empty stack One other point: Although Stack is not deprecated, ArrayDeque is a better choice. Dictionary is an abstract class that represents a key/value storage repository and operates much like Map. Given a key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key. Thus, like a map, a dictionary can be thought of as a list of key/value pairs. Although not currently deprecated, Dictionary is classified as obsolete, because it is fully superseded by Map. However, Dictionary is still in use and thus is fully discussed here. With the advent of JDK 5, Dictionary was made generic. It is declared as shown here: class Dictionary Here, K specifies the type of keys, and V specifies the type of values. The abstract methods defined by Dictionary are listed in Table 17-17. To add a key and a value, use the put( ) method. Use get( ) to retrieve the value of a given key. The keys and values can each be returned as an Enumeration by the keys( ) and elements( ) methods, respectively. The size( ) method returns the number of key/value Method Purpose Enumeration elements( ) Returns an enumeration of the values contained in the dictionary. V get(Object key) Returns the object that contains the value associated with key. If key is not in the dictionary, a null object is returned. boolean isEmpty( ) Returns true if the dictionary is empty, and returns false if it contains at least one key. Enumeration keys( ) Returns an enumeration of the keys contained in the dictionary. V put(K key, V value) Inserts a key and its value into the dictionary. Returns null if key is not already in the dictionary; returns the previous value associated with key if key is already in the dictionary. V remove(Object key) Removes key and its value. Returns the value associated with key. If key is not in the dictionary, a null is returned. int size( ) Returns the number of entries in the dictionary. Table 17-17 The Abstract Methods Defined by Dictionary Part II Dictionary 516 PART II The Java Library pairs stored in a dictionary, and isEmpty( ) returns true when the dictionary is empty. You can use the remove( ) method to delete a key/value pair. REMEMBER The Dictionary class is obsolete. You should implement the Map interface to obtain key/value storage functionality. Hashtable Hashtable was part of the original java.util and is a concrete implementation of a Dictionary. However, with the advent of collections, Hashtable was reengineered to also implement the Map interface. Thus, Hashtable is integrated into the Collections Framework. It is similar to HashMap, but is synchronized. Like HashMap, Hashtable stores key/value pairs in a hash table. However, neither keys nor values can be null. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. Hashtable was made generic by JDK 5. It is declared like this: class Hashtable Here, K specifies the type of keys, and V specifies the type of values. A hash table can only store objects that override the hashCode( ) and equals( ) methods that are defined by Object. The hashCode( ) method must compute and return the hash code for the object. Of course, equals( ) compares two objects. Fortunately, many of Java’s built-in classes already implement the hashCode( ) method. For example, the most common type of Hashtable uses a String object as the key. String implements both hashCode( ) and equals( ). The Hashtable constructors are shown here: Hashtable( ) Hashtable(int size) Hashtable(int size, float fillRatio) Hashtable(Map m) The first version is the default constructor. The second version creates a hash table that has an initial size specified by size. (The default size is 11.) The third version creates a hash table that has an initial size specified by size and a fill ratio specified by fillRatio. This ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resized upward. Specifically, when the number of elements is greater than the capacity of the hash table multiplied by its fill ratio, the hash table is expanded. If you do not specify a fill ratio, then 0.75 is used. Finally, the fourth version creates a hash table that is initialized with the elements in m. The default load factor of 0.75 is used. In addition to the methods defined by the Map interface, which Hashtable now implements, Hashtable defines the legacy methods listed in Table 17-18. Several methods throw NullPointerException if an attempt is made to use a null key or value. java.util Part 1: The Collections Framework Method Description void clear( ) Resets and empties the hash table. Object clone( ) Returns a duplicate of the invoking object. boolean contains(Object value) Returns true if some value equal to value exists within the hash table. Returns false if the value isn’t found. boolean containsKey(Object key) Returns true if some key equal to key exists within the hash table. Returns false if the key isn’t found. boolean containsValue(Object value) Returns true if some value equal to value exists within the hash table. Returns false if the value isn’t found. Enumeration elements( ) Returns an enumeration of the values contained in the hash table. V get(Object key) Returns the object that contains the value associated with key. If key is not in the hash table, a null object is returned. boolean isEmpty( ) Returns true if the hash table is empty; returns false if it contains at least one key. Enumeration keys( ) Returns an enumeration of the keys contained in the hash table. V put(K key, V value) Inserts a key and a value into the hash table. Returns null if key isn’t already in the hash table; returns the previous value associated with key if key is already in the hash table. void rehash( ) Increases the size of the hash table and rehashes all of its keys. V remove(Object key) Removes key and its value. Returns the value associated with key. If key is not in the hash table, a null object is returned. int size( ) Returns the number of entries in the hash table. String toString( ) Returns the string equivalent of a hash table. 517 Table 17-18 The Legacy Methods Defined by Hashtable The following example reworks the bank account program, shown earlier, so that it uses a Hashtable to store the names of bank depositors and their current balances: // Demonstrate a Hashtable. import java.util.*; class HTDemo { public static void main(String args[]) { Hashtable balance = new Hashtable(); Part II Chapter 17 518 PART II The Java Library Enumeration names; String str; double bal; balance.put("John Doe", 3434.34); balance.put("Tom Smith", 123.22); balance.put("Jane Baker", 1378.00); balance.put("Tod Hall", 99.22); balance.put("Ralph Smith", -19.08); // Show all balances in hashtable. names = balance.keys(); while(names.hasMoreElements()) { str = names.nextElement(); System.out.println(str + ": " + balance.get(str)); } System.out.println(); // Deposit 1,000 into John Doe's account. bal = balance.get("John Doe"); balance.put("John Doe", bal+1000); System.out.println("John Doe's new balance: " + balance.get("John Doe")); } } The output from this program is shown here: Todd Hall: 99.22 Ralph Smith: -19.08 John Doe: 3434.34 Jane Baker: 1378.0 Tom Smith: 123.22 John Doe's new balance: 4434.34 One important point: Like the map classes, Hashtable does not directly support iterators. Thus, the preceding program uses an enumeration to display the contents of balance. However, you can obtain set-views of the hash table, which permits the use of iterators. To do so, you simply use one of the collection-view methods defined by Map, such as entrySet( ) or keySet( ). For example, you can obtain a set-view of the keys and cycle through them using either an iterator or an enhanced for loop. Here is a reworked version of the program that shows this technique: // Use iterators with a Hashtable. import java.util.*; class HTDemo2 { public static void main(String args[]) { Hashtable balance = new Hashtable(); Chapter 17 java.util Part 1: The Collections Framework 519 String str; double bal; // Show all balances in hashtable. // First, get a set view of the keys. Set set = balance.keySet(); // Get an iterator. Iterator itr = set.iterator(); while(itr.hasNext()) { str = itr.next(); System.out.println(str + ": " + balance.get(str)); } System.out.println(); // Deposit 1,000 into John Doe's account. bal = balance.get("John Doe"); balance.put("John Doe", bal+1000); System.out.println("John Doe's new balance: " + balance.get("John Doe")); } } Properties Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String. The Properties class is used by many other Java classes. For example, it is the type of object returned by System.getProperties( ) when obtaining environmental values. Although the Properties class, itself, is not generic, several of its methods are. Properties defines the following instance variable: Properties defaults; This variable holds a default property list associated with a Properties object. Properties defines these constructors: Properties( ) Properties(Properties propDefault) The first version creates a Properties object that has no default values. The second creates an object that uses propDefault for its default values. In both cases, the property list is empty. In addition to the methods that Properties inherits from Hashtable, Properties defines the methods listed in Table 17-19. Properties also contains one deprecated method: save( ). This was replaced by store( ) because save( ) did not handle errors correctly. Part II balance.put("John Doe", 3434.34); balance.put("Tom Smith", 123.22); balance.put("Jane Baker", 1378.00); balance.put("Tod Hall", 99.22); balance.put("Ralph Smith", -19.08); 520 PART II The Java Library Method Description String getProperty(String key) Returns the value associated with key. A null object is returned if key is neither in the list nor in the default property list. String getProperty(String key, String defaultProperty) Returns the value associated with key. defaultProperty is returned if key is neither in the list nor in the default property list. void list(PrintStream streamOut) Sends the property list to the output stream linked to streamOut. void list(PrintWriter streamOut) Sends the property list to the output stream linked to streamOut. void load(InputStream streamIn) throws IOException Inputs a property list from the input stream linked to streamIn. void load(Reader streamIn) throws IOException Inputs a property list from the input stream linked to streamIn. void loadFromXML(InputStream streamIn) throws IOException, InvalidPropertiesFormatException Inputs a property list from an XML document linked to streamIn. Enumeration propertyNames( ) Returns an enumeration of the keys. This includes those keys found in the default property list, too. Object setProperty(String key, String value) Associates value with key. Returns the previous value associated with key, or returns null if no such association exists. void store(OutputStream streamOut, String description) throws IOException After writing the string specified by description, the property list is written to the output stream linked to streamOut. void store(Writer streamOut, String description) throws IOException After writing the string specified by description, the property list is written to the output stream linked to streamOut. void storeToXML(OutputStream streamOut, String description) throws IOException After writing the string specified by description, the property list is written to the XML document linked to streamOut. void storeToXML(OutputStream streamOut, String description, String enc) The property list and the string specified by description is written to the XML document linked to streamOut using the specified character encoding. Set stringPropertyNames( ) Returns a set of keys. Table 17-19 The Methods Defined by Properties One useful capability of the Properties class is that you can specify a default property that will be returned if no value is associated with a certain key. For example, a default value can be specified along with the key in the getProperty( ) method—such as getProperty( "name" ,"default value"). If the "name" value is not found, then "default Chapter 17 java.util Part 1: The Collections Framework 521 // Demonstrate a Property list. import java.util.*; class PropDemo { public static void main(String args[]) { Properties capitals = new Properties(); capitals.put("Illinois", "Springfield"); capitals.put("Missouri", "Jefferson City"); capitals.put("Washington", "Olympia"); capitals.put("California", "Sacramento"); capitals.put("Indiana", "Indianapolis"); // Get a set-view of the keys. Set states = capitals.keySet(); // Show all of the states and capitals. for(Object name : states) System.out.println("The capital of " + name + " is " + capitals.getProperty((String)name) + "."); System.out.println(); // Look for state not in list -- specify default. String str = capitals.getProperty("Florida", "Not Found"); System.out.println("The capital of Florida is " + str + "."); } } The output from this program is shown here: The The The The The capital capital capital capital capital of of of of of Missouri is Jefferson City. Illinois is Springfield. Indiana is Indianapolis. California is Sacramento. Washington is Olympia. The capital of Florida is Not Found. Since Florida is not in the list, the default value is used. Part II value" is returned. When you construct a Properties object, you can pass another instance of Properties to be used as the default properties for the new instance. In this case, if you call getProperty("foo") on a given Properties object, and "foo" does not exist, Java looks for "foo" in the default Properties object. This allows for arbitrary nesting of levels of default properties. The following example demonstrates Properties. It creates a property list in which the keys are the names of states and the values are the names of their capitals. Notice that the attempt to find the capital for Florida includes a default value. 522 PART II The Java Library Although it is perfectly valid to use a default value when you call getProperty( ), as the preceding example shows, there is a better way of handling default values for most applications of property lists. For greater flexibility, specify a default property list when constructing a Properties object. The default list will be searched if the desired key is not found in the main list. For example, the following is a slightly reworked version of the preceding program, with a default list of states specified. Now, when Florida is sought, it will be found in the default list: // Use a default property list. import java.util.*; class PropDemoDef { public static void main(String args[]) { Properties defList = new Properties(); defList.put("Florida", "Tallahassee"); defList.put("Wisconsin", "Madison"); Properties capitals = new Properties(defList); capitals.put("Illinois", "Springfield"); capitals.put("Missouri", "Jefferson City"); capitals.put("Washington", "Olympia"); capitals.put("California", "Sacramento"); capitals.put("Indiana", "Indianapolis"); // Get a set-view of the keys. Set states = capitals.keySet(); // Show all of the states and capitals. for(Object name : states) System.out.println("The capital of " + name + " is " + capitals.getProperty((String)name) + "."); System.out.println(); // Florida will now be found in the default list. String str = capitals.getProperty("Florida"); System.out.println("The capital of Florida is " + str + "."); } } Using store( ) and load( ) One of the most useful aspects of Properties is that the information contained in a Properties object can be easily stored to or loaded from disk with the store( ) and load( ) methods. At any time, you can write a Properties object to a stream or read it back. This makes property lists especially convenient for implementing simple databases. For example, the following program uses a property list to create a simple computerized telephone book that stores names and phone numbers. To find a person’s number, you enter his or her Chapter 17 java.util Part 1: The Collections Framework 523 name. The program uses the store( ) and load( ) methods to store and retrieve the list. When the program executes, it first tries to load the list from a file called phonebook.dat. If this file exists, the list is loaded. You can then add to the list. If you do, the new list is saved when you terminate the program. Notice how little code is required to implement a small, but functional, computerized phone book. class Phonebook { public static void main(String args[]) throws IOException { Properties ht = new Properties(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String name, number; FileInputStream fin = null; boolean changed = false; // Try to open phonebook.dat file. try { fin = new FileInputStream("phonebook.dat"); } catch(FileNotFoundException e) { // ignore missing file } /* If phonebook file already exists, load existing telephone numbers. */ try { if(fin != null) { ht.load(fin); fin.close(); } } catch(IOException e) { System.out.println("Error reading file."); } // Let user enter new names and numbers. do { System.out.println("Enter new name" + " ('quit' to stop): "); name = br.readLine(); if(name.equals("quit")) continue; System.out.println("Enter number: "); number = br.readLine(); ht.put(name, number); changed = true; } while(!name.equals("quit")); Part II /* A simple telephone number database that uses a property list. */ import java.io.*; import java.util.*; 524 PART II The Java Library // If phone book data has changed, save it. if(changed) { FileOutputStream fout = new FileOutputStream("phonebook.dat"); ht.store(fout, "Telephone Book"); fout.close(); } // Look up numbers given a name. do { System.out.println("Enter name to find" + " ('quit' to quit): "); name = br.readLine(); if(name.equals("quit")) continue; number = (String) ht.get(name); System.out.println(number); } while(!name.equals("quit")); } } Parting Thoughts on Collections The Collections Framework gives you, the programmer, a powerful set of well-engineered solutions to some of programming’s most common tasks. Consider using a collection the next time that you need to store and retrieve information. Remember, collections need not be reserved for only the “large jobs,” such as corporate databases, mailing lists, or inventory systems. They are also effective when applied to smaller jobs. For example, a TreeMap might make an excellent collection to hold the directory structure of a set of files. A TreeSet could be quite useful for storing project-management information. Frankly, the types of problems that will benefit from a collections-based solution are limited only by your imagination. CHAPTER 18 java.util Part 2: More Utility Classes This chapter continues our discussion of java.util by examining those classes and interfaces that are not part of the Collections Framework. These include classes that tokenize strings, work with dates, compute random numbers, bundle resources, and observe events. Also covered are the Formatter and Scanner classes which make it easy to write and read formatted data. Finally, the subpackages of java.util are briefly mentioned at the end of this chapter. StringTokenizer The processing of text often consists of parsing a formatted input string. Parsing is the division of text into a set of discrete parts, or tokens, which in a certain sequence can convey a semantic meaning. The StringTokenizer class provides the first step in this parsing process, often called the lexer (lexical analyzer) or scanner. StringTokenizer implements the Enumeration interface. Therefore, given an input string, you can enumerate the individual tokens contained in it using StringTokenizer. To use StringTokenizer, you specify an input string and a string that contains delimiters. Delimiters are characters that separate tokens. Each character in the delimiters string is considered a valid delimiter—for example, ",;:" sets the delimiters to a comma, semicolon, and colon. The default set of delimiters consists of the whitespace characters: space, tab, newline, and carriage return. The StringTokenizer constructors are shown here: StringTokenizer(String str) StringTokenizer(String str, String delimiters) StringTokenizer(String str, String delimiters, boolean delimAsToken) In all versions, str is the string that will be tokenized. In the first version, the default delimiters are used. In the second and third versions, delimiters is a string that specifies the delimiters. In the third version, if delimAsToken is true, then the delimiters are also returned as tokens when the string is parsed. Otherwise, the delimiters are not returned. Delimiters are not returned as tokens by the first two forms. 525 526 PART II The Java Library Once you have created a StringTokenizer object, the nextToken( ) method is used to extract consecutive tokens. The hasMoreTokens( ) method returns true while there are more tokens to be extracted. Since StringTokenizer implements Enumeration, the hasMoreElements( ) and nextElement( ) methods are also implemented, and they act the same as hasMoreTokens( ) and nextToken( ), respectively. The StringTokenizer methods are shown in Table 18-1. Here is an example that creates a StringTokenizer to parse "key=value" pairs. Consecutive sets of "key=value" pairs are separated by a semicolon. // Demonstrate StringTokenizer. import java.util.StringTokenizer; class STDemo { static String in = "title=Java: The Complete Reference;" + "author=Schildt;" + "publisher=McGraw-Hill;" + "copyright=2011"; public static void main(String args[]) { StringTokenizer st = new StringTokenizer(in, "=;"); while(st.hasMoreTokens()) { String key = st.nextToken(); String val = st.nextToken(); System.out.println(key + "\t" + val); } } } The output from this program is shown here: title Java: The Complete Reference author Schildt publisher McGraw-Hill copyright 2011 Method Description int countTokens( ) Using the current set of delimiters, the method determines the number of tokens left to be parsed and returns the result. boolean hasMoreElements( ) Returns true if one or more tokens remain in the string and returns false if there are none. boolean hasMoreTokens( ) Returns true if one or more tokens remain in the string and returns false if there are none. Object nextElement( ) Returns the next token as an Object. String nextToken( ) Returns the next token as a String. String nextToken(String delimiters) Returns the next token as a String and sets the delimiters string to that specified by delimiters. Table 18-1 The Methods Defined by StringTokenizer Chapter 18 java.util Part 2: More Utility Classes 527 BitSet A BitSet class creates a special type of array that holds bit values. This array can increase in size as needed. This makes it similar to a vector of bits. The BitSet constructors are shown here: The first version creates a default object. The second version allows you to specify its initial size (that is, the number of bits that it can hold). All bits are initialized to zero. BitSet defines the methods listed in Table 18-2. Method Description void and(BitSet bitSet) ANDs the contents of the invoking BitSet object with those specified by bitSet. The result is placed into the invoking object. void andNot(BitSet bitSet) For each 1 bit in bitSet, the corresponding bit in the invoking BitSet is cleared. int cardinality( ) Returns the number of set bits in the invoking object. void clear( ) Zeros all bits. void clear(int index) Zeros the bit specified by index. void clear(int startIndex, int endIndex) Zeros the bits from startIndex to endIndex –1. Object clone( ) Duplicates the invoking BitSet object. boolean equals(Object bitSet) Returns true if the invoking bit set is equivalent to the one passed in bitSet. Otherwise, the method returns false. void flip(int index) Reverses the bit specified by index. void flip(int startIndex, int endIndex) Reverses the bits from startIndex to endIndex –1. boolean get(int index) Returns the current state of the bit at the specified index. BitSet get(int startIndex, int endIndex) Returns a BitSet that consists of the bits from startIndex to endIndex –1. The invoking object is not changed. int hashCode( ) Returns the hash code for the invoking object. boolean intersects(BitSet bitSet) Returns true if at least one pair of corresponding bits within the invoking object and bitSet are 1. boolean isEmpty( ) Returns true if all bits in the invoking object are zero. int length( ) Returns the number of bits required to hold the contents of the invoking BitSet. This value is determined by the location of the last 1 bit. int nextClearBit(int startIndex) Returns the index of the next cleared bit (that is, the next zero bit), starting from the index specified by startIndex. Table 18-2 The Methods Defined by BitSet Part II BitSet( ) BitSet(int size) 528 PART II The Java Library Method Description int nextSetBit(int startIndex) Returns the index of the next set bit (that is, the next 1 bit), starting from the index specified by startIndex. If no bit is set, –1 is returned. void or(BitSet bitSet) ORs the contents of the invoking BitSet object with that specified by bitSet. The result is placed into the invoking object. int previousClearBit(int startIndex) Returns the index of the next cleared bit (that is, the next 0 bit) at or prior to the index specified by startIndex. If no cleared bit is found, –1 is returned. (Added by JDK 7.) int previousSetBit(int startIndex) Returns the index of the next set bit (that is, the next 1 bit) at or prior to the index specified by startIndex. If no set bit is found, –1 is returned. (Added by JDK 7.) void set(int index) Sets the bit specified by index. void set(int index, boolean v) Sets the bit specified by index to the value passed in v. true sets the bit; false clears the bit. void set(int startIndex, int endIndex) Sets the bits from startIndex to endIndex –1. void set(int startIndex, int endIndex, boolean v) Sets the bits from startIndex to endIndex –1 to the value passed in v. true sets the bits; false clears the bits. int size( ) Returns the number of bits in the invoking BitSet object. byte[ ] toByteArray( ) Returns a byte array that contains the invoking BitSet object. (Added by JDK 7.) long[ ] toLongArray( ) Returns a long array that contains the invoking BitSet object. (Added by JDK 7.) String toString( ) Returns the string equivalent of the invoking BitSet object. static BitSet valueOf(byte[ ] v) Returns a BitSet that contains the bits in v. (Added by JDK 7.) static BitSet valueOf(ByteBuffer v) Returns a BitSet that contains the bits in v. (Added by JDK 7.) static BitSet valueOf(long[ ] v) Returns a BitSet that contains the bits in v. (Added by JDK 7.) static BitSet valueOf(LongBuffer v) Returns a BitSet that contains the bits in v. (Added by JDK 7.) void xor(BitSet bitSet) XORs the contents of the invoking BitSet object with that specified by bitSet. The result is placed into the invoking object. Table 18-2 The Methods Defined by BitSet (continued) Chapter 18 java.util Part 2: More Utility Classes 529 Here is an example that demonstrates BitSet: // BitSet Demonstration. import java.util.BitSet; // set some bits for(int i=0; i<16; i++) { if((i%2) == 0) bits1.set(i); if((i%5) != 0) bits2.set(i); } System.out.println("Initial pattern in bits1: "); System.out.println(bits1); System.out.println("\nInitial pattern in bits2: "); System.out.println(bits2); // AND bits bits2.and(bits1); System.out.println("\nbits2 AND bits1: "); System.out.println(bits2); // OR bits bits2.or(bits1); System.out.println("\nbits2 OR bits1: "); System.out.println(bits2); // XOR bits bits2.xor(bits1); System.out.println("\nbits2 XOR bits1: "); System.out.println(bits2); } } The output from this program is shown here. When toString( ) converts a BitSet object to its string equivalent, each set bit is represented by its bit position. Cleared bits are not shown. Initial pattern in bits1: {0, 2, 4, 6, 8, 10, 12, 14} Initial pattern in bits2: {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14} bits2 AND bits1: {2, 4, 6, 8, 12, 14} Part II class BitSetDemo { public static void main(String args[]) { BitSet bits1 = new BitSet(16); BitSet bits2 = new BitSet(16); 530 PART II The Java Library bits2 OR bits1: {0, 2, 4, 6, 8, 10, 12, 14} bits2 XOR bits1: {} Date The Date class encapsulates the current date and time. Before beginning our examination of Date, it is important to point out that it has changed substantially from its original version defined by Java 1.0. When Java 1.1 was released, many of the functions carried out by the original Date class were moved into the Calendar and DateFormat classes, and as a result, many of the original 1.0 Date methods were deprecated. Since the deprecated 1.0 methods should not be used for new code, they are not described here. Date supports the following constructors: Date( ) Date(long millisec) The first constructor initializes the object with the current date and time. The second constructor accepts one argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970. The nondeprecated methods defined by Date are shown in Table 18-3. Date also implements the Comparable interface. Method Description boolean after(Date date) Returns true if the invoking Date object contains a date that is later than the one specified by date. Otherwise, it returns false. boolean before(Date date) Returns true if the invoking Date object contains a date that is earlier than the one specified by date. Otherwise, it returns false. Object clone( ) Duplicates the invoking Date object. int compareTo(Date date) Compares the value of the invoking object with that of date. Returns 0 if the values are equal. Returns a negative value if the invoking object is earlier than date. Returns a positive value if the invoking object is later than date. boolean equals(Object date) Returns true if the invoking Date object contains the same time and date as the one specified by date. Otherwise, it returns false. long getTime( ) Returns the number of milliseconds that have elapsed since January 1, 1970. int hashCode( ) Returns a hash code for the invoking object. void setTime(long time) Sets the time and date as specified by time, which represents an elapsed time in milliseconds from midnight, January 1, 1970. String toString( ) Converts the invoking Date object into a string and returns the result. Table 18-3 The Nondeprecated Methods Defined by Date Chapter 18 java.util Part 2: More Utility Classes 531 As you can see by examining Table 18-3, the non-deprecated Date features do not allow you to obtain the individual components of the date or time. As the following program demonstrates, you can only obtain the date and time in terms of milliseconds or in its default string representation as returned by toString( ). To obtain more-detailed information about the date and time, you will use the Calendar class. class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new Date(); // display time and date using toString() System.out.println(date); // Display number of milliseconds since midnight, January 1, 1970 GMT long msec = date.getTime(); System.out.println("Milliseconds since Jan. 1, 1970 GMT = " + msec); } } Sample output is shown here: Sat Jan 01 10:27:33 CST 2011 Milliseconds since Jan. 1, 1970 GMT = 1293899253417 Calendar The abstract Calendar class provides a set of methods that allows you to convert a time in milliseconds to a number of useful components. Some examples of the type of information that can be provided are year, month, day, hour, minute, and second. It is intended that subclasses of Calendar will provide the specific functionality to interpret time information according to their own rules. This is one aspect of the Java class library that enables you to write programs that can operate in international environments. An example of such a subclass is GregorianCalendar. Calendar provides no public constructors. Calendar defines several protected instance variables. areFieldsSet is a boolean that indicates if the time components have been set. fields is an array of ints that holds the components of the time. isSet is a boolean array that indicates if a specific time component has been set. time is a long that holds the current time for this object. isTimeSet is a boolean that indicates if the current time has been set. Some commonly used methods defined by Calendar are shown in Table 18-4. Part II // Show date and time using only Date methods. import java.util.Date; 532 PART II The Java Library Method Description abstract void add(int which, int val) Adds val to the time or date component specified by which. To subtract, add a negative value. which must be one of the fields defined by Calendar, such as Calendar.HOUR. boolean after(Object calendarObj) Returns true if the invoking Calendar object contains a date that is later than the one specified by calendarObj. Otherwise, it returns false. boolean before(Object calendarObj) Returns true if the invoking Calendar object contains a date that is earlier than the one specified by calendarObj. Otherwise, it returns false. final void clear( ) Zeros all time components in the invoking object. final void clear(int which) Zeros the time component specified by which in the invoking object. Object clone( ) Returns a duplicate of the invoking object. boolean equals(Object calendarObj) Returns true if the invoking Calendar object contains a date that is equal to the one specified by calendarObj. Otherwise, it returns false. int get(int calendarField) Returns the value of one component of the invoking object. The component is indicated by calendarField. Examples of the components that can be requested are Calendar.YEAR, Calendar.MONTH, Calendar.MINUTE, and so forth. static Locale[ ] getAvailableLocales( ) Returns an array of Locale objects that contains the locales for which calendars are available. static Calendar getInstance( ) Returns a Calendar object for the default locale and time zone. static Calendar getInstance(TimeZone tz) Returns a Calendar object for the time zone specified by tz. The default locale is used. static Calendar getInstance(Locale locale) Returns a Calendar object for the locale specified by locale. The default time zone is used. static Calendar getInstance(TimeZone tz, Locale locale) Returns a Calendar object for the time zone specified by tz and the locale specified by locale. final Date getTime( ) Returns a Date object equivalent to the time of the invoking object. TimeZone getTimeZone( ) Returns the time zone for the invoking object. final boolean isSet(int which) Returns true if the specified time component is set. Otherwise, it returns false. void set(int which, int val) Sets the date or time component specified by which to the value specified by val in the invoking object. which must be one of the fields defined by Calendar, such as Calendar.HOUR. Table 18-4 Commonly Used Methods Defined by Calendar java.util Part 2: More Utility Classes Method Description final void set(int year, int month, int dayOfMonth) Sets various date and time components of the invoking object. final void set(int year, int month, int dayOfMonth, int hours, int minutes) Sets various date and time components of the invoking object. final void set(int year, int month, int dayOfMonth, int hours, int minutes, int seconds) Sets various date and time components of the invoking object. final void setTime(Date d) Sets various date and time components of the invoking object. This information is obtained from the Date object d. void setTimeZone(TimeZone tz) Sets the time zone for the invoking object to that specified by tz. Table 18-4 Commonly Used Methods Defined by Calendar (continued) Calendar defines the following int constants, which are used when you get or set components of the calendar: ALL_STYLES FRIDAY PM AM HOUR SATURDAY AM_PM HOUR_OF_DAY SECOND APRIL JANUARY SEPTEMBER AUGUST JULY SHORT DATE JUNE SUNDAY DAY_OF_MONTH LONG THURSDAY DAY_OF_WEEK MARCH TUESDAY DAY_OF_WEEK_IN_MONTH MAY UNDECIMBER DAY_OF_YEAR MILLISECOND WEDNESDAY DECEMBER MINUTE WEEK_OF_MONTH DST_OFFSET MONDAY WEEK_OF_YEAR ERA MONTH YEAR FEBRUARY NOVEMBER ZONE_OFFSET FIELD_COUNT OCTOBER The following program demonstrates several Calendar methods: // Demonstrate Calendar import java.util.Calendar; class CalendarDemo { public static void main(String args[]) { String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 533 Part II Chapter 18 534 PART II The Java Library // Create a calendar initialized with the // current date and time in the default // locale and timezone. Calendar calendar = Calendar.getInstance(); // Display current time and date information. System.out.print("Date: "); System.out.print(months[calendar.get(Calendar.MONTH)]); System.out.print(" " + calendar.get(Calendar.DATE) + " "); System.out.println(calendar.get(Calendar.YEAR)); System.out.print("Time: "); System.out.print(calendar.get(Calendar.HOUR) + ":"); System.out.print(calendar.get(Calendar.MINUTE) + ":"); System.out.println(calendar.get(Calendar.SECOND)); // Set the time and date information and display it. calendar.set(Calendar.HOUR, 10); calendar.set(Calendar.MINUTE, 29); calendar.set(Calendar.SECOND, 22); System.out.print("Updated time: "); System.out.print(calendar.get(Calendar.HOUR) + ":"); System.out.print(calendar.get(Calendar.MINUTE) + ":"); System.out.println(calendar.get(Calendar.SECOND)); } } Sample output is shown here: Date: Jan 1 2011 Time: 11:24:25 Updated time: 10:29:22 GregorianCalendar GregorianCalendar is a concrete implementation of a Calendar that implements the normal Gregorian calendar with which you are familiar. The getInstance( ) method of Calendar will typically return a GregorianCalendar initialized with the current date and time in the default locale and time zone. GregorianCalendar defines two fields: AD and BC. These represent the two eras defined by the Gregorian calendar. There are also several constructors for GregorianCalendar objects. The default, GregorianCalendar( ), initializes the object with the current date and time in the default locale and time zone. Three more constructors offer increasing levels of specificity: GregorianCalendar(int year, int month, int dayOfMonth) GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes) GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes, int seconds) Chapter 18 java.util Part 2: More Utility Classes 535 GregorianCalendar(Locale locale) GregorianCalendar(TimeZone timeZone) GregorianCalendar(TimeZone timeZone, Locale locale) GregorianCalendar provides an implementation of all the abstract methods in Calendar. It also provides some additional methods. Perhaps the most interesting is isLeapYear( ), which tests if the year is a leap year. Its form is boolean isLeapYear(int year) This method returns true if year is a leap year and false otherwise. The following program demonstrates GregorianCalendar: // Demonstrate GregorianCalendar import java.util.*; class GregorianCalendarDemo { public static void main(String args[]) { String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; int year; // Create a Gregorian calendar initialized // with the current date and time in the // default locale and timezone. GregorianCalendar gcalendar = new GregorianCalendar(); // Display current time and date information. System.out.print("Date: "); System.out.print(months[gcalendar.get(Calendar.MONTH)]); System.out.print(" " + gcalendar.get(Calendar.DATE) + " "); System.out.println(year = gcalendar.get(Calendar.YEAR)); System.out.print("Time: "); System.out.print(gcalendar.get(Calendar.HOUR) + ":"); System.out.print(gcalendar.get(Calendar.MINUTE) + ":"); System.out.println(gcalendar.get(Calendar.SECOND)); // Test if the current year is a leap year if(gcalendar.isLeapYear(year)) { System.out.println("The current year is a leap year"); } else { Part II All three versions set the day, month, and year. Here, year specifies the year. The month is specified by month, with zero indicating January. The day of the month is specified by dayOfMonth. The first version sets the time to midnight. The second version also sets the hours and the minutes. The third version adds seconds. You can also construct a GregorianCalendar object by specifying the locale and/or time zone. The following constructors create objects initialized with the current date and time using the specified time zone and/or locale: 536 PART II The Java Library System.out.println("The current year is not a leap year"); } } } Sample output is shown here: Date: Jan 1 2011 Time: 11:25:27 The current year is not a leap year TimeZone Another time-related class is TimeZone. The abstract TimeZone class allows you to work with time zone offsets from Greenwich mean time (GMT), also referred to as Coordinated Universal Time (UTC). It also computes daylight saving time. TimeZone only supplies the default constructor. A sampling of methods defined by TimeZone is given in Table 18-5. Method Description Object clone( ) Returns a TimeZone-specific version of clone( ). static String[ ] getAvailableIDs( ) Returns an array of String objects representing the names of all time zones. static String[ ] getAvailableIDs(int timeDelta) Returns an array of String objects representing the names of all time zones that are timeDelta offset from GMT. static TimeZone getDefault( ) Returns a TimeZone object that represents the default time zone used on the host computer. String getID( ) Returns the name of the invoking TimeZone object. abstract int getOffset(int era, int year, int month, int dayOfMonth, int dayOfWeek, int millisec) Returns the offset that should be added to GMT to compute local time. This value is adjusted for daylight saving time. The parameters to the method represent date and time components. abstract int getRawOffset( ) Returns the raw offset (in milliseconds) that should be added to GMT to compute local time. This value is not adjusted for daylight saving time. static TimeZone getTimeZone(String tzName) Returns the TimeZone object for the time zone named tzName. abstract boolean inDaylightTime(Date d) Returns true if the date represented by d is in daylight saving time in the invoking object. Otherwise, it returns false. static void setDefault(TimeZone tz) Sets the default time zone to be used on this host. tz is a reference to the TimeZone object to be used. Table 18-5 Some of the Methods Defined by TimeZone Chapter 18 java.util Part 2: More Utility Classes Method Description void setID(String tzName) Sets the name of the time zone (that is, its ID) to that specified by tzName. abstract void setRawOffset(int millis) Sets the offset in milliseconds from GMT. abstract boolean useDaylightTime( ) Returns true if the invoking object uses daylight saving time. Otherwise, it returns false. 537 SimpleTimeZone The SimpleTimeZone class is a convenient subclass of TimeZone. It implements TimeZone's abstract methods and allows you to work with time zones for a Gregorian calendar. It also computes daylight saving time. SimpleTimeZone defines four constructors. One is SimpleTimeZone(int timeDelta, String tzName) This constructor creates a SimpleTimeZone object. The offset relative to Greenwich mean time (GMT) is timeDelta. The time zone is named tzName. The second SimpleTimeZone constructor is SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1) Here, the offset relative to GMT is specified in timeDelta. The time zone name is passed in tzId. The start of daylight saving time is indicated by the parameters dstMonth0, dstDayInMonth0, dstDay0, and time0. The end of daylight saving time is indicated by the parameters dstMonth1, dstDayInMonth1, dstDay1, and time1. The third SimpleTimeZone constructor is SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1, int dstDelta) Here, dstDelta is the number of milliseconds saved during daylight saving time. The fourth SimpleTimeZone constructor is SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int time0mode, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1, int time1mode, int dstDelta) Here, time0mode specifies the mode of the starting time, and time1mode specifies the mode of the ending time. Valid mode values include STANDARD_TIME WALL_TIME UTC_TIME Part II Table 18-5 Some of the Methods Defined by TimeZone (continued) 538 PART II The Java Library The time mode indicates how the time values are interpreted. The default mode used by the other constructors is WALL_TIME. Locale The Locale class is instantiated to produce objects that describe a geographical or cultural region. It is one of several classes that provide you with the ability to write programs that can execute in different international environments. For example, the formats used to display dates, times, and numbers are different in various regions. Internationalization is a large topic that is beyond the scope of this book. However, many programs will only need to deal with its basics, which include setting the current locale. The Locale class defines the following constants that are useful for dealing with the most common locales: CANADA GERMAN KOREAN CANADA_FRENCH GERMANY PRC CHINA ITALIAN SIMPLIFIED_CHINESE CHINESE ITALY TAIWAN ENGLISH JAPAN TRADITIONAL_CHINESE FRANCE JAPANESE UK FRENCH KOREA US For example, the expression Locale.CANADA represents the Locale object for Canada. The constructors for Locale are Locale(String language) Locale(String language, String country) Locale(String language, String country, String variant) These constructors build a Locale object to represent a specific language and in the case of the last two, country. These values must contain standard language and country codes. Auxiliary variant information can be provided in variant. Locale defines several methods. One of the most important is setDefault( ), shown here: static void setDefault(Locale localeObj) This sets the default locale used by the JVM to that specified by localeObj. Some other interesting methods are the following: final String getDisplayCountry( ) final String getDisplayLanguage( ) final String getDisplayName( ) These return human-readable strings that can be used to display the name of the country, the name of the language, and the complete description of the locale. The default locale can be obtained using getDefault( ), shown here: static Locale getDefault( ) java.util Part 2: More Utility Classes 539 JDK 7 adds significant upgrades to the Locale class that support Internet Engineering Task Force (IETF) BCP 47, which defines tags for identifying languages, and Unicode Technical Standard (UTS) 35, which defines the Locale Data Markup Language (LDML). Support for BCP 47 and UTS 35 caused several features to be added to Locale, including several new methods and the Locale.Builder class. Among others, new methods include getScript( ), which obtains the locale’s script, and toLanguageTag( ), which obtains a string that contains the locale’s language tag. The Locale.Builder class constructs Locale instances. It ensures that a locale specification is well-formed as defined by BCP 47. (The Locale constructors do not provide such a check.) Calendar and GregorianCalendar are examples of classes that operate in a localesensitive manner. DateFormat and SimpleDateFormat also depend on the locale. Random The Random class is a generator of pseudorandom numbers. These are called pseudorandom numbers because they are simply uniformly distributed sequences. Random defines the following constructors: Random( ) Random(long seed) The first version creates a number generator that uses a reasonably unique seed. The second form allows you to specify a seed value manually. If you initialize a Random object with a seed, you define the starting point for the random sequence. If you use the same seed to initialize another Random object, you will extract the same random sequence. If you want to generate different sequences, specify different seed values. One way to do this is to use the current time to seed a Random object. This approach reduces the possibility of getting repeated sequences. The public methods defined by Random are shown in Table 18-6. As you can see, there are seven types of random numbers that you can extract from a Random object. Random Boolean values are available from nextBoolean( ). Random bytes can be obtained by calling nextBytes( ). Integers can be extracted via the nextInt( ) method. Long integers, uniformly distributed over their range, can be obtained with nextLong( ). Method Description boolean nextBoolean( ) Returns the next boolean random number. void nextBytes(byte vals[ ]) Fills vals with randomly generated values. double nextDouble( ) Returns the next double random number. float nextFloat( ) Returns the next float random number. double nextGaussian( ) Returns the next Gaussian random number. int nextInt( ) Returns the next int random number. int nextInt(int n) Returns the next int random number within the range zero to n. long nextLong( ) Returns the next long random number. void setSeed(long newSeed) Sets the seed value (that is, the starting point for the random number generator) to that specified by newSeed. Table 18-6 The Methods Defined by Random Part II Chapter 18 540 PART II The Java Library The nextFloat( ) and nextDouble( ) methods return a uniformly distributed float and double, respectively, between 0.0 and 1.0. Finally, nextGaussian( ) returns a double value centered at 0.0 with a standard deviation of 1.0. This is what is known as a bell curve. Here is an example that demonstrates the sequence produced by nextGaussian( ). It obtains 100 random Gaussian values and averages these values. The program also counts the number of values that fall within two standard deviations, plus or minus, using increments of 0.5 for each category. The result is graphically displayed sideways on the screen. // Demonstrate random Gaussian values. import java.util.Random; class RandDemo { public static void main(String args[]) { Random r = new Random(); double val; double sum = 0; int bell[] = new int[10]; for(int i=0; i<100; i++) { val = r.nextGaussian(); sum += val; double t = -2; for(int x=0; x<10; x++, t += 0.5) if(val < t) { bell[x]++; break; } } System.out.println("Average of values: " + (sum/100)); // display bell curve, sideways for(int i=0; i<10; i++) { for(int x=bell[i]; x>0; x--) System.out.print("*"); System.out.println(); } } } Here is a sample program run. As you can see, a bell-like distribution of numbers is obtained. Average of values: 0.0702235271133344 ** ******* ****** *************** ****************** ***************** ************* ********** ******** *** Chapter 18 java.util Part 2: More Utility Classes 541 The Observable class is used to create subclasses that other parts of your program can observe. When an object of such a subclass undergoes a change, observing classes are notified. Observing classes must implement the Observer interface, which defines the update( ) method. The update( ) method is called when an observer is notified of a change in an observed object. Observable defines the methods shown in Table 18-7. An object that is being observed must follow two simple rules. First, if it has changed, it must call setChanged( ). Second, when it is ready to notify observers of this change, it must call notifyObservers( ). This causes the update( ) method in the observing object(s) to be called. Be careful—if the object calls notifyObservers( ) without having previously called setChanged( ), no action will take place. The observed object must call both setChanged( ) and notifyObservers( ) before update( ) will be called. Notice that notifyObservers( ) has two forms: one that takes an argument and one that does not. If you call notifyObservers( ) with an argument, this object is passed to the observer’s update( ) method as its second parameter. Otherwise, null is passed to update( ). You can use the second parameter for passing any type of object that is appropriate for your application. The Observer Interface To observe an observable object, you must implement the Observer interface. This interface defines only the one method shown here: void update(Observable observOb, Object arg) Here, observOb is the object being observed, and arg is the value passed by notifyObservers( ). The update( ) method is called when a change in the observed object takes place. Method Description void addObserver(Observer obj) Adds obj to the list of objects observing the invoking object. protected void clearChanged( ) Calling this method returns the status of the invoking object to "unchanged." int countObservers( ) Returns the number of objects observing the invoking object. void deleteObserver(Observer obj) Removes obj from the list of objects observing the invoking object. void deleteObservers( ) Removes all observers for the invoking object. boolean hasChanged( ) Returns true if the invoking object has been modified and false if it has not. void notifyObservers( ) Notifies all observers of the invoking object that it has changed by calling update( ). A null is passed as the second argument to update( ). void notifyObservers(Object obj) Notifies all observers of the invoking object that it has changed by calling update( ). obj is passed as an argument to update( ). protected void setChanged( ) Called when the invoking object has changed. Table 18-7 The Methods Defined by Observable Part II Observable 542 PART II The Java Library An Observer Example Here is an example that demonstrates an observable object. It creates an observer class, called Watcher, that implements the Observer interface. The class being monitored is called BeingWatched. It extends Observable. Inside BeingWatched is the method counter( ), which simply counts down from a specified value. It uses sleep( ) to wait a tenth of a second between counts. Each time the count changes, notifyObservers( ) is called with the current count passed as its argument. This causes the update( ) method inside Watcher to be called, which displays the current count. Inside main( ), a Watcher and a BeingWatched object, called observing and observed, respectively, are created. Then, observing is added to the list of observers for observed. This means that observing.update( ) will be called each time counter( ) calls notifyObservers( ). /* Demonstrate the Observable class and the Observer interface. */ import java.util.*; // This is the observing class. class Watcher implements Observer { public void update(Observable obj, Object arg) { System.out.println("update() called, count is " + ((Integer)arg).intValue()); } } // This is the class being observed. class BeingWatched extends Observable { void counter(int period) { for( ; period >=0; period--) { setChanged(); notifyObservers(new Integer(period)); try { Thread.sleep(100); } catch(InterruptedException e) { System.out.println("Sleep interrupted"); } } } } class ObserverDemo { public static void main(String args[]) { BeingWatched observed = new BeingWatched(); Watcher observing = new Watcher(); /* Add the observing to the list of observers for observed object. */ observed.addObserver(observing); observed.counter(10); } } Chapter 18 java.util Part 2: More Utility Classes 543 update() update() update() update() update() update() update() update() update() update() update() called, called, called, called, called, called, called, called, called, called, called, count count count count count count count count count count count is is is is is is is is is is is 10 9 8 7 6 5 4 3 2 1 0 More than one object can be an observer. For example, the following program implements two observing classes and adds an object of each class to the BeingWatched observer list. The second observer waits until the count reaches zero and then rings the bell. /* An object may be observed by two or more observers. */ import java.util.*; // This is the first observing class. class Watcher1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("update() called, count is " + ((Integer)arg).intValue()); } } // This is the second observing class. class Watcher2 implements Observer { public void update(Observable obj, Object arg) { // Ring bell when done if(((Integer)arg).intValue() == 0) System.out.println("Done" + '\7'); } } // This is the class being observed. class BeingWatched extends Observable { void counter(int period) { for( ; period >=0; period--) { setChanged(); notifyObservers(new Integer(period)); try { Thread.sleep(100); } catch(InterruptedException e) { System.out.println("Sleep interrupted"); Part II The output from this program is shown here: 544 PART II The Java Library } } } } class TwoObservers { public static void main(String args[]) { BeingWatched observed = new BeingWatched(); Watcher1 observing1 = new Watcher1(); Watcher2 observing2 = new Watcher2(); // add both observers observed.addObserver(observing1); observed.addObserver(observing2); observed.counter(10); } } The Observable class and the Observer interface allow you to implement sophisticated program architectures based on the document/view methodology. They are also useful in multithreaded situations. Timer and TimerTask An interesting and useful feature offered by java.util is the ability to schedule a task for execution at some future time. The classes that support this are Timer and TimerTask. Using these classes, you can create a thread that runs in the background, waiting for a specific time. When the time arrives, the task linked to that thread is executed. Various options allow you to schedule a task for repeated execution, and to schedule a task to run on a specific date. Although it was always possible to manually create a task that would be executed at a specific time using the Thread class, Timer and TimerTask greatly simplify this process. Timer and TimerTask work together. Timer is the class that you will use to schedule a task for execution. The task being scheduled must be an instance of TimerTask. Thus, to schedule a task, you will first create a TimerTask object and then schedule it for execution using an instance of Timer. TimerTask implements the Runnable interface; thus, it can be used to create a thread of execution. Its constructor is shown here: TimerTask( ) TimerTask defines the methods shown in Table 18-8. Notice that run( ) is abstract, which means that it must be overridden. The run( ) method, defined by the Runnable interface, contains the code that will be executed. Thus, the easiest way to create a timer task is to extend TimerTask and override run( ). Chapter 18 java.util Part 2: More Utility Classes Method Description boolean cancel( ) Terminates the task. Returns true if an execution of the task is prevented. Otherwise, returns false. abstract void run( ) Contains the code for the timer task. long scheduledExecutionTime( ) Returns the time at which the last execution of the task was scheduled to have occurred. 545 Once a task has been created, it is scheduled for execution by an object of type Timer. The constructors for Timer are shown here: Timer( ) Timer(boolean DThread) Timer(String tName) Timer(String tName, boolean DThread) The first version creates a Timer object that runs as a normal thread. The second uses a daemon thread if DThread is true. A daemon thread will execute only as long as the rest of the program continues to execute. The third and fourth constructors allow you to specify a name for the Timer thread. The methods defined by Timer are shown in Table 18-9. Once a Timer has been created, you will schedule a task by calling schedule( ) on the Timer that you created. As Table 18-9 shows, there are several forms of schedule( ) which allow you to schedule tasks in a variety of ways. Method Description void cancel( ) Cancels the timer thread. int purge( ) Deletes cancelled tasks from the timer’s queue. void schedule(TimerTask TTask, long wait) TTask is scheduled for execution after the period passed in wait has elapsed. The wait parameter is specified in milliseconds. void schedule(TimerTask TTask, long wait, long repeat) TTask is scheduled for execution after the period passed in wait has elapsed. The task is then executed repeatedly at the interval specified by repeat. Both wait and repeat are specified in milliseconds. void schedule(TimerTask TTask, Date targetTime) TTask is scheduled for execution at the time specified by targetTime. void schedule(TimerTask TTask, Date targetTime, long repeat) TTask is scheduled for execution at the time specified by targetTime. The task is then executed repeatedly at the interval passed in repeat. The repeat parameter is specified in milliseconds. Table 18-9 The Methods Defined by Timer Part II Table 18-8 The Methods Defined by TimerTask 546 PART II The Java Library Method Description void scheduleAtFixedRate( TimerTask TTask, long wait, long repeat) TTask is scheduled for execution after the period passed in wait has elapsed. The task is then executed repeatedly at the interval specified by repeat. Both wait and repeat are specified in milliseconds. The time of each repetition is relative to the first execution, not the preceding execution. Thus, the overall rate of execution is fixed. void scheduleAtFixedRate( TimerTask TTask, Date targetTime, long repeat) TTask is scheduled for execution at the time specified by targetTime. The task is then executed repeatedly at the interval passed in repeat. The repeat parameter is specified in milliseconds. The time of each repetition is relative to the first execution, not the preceding execution. Thus, the overall rate of execution is fixed. Table 18-9 The Methods Defined by Timer (continued) If you create a non-daemon task, then you will want to call cancel( ) to end the task when your program ends. If you don’t do this, then your program may "hang" for a period of time. The following program demonstrates Timer and TimerTask. It defines a timer task whose run( ) method displays the message "Timer task executed." This task is scheduled to run once every half second after an initial delay of one second. // Demonstrate Timer and TimerTask. import java.util.*; class MyTimerTask extends TimerTask { public void run() { System.out.println("Timer task executed."); } } class TTest { public static void main(String args[]) { MyTimerTask myTask = new MyTimerTask(); Timer myTimer = new Timer(); /* Set an initial delay of 1 second, then repeat every half second. */ myTimer.schedule(myTask, 1000, 500); try { Thread.sleep(5000); } catch (InterruptedException exc) {} myTimer.cancel(); } } Chapter 18 java.util Part 2: More Utility Classes 547 Currency The Currency class encapsulates information about a currency. It defines no constructors. The methods supported by Currency are shown in Table 18-10. The following program demonstrates Currency: class CurDemo { public static void main(String args[]) { Currency c; c = Currency.getInstance(Locale.US); System.out.println("Symbol: " + c.getSymbol()); System.out.println("Default fractional digits: " + c.getDefaultFractionDigits()); } } The output is shown here: Symbol: $ Default fractional digits: 2 Method Description static Set getAvailableCurrencies( ) Returns a set of the supported currencies. (Added by JDK 7.) String getCurrencyCode( ) Returns the code (as defined by ISO 4217) that describes the invoking currency. int getDefaultFractionDigits( ) Returns the number of digits after the decimal point that are normally used by the invoking currency. For example, there are two fractional digits normally used for dollars. String getDisplayName( ) Returns the name of the invoking currency for the default locale. (Added by JDK 7.) String getDisplayName(Locale loc) Returns the name of the invoking currency for the specified locale. (Added by JDK 7.) static Currency getInstance(Locale localeObj) Returns a Currency object for the locale specified by localeObj. static Currency getInstance(String code) Returns a Currency object associated with the currency code passed in code. Table 18-10 The Methods Defined by Currency Part II // Demonstrate Currency. import java.util.*; 548 PART II The Java Library Method Description int getNumericCode( ) Returns the numeric code (as defined by ISO 4217) for the invoking currency. (Added by JDK 7.) String getSymbol( ) Returns the currency symbol (such as $) for the invoking object. String getSymbol(Locale localeObj) Returns the currency symbol (such as $) for the locale passed in localeObj. String toString( ) Returns the currency code for the invoking object. Table 18-10 The Methods Defined by Currency (continued) Formatter At the core of Java’s support for creating formatted output is the Formatter class. It provides format conversions that let you display numbers, strings, and time and date in virtually any format you like. It operates in a manner similar to the C/C++ printf( ) function, which means that if you are familiar with C/C++, then learning to use Formatter will be very easy. It also further streamlines the conversion of C/C++ code to Java. If you are not familiar with C/C++, it is still quite easy to format data. NOTE Although Java’s Formatter class operates in a manner very similar to the C/C++ printf( ) function, there are some differences, and some new features. Therefore, if you have a C/C++ background, a careful reading is advised. The Formatter Constructors Before you can use Formatter to format output, you must create a Formatter object. In general, Formatter works by converting the binary form of data used by a program into formatted text. It stores the formatted text in a buffer, the contents of which can be obtained by your program whenever they are needed. It is possible to let Formatter supply this buffer automatically, or you can specify the buffer explicitly when a Formatter object is created. It is also possible to have Formatter output its buffer to a file. The Formatter class defines many constructors, which enable you to construct a Formatter in a variety of ways. Here is a sampling: Formatter( ) Formatter(Appendable buf) Formatter(Appendable buf, Locale loc) Formatter(String filename) throws FileNotFoundException Formatter(String filename, String charset) throws FileNotFoundException, UnsupportedEncodingException Formatter(File outF) throws FileNotFoundException Formatter(OutputStream outStrm) java.util Part 2: More Utility Classes Here, buf specifies a buffer for the formatted output. If buf is null, then Formatter automatically allocates a StringBuilder to hold the formatted output. The loc parameter specifies a locale. If no locale is specified, the default locale is used. The filename parameter specifies the name of a file that will receive the formatted output. The charset parameter specifies the character set. If no character set is specified, then the default character set is used. The outF parameter specifies a reference to an open file that will receive output. The outStrm parameter specifies a reference to an output stream that will receive output. When using a file, output is also written to the file. Perhaps the most widely used constructor is the first, which has no parameters. It automatically uses the default locale and allocates a StringBuilder to hold the formatted output. The Formatter Methods Formatter defines the methods shown in Table 18-11. Formatting Basics After you have created a Formatter, you can use it to create a formatted string. To do so, use the format( ) method. The most commonly used version is shown here: Formatter format(String fmtString, Object ... args) The fmtSring consists of two types of items. The first type is composed of characters that are simply copied to the output buffer. The second type contains format specifiers that define the way the subsequent arguments are displayed. Method Description void close( ) Closes the invoking Formatter. This causes any resources used by the object to be released. After a Formatter has been closed, it cannot be reused. An attempt to use a closed Formatter results in a FormatterClosedException. void flush( ) Flushes the format buffer. This causes any output currently in the buffer to be written to the destination. This applies mostly to a Formatter tied to a file. Formatter format(String fmtString, Object ... args) Formats the arguments passed via args according to the format specifiers contained in fmtString. Returns the invoking object. Formatter format(Locale loc, String fmtString, Object ... args) Formats the arguments passed via args according to the format specifiers contained in fmtString. The locale specified by loc is used for this format. Returns the invoking object. IOException ioException( ) If the underlying object that is the destination for output throws an IOException, then this exception is returned. Otherwise, null is returned. Locale locale( ) Returns the invoking object’s locale. Appendable out( ) Returns a reference to the underlying object that is the destination for output. String toString( ) Returns a String containing the formatted output. Table 18-11 The Methods Defined by Formatter 549 Part II Chapter 18 550 PART II The Java Library In its simplest form, a format specifier begins with a percent sign followed by the format conversion specifier. All format conversion specifiers consist of a single character. For example, the format specifier for floating-point data is %f. In general, there must be the same number of arguments as there are format specifiers, and the format specifiers and the arguments are matched in order from left to right. For example, consider this fragment: Formatter fmt = new Formatter(); fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6); This sequence creates a Formatter that contains the following string: Formatting with Java is easy 10 98.600000 In this example, the format specifiers, %s, %d, and %f, are replaced with the arguments that follow the format string. Thus, %s is replaced by “with Java”, %d is replaced by 10, and %f is replaced by 98.6. All other characters are simply used as-is. As you might guess, the format specifier %s specifies a string, and %d specifies an integer value. As mentioned earlier, the %f specifies a floating-point value. The format( ) method accepts a wide variety of format specifiers, which are shown in Table 18-12. Notice that many specifiers have both upper- and lowercase forms. When an uppercase specifier is used, then letters are shown in uppercase. Otherwise, the upper- and lowercase specifiers perform the same conversion. It is important to understand that Java type-checks each format specifier against its corresponding argument. If the argument doesn’t match, an IllegalFormatException is thrown. Once you have formatted a string, you can obtain it by calling toString( ). For example, continuing with the preceding example, the following statement obtains the formatted string contained in fmt: String str = fmt.toString(); Of course, if you simply want to display the formatted string, there is no reason to first assign it to a String object. When a Formatter object is passed to println( ), for example, its toString( ) method is automatically called. Here is a short program that puts together all of the pieces, showing how to create and display a formatted string: // A very simple example that uses Formatter. import java.util.*; class FormatDemo { public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6); System.out.println(fmt); fmt.close(); } } java.util Part 2: More Utility Classes Format Specifier Conversion Applied %a %A Floating-point hexadecimal %b %B Boolean %c Character %d Decimal integer %h %H Hash code of the argument %e %E Scientific notation %f Decimal floating-point %g %G Uses %e or %f, whichever is shorter %o Octal integer %n Inserts a newline character %s %S String %t %T Time and date %x %X Integer hexadecimal %% Inserts a % sign 551 Table 18-12 The Format Specifiers One other point: You can obtain a reference to the underlying output buffer by calling out( ). It returns a reference to an Appendable object. Now that you know the general mechanism used to create a formatted string, the remainder of this section discusses in detail each conversion. It also describes various options, such as justification, minimum field width, and precision. Formatting Strings and Characters To format an individual character, use %c. This causes the matching character argument to be output, unmodified. To format a string, use %s. Formatting Numbers To format an integer in decimal format, use %d. To format a floating-point value in decimal format, use %f. To format a floating-point value in scientific notation, use %e. Numbers represented in scientific notation take this general form: x.dddddde+/–yy Part II Chapter 18 552 PART II The Java Library The %g format specifier causes Formatter to use either %f or %e, whichever is shorter. The following program demonstrates the effect of the %g format specifier: // Demonstrate the %g format specifier. import java.util.*; class FormatDemo2 { public static void main(String args[]) { Formatter fmt = new Formatter(); for(double i=1000; i < 1.0e+10; i *= 100) { fmt.format("%g ", i); System.out.println(fmt); } fmt.close(); } } It produces the following output: 1000.000000 1000.000000 100000.000000 1000.000000 100000.000000 1.000000e+07 1000.000000 100000.000000 1.000000e+07 1.000000e+09 You can display integers in octal or hexadecimal format by using %o and %x, respectively. For example, this fragment: fmt.format("Hex: %x, Octal: %o", 196, 196); produces this output: Hex: c4, Octal: 304 You can display floating-point values in hexadecimal format by using %a. The format produced by %a appears a bit strange at first glance. This is because its representation uses a form similar to scientific notation that consists of a significand and an exponent, both in hexadecimal. Here is the general format: 0x1.sigpexp Here, sig contains the fractional portion of the significand and exp contains the exponent. The p indicates the start of the exponent. For example, this call: fmt.format("%a", 123.123); produces this output: 0x1.ec7df3b645a1dp6 Formatting Time and Date One of the more powerful conversion specifiers is %t. It lets you format time and date information. The %t specifier works a bit differently than the others because it requires the use of a suffix to describe the portion and precise format of the time or date desired. The Chapter 18 java.util Part 2: More Utility Classes 553 Suffix Replaced By a Abbreviated weekday name A Full weekday name b Abbreviated month name B Full month name c Standard date and time string formatted as day month date hh::mm:ss tzone year C First two digits of year d Day of month as a decimal (01—31) D month/day/year e Day of month as a decimal (1—31) F year-month-day h Abbreviated month name H Hour (00 to 23) I Hour (01 to 12) j Day of year as a decimal (001 to 366) k Hour (0 to 23) l Hour (1 to 12) L Millisecond (000 to 999) m Month as decimal (01 to 13) M Minute as decimal (00 to 59) N Nanosecond (000000000 to 999999999) p Locale’s equivalent of AM or PM in lowercase Q Milliseconds from 1/1/1970 r hh:mm:ss (12-hour format) R hh:mm (24-hour format) S Seconds (00 to 60) s Seconds from 1/1/1970 UTC T hh:mm:ss (24-hour format) y Year in decimal without century (00 to 99) Y Year in decimal including century (0001 to 9999) z Offset from UTC Z Time zone name Table 18-13 The Time and Date Format Suffixes Part II suffixes are shown in Table 18-13. For example, to display minutes, you would use %tM, where M indicates minutes in a two-character field. The argument corresponding to the %t specifier must be of type Calendar, Date, Long, or long. 554 PART II The Java Library Here is a program that demonstrates several of the formats: // Formatting time and date. import java.util.*; class TimeDateFormat { public static void main(String args[]) { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); // Display standard 12-hour time format. fmt.format("%tr", cal); System.out.println(fmt); fmt.close(); // Display complete time and date information. fmt = new Formatter(); fmt.format("%tc", cal); System.out.println(fmt); fmt.close(); // Display just hour and minute. fmt = new Formatter(); fmt.format("%tl:%tM", cal, cal); System.out.println(fmt); fmt.close(); // Display month by name and number. fmt = new Formatter(); fmt.format("%tB %tb %tm", cal, cal, cal); System.out.println(fmt); fmt.close(); } } Sample output is shown here: 09:17:15 AM Sat Jan 01 09:17:15 CST 2011 9:17 January Jan 01 The %n and %% Specifiers The %n and%% format specifiers differ from the others in that they do not match an argument. Instead, they are simply escape sequences that insert a character into the output sequence. The %n inserts a newline. The %% inserts a percent sign. Neither of these characters can be entered directly into the format string. Of course, you can also use the standard escape sequence \n to embed a newline character. Here is an example that demonstrates the %n and %% format specifiers: // Demonstrate the %n and %% format specifiers. import java.util.*; Chapter 18 java.util Part 2: More Utility Classes 555 class FormatDemo3 { public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("Copying file%nTransfer is %d%% complete", 88); System.out.println(fmt); fmt.close(); } } Copying file Transfer is 88% complete Specifying a Minimum Field Width An integer placed between the % sign and the format conversion code acts as a minimum field-width specifier. This pads the output with spaces to ensure that it reaches a certain minimum length. If the string or number is longer than that minimum, it will still be printed in full. The default padding is done with spaces. If you want to pad with 0’s, place a 0 before the field-width specifier. For example, %05d will pad a number of less than five digits with 0’s so that its total length is five. The field-width specifier can be used with all format specifiers except %n. The following program demonstrates the minimum field-width specifier by applying it to the %f conversion: // Demonstrate a field-width specifier. import java.util.*; class FormatDemo4 { public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("|%f|%n|%12f|%n|%012f|", 10.12345, 10.12345, 10.12345); System.out.println(fmt); fmt.close(); } } This program produces the following output: |10.123450| | 10.123450| |00010.123450| The first line displays the number 10.12345 in its default width. The second line displays that value in a 12-character field. The third line displays the value in a 12-character field, padded with leading zeros. Part II It displays the following output: 556 PART II The Java Library The minimum field-width modifier is often used to produce tables in which the columns line up. For example, the next program produces a table of squares and cubes for the numbers between 1 and 10: // Create a table of squares and cubes. import java.util.*; class FieldWidthDemo { public static void main(String args[]) { Formatter fmt; for(int i=1; i <= 10; i++) { fmt = new Formatter(); fmt.format("%4d %4d %4d", i, i*i, i*i*i); System.out.println(fmt); fmt.close(); } } } Its output is shown here: 1 2 3 4 5 6 7 8 9 10 1 1 4 8 9 27 16 64 25 125 36 216 49 343 64 512 81 729 100 1000 Specifying Precision A precision specifier can be applied to the %f, %e, %g, and %s format specifiers. It follows the minimum field-width specifier (if there is one) and consists of a period followed by an integer. Its exact meaning depends upon the type of data to which it is applied. When you apply the precision specifier to floating-point data using the %f or %e specifiers, it determines the number of decimal places displayed. For example, %10.4f displays a number at least ten characters wide with four decimal places. When using %g, the precision determines the number of significant digits. The default precision is 6. Applied to strings, the precision specifier specifies the maximum field length. For example, %5.7s displays a string of at least five and not exceeding seven characters long. If the string is longer than the maximum field width, the end characters will be truncated. The following program illustrates the precision specifier: // Demonstrate the precision modifier. import java.util.*; Chapter 18 java.util Part 2: More Utility Classes 557 class PrecisionDemo { public static void main(String args[]) { Formatter fmt = new Formatter(); // Format to 2 decimal places in a 16 character field fmt = new Formatter(); fmt.format("%16.2e", 123.1234567); System.out.println(fmt); fmt.close(); // Display at most 15 characters in a string. fmt = new Formatter(); fmt.format("%.15s", "Formatting with Java is now easy."); System.out.println(fmt); fmt.close(); } } It produces the following output: 123.1235 1.23e+02 Formatting with Using the Format Flags Formatter recognizes a set of format flags that lets you control various aspects of a conversion. All format flags are single characters, and a format flag follows the % in a format specification. The flags are shown here: Flag Effect – Left justification # Alternate conversion format 0 Output is padded with zeros rather than spaces space Positive numeric output is preceded by a space + Positive numeric output is preceded by a + sign , Numeric values include grouping separators ( Negative numeric values are enclosed within parentheses Not all flags apply to all format specifiers. The following sections explain each in detail. Part II // Format 4 decimal places. fmt.format("%.4f", 123.1234567); System.out.println(fmt); fmt.close(); 558 PART II The Java Library Justifying Output By default, all output is right-justified. That is, if the field width is larger than the data printed, the data will be placed on the right edge of the field. You can force output to be left-justified by placing a minus sign directly after the %. For instance, %–10.2f left-justifies a floating-point number with two decimal places in a 10-character field. For example, consider this program: // Demonstrate left justification. import java.util.*; class LeftJustify { public static void main(String args[]) { Formatter fmt = new Formatter(); // Right justify by default fmt.format("|%10.2f|", 123.123); System.out.println(fmt); fmt.close(); // Now, left justify. fmt = new Formatter(); fmt.format("|%-10.2f|", 123.123); System.out.println(fmt); fmt.close(); } } It produces the following output: | 123.12| |123.12 | As you can see, the second line is left-justified within a 10-character field. The Space, +, 0, and ( Flags To cause a + sign to be shown before positive numeric values, add the + flag. For example, fmt.format("%+d", 100); creates this string: +100 When creating columns of numbers, it is sometimes useful to output a space before positive values so that positive and negative values line up. To do this, add the space flag. For example, // Demonstrate the space format specifiers. import java.util.*; class FormatDemo5 { public static void main(String args[]) { Chapter 18 java.util Part 2: More Utility Classes 559 Formatter fmt = new Formatter(); fmt.format("% d", -100); System.out.println(fmt); fmt.close(); fmt = new Formatter(); fmt.format("% d", -200); System.out.println(fmt); fmt.close(); fmt = new Formatter(); fmt.format("% d", 200); System.out.println(fmt); fmt.close(); } } The output is shown here: -100 100 -200 200 Notice that the positive values have a leading space, which causes the digits in the column to line up properly. To show negative numeric output inside parentheses, rather than with a leading –, use the ( flag. For example, fmt.format("%(d", -100); creates this string: (100) The 0 flag causes output to be padded with zeros rather than spaces. The Comma Flag When displaying large numbers, it is often useful to add grouping separators, which in English are commas. For example, the value 1234567 is more easily read when formatted as 1,234,567. To add grouping specifiers, use the comma (,) flag. For example, fmt.format("%,.2f", 4356783497.34); creates this string: 4,356,783,497.34 Part II fmt = new Formatter(); fmt.format("% d", 100); System.out.println(fmt); fmt.close(); 560 PART II The Java Library The # Flag The # can be applied to %o, %x, %e, and %f. For %e, and %f, the # ensures that there will be a decimal point even if there are no decimal digits. If you precede the %x format specifier with a #, the hexadecimal number will be printed with a 0x prefix. Preceding the %o specifier with # causes the number to be printed with a leading zero. The Uppercase Option As mentioned earlier, several of the format specifiers have uppercase versions that cause the conversion to use uppercase where appropriate. The following table describes the effect. Specifier Effect %A Causes the hexadecimal digits a through f to be displayed in uppercase as A through F. Also, the prefix 0x is displayed as 0X, and the p will be displayed as P. %B Uppercases the values true and false. %E Causes the e symbol that indicates the exponent to be displayed in uppercase. %G Causes the e symbol that indicates the exponent to be displayed in uppercase. %H Causes the hexadecimal digits a through f to be displayed in uppercase as A through F. %S Uppercases the corresponding string. %T Causes all alphabetical output to be displayed in uppercase. %X Causes the hexadecimal digits a through f to be displayed in uppercase as A through F. Also, the optional prefix 0x is displayed as 0X, if present. For example, this call: fmt.format("%X", 250); creates this string: FA This call: fmt.format("%E", 123.1234); creates this string: 1.231234E+02 Using an Argument Index Formatter includes a very useful feature that lets you specify the argument to which a format specifier applies. Normally, format specifiers and arguments are matched in order, from left to right. That is, the first format specifier matches the first argument, the second format specifier matches the second argument, and so on. However, by using an argument index, you can explicitly control which argument a format specifier matches. Chapter 18 java.util Part 2: More Utility Classes 561 An argument index immediately follows the % in a format specifier. It has the following format: n$ where n is the index of the desired argument, beginning with 1. For example, consider this example: fmt.format("%3$d %1$d %2$d", 10, 20, 30); 30 10 20 In this example, the first format specifier matches 30, the second matches 10, and the third matches 20. Thus, the arguments are used in an order other than strictly left to right. One advantage of argument indexes is that they enable you to reuse an argument without having to specify it twice. For example, consider this line: fmt.format("%d in hex is %1$x", 255); It produces the following string: 255 in hex is ff As you can see, the argument 255 is used by both format specifiers. There is a convenient shorthand called a relative index that enables you to reuse the argument matched by the preceding format specifier. Simply specify < for the argument index. For example, the following call to format( ) produces the same results as the previous example: fmt.format("%d in hex is % getKeys( ) Returns the resource bundle keys as an enumeration of strings. Any parent’s keys are also obtained. Locale getLocale( ) Returns the locale supported by the resource bundle. Table 18-17 The Methods Defined by ResourceBundle java.util Part 2: More Utility Classes Method Description final Object getObject(String k) Returns the object associated with the key passed via k. Throws MissingResourceException if k is not in the resource bundle. final String getString(String k) Returns the string associated with the key passed via k. Throws MissingResourceException if k is not in the resource bundle. Throws ClassCastException if the object associated with k is not a string. final String[ ] getStringArray(String k) Returns the string array associated with the key passed via k. Throws MissingResourceException if k is not in the resource bundle. Throws MissingResourceException if the object associated with k is not a string array. protected abstract Object handleGetObject(String k) Returns the object associated with the key passed via k. Returns null if k is not in the resource bundle. protected Set handleKeySet( ) Returns the resource bundle keys as a set of strings. No parent’s keys are obtained. Also, keys with null values are not obtained. Set keySet( ) Returns the resource bundle keys as a set of strings. Any parent keys are also obtained. protected void setParent(ResourceBundle parent) Sets parent as the parent bundle for the resource bundle. When a key is looked up, the parent will be searched if the key is not found in the invoking resource object. 575 Table 18-17 The Methods Defined by ResourceBundle (continued) There are two subclasses of ResourceBundle. The first is PropertyResourceBundle, which manages resources by using property files. PropertyResourceBundle adds no methods of its own. The second is the abstract class ListResourceBundle, which manages resources in an array of key/value pairs. ListResourceBundle adds the method getContents( ), which all subclasses must implement. It is shown here: protected abstract Object[ ][ ] getContents( ) It returns a two-dimensional array that contains key/value pairs that represent resources. The keys must be strings. The values are typically strings, but can be other types of objects. Here is an example that demonstrates using a resource bundle. The resource bundle has the family name SampleRB. Two resource bundle classes of this family are created by extending ListResourceBundle. The first is called SampleRB, and it is the default bundle (which uses English). It is shown here: import java.util.*; public class SampleRB extends ListResourceBundle { protected Object[][] getContents() { Object[][] resources = new Object[3][2]; Part II Chapter 18 576 PART II The Java Library resources[0][0] = "title"; resources[0][1] = "My Program"; resources[1][0] = "StopText"; resources[1][1] = "Stop"; resources[2][0] = "StartText"; resources[2][1] = "Start"; return resources; } } The second resource bundle, shown next, is called SampleRB_de. It contains the German translation. import java.util.*; // German version. public class SampleRB_de extends ListResourceBundle { protected Object[][] getContents() { Object[][] resources = new Object[3][2]; resources[0][0] = "title"; resources[0][1] = "Mein Programm"; resources[1][0] = "StopText"; resources[1][1] = "Anschlag"; resources[2][0] = "StartText"; resources[2][1] = "Anfang"; return resources; } } The following program demonstrates these two resource bundles by displaying the string associated with each key for both the default (English) version and the German version: // Demonstrate a resource bundle. import java.util.*; class LRBDemo { public static void main(String args[]) { // Load the default bundle. ResourceBundle rd = ResourceBundle.getBundle("SampleRB"); System.out.println("English version: "); System.out.println("String for Title key : " + rd.getString("title")); Chapter 18 java.util Part 2: More Utility Classes 577 System.out.println("String for StopText key: " + rd.getString("StopText")); System.out.println("String for StartText key: " + rd.getString("StartText")); System.out.println("\nGerman version: "); System.out.println("String for Title key : " + rd.getString("title")); System.out.println("String for StopText key: " + rd.getString("StopText")); System.out.println("String for StartText key: " + rd.getString("StartText")); } } The output from the program is shown here: English version: String for Title key : My Program String for StopText key: Stop String for StartText key: Start German String String String version: for Title key : Mein Programm for StopText key: Anschlag for StartText key: Anfang Miscellaneous Utility Classes and Interfaces In addition to the classes already discussed, java.util includes the following classes: EventListenerProxy Extends the EventListener class to allow additional parameters. See Chapter 23 for a discussion of event listeners. EventObject The superclass for all event classes. Events are discussed in Chapter 23. FormattableFlags Defines formatting flags that are used with the Formattable interface. Objects Various methods that operate on objects. (Added by JDK 7.) PropertyPermission Manages property permissions. ServiceLoader Provides a means of finding service providers. UUID Encapsulates and manages Universally Unique Identifiers (UUIDs). Part II // Load the German bundle. rd = ResourceBundle.getBundle("SampleRB", Locale.GERMAN); 578 PART II The Java Library The following interfaces are also packaged in java.util: EventListener Indicates that a class is an event listener. Events are discussed in Chapter 23. Formattable Enables a class to provide custom formatting. The java.util Subpackages Java defines the following subpackages to java.util: • java.util.concurrent • java.util.concurrent.atomic • java.util.concurrent.locks • java.util.jar • java.util.logging • java.util.prefs • java.util.regex • java.util.spi • java.util.zip Each is briefly examined here. java.util.concurrent, java.util.concurrent.atomic, and java.util.concurrent.locks The java.util.concurrent package along with its two subpackages, java.util.concurrent.atomic and java.util.concurrent.locks, support concurrent programming. These packages provide a high-performance alternative to using Java’s built-in synchronization features when threadsafe operation is required. Beginning with JDK 7, java.util.concurrent also provides the Fork/Join Framework. These packages are examined in detail in Chapter 27. java.util.jar The java.util.jar package provides the ability to read and write Java Archive (JAR) files. java.util.logging The java.util.logging package provides support for program activity logs, which can be used to record program actions, and to help find and debug problems. java.util.prefs The java.util.prefs package provides support for user preferences. It is typically used to support program configuration. Chapter 18 java.util Part 2: More Utility Classes 579 java.util.regex The java.util.regex package provides support for regular expression handling. It is described in detail in Chapter 28. java.util.spi The java.util.spi package provides support for service providers. The java.util.zip package provides the ability to read and write files in the popular ZIP and GZIP formats. Both ZIP and GZIP input and output streams are available. Part II java.util.zip This page intentionally left blank CHAPTER 19 Input/Output: Exploring java.io This chapter explores java.io, which provides support for I/O operations. Chapter 13 presented an overview of Java’s I/O system, including basic techniques for reading and writing files, handling I/O exceptions, and closing a file. Here, we will examine the Java I/O system in greater detail. As all programmers learn early on, most programs cannot accomplish their goals without accessing external data. Data is retrieved from an input source. The results of a program are sent to an output destination. In Java, these sources or destinations are defined very broadly. For example, a network connection, memory buffer, or disk file can be manipulated by the Java I/O classes. Although physically different, these devices are all handled by the same abstraction: the stream. A stream, as explained in Chapter 13, is a logical entity that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices they are linked to differ. NOTE The stream-based I/O system packaged in java.io and described in this chapter has been part of Java since its original release and is widely used. However, beginning with version 1.4, a second I/O system was added to Java. It is called NIO (which was originally an acronym for New I/O). NIO is packaged in java.nio and its subpackages. With the release of JDK 7, the capabilities of the NIO system have been greatly expanded and its use is expected to grow. The NIO system is described in Chapter 20. The I/O Classes and Interfaces The I/O classes defined by java.io are listed here: BufferedInputStream FileWriter PipedOutputStream BufferedOutputStream FilterInputStream PipedReader BufferedReader FilterOutputStream PipedWriter 581 582 PART II The Java Library BufferedWriter FilterReader PrintStream ByteArrayInputStream FilterWriter PrintWriter ByteArrayOutputStream InputStream PushbackInputStream CharArrayReader InputStreamReader PushbackReader CharArrayWriter LineNumberReader RandomAccessFile Console ObjectInputStream Reader DataInputStream ObjectInputStream.GetField SequenceInputStream DataOutputStream ObjectOutputStream SerializablePermission File ObjectOutputStream.PutField StreamTokenizer FileDescriptor ObjectStreamClass StringReader FileInputStream ObjectStreamField StringWriter FileOutputStream OutputStream Writer FilePermission OutputStreamWriter FileReader PipedInputStream The java.io package also contains two deprecated classes that are not shown in the preceding table: LineNumberInputStream and StringBufferInputStream. These classes should not be used for new code. The following interfaces are defined by java.io: Closeable FileFilter ObjectInputValidation DataInput FilenameFilter ObjectOutput DataOutput Flushable ObjectStreamConstants Externalizable ObjectInput Serializable As you can see, there are many classes and interfaces in the java.io package. These include byte and character streams, and object serialization (the storage and retrieval of objects). This chapter examines several of the most commonly used I/O components. We begin our discussion with one of the most distinctive I/O classes: File. File Although most of the classes defined by java.io operate on streams, the File class does not. It deals directly with files and the file system. That is, the File class does not specify how information is retrieved from or stored in files; it describes the properties of a file itself. A File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies. NOTE The Path interface and Files class, added by JDK 7 to the NIO system, offer a powerful alternative to File in many cases. See Chapter 20 for details. Chapter 19 Input/Output: Exploring java.io 583 File(String directoryPath) File(String directoryPath, String filename) File(File dirObj, String filename) File(URI uriObj) Here, directoryPath is the path name of the file; filename is the name of the file or subdirectory; dirObj is a File object that specifies a directory; and uriObj is a URI object that describes a file. The following example creates three files: f1, f2, and f3. The first File object is constructed with a directory path as the only argument. The second includes two arguments—the path and the filename. The third includes the file path assigned to f1 and a filename; f3 refers to the same file as f2. File f1 = new File("/"); File f2 = new File("/","autoexec.bat"); File f3 = new File(f1,"autoexec.bat"); NOTE Java does the right thing with path separators between UNIX and Windows conventions. If you use a forward slash (/) on a Windows version of Java, the path will still resolve correctly. Remember, if you are using the Windows convention of a backslash character (\), you will need to use its escape sequence (\\) within a string. File defines many methods that obtain the standard properties of a File object. For example, getName( ) returns the name of the file; getParent( ) returns the name of the parent directory; and exists( ) returns true if the file exists, false if it does not. The File class, however, is not symmetrical. There are a few methods that allow you to examine the properties of a simple file object, but no corresponding function exists to change those attributes. The following example demonstrates several of the File methods. It assumes that a directory called java exists off the root directory and that it contains a file called COPYRIGHT. // Demonstrate File. import java.io.File; class FileDemo { static void p(String s) { System.out.println(s); } public static void main(String args[]) { File f1 = new File("/java/COPYRIGHT"); Part II Files are a primary source and destination for data within many programs. Although there are severe restrictions on their use within applets for security reasons, files are still a central resource for storing persistent and shared information. A directory in Java is treated simply as a File with one additional property—a list of filenames that can be examined by the list( ) method. The following constructors can be used to create File objects: 584 PART II The Java Library p("File Name: " + f1.getName()); p("Path: " + f1.getPath()); p("Abs Path: " + f1.getAbsolutePath()); p("Parent: " + f1.getParent()); p(f1.exists() ? "exists" : "does not exist"); p(f1.canWrite() ? "is writeable" : "is not writeable"); p(f1.canRead() ? "is readable" : "is not readable"); p("is " + (f1.isDirectory() ? "" : "not" + " a directory")); p(f1.isFile() ? "is normal file" : "might be a named pipe"); p(f1.isAbsolute() ? "is absolute" : "is not absolute"); p("File last modified: " + f1.lastModified()); p("File size: " + f1.length() + " Bytes"); } } This program will produce output similar to this: File Name: COPYRIGHT Path: \java\COPYRIGHT Abs Path: C:\java\COPYRIGHT Parent: \java exists is writeable is readable is not a directory is normal file is not absolute File last modified: 1282832030047 File size: 695 Bytes Most of the File methods are self-explanatory. isFile( ) and isAbsolute( ) are not. isFile( ) returns true if called on a file and false if called on a directory. Also, isFile( ) returns false for some special files, such as device drivers and named pipes, so this method can be used to make sure the file will behave as a file. The isAbsolute( ) method returns true if the file has an absolute path and false if its path is relative. File also includes two useful utility methods. The first is renameTo( ), shown here: boolean renameTo(File newName) Here, the filename specified by newName becomes the new name of the invoking File object. It will return true upon success and false if the file cannot be renamed (if you attempt to rename a file so that it uses an existing filename, for example). The second utility method is delete( ), which deletes the disk file represented by the path of the invoking File object. It is shown here: boolean delete( ) You can also use delete( ) to delete a directory if the directory is empty. delete( ) returns true if it deletes the file and false if the file cannot be removed. Here are some other File methods that you will find helpful: Input/Output: Exploring java.io Method Description void deleteOnExit( ) Removes the file associated with the invoking object when the Java Virtual Machine terminates. long getFreeSpace( ) Returns the number of free bytes of storage available on the partition associated with the invoking object. long getTotalSpace( ) Returns the storage capacity of the partition associated with the invoking object. long getUsableSpace( ) Returns the number of usable free bytes of storage available on the partition associated with the invoking object. boolean isHidden( ) Returns true if the invoking file is hidden. Returns false otherwise. boolean setLastModified(long millisec) Sets the time stamp on the invoking file to that specified by millisec, which is the number of milliseconds from January 1, 1970, Coordinated Universal Time (UTC). boolean setReadOnly( ) Sets the invoking file to read-only. 585 Methods also exist to mark files as readable, writable, and executable. Because File implements the Comparable interface, the method compareTo( ) is also supported. JDK 7 adds a new method to File called toPath( ), which is shown here: Path toPath( ) toPath( ) returns a Path object that represents the file encapsulated by the invoking File object. (In other words, toPath( ) converts a File into a Path.) Path is a new interface added by JDK 7. It is packaged in java.nio.file and is part of NIO. Thus, toPath( ) forms a bridge between the older File class and the new Path interface. (See Chapter 20 for a discussion of Path.) Directories A directory is a File that contains a list of other files and directories. When you create a File object that is a directory, the isDirectory( ) method will return true. In this case, you can call list( ) on that object to extract the list of other files and directories inside. It has two forms. The first is shown here: String[ ] list( ) The list of files is returned in an array of String objects. The program shown here illustrates how to use list( ) to examine the contents of a directory: // Using directories. import java.io.File; class DirList { public static void main(String args[]) { String dirname = "/java"; File f1 = new File(dirname); Part II Chapter 19 586 PART II The Java Library if (f1.isDirectory()) { System.out.println("Directory of " + dirname); String s[] = f1.list(); for (int i=0; i < s.length; i++) { File f = new File(dirname + "/" + s[i]); if (f.isDirectory()) { System.out.println(s[i] + " is a directory"); } else { System.out.println(s[i] + " is a file"); } } } else { System.out.println(dirname + " is not a directory"); } } } Here is sample output from the program. (Of course, the output you see will be different, based on what is in the directory.) Directory of /java bin is a directory lib is a directory demo is a directory COPYRIGHT is a file README is a file index.html is a file include is a directory src.zip is a file src is a directory Using FilenameFilter You will often want to limit the number of files returned by the list( ) method to include only those files that match a certain filename pattern, or filter. To do this, you must use a second form of list( ), shown here: String[ ] list(FilenameFilter FFObj) In this form, FFObj is an object of a class that implements the FilenameFilter interface. FilenameFilter defines only a single method, accept( ), which is called once for each file in a list. Its general form is given here: boolean accept(File directory, String filename) The accept( ) method returns true for files in the directory specified by directory that should be included in the list (that is, those that match the filename argument) and returns false for those files that should be excluded. The OnlyExt class, shown next, implements FilenameFilter. It will be used to modify the preceding program to restrict the visibility of the filenames returned by list( ) to files with names that end in the file extension specified when the object is constructed. Chapter 19 Input/Output: Exploring java.io 587 import java.io.*; public class OnlyExt implements FilenameFilter { String ext; public boolean accept(File dir, String name) { return name.endsWith(ext); } } The modified directory listing program is shown here. Now it will only display files that use the .html extension. // Directory of .HTML files. import java.io.*; class DirListOnly { public static void main(String args[]) { String dirname = "/java"; File f1 = new File(dirname); FilenameFilter only = new OnlyExt("html"); String s[] = f1.list(only); for (int i=0; i < s.length; i++) { System.out.println(s[i]); } } } The listFiles( ) Alternative There is a variation to the list( ) method, called listFiles( ), which you might find useful. The signatures for listFiles( ) are shown here: File[ ] listFiles( ) File[ ] listFiles(FilenameFilter FFObj) File[ ] listFiles(FileFilter FObj) These methods return the file list as an array of File objects instead of strings. The first method returns all files, and the second returns those files that satisfy the specified FilenameFilter. Aside from returning an array of File objects, these two versions of listFiles( ) work like their equivalent list( ) methods. The third version of listFiles( ) returns those files with path names that satisfy the specified FileFilter. FileFilter defines only a single method, accept( ), which is called once for each file in a list. Its general form is given here: boolean accept(File path) The accept( ) method returns true for files that should be included in the list (that is, those that match the path argument) and false for those that should be excluded. Part II public OnlyExt(String ext) { this.ext = "." + ext; } 588 PART II The Java Library Creating Directories Another two useful File utility methods are mkdir( ) and mkdirs( ). The mkdir( ) method creates a directory, returning true on success and false on failure. Failure can occur for various reasons, such as the path specified in the File object already exists, or the directory cannot be created because the entire path does not exist yet. To create a directory for which no path exists, use the mkdirs( ) method. It creates both a directory and all the parents of the directory. The AutoCloseable, Closeable, and Flushable Interfaces There are three interfaces that are quite important to the stream classes. Two are Closeable and Flushable. They are defined in java.io and were added by JDK 5. The third, AutoCloseable, is new, being added by JDK 7. It is packaged in java.lang. AutoCloseable provides support for JDK 7’s new try-with-resources statement, which automates the process of closing a resource. (See Chapter 13.) Only objects of classes that implement AutoCloseable can be managed by try-with-resources. AutoCloseable is discussed in Chapter 16, but it is reviewed here for convenience. The AutoCloseable interface defines only the close( ) method: void close( ) throws Exception This method closes the invoking object, releasing any resources that it may hold. It is called automatically at the end of a try-with-resources statement, thus eliminating the need to explicitly call close( ). Because this interface is implemented by all of the I/O classes that open a stream, all such streams can be automatically closed by a try-with-resources statement. Automatically closing a stream ensures that it is properly closed when it is no longer needed, thus preventing memory leaks and other problems. The Closeable interface also defines the close( ) method. Objects of a class that implement Closeable can be closed. Beginning with JDK 7, Closeable extends AutoCloseable. Therefore, in JDK 7, any class that implements Closeable also implements AutoCloseable. Objects of a class that implements Flushable can force buffered output to be written to the stream to which the object is attached. It defines the flush( ) method, shown here: void flush( ) throws IOException Flushing a stream typically causes buffered output to be physically written to the underlying device. This interface is implemented by all of the I/O classes that write to a stream. I/O Exceptions Two exceptions play an important role in I/O handling. The first is IOException. As it relates to most of the I/O classes described in this chapter, if an I/O error occurs, an IOException is thrown. In many cases, if a file cannot be opened, a FileNotFoundException is thrown. FileNotFoundException is a subclass of IOException, so both can be caught with a single catch that catches IOException. For brevity, this is the approach used by most of the sample code in this chapter. However, in your own applications, you might find it useful to catch each exception separately. Chapter 19 Input/Output: Exploring java.io 589 Two Ways to Close a Stream In general, a stream must be closed when it is no longer needed. Failure to do so can lead to memory leaks and resource starvation. The techniques used to close a stream were described in Chapter 13, but because of their importance, they warrant a brief review here before the stream classes are examined. Beginning with JDK 7, there are two basic ways in which you can close a stream. The first is to explicitly call close( ) on the stream. This is the traditional approach that has been used since the original release of Java. With this approach, close( ) is typically called within a finally block. Thus, a simplified skeleton for the traditional approach is shown here: try { // open and access file } catch( I/O-exception) { // ... } finally { // close the file } This general technique (or variation thereof) is common in code that predates JDK 7. The second approach to closing a stream is to automate the process by using the new try-with-resources statement that was added by JDK 7. The try-with-resources statement is an enhanced form of try that has the following form: try (resource-specification) { // use the resource } Here, resource-specification is a statement or statements that declares and initializes a resource, such as a file or other stream-related resource. It consists of a variable declaration in which the variable is initialized with a reference to the object being managed. When the try block ends, the resource is automatically released. In the case of a file, this means that the file is automatically closed. Thus, there is no need to call close( ) explicitly. Here are three key points about the try-with-resources statement: • Resources managed by try-with-resources must be objects of classes that implement AutoCloseable. • The resource declared in the try is implicitly final. • You can manage more than one resource by separating each declaration by a semicolon. Part II Another exception class that is sometimes important when performing I/O is SecurityException. As explained in Chapter 13, in situations in which a security manager is present, several of the file classes will throw a SecurityException if a security violation occurs when attempting to open a file. By default, applications run via java do not use a security manager. For that reason, the I/O examples in this book do not need to watch for a possible SecurityException. However, applets will use the security manager provided by the browser, and file I/O performed by an applet could generate a SecurityException. In such a case, you will need to handle this exception. 590 PART II The Java Library Also, remember that the scope of the declared resource is limited to the try-with-resources statement. The principal advantage of try-with-resources is that the resource (in this case, a stream) is closed automatically when the try block ends. Thus, it is not possible to forget to close the stream, for example. The try-with-resources approach also typically results in shorter, clearer, easier-to-maintain source code. Because of its advantages, try-with-resources is expected to be used extensively in new code. As a result, most of the code in this chapter (and in this book) will use it. However, because there are millions of lines of pre-JDK 7 code, it is important for all programmers to also be familiar with the traditional approach to closing a stream. For example, you will quite likely have to work on legacy code that uses the traditional approach or in an environment that uses a version of Java that predates JDK 7. There may also be times when the automated approach is not appropriate because of other aspects of your code. For this reason, a few I/O examples in this book will demonstrate the traditional approach so you can see it in action. One last point: The examples that use try-with-resources must be compiled by a JDK 7 or later. They won’t work with an older compiler. The examples that use the traditional approach can be compiled by older versions of Java. REMEMBER Because try-with-resources streamlines the process of releasing a resource and eliminates the possibility of accidentally forgetting to release a resource, it is the approach recommended for new code when its use is appropriate. The Stream Classes Java’s stream-based I/O is built upon four abstract classes: InputStream, OutputStream, Reader, and Writer. These classes were briefly discussed in Chapter 13. They are used to create several concrete stream subclasses. Although your programs perform their I/O operations through concrete subclasses, the top-level classes define the basic functionality common to all stream classes. InputStream and OutputStream are designed for byte streams. Reader and Writer are designed for character streams. The byte stream classes and the character stream classes form separate hierarchies. In general, you should use the character stream classes when working with characters or strings and use the byte stream classes when working with bytes or other binary objects. In the remainder of this chapter, both the byte- and character-oriented streams are examined. The Byte Streams The byte stream classes provide a rich environment for handling byte-oriented I/O. A byte stream can be used with any type of object, including binary data. This versatility makes byte streams important to many types of programs. Since the byte stream classes are topped by InputStream and OutputStream, our discussion begins with them. Chapter 19 Input/Output: Exploring java.io 591 InputStream InputStream is an abstract class that defines Java’s model of streaming byte input. It implements the AutoCloseable and Closeable interfaces. Most of the methods in this class will throw an IOException when an I/O error occurs. (The exceptions are mark( ) and markSupported( ).) Table 19-1 shows the methods in InputStream. OutputStream OutputStream is an abstract class that defines streaming byte output. It implements the AutoCloseable, Closeable, and Flushable interfaces. Most of the methods in this class return void and throw an IOException in the case of I/O errors. Table 19-2 shows the methods in OutputStream. Method Description int available( ) Returns the number of bytes of input currently available for reading. void close( ) Closes the input source. Further read attempts will generate an IOException. void mark(int numBytes) Places a mark at the current point in the input stream that will remain valid until numBytes bytes are read. boolean markSupported( ) Returns true if mark( ) / reset( ) are supported by the invoking stream. int read( ) Returns an integer representation of the next available byte of input. –1 is returned when the end of the file is encountered. int read(byte buffer[ ]) Attempts to read up to buffer.length bytes into buffer and returns the actual number of bytes that were successfully read. –1 is returned when the end of the file is encountered. int read(byte buffer[ ], int offset, int numBytes) Attempts to read up to numBytes bytes into buffer starting at buffer[offset], returning the number of bytes successfully read. –1 is returned when the end of the file is encountered. void reset( ) Resets the input pointer to the previously set mark. long skip(long numBytes) Ignores (that is, skips) numBytes bytes of input, returning the number of bytes actually ignored. Table 19-1 The Methods Defined by InputStream Part II NOTE Most of the methods described in Table 19-1 are implemented by the subclasses of InputStream. The mark( ) and reset( ) methods are exceptions; notice their use, or lack thereof, by each subclass in the discussions that follow. 592 PART II The Java Library Method Description void close( ) Closes the output stream. Further write attempts will generate an IOException. void flush( ) Finalizes the output state so that any buffers are cleared. That is, it flushes the output buffers. void write(int b) Writes a single byte to an output stream. Note that the parameter is an int, which allows you to call write( ) with an expression without having to cast it back to byte. void write(byte buffer[ ]) Writes a complete array of bytes to an output stream. void write(byte buffer[ ], int offset, int numBytes) Writes a subrange of numBytes bytes from the array buffer, beginning at buffer[offset]. Table 19-2 The Methods Defined by OutputStream FileInputStream The FileInputStream class creates an InputStream that you can use to read bytes from a file. Two commonly used constructors are shown here: FileInputStream(String filePath) FileInputStream(File fileObj) Either can throw a FileNotFoundException. Here, filePath is the full path name of a file, and fileObj is a File object that describes the file. The following example creates two FileInputStreams that use the same disk file and each of the two constructors: FileInputStream f0 = new FileInputStream("/autoexec.bat") File f = new File("/autoexec.bat"); FileInputStream f1 = new FileInputStream(f); Although the first constructor is probably more commonly used, the second allows you to closely examine the file using the File methods, before attaching it to an input stream. When a FileInputStream is created, it is also opened for reading. FileInputStream overrides six of the methods in the abstract class InputStream. The mark( ) and reset( ) methods are not overridden, and any attempt to use reset( ) on a FileInputStream will generate an IOException. The next example shows how to read a single byte, an array of bytes, and a subrange of an array of bytes. It also illustrates how to use available( ) to determine the number of bytes remaining and how to use the skip( ) method to skip over unwanted bytes. The program reads its own source file, which must be in the current directory. Notice that it uses JDK 7’s new try-with-resources statement to automatically close the file when it is no longer needed. // Demonstrate FileInputStream. // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; Chapter 19 Input/Output: Exploring java.io 593 class FileInputStreamDemo { public static void main(String args[]) { int size; // Use try-with-resources to close the stream. try ( FileInputStream f = new FileInputStream("FileInputStreamDemo.java") ) { int n = size/40; System.out.println("First " + n + " bytes of the file one read() at a time"); for (int i=0; i < n; i++) { System.out.print((char) f.read()); } System.out.println("\nStill Available: " + f.available()); System.out.println("Reading the next " + n + " with one read(b[])"); byte b[] = new byte[n]; if (f.read(b) != n) { System.err.println("couldn’t read " + n + " bytes."); } System.out.println(new String(b, 0, n)); System.out.println("\nStill Available: " + (size = f.available())); System.out.println("Skipping half of remaining bytes with skip()"); f.skip(size/2); System.out.println("Still Available: " + f.available()); System.out.println("Reading " + n/2 + " into the end of array"); if (f.read(b, n/2, n/2) != n/2) { System.err.println("couldn’t read " + n/2 + " bytes."); } System.out.println(new String(b, 0, b.length)); System.out.println("\nStill Available: " + f.available()); } catch(IOException e) { System.out.println("I/O Error: " + e); } } } Here is the output produced by this program: Total Available Bytes: 1785 First 44 bytes of the file one read() at a time // Demonstrate FileInputStream. // This pr Still Available: 1741 Part II System.out.println("Total Available Bytes: " + (size = f.available())); 594 PART II The Java Library Reading the next 44 with one read(b[]) ogram uses try-with-resources. It requires J Still Available: 1697 Skipping half of remaining bytes with skip() Still Available: 849 Reading 22 into the end of array ogram uses try-with-rebyte[n]; if ( Still Available: 827 This somewhat contrived example demonstrates how to read three ways, to skip input, and to inspect the amount of data available on a stream. NOTE The preceding example and the other examples in this chapter handle any I/O exceptions that might occur as described in Chapter 13. See Chapter 13 for details and alternatives. FileOutputStream FileOutputStream creates an OutputStream that you can use to write bytes to a file. It implements the AutoCloseable, Closeable, and Flushable interfaces. Four of its constructors are shown here: FileOutputStream(String filePath) FileOutputStream(File fileObj) FileOutputStream(String filePath, boolean append) FileOutputStream(File fileObj, boolean append) They can throw a FileNotFoundException. Here, filePath is the full path name of a file, and fileObj is a File object that describes the file. If append is true, the file is opened in append mode. Creation of a FileOutputStream is not dependent on the file already existing. FileOutputStream will create the file before opening it for output when you create the object. In the case where you attempt to open a read-only file, an exception will be thrown. The following example creates a sample buffer of bytes by first making a String and then using the getBytes( ) method to extract the byte array equivalent. It then creates three files. The first, file1.txt, will contain every other byte from the sample. The second, file2.txt, will contain the entire set of bytes. The third and last, file3.txt, will contain only the last quarter. // Demonstrate FileOutputStream. // This program uses the traditional approach to closing a file. import java.io.*; class FileOutputStreamDemo { public static void main(String args[]) { String source = "Now is the time for all good men\n" + " to come to the aid of their country\n" + " and pay their due taxes."; Chapter 19 Input/Output: Exploring java.io 595 byte buf[] = source.getBytes(); FileOutputStream f0 = null; FileOutputStream f1 = null; FileOutputStream f2 = null; // write to first file for (int i=0; i < buf.length; i += 2) f0.write(buf[i]); // write to second file f1.write(buf); // write to third file f2.write(buf, buf.length-buf.length/4, buf.length/4); } catch(IOException e) { System.out.println("An I/O Error Occurred"); } finally { try { if(f0 != null) f0.close(); } catch(IOException e) { System.out.println("Error Closing file1.txt"); } try { if(f1 != null) f1.close(); } catch(IOException e) { System.out.println("Error Closing file2.txt"); } try { if(f2 != null) f2.close(); } catch(IOException e) { System.out.println("Error Closing file3.txt"); } } } } Here are the contents of each file after running this program. First, file1.txt: Nwi h iefralgo e t oet h i ftercuty n a hi u ae. Next, file2.txt: Now is the time for all good men to come to the aid of their country and pay their due taxes. Finally, file3.txt: nd pay their due taxes. Part II try { f0 = new FileOutputStream("file1.txt"); f1 = new FileOutputStream("file2.txt"); f2 = new FileOutputStream("file3.txt"); 596 PART II The Java Library As the comment at the top of the program states, the preceding program shows an example that uses the traditional approach to closing a file when it is no longer needed. This approach is required by all versions of Java prior to JDK 7 and is widely used in legacy code. As you can see, quite a bit of rather awkward code is required to explicitly call close( ) because each call could generate an IOException if the close operation fails. This program can be substantially improved by using the new try-with-resources statement. For comparison, here is the revised version. Notice that it is much shorter and streamlined: // Demonstrate FileOutputStream. // This version uses try-with-resources. It requires JDK 7 or later. import java.io.*; class FileOutputStreamDemo { public static void main(String args[]) { String source = "Now is the time for all good men\n" + " to come to the aid of their country\n" + " and pay their due taxes."; byte buf[] = source.getBytes(); // Use try-with-resources to close the files. try (FileOutputStream f0 = new FileOutputStream("file1.txt"); FileOutputStream f1 = new FileOutputStream("file2.txt"); FileOutputStream f2 = new FileOutputStream("file3.txt") ) { // write to first file for (int i=0; i < buf.length; i += 2) f0.write(buf[i]); // write to second file f1.write(buf); // write to third file f2.write(buf, buf.length-buf.length/4, buf.length/4); } catch(IOException e) { System.out.println("An I/O Error Occurred"); } } } ByteArrayInputStream ByteArrayInputStream is an implementation of an input stream that uses a byte array as the source. This class has two constructors, each of which requires a byte array to provide the data source: ByteArrayInputStream(byte array [ ]) ByteArrayInputStream(byte array [ ], int start, int numBytes) Here, array is the input source. The second constructor creates an InputStream from a subset of the byte array that begins with the character at the index specified by start and is numBytes long. The close( ) method has no effect on a ByteArrayInputStream. Therefore, it is not necessary to call close( ) on a ByteArrayInputStream, but doing so is not an error. Chapter 19 Input/Output: Exploring java.io 597 The following example creates a pair of ByteArrayInputStreams, initializing them with the byte representation of the alphabet: // Demonstrate ByteArrayInputStream. import java.io.*; ByteArrayInputStream input1 = new ByteArrayInputStream(b); ByteArrayInputStream input2 = new ByteArrayInputStream(b,0,3); } } The input1 object contains the entire lowercase alphabet, whereas input2 contains only the first three letters. A ByteArrayInputStream implements both mark( ) and reset( ). However, if mark( ) has not been called, then reset( ) sets the stream pointer to the start of the stream—which, in this case, is the start of the byte array passed to the constructor. The next example shows how to use the reset( ) method to read the same input twice. In this case, the program reads and prints the letters "abc" once in lowercase and then again in uppercase. import java.io.*; class ByteArrayInputStreamReset { public static void main(String args[]) { String tmp = "abc"; byte b[] = tmp.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(b); for (int i=0; i<2; i++) { int c; while ((c = in.read()) != -1) { if (i == 0) { System.out.print((char) c); } else { System.out.print(Character.toUpperCase((char) c)); } } System.out.println(); in.reset(); } } } This example first reads each character from the stream and prints it as-is in lowercase. It then resets the stream and begins reading again, this time converting each character to uppercase before printing. Here’s the output: abc ABC Part II class ByteArrayInputStreamDemo { public static void main(String args[]) { String tmp = "abcdefghijklmnopqrstuvwxyz"; byte b[] = tmp.getBytes(); 598 PART II The Java Library ByteArrayOutputStream ByteArrayOutputStream is an implementation of an output stream that uses a byte array as the destination. ByteArrayOutputStream has two constructors, shown here: ByteArrayOutputStream( ) ByteArrayOutputStream(int numBytes) In the first form, a buffer of 32 bytes is created. In the second, a buffer is created with a size equal to that specified by numBytes. The buffer is held in the protected buf field of ByteArrayOutputStream. The buffer size will be increased automatically, if needed. The number of bytes held by the buffer is contained in the protected count field of ByteArrayOutputStream. The close( ) method has no effect on a ByteArrayOutputStream. Therefore, it is not necessary to call close( ) on a ByteArrayOutputStream, but doing so is not an error. The following example demonstrates ByteArrayOutputStream: // Demonstrate ByteArrayOutputStream. // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; class ByteArrayOutputStreamDemo { public static void main(String args[]) { ByteArrayOutputStream f = new ByteArrayOutputStream(); String s = "This should end up in the array"; byte buf[] = s.getBytes(); try { f.write(buf); } catch(IOException e) { System.out.println("Error Writing to Buffer"); return; } System.out.println("Buffer as a string"); System.out.println(f.toString()); System.out.println("Into array"); byte b[] = f.toByteArray(); for (int i=0; i streamEnum) Operationally, the class fulfills read requests from the first InputStream until it runs out and then switches over to the second one. In the case of an Enumeration, it will continue through all of the InputStreams until the end of the last one is reached. When the end of each file is reached, its associated stream is closed. Closing the stream created by SequenceInputStream causes all unclosed streams to be closed. Here is a simple example that uses a SequenceInputStream to output the contents of two files. For demonstration purposes, this program uses the traditional technique used to Part II Chapter 19 604 PART II The Java Library close a file. As an exercise, you might want to try changing it to use the try-with-resources statement. // Demonstrate sequenced input. // This program uses the traditional approach to closing a file. import java.io.*; import java.util.*; class InputStreamEnumerator implements Enumeration { private Enumeration files; public InputStreamEnumerator(Vector files) { this.files = files.elements(); } public boolean hasMoreElements() { return files.hasMoreElements(); } public FileInputStream nextElement() { try { return new FileInputStream(files.nextElement().toString()); } catch (IOException e) { return null; } } } class SequenceInputStreamDemo { public static void main(String args[]) { int c; Vector files = new Vector(); files.addElement("file1.txt"); files.addElement("file2.txt"); files.addElement("file3.txt"); InputStreamEnumerator ise = new InputStreamEnumerator(files); InputStream input = new SequenceInputStream(ise); try { while ((c = input.read()) != -1) System.out.print((char) c); } catch(NullPointerException e) { System.out.println("Error Opening File."); } catch(IOException e) { System.out.println("I/O Error: " + e); } finally { try { input.close(); } catch(IOException e) { System.out.println("Error Closing SequenceInputStream"); Chapter 19 Input/Output: Exploring java.io 605 } } } This example creates a Vector and then adds three filenames to it. It passes that vector of names to the InputStreamEnumerator class, which is designed to provide a wrapper on the vector where the elements returned are not the filenames but, rather, open FileInputStreams on those names. The SequenceInputStream opens each file in turn, and this example prints the contents of the files. Notice in nextElement( ) that if a file cannot be opened, null is returned. This results in a NullPointerException, which is caught in main( ). PrintStream The PrintStream class provides all of the output capabilities we have been using from the System file handle, System.out, since the beginning of the book. This makes PrintStream one of Java’s most often used classes. It implements the Appendable, AutoCloseable, Closeable, and Flushable interfaces. PrintStream defines several constructors. The ones shown next have been specified from the start: PrintStream(OutputStream outputStream) PrintStream(OutputStream outputStream, boolean flushOnNewline) PrintStream(OutputStream outputStream, boolean flushOnNewline, String charSet) throws UnsupportedEncodingException Here, outputStream specifies an open OutputStream that will receive output. The flushOnNewline parameter controls whether the output buffer is automatically flushed every time a newline (\n) character or a byte array is written or when println( ) is called. If flushOnNewline is true, flushing automatically takes place. If it is false, flushing is not automatic. The first constructor does not automatically flush. You can specify a character encoding by passing its name in charSet. The next set of constructors gives you an easy way to construct a PrintStream that writes its output to a file: PrintStream(File outputFile) throws FileNotFoundException PrintStream(File outputFile, String charSet) throws FileNotFoundException, UnsupportedEncodingException PrintStream(String outputFileName) throws FileNotFoundException PrintStream(String outputFileName, String charSet) throws FileNotFoundException, UnsupportedEncodingException These allow a PrintStream to be created from a File object or by specifying the name of a file. In either case, the file is automatically created. Any preexisting file by the same name is destroyed. Once created, the PrintStream object directs all output to the specified file. You can specify a character encoding by passing its name in charSet. Part II } 606 PART II The Java Library NOTE If a security manager is present, some PrintStream constructors will throw a SecurityException if a security violation occurs. PrintStream supports the print( ) and println( ) methods for all types, including Object. If an argument is not a primitive type, the PrintStream methods will call the object’s toString( ) method and then display the result. Somewhat recently (with the release of JDK 5), the printf( ) method was added to PrintStream. It allows you to specify the precise format of the data to be written. The printf( ) method uses the Formatter class (described in Chapter 18) to format data. It then writes this data to the invoking stream. Although formatting can be done manually, by using Formatter directly, printf( ) streamlines the process. It also parallels the C/C++ printf( ) function, which makes it easy to convert existing C/C++ code into Java. Frankly, printf( ) was a much welcome addition to the Java API because it greatly simplified the output of formatted data to the console. The printf( ) method has the following general forms: PrintStream printf(String fmtString, Object … args) PrintStream printf(Locale loc, String fmtString, Object … args) The first version writes args to standard output in the format specified by fmtString, using the default locale. The second lets you specify a locale. Both return the invoking PrintStream. In general, printf( ) works in a manner similar to the format( ) method specified by Formatter. The fmtString consists of two types of items. The first type is composed of characters that are simply copied to the output buffer. The second type contains format specifiers that define the way the subsequent arguments, specified by args, are displayed. For complete information on formatting output, including a description of the format specifiers, see the Formatter class in Chapter 18. Because System.out is a PrintStream, you can call printf( ) on System.out. Thus, printf( ) can be used in place of println( ) when writing to the console whenever formatted output is desired. For example, the following program uses printf( ) to output numeric values in various formats. Prior to JDK 5, such formatting required a bit of work. With the addition of printf( ), this is now an easy task. // Demonstrate printf(). class PrintfDemo { public static void main(String args[]) { System.out.println("Here are some numeric values " + "in different formats.\n"); System.out.printf("Various integer formats: "); System.out.printf("%d %(d %+d %05d\n", 3, -3, 3, 3); System.out.println(); System.out.printf("Default floating-point format: %f\n", 1234567.123); System.out.printf("Floating-point with commas: %,f\n", 1234567.123); Chapter 19 Input/Output: Exploring java.io 607 System.out.printf("Negative floating-point default: %,f\n", -1234567.123); System.out.printf("Negative floating-point option: %,(f\n", -1234567.123); System.out.println(); System.out.printf("Line up positive and negative values:\n"); System.out.printf("% ,.2f\n% ,.2f\n", 1234567.123, -1234567.123); The output is shown here: Here are some numeric values in different formats. Various integer formats: 3 (3) +3 00003 Default floating-point format: 1234567.123000 Floating-point with commas: 1,234,567.123000 Negative floating-point default: -1,234,567.123000 Negative floating-point option: (1,234,567.123000) Line up positive and negative values: 1,234,567.12 -1,234,567.12 PrintStream also defines the format( ) method. It has these general forms: PrintStream format(String fmtString, Object … args) PrintStream format(Locale loc, String fmtString, Object … args) It works exactly like printf( ). DataOutputStream and DataInputStream DataOutputStream and DataInputStream enable you to write or read primitive data to or from a stream. They implement the DataOutput and DataInput interfaces, respectively. These interfaces define methods that convert primitive values to or from a sequence of bytes. These streams make it easy to store binary data, such as integers or floating-point values, in a file. Each is examined here. DataOutputStream extends FilterOutputStream, which extends OutputStream. In addition to implementing DataOutput, DataOutputStream also implements AutoCloseable, Closeable, and Flushable. DataOutputStream defines the following constructor: DataOutputStream(OutputStream outputStream) Here, outputStream specifies the output stream to which data will be written. When a DataOutputStream is closed (by calling close( )), the underlying stream specified by outputStream is also closed automatically. Part II } } 608 PART II The Java Library DataOutputStream supports all of the methods defined by its superclasses. However, it is the methods defined by the DataOutput interface, which it implements, that make it interesting. DataOutput defines methods that convert values of a primitive type into a byte sequence and then writes it to the underlying stream. Here is a sampling of these methods: final void writeDouble(double value) throws IOException final void writeBoolean(boolean value) throws IOException final void writeInt(int value) throws IOException Here, value is the value written to the stream. DataInputStream is the complement of DataOuputStream. It extends FilterInputStream, which extends InputStream. In addition to implementing the DataInput interface, DataInputStream also implements AutoCloseable and Closeable. Here is its only constructor: DataInputStream(InputStream inputStream) Here, inputStream specifies the input stream from which data will be read. When a DataInputStream is closed (by calling close( )), the underlying stream specified by inputStream is also closed automatically. Like DataOutputStream, DataInputStream supports all of the methods of its superclasses, but it is the methods defined by the DataInput interface that make it unique. These methods read a sequence of bytes and convert them into values of a primitive type. Here is a sampling of these methods: final double readDouble( ) throws IOException final boolean readBoolean( ) throws IOException final int readInt( ) throws IOException The following program demonstrates the use of DataOutputStream and DataInputStream: // Demonstrate DataInputStream and DataOutputStream. // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; class DataIODemo { public static void main(String args[]) throws IOException { // First, write the data. try ( DataOutputStream dout = new DataOutputStream(new FileOutputStream("Test.dat")) ) { dout.writeDouble(98.6); dout.writeInt(1000); dout.writeBoolean(true); Chapter 19 Input/Output: Exploring java.io 609 } catch(FileNotFoundException e) { System.out.println("Cannot Open Output File"); return; } catch(IOException e) { System.out.println("I/O Error: " + e); } double d = din.readDouble(); int i = din.readInt(); boolean b = din.readBoolean(); System.out.println("Here are the values: " + d + " " + i + " " + b); } catch(FileNotFoundException e) { System.out.println("Cannot Open Input File"); return; } catch(IOException e) { System.out.println("I/O Error: " + e); } } } The output is shown here: Here are the values: 98.6 1000 true RandomAccessFile RandomAccessFile encapsulates a random-access file. It is not derived from InputStream or OutputStream. Instead, it implements the interfaces DataInput and DataOutput, which define the basic I/O methods. It also implements the AutoCloseable and Closeable interfaces. RandomAccessFile is special because it supports positioning requests—that is, you can position the file pointer within the file. It has these two constructors: RandomAccessFile(File fileObj, String access) throws FileNotFoundException RandomAccessFile(String filename, String access) throws FileNotFoundException In the first form, fileObj specifies the file to open as a File object. In the second form, the name of the file is passed in filename. In both cases, access determines what type of file access is permitted. If it is "r", then the file can be read, but not written. If it is "rw", then the file is opened in read-write mode. If it is "rws", the file is opened for read-write operations and Part II // Now, read the data back. try ( DataInputStream din = new DataInputStream(new FileInputStream("Test.dat")) ) { 610 PART II The Java Library every change to the file’s data or metadata will be immediately written to the physical device. If it is "rwd", the file is opened for read-write operations and every change to the file’s data will be immediately written to the physical device. The method seek( ), shown here, is used to set the current position of the file pointer within the file: void seek(long newPos) throws IOException Here, newPos specifies the new position, in bytes, of the file pointer from the beginning of the file. After a call to seek( ), the next read or write operation will occur at the new file position. RandomAccessFile implements the standard input and output methods, which you can use to read and write to random access files. It also includes some additional methods. One is setLength( ). It has this signature: void setLength(long len) throws IOException This method sets the length of the invoking file to that specified by len. This method can be used to lengthen or shorten a file. If the file is lengthened, the added portion is undefined. The Character Streams While the byte stream classes provide sufficient functionality to handle any type of I/O operation, they cannot work directly with Unicode characters. Since one of the main purposes of Java is to support the "write once, run anywhere" philosophy, it was necessary to include direct I/O support for characters. In this section, several of the character I/O classes are discussed. As explained earlier, at the top of the character stream hierarchies are the Reader and Writer abstract classes. We will begin with them. NOTE As discussed in Chapter 13, the character I/O classes were added by the 1.1 release of Java. Because of this, you may still find legacy code that uses byte streams where character streams would be more appropriate. When working on such code, it is a good idea to update it. Reader Reader is an abstract class that defines Java’s model of streaming character input. It implements the AutoCloseable, Closeable, and Readable interfaces. All of the methods in this class (except for markSupported( )) will throw an IOException on error conditions. Table 19-3 provides a synopsis of the methods in Reader. Writer Writer is an abstract class that defines streaming character output. It implements the AutoCloseable, Closeable, Flushable, and Appendable interfaces. All of the methods in this class throw an IOException in the case of errors. Table 19-4 shows a synopsis of the methods in Writer. Input/Output: Exploring java.io Method Description abstract void close( ) Closes the input source. Further read attempts will generate an IOException. void mark(int numChars) Places a mark at the current point in the input stream that will remain valid until numChars characters are read. boolean markSupported( ) Returns true if mark( )/reset( ) are supported on this stream. int read( ) Returns an integer representation of the next available character from the invoking input stream. –1 is returned when the end of the file is encountered. int read(char buffer[ ]) Attempts to read up to buffer.length characters into buffer and returns the actual number of characters that were successfully read. –1 is returned when the end of the file is encountered. int read(CharBuffer buffer) Attempts to read characters into buffer and returns the actual number of characters that were successfully read. –1 is returned when the end of the file is encountered. abstract int read(char buffer[ ], int offset, int numChars) Attempts to read up to numChars characters into buffer starting at buffer[offset], returning the number of characters successfully read. –1 is returned when the end of the file is encountered. boolean ready( ) Returns true if the next input request will not wait. Otherwise, it returns false. void reset( ) Resets the input pointer to the previously set mark. long skip(long numChars) Skips over numChars characters of input, returning the number of characters actually skipped. Table 19-3 The Methods Defined by Reader Method Description Writer append(char ch) Appends ch to the end of the invoking output stream. Returns a reference to the invoking stream. Writer append(CharSequence chars) Appends chars to the end of the invoking output stream. Returns a reference to the invoking stream. Writer append(CharSequence chars, int begin, int end) Appends the subrange of chars specified by begin and end–1 to the end of the invoking output stream. Returns a reference to the invoking stream. abstract void close( ) Closes the output stream. Further write attempts will generate an IOException. abstract void flush( ) Finalizes the output state so that any buffers are cleared. That is, it flushes the output buffers. Table 19-4 The Methods Defined by Writer 611 Part II Chapter 19 612 PART II The Java Library Method Description void write(int ch) Writes a single character to the invoking output stream. Note that the parameter is an int, which allows you to call write with an expression without having to cast it back to char. However, only the low-order 16 bits are written. void write(char buffer[ ]) Writes a complete array of characters to the invoking output stream. abstract void write(char buffer[ ], int offset, int numChars) Writes a subrange of numChars characters from the array buffer, beginning at buffer[offset] to the invoking output stream. void write(String str) Writes str to the invoking output stream. void write(String str, int offset, int numChars) Writes a subrange of numChars characters from the string str, beginning at the specified offset. Table 19-4 The Methods Defined by Writer (continued) FileReader The FileReader class creates a Reader that you can use to read the contents of a file. Its two most commonly used constructors are shown here: FileReader(String filePath) FileReader(File fileObj) Either can throw a FileNotFoundException. Here, filePath is the full path name of a file, and fileObj is a File object that describes the file. The following example shows how to read lines from a file and display them on the standard output device. It reads its own source file, which must be in the current directory. // Demonstrate FileReader. // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; class FileReaderDemo { public static void main(String args[]) { try ( FileReader fr = new FileReader("FileReaderDemo.java") ) { int c; // Read and display the file. while((c = fr.read()) != -1) System.out.print((char) c); } catch(IOException e) { System.out.println("I/O Error: " + e); } } } Chapter 19 Input/Output: Exploring java.io 613 FileWriter FileWriter creates a Writer that you can use to write to a file. Four of its most commonly used constructors are shown here: They can all throw an IOException. Here, filePath is the full path name of a file, and fileObj is a File object that describes the file. If append is true, then output is appended to the end of the file. Creation of a FileWriter is not dependent on the file already existing. FileWriter will create the file before opening it for output when you create the object. In the case where you attempt to open a read-only file, an IOException will be thrown. The following example is a character stream version of an example shown earlier when FileOutputStream was discussed. This version creates a sample buffer of characters by first making a String and then using the getChars( ) method to extract the character array equivalent. It then creates three files. The first, file1.txt, will contain every other character from the sample. The second, file2.txt, will contain the entire set of characters. Finally, the third, file3.txt, will contain only the last quarter. // Demonstrate FileWriter. // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; class FileWriterDemo { public static void main(String args[]) throws IOException { String source = "Now is the time for all good men\n" + " to come to the aid of their country\n" + " and pay their due taxes."; char buffer[] = new char[source.length()]; source.getChars(0, source.length(), buffer, 0); try ( FileWriter f0 = new FileWriter("file1.txt"); FileWriter f1 = new FileWriter("file2.txt"); FileWriter f2 = new FileWriter("file3.txt") ) { // write to first file for (int i=0; i < buffer.length; i += 2) { f0.write(buffer[i]); } // write to second file f1.write(buffer); // write to third file f2.write(buffer,buffer.length-buffer.length/4,buffer.length/4); Part II FileWriter(String filePath) FileWriter(String filePath, boolean append) FileWriter(File fileObj) FileWriter(File fileObj, boolean append) 614 PART II The Java Library } catch(IOException e) { System.out.println("An I/O Error Occurred"); } } } CharArrayReader CharArrayReader is an implementation of an input stream that uses a character array as the source. This class has two constructors, each of which requires a character array to provide the data source: CharArrayReader(char array [ ]) CharArrayReader(char array [ ], int start, int numChars) Here, array is the input source. The second constructor creates a Reader from a subset of your character array that begins with the character at the index specified by start and is numChars long. The close( ) method implemented by CharArrayReader does not throw any exceptions. This is because it cannot fail. The following example uses a pair of CharArrayReaders: // Demonstrate CharArrayReader. // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; public class CharArrayReaderDemo { public static void main(String args[]) { String tmp = "abcdefghijklmnopqrstuvwxyz"; int length = tmp.length(); char c[] = new char[length]; tmp.getChars(0, length, c, 0); int i; try (CharArrayReader input1 = new CharArrayReader(c) ) { System.out.println("input1 is:"); while((i = input1.read()) != -1) { System.out.print((char)i); } System.out.println(); } catch(IOException e) { System.out.println("I/O Error: " + e); } try ( CharArrayReader input2 = new CharArrayReader(c, 0, 5) ) { System.out.println("input2 is:"); while((i = input2.read()) != -1) { System.out.print((char)i); } Chapter 19 Input/Output: Exploring java.io 615 System.out.println(); } catch(IOException e) { System.out.println("I/O Error: " + e); } } } input1 is: abcdefghijklmnopqrstuvwxyz input2 is: abcde CharArrayWriter CharArrayWriter is an implementation of an output stream that uses an array as the destination. CharArrayWriter has two constructors, shown here: CharArrayWriter( ) CharArrayWriter(int numChars) In the first form, a buffer with a default size is created. In the second, a buffer is created with a size equal to that specified by numChars. The buffer is held in the buf field of CharArrayWriter. The buffer size will be increased automatically, if needed. The number of characters held by the buffer is contained in the count field of CharArrayWriter. Both buf and count are protected fields. The close( ) method has no effect on a CharArrayWriter. The following example demonstrates CharArrayWriter by reworking the sample program shown earlier for ByteArrayOutputStream. It produces the same output as the previous version. // Demonstrate CharArrayWriter. // This program uses try-with-resources. It requires JDK 7 or later. import java.io.*; class CharArrayWriterDemo { public static void main(String args[]) throws IOException { CharArrayWriter f = new CharArrayWriter(); String s = "This should end up in the array"; char buf[] = new char[s.length()]; s.getChars(0, s.length(), buf, 0); try { f.write(buf); } catch(IOException e) { System.out.println("Error Writing to Buffer"); return; } Part II The input1 object is constructed using the entire lowercase alphabet, whereas input2 contains only the first five letters. Here is the output: 616 PART II The Java Library System.out.println("Buffer as a string"); System.out.println(f.toString()); System.out.println("Into array"); char c[] = f.toCharArray(); for (int i=0; i "); else System.out.print(" "); System.out.println(entry.getName(1)); } } catch(InvalidPathException e) { System.out.println("Path Error " + e); } catch(NotDirectoryException e) { System.out.println(dirname + " is not a directory."); } catch (IOException e) { System.out.println("I/O Error: " + e); } } } Here is sample output from the program: Directory of \MyDir DirList.class DirList.java examples Test.txt You can filter the contents of a directory in two ways. The easiest is to use this version of newDirectoryStream( ): static DirectoryStream newDirectoryStream(Path dirPath, String wildcard) throws IOException In this version, only files that match the wildcard filename specified by wildcard will be obtained. For wildcard, you can specify either a complete filename or a glob. A glob is a string that defines a general pattern that will match one or more files using the familiar * and ? wildcard characters. These match zero or more of any character and any one character, respectively. The following are also recognized within a glob. Part II // Obtain and manage a directory stream within a try block. try ( DirectoryStream dirstrm = Files.newDirectoryStream(Paths.get(dirname)) ) { System.out.println("Directory of " + dirname); 656 PART II The Java Library ** Matches zero or more of any character across directories. [chars] Matches any one character in chars. A * or ? within chars will be treated as a normal character, not a wildcard. A range can be specified by use of a hyphen, such as [x-z]. {globlist} Matches any one of the globs specified in a comma-separated list of globs in globlist. You can specify a * or ? character, using \* and \?. To specify a \, use \\. You can experiment with a glob by substituting this call to newDirectoryStream( ) into the previous program: Files.newDirectoryStream(Paths.get(dirname), "{Path,Dir}*.{java,class}") This obtains a directory stream that contains only those files whose names begin with either "Path" or "Dir" and use either the "java" or "class" extension. Thus, it would match names like DirList.java and PathDemo.java, but not MyPathDemo.java, for example. Another way to filter a directory is to use this version of newDirectoryStream( ): static DirectoryStream newDirectoryStream(Path dirPath, DirectoryStream.Filter filefilter) throws IOException Here, DirectoryStream.Filter is an interface that specifies the following method: boolean accept(T entry) throws IOException In this case, T will be Path. If you want to include entry in the list, return true. Otherwise, return false. This form of newDirectoryStream( ) offers the advantage of being able to filter a directory based on something other than a filename. For example, you can filter based on size, creation date, modification date, or attribute, to name a few. The following program demonstrates the process. It will list only those files that are writable. // Display a directory of only those files that are writable. import java.io.*; import java.nio.file.*; import java.nio.file.attribute.*; class DirList { public static void main(String args[]) { String dirname = "\\MyDir"; // Create a filter that returns true only for writable files. DirectoryStream.Filter how = new DirectoryStream.Filter() { public boolean accept(Path filename) throws IOException { if(Files.isWritable(filename)) return true; return false; } }; Chapter 20 Exploring NIO 657 // Obtain and manage a directory stream of writable files. try (DirectoryStream dirstrm = Files.newDirectoryStream(Paths.get(dirname), how) ) { System.out.println("Directory of " + dirname); if(attribs.isDirectory()) System.out.print(" "); else System.out.print(" "); System.out.println(entry.getName(1)); } } catch(InvalidPathException e) { System.out.println("Path Error " + e); } catch(NotDirectoryException e) { System.out.println(dirname + " is not a directory."); } catch (IOException e) { System.out.println("I/O Error: " + e); } } } Use walkFileTree( ) to List a Directory Tree The preceding examples have obtained the contents of only a single directory. However, sometimes you will want to obtain a list of the files in a directory tree. In the past, this was quite a chore, but NIO.2 makes it easy because now you can use the walkFileTree( ) method defined by Files to process a directory tree. It has two forms. The one used in this chapter is shown here: static Path walkFileTree(Path root, FileVisitor fv) throws IOException The path to the starting point of the directory walk is passed in root. An instance of FileVisitor is passed in fv. The implementation of FileVisitor determines how the directory tree is traversed, and it gives you access to the directory information. If an I/O error occurs, an IOException is thrown. A SecurityException is also possible. FileVisitor is an interface that defines how files are visited when a directory tree is traversed. It is a generic interface that is declared like this: interface FileVisitor For use in walkFileTree( ), T will be Path (or any type derived from Path). FileVisitor defines the following methods. Part II for(Path entry : dirstrm) { BasicFileAttributes attribs = Files.readAttributes(entry, BasicFileAttributes.class); 658 PART II The Java Library Method Description FileVisitResult postVisitDirectory(T dir, IOException exc) throws IOException Called after a directory has been visited. The directory is passed in dir, and any IOException is passed in exc. If exc is null, no exception occurred. The result is returned. FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attribs) throws IOException Called before a directory is visited. The directory is passed in dir, and the attributes associated with the directory are passed in attribs. The result is returned. To examine the directory, return FileVisitResult.CONTINUE. FileVisitResult visitFile(T file, BasicFileAttributes attribs) throws IOException Called when a file is visited. The file is passed in file, and the attributes associated with the file are passed in attribs. The result is returned. FileVisitResult visitFileFailed(T file, IOException exc) throws IOException Called when an attempt to visit a file fails. The file that failed is passed in file, and the IOException is passed in exc. The result is returned. Notice that each method returns a FileVisitResult. This enumeration defines the following values: CONTINUE SKIP_SIBLINGS SKIP_SUBTREE TERMINATE In general, to continue traversing the directory and subdirectories, a method should return CONTINUE. For preVisitDirectory( ), return SKIP_SIBLINGS to bypass the directory and its siblings and prevent postVisitDirectory( ) from being called. To bypass just the directory and subdirectories, return SKIP_SUBTREE. To stop the directory traversal, return TERMINATE. Although it is certainly possible to create your own visitor class that implements these methods defined by FileVisitor, you won’t normally do so because a simple implementation is provided by SimpleFileVisitor. You can just override the default implementation of the method or methods in which you are interested. Here is a short example that illustrates the process. It displays all files in the directory tree that has \MyDir as its root. Notice how short this program is. // A simple example that uses walkFileTree( ) to display a directory tree. // Requires JDK 7 or later. import java.io.*; import java.nio.file.*; import java.nio.file.attribute.*; // Create a custom version of SimpleFileVisitor that overrides // the visitFile( ) method. class MyFileVisitor extends SimpleFileVisitor { public FileVisitResult visitFile(Path path, BasicFileAttributes attribs) Chapter 20 Exploring NIO 659 throws IOException { System.out.println(path); return FileVisitResult.CONTINUE; } } System.out.println("Directory tree starting with " + dirname + ":\n"); try { Files.walkFileTree(Paths.get(dirname), new MyFileVisitor()); } catch (IOException exc) { System.out.println("I/O Error"); } } } Here is sample output produced by the program when used on the same MyDir directory shown earlier. In this example, the subdirectory called examples contains one file called MyProgram.java. Directory tree starting with \MyDir: \MyDir\DirList.class \MyDir\DirList.java \MyDir\examples\MyProgram.java \MyDir\Test.txt In the program, the class MyFileVisitor extends SimpleFileVisitor, overriding only the visitFile( ) method. In this example, visitFile( ) simply displays the files, but more sophisticated functionality is easy to achieve. For example, you could filter the files or perform actions on the files, such as copying them to a backup device. For the sake of clarity, a named class was used to override visitFile( ), but you could also use an anonymous inner class. One last point: It is possible to watch a directory for changes by using java.nio.file.WatchService. Pre-JDK 7 Channel-Based Examples Before concluding this chapter, one more aspect of NIO needs to be covered. The preceding sections have used several of the new features added to NIO by JDK 7. However, there is still much pre-JDK 7 code in existence that will need to be maintained or possibly converted to use the new features. For this reason, the following sections show how to read and write files using the pre-JDK 7 NIO system. They do so by reworking some of the examples shown earlier so that they use the original NIO features, rather than the new JDK 7 (NIO.2) features. This means that the examples in this section will work with versions of Java prior to JDK 7. Part II class DirTreeList { public static void main(String args[]) { String dirname = "\\MyDir"; 660 PART II The Java Library The key difference between pre-JDK 7 NIO code and new NIO code is the Path interface, which was added by JDK 7. Thus, pre-JDK 7 code does not use Path to describe a file or open a channel to it. Also, pre-JDK 7 code does not use try-with-resource statements since automatic resource management was also added by JDK 7. REMEMBER The examples in this section describe how legacy NIO code works. This section is strictly for the benefit of those programmers working on pre-JDK 7 code or using pre-JDK 7 compilers. New code should take advantage of the NIO features added by JDK 7. Read a File, Pre-JDK 7 This section reworks the two channel-based file input examples shown earlier so they use only pre-JDK 7 features. The first example reads a file by manually allocating a buffer and then performing an explicit read operation. The second example uses a mapped file, which automates the process. When using a pre-JDK 7 version of Java to read a file using a channel and a manually allocated buffer, you first open the file for input using FileInputStream, using the same mechanism explained in Chapter 19. Next, obtain a channel to this file by calling getChannel( ) on the FileInputStream object. It has this general form: FileChannel getChannel( ) It returns a FileChannel object, which encapsulates the channel for file operations. Then, call allocate( ) to allocate a buffer. Because file channels operate on byte buffers, you will use the allocate( ) method defined by ByteBuffer, which works as previously described. The following program shows how to read and display a file called test.txt through a channel using explicit input operations for versions of Java prior to JDK 7: // Use import import import Channels to read a file. Pre-JDK 7 version. java.io.*; java.nio.*; java.nio.channels.*; public class ExplicitChannelRead { public static void main(String args[]) { FileInputStream fIn = null; FileChannel fChan = null; ByteBuffer mBuf; int count; try { // First, open a file for input. fIn = new FileInputStream("test.txt"); // Next, obtain a channel to that file. fChan = fIn.getChannel(); // Allocate a buffer. mBuf = ByteBuffer.allocate(128); do { Chapter 20 Exploring NIO 661 // Read a buffer. count = fChan.read(mBuf); // Stop when end of file is reached. if(count != -1) { // Read bytes from the buffer and show // them on the screen. for(int i=0; i < count; i++) System.out.print((char)mBuf.get()); } } while(count != -1); System.out.println(); } catch (IOException e) { System.out.println("I/O Error " + e); } finally { try { if(fChan != null) fChan.close(); // close channel } catch(IOException e) { System.out.println("Error Closing Channel."); } try { if(fIn != null) fIn.close(); // close file } catch(IOException e) { System.out.println("Error Closing File."); } } } } In this program, notice that the file is opened by using the FileInputStream constructor, and a reference to that object is assigned to fIn. Next, a channel connected to the file is obtained by calling getChannel( ) on fIn. After this point, the program works like the JDK 7 version shown previously. To synopsize: The program then calls the allocate( ) method of ByteBuffer to allocate a buffer that will hold the contents of the file when it is read. A byte buffer is used because FileChannel operates on bytes. A reference to this buffer is stored in mBuf. The contents of the file are then read, one buffer at a time, into mBuf through a call to read( ). The number of bytes read is stored in count. Next, the buffer is rewound through a call to rewind( ). This call is necessary because the current position is at the end of the buffer after the call to read( ), and it must be reset to the start of the buffer in order for the bytes in mBuf to be read by calling get( ). When the end of the file has been reached, the value returned by read( ) will be –1. When this occurs, the program ends, explicitly closing the channel and the file. Another way to read a file is to map it to a buffer. As explained earlier, a principal advantage to this approach is that the buffer automatically contains the contents of the file. No explicit read operation is necessary. To map and read the contents of a file using Part II // Rewind the buffer so that it can be read. mBuf.rewind(); 662 PART II The Java Library pre-JDK 7 NIO, first open the file using FileInputStream. Next, obtain a channel to that file by calling getChannel( ) on the file object. Then, map the channel to a buffer by calling map( ) on the FileChannel object. The map( ) method works as described earlier. The following program reworks the preceding example so that it uses only pre-JDK 7 features to create a mapped file: // Use a mapped file to read a file. Pre-JDK 7 version. import java.io.*; import java.nio.*; import java.nio.channels.*; public class MappedChannelRead { public static void main(String args[]) { FileInputStream fIn = null; FileChannel fChan = null; long fSize; MappedByteBuffer mBuf; try { // First, open a file for input. fIn = new FileInputStream("test.txt"); // Next, obtain a channel to that file. fChan = fIn.getChannel(); // Get the size of the file. fSize = fChan.size(); // Now, map the file into a buffer. mBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); // Read and display bytes from buffer. for(int i=0; i < fSize; i++) System.out.print((char)mBuf.get()); } catch (IOException e) { System.out.println("I/O Error " + e); } finally { try { if(fChan != null) fChan.close(); // close channel } catch(IOException e) { System.out.println("Error Closing Channel."); } try { if(fIn != null) fIn.close(); // close file } catch(IOException e) { System.out.println("Error Closing File."); } } } } Chapter 20 Exploring NIO 663 In the program, the file is opened by using the FileInputStream constructor, and a reference to that object is assigned to fIn. A channel connected to the file is obtained by calling getChannel( ) on fIn. Next, the size of the file is obtained. Then, the entire file is mapped into memory by calling map( ), and a reference to the buffer is stored in mBuf. The bytes in mBuf are read by calling get( ). This section reworks the two channel-based file output examples shown earlier so that they use only pre-JDK 7 features. The first example writes to a file by manually allocating a buffer and then performing an explicit output operation. The second example uses a mapped file, which automates the process. In both cases, neither Path nor try-with-resources is used. This is because neither were part of Java until JDK 7. When using a pre-JDK 7 version of Java to write a file using a channel and a manually allocated buffer, first open the file for output. This is done by creating a FileOutputStream, as described in Chapter 19. Next, obtain a channel to the file by calling getChannel( ) and then allocate a byte buffer by calling allocate( ), as described in the previous section. Next, put the data you want to write into that buffer, and then call write( ) on the channel. The following program demonstrates this procedure. It writes the alphabet to a file called test.txt. // Write to a file using NIO. Pre-JDK 7 Version. import java.io.*; import java.nio.*; import java.nio.channels.*; public class ExplicitChannelWrite { public static void main(String args[]) { FileOutputStream fOut = null; FileChannel fChan = null; ByteBuffer mBuf; try { // First, open the output file. fOut = new FileOutputStream("test.txt"); // Next, get a channel to the output file. fChan = fOut.getChannel(); // Create a buffer. mBuf = ByteBuffer.allocate(26); // Write some bytes to the buffer. for(int i=0; i<26; i++) mBuf.put((byte)('A' + i)); // Rewind the buffer so that it can be written. mBuf.rewind(); // Write the buffer to the output file. fChan.write(mBuf); Part II Write to a File, Pre-JDK 7 664 PART II The Java Library } catch (IOException e) { System.out.println("I/O Error " + e); } finally { try { if(fChan != null) fChan.close(); // close channel } catch(IOException e) { System.out.println("Error Closing Channel."); } try { if(fOut != null) fOut.close(); // close file } catch(IOException e) { System.out.println("Error Closing File."); } } } } The call to rewind( ) on mBuf is necessary in order to reset the current position to zero after data has been written to mBuf. Remember, each call to put( ) advances the current position. Therefore, it is necessary for the current position to be reset to the start of the buffer before calling write( ). If this is not done, write( ) will think that there is no data in the buffer. When using a pre-JDK 7 version of Java to write to a file using a mapped file, follow these steps. First, open the file for read/write operations by creating a RandomAccessFile object. This is necessary to enable the file to be both read from and written to. Next, map that file to a buffer by calling map( ) on that object. Then, write to the buffer. Because the buffer is mapped to the file, any changes to that buffer are automatically reflected in the file. Thus, no explicit write operations to the channel are necessary. Here is the preceding program reworked so that a mapped file is used: // Write to a mapped file. Pre JDK 7 version. import java.io.*; import java.nio.*; import java.nio.channels.*; public class MappedChannelWrite { public static void main(String args[]) { RandomAccessFile fOut = null; FileChannel fChan = null; ByteBuffer mBuf; try { fOut = new RandomAccessFile("test.txt", "rw"); // Next, obtain a channel to that file. fChan = fOut.getChannel(); // Then, map the file into a buffer. mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26); Chapter 20 Exploring NIO 665 } catch (IOException e) { System.out.println("I/O Error " + e); } finally { try { if(fChan != null) fChan.close(); // close channel } catch(IOException e) { System.out.println("Error Closing Channel."); } try { if(fOut != null) fOut.close(); // close file } catch(IOException e) { System.out.println("Error Closing File."); } } } } As you can see, there are no explicit write operations to the channel itself. Because mBuf is mapped to the file, changes to mBuf are automatically reflected in the underlying file. Part II // Write some bytes to the buffer. for(int i=0; i<26; i++) mBuf.put((byte)('A' + i)); This page intentionally left blank CHAPTER 21 Networking As all readers know, Java is practically a synonym for Internet programming. There are a number of reasons for this, not the least of which is its ability to generate secure, crossplatform, portable code. However, one of the most important reasons that Java is the premier language for network programming are the classes defined in the java.net package. They provide an easy-to-use means by which programmers of all skill levels can access network resources. This chapter explores the java.net package. It is important to emphasize that networking is a very large and at times complicated topic. It is not possible for this book to discuss all of the capabilities contained in java.net. Instead, this chapter focuses on several of its core classes and interfaces. Networking Basics Before we begin, it will be useful to review some key networking concepts and terms. At the core of Java’s networking support is the concept of a socket. A socket identifies an endpoint in a network. The socket paradigm was part of the 4.2BSD Berkeley UNIX release in the early 1980s. Because of this, the term Berkeley socket is also used. Sockets are at the foundation of modern networking because a socket allows a single computer to serve many different clients at once, as well as to serve many different types of information. This is accomplished through the use of a port, which is a numbered socket on a particular machine. A server process is said to "listen" to a port until a client connects to it. A server is allowed to accept multiple clients connected to the same port number, although each session is unique. To manage multiple client connections, a server process must be multithreaded or have some other means of multiplexing the simultaneous I/O. Socket communication takes place via a protocol. Internet Protocol (IP) is a low-level routing protocol that breaks data into small packets and sends them to an address across a network, which does not guarantee to deliver said packets to the destination. Transmission Control Protocol (TCP) is a higher-level protocol that manages to robustly string together these packets, sorting and retransmitting them as necessary to reliably transmit data. A third protocol, User Datagram Protocol (UDP), sits next to TCP and can be used directly to support fast, connectionless, unreliable transport of packets. 667 668 PART II The Java Library Once a connection has been established, a higher-level protocol ensues, which is dependent on which port you are using. TCP/IP reserves the lower 1,024 ports for specific protocols. Many of these will seem familiar to you if you have spent any time surfing the Internet. Port number 21 is for FTP; 23 is for Telnet; 25 is for e-mail; 43 is for whois; 80 is for HTTP; 119 is for netnews—and the list goes on. It is up to each protocol to determine how a client should interact with the port. For example, HTTP is the protocol that web browsers and servers use to transfer hypertext pages and images. It is a quite simple protocol for a basic page-browsing web server. Here’s how it works. When a client requests a file from an HTTP server, an action known as a hit, it simply sends the name of the file in a special format to a predefined port and reads back the contents of the file. The server also responds with a status code to tell the client whether or not the request can be fulfilled and why. A key component of the Internet is the address. Every computer on the Internet has one. An Internet address is a number that uniquely identifies each computer on the Net. Originally, all Internet addresses consisted of 32-bit values, organized as four 8-bit values. This address type was specified by IPv4 (Internet Protocol, version 4). However, a new addressing scheme, called IPv6 (Internet Protocol, version 6) has come into play. IPv6 uses a 128-bit value to represent an address, organized into eight 16-bit chunks. Although there are several reasons for and advantages to IPv6, the main one is that it supports a much larger address space than does IPv4. Fortunately, when using Java, you won’t normally need to worry about whether IPv4 or IPv6 addresses are used because Java handles the details for you. Just as the numbers of an IP address describe a network hierarchy, the name of an Internet address, called its domain name, describes a machine’s location in a name space. For example, www.HerbSchildt.com is in the COM top-level domain (reserved for U.S. commercial sites); it is called HerbSchildt, and www identifies the server for web requests. An Internet domain name is mapped to an IP address by the Domain Naming Service (DNS). This enables users to work with domain names, but the Internet operates on IP addresses. The Networking Classes and Interfaces Java supports TCP/IP both by extending the already established stream I/O interface introduced in Chapter 19 and by adding the features required to build I/O objects across the network. Java supports both the TCP and UDP protocol families. TCP is used for reliable stream-based I/O across the network. UDP supports a simpler, hence faster, point-to-point datagram-oriented model. The classes contained in the java.net package are shown here: Authenticator Inet6Address ServerSocket CacheRequest InetAddress Socket CacheResponse InetSocketAddress SocketAddress ContentHandler InterfaceAddress SocketImpl CookieHandler JarURLConnection SocketPermission CookieManager MulticastSocket StandardSocketOption (Added by JDK 7.) DatagramPacket NetPermission URI Chapter 21 DatagramSocket NetworkInterface Networking 669 URL DatagramSocketImpl PasswordAuthentication URLClassLoader HttpCookie Proxy URLConnection HttpURLConnection ProxySelector URLDecoder IDN ResponseCache URLEncoder Inet4Address SecureCacheResponse URLStreamHandler ContentHandlerFactory FileNameMap SocketOptions CookiePolicy ProtocolFamily (Added by JDK 7.) URLStreamHandlerFactory CookieStore SocketImplFactory DatagramSocketImplFactory SocketOption (Added by JDK 7.) In the sections that follow, we will examine the main networking classes and show several examples that apply to them. Once you understand these core networking classes, you will be able to easily explore the others on your own. InetAddress The InetAddress class is used to encapsulate both the numerical IP address and the domain name for that address. You interact with this class by using the name of an IP host, which is more convenient and understandable than its IP address. The InetAddress class hides the number inside. InetAddress can handle both IPv4 and IPv6 addresses. Factory Methods The InetAddress class has no visible constructors. To create an InetAddress object, you have to use one of the available factory methods. Factory methods are merely a convention whereby static methods in a class return an instance of that class. This is done in lieu of overloading a constructor with various parameter lists when having unique method names makes the results much clearer. Three commonly used InetAddress factory methods are shown here: static InetAddress getLocalHost( ) throws UnknownHostException static InetAddress getByName(String hostName) throws UnknownHostException static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException The getLocalHost( ) method simply returns the InetAddress object that represents the local host. The getByName( ) method returns an InetAddress for a host name passed to it. If these methods are unable to resolve the host name, they throw an UnknownHostException. Part II The java.net package’s interfaces are listed here: 670 PART II The Java Library On the Internet, it is common for a single name to be used to represent several machines. In the world of web servers, this is one way to provide some degree of scaling. The getAllByName( ) factory method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. It will also throw an UnknownHostException if it can’t resolve the name to at least one address. InetAddress also includes the factory method getByAddress( ), which takes an IP address and returns an InetAddress object. Either an IPv4 or an IPv6 address can be used. The following example prints the addresses and names of the local machine and two Internet web sites: // Demonstrate InetAddress. import java.net.*; class InetAddressTest { public static void main(String args[]) throws UnknownHostException { InetAddress Address = InetAddress.getLocalHost(); System.out.println(Address); Address = InetAddress.getByName("www.HerbSchildt.com"); System.out.println(Address); InetAddress SW[] = InetAddress.getAllByName("www.nba.com"); for (int i=0; i> getHeaderFields( ) Returns a map that contains all of the header fields and values. long getLastModified( ) Returns the time and date, represented in terms of milliseconds since January 1, 1970 GMT, of the last modification of the resource. Zero is returned if the last-modified date is unavailable. InputStream getInputStream( ) throws IOException Returns an InputStream that is linked to the resource. This stream can be used to obtain the content of the resource. Notice that URLConnection defines several methods that handle header information. A header consists of pairs of keys and values represented as strings. By using getHeaderField( ), you can obtain the value associated with a header key. By calling getHeaderFields( ), you can obtain a map that contains all of the headers. Several standard header fields are available directly through methods such as getDate( ) and getContentType( ). Part II are exposed by the HTTP protocol specification and, as such, only make sense for URL objects that are using the HTTP protocol. URLConnection defines several methods. Here is a sampling: 678 PART II The Java Library The following example creates a URLConnection using the openConnection( ) method of a URL object and then uses it to examine the document’s properties and content: // Demonstrate URLConnection. import java.net.*; import java.io.*; import java.util.Date; class UCDemo { public static void main(String args[]) throws Exception { int c; URL hp = new URL(http://www.internic.net"); URLConnection hpCon = hp.openConnection(); // get date long d = hpCon.getDate(); if(d==0) System.out.println("No date information."); else System.out.println("Date: " + new Date(d)); // get content type System.out.println("Content-Type: " + hpCon.getContentType()); // get expiration date d = hpCon.getExpiration(); if(d==0) System.out.println("No expiration information."); else System.out.println("Expires: " + new Date(d)); // get last-modified date d = hpCon.getLastModified(); if(d==0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(d)); // get content length long len = hpCon.getContentLengthLong(); if(len == -1) System.out.println("Content length unavailable."); else System.out.println("Content-Length: " + len); if(len != 0) { System.out.println("=== Content ==="); InputStream input = hpCon.getInputStream(); while (((c = input.read()) != -1)) { System.out.print((char) c); } input.close(); Chapter 21 Networking 679 } else { System.out.println("No content available."); } } } Date: Mon Oct 04 15:53:24 CDT 2010 Content-Type: text/html; charset=UTF-8 No expiration information. Last-Modified: Thu Sep 24 15:22:52 CDT 2009 Content-Length: 7316 === Content === InterNIC | The Internet's Network Information Center . . . HttpURLConnection Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection. You obtain an HttpURLConnection in the same way just shown, by calling openConnection( ) on a URL object, but you must cast the result to HttpURLConnection. (Of course, you must make sure that you are actually opening an HTTP connection.) Once you have obtained a reference to an HttpURLConnection object, you can use any of the methods inherited from URLConnection. You can also use any of the several methods defined by HttpURLConnection. Here is a sampling: static boolean getFollowRedirects( ) Returns true if redirects are automatically followed and false otherwise. This feature is on by default. String getRequestMethod( ) Returns a string representing how URL requests are made. The default is GET. Other options, such as POST, are available. int getResponseCode( ) throws IOException Returns the HTTP response code. –1 is returned if no response code can be obtained. An IOException is thrown if the connection fails. String getResponseMessage( ) throws IOException Returns the response message associated with the response code. Returns null if no message is available. An IOException is thrown if the connection fails. static void setFollowRedirects(boolean how) If how is true, then redirects are automatically followed. If how is false, redirects are not automatically followed. By default, redirects are automatically followed. Part II The program establishes an HTTP connection to www.internic.net over port 80. It then displays several header values and retrieves the content. Here are the first lines of the output (the precise output will vary over time). 680 PART II The Java Library void setRequestMethod(String how) throws ProtocolException Sets the method by which HTTP requests are made to that specified by how. The default method is GET, but other options, such as POST, are available. If how is invalid, a ProtocolException is thrown. The following program demonstrates HttpURLConnection. It first establishes a connection to www.google.com. Then it displays the request method, the response code, and the response message. Finally, it displays the keys and values in the response header. // Demonstrate HttpURLConnection. import java.net.*; import java.io.*; import java.util.*; class HttpURLDemo { public static void main(String args[]) throws Exception { URL hp = new URL(http://www.google.com"); HttpURLConnection hpCon = (HttpURLConnection) hp.openConnection(); // Display request method. System.out.println("Request method is " + hpCon.getRequestMethod()); // Display response code. System.out.println("Response code is " + hpCon.getResponseCode()); // Display response message. System.out.println("Response Message is " + hpCon.getResponseMessage()); // Get a list of the header fields and a set // of the header keys. Map> hdrMap = hpCon.getHeaderFields(); Set hdrField = hdrMap.keySet(); System.out.println("\nHere is the header:"); // Display all header keys and values. for(String k : hdrField) { System.out.println("Key: " + k + " Value: " + hdrMap.get(k)); } } } Chapter 21 Networking 681 Request method is GET Response code is 200 Response Message is OK Here is the header: Key: null Value: [HTTP/1.1 200 OK] Key: Date Value: [Mon, 04 Oct 2010 21:11:53 GMT] Key: Transfer-Encoding Value: [chunked] Key: Expires Value: [-1] Key: X-XSS-Protection Value: [1; mode=block] Key: Set-Cookie Value: [NID=39=uAS1DdTfLelHcxkEiRy7xNtExX3zJaKS9mjdTy8_XejjBkpjWvcqyMXgC4Ha4VT_5IZN2pnxslooNlGHvcK0AIqXPhFcnCd1R1Ww4WgbiY7KrthNXCQxfXbHJwNgue; expires=Tue, 05-Apr-2011 21:11:53 GMT; path=/; domain=.google.com; HttpOnly, PREF=ID=6644372b1f96120c:TM=1286226713:LM=1286226713:S=iNeZU0xRTrGPxg2K; expires=Wed, 03-Oct-2012 21:11:53 GMT; path=/; domain=.google.com] Key: Content-Type Value: [text/html; charset=ISO-8859-1] Key: Server Value: [gws] Key: Cache-Control Value: [private, max-age=0] Notice how the header keys and values are displayed. First, a map of the header keys and values is obtained by calling getHeaderFields( ) (which is inherited from URLConnection). Next, a set of the header keys is retrieved by calling keySet( ) on the map. Then the key set is cycled through by using a for-each style for loop. The value associated with each key is obtained by calling get( ) on the map. The URI Class The URI class encapsulates a Uniform Resource Identifier (URI). URIs are similar to URLs. In fact, URLs constitute a subset of URIs. A URI represents a standard way to identify a resource. A URL also describes how to access the resource. Cookies The java.net package includes classes and interfaces that help manage cookies and can be used to create a stateful (as opposed to stateless) HTTP session. The classes are CookieHandler, CookieManager, and HttpCookie. The interfaces are CookiePolicy and CookieStore. All but CookieHandler were added by Java SE 6. (CookieHandler was added by JDK 5.) The creation of a stateful HTTP session is beyond the scope of this book. NOTE For information about using cookies with servlets, see Chapter 32. TCP/IP Server Sockets As mentioned earlier, Java has a different socket class that must be used for creating server applications. The ServerSocket class is used to create servers that listen for either local or remote client programs to connect to them on published ports. ServerSockets are quite Part II The output produced by the program is shown here. (Of course, the exact response returned by www.google.com will vary over time.) 682 PART II The Java Library different from normal Sockets. When you create a ServerSocket, it will register itself with the system as having an interest in client connections. The constructors for ServerSocket reflect the port number that you want to accept connections on and, optionally, how long you want the queue for said port to be. The queue length tells the system how many client connections it can leave pending before it should simply refuse connections. The default is 50. The constructors might throw an IOException under adverse conditions. Here are three of its constructors: ServerSocket(int port) throws IOException Creates server socket on the specified port with a queue length of 50. ServerSocket(int port, int maxQueue) throws IOException Creates a server socket on the specified port with a maximum queue length of maxQueue. ServerSocket(int port, int maxQueue, InetAddress localAddress) throws IOException Creates a server socket on the specified port with a maximum queue length of maxQueue. On a multihomed host, localAddress specifies the IP address to which this socket binds. ServerSocket has a method called accept( ), which is a blocking call that will wait for a client to initiate communications and then return with a normal Socket that is then used for communication with the client. Datagrams TCP/IP-style networking is appropriate for most networking needs. It provides a serialized, predictable, reliable stream of packet data. This is not without its cost, however. TCP includes many complicated algorithms for dealing with congestion control on crowded networks, as well as pessimistic expectations about packet loss. This leads to a somewhat inefficient way to transport data. Datagrams provide an alternative. Datagrams are bundles of information passed between machines. They are somewhat like a hard throw from a well-trained but blindfolded catcher to the third baseman. Once the datagram has been released to its intended target, there is no assurance that it will arrive or even that someone will be there to catch it. Likewise, when the datagram is received, there is no assurance that it hasn’t been damaged in transit or that whoever sent it is still there to receive a response. Java implements datagrams on top of the UDP protocol by using two classes: the DatagramPacket object is the data container, while the DatagramSocket is the mechanism used to send or receive the DatagramPackets. Each is examined here. DatagramSocket DatagramSocket defines four public constructors. They are shown here: DatagramSocket( ) throws SocketException DatagramSocket(int port) throws SocketException DatagramSocket(int port, InetAddress ipAddress) throws SocketException DatagramSocket(SocketAddress address) throws SocketException Chapter 21 Networking 683 void send(DatagramPacket packet) throws IOException void receive(DatagramPacket packet) throws IOException The send( ) method sends a packet to the port specified by packet. The receive( ) method waits for a packet to be received from the port specified by packet and returns the result. DatagramSocket also defines the close( )method, which closes the socket. Beginning with JDK 7, DatagramSocket implements AutoCloseable, which means that a DatagramSocket can be managed by a try-with-resources block. Other methods give you access to various attributes associated with a DatagramSocket. Here is a sampling: InetAddress getInetAddress( ) If the socket is connected, then the address is returned. Otherwise, null is returned. int getLocalPort( ) Returns the number of the local port. int getPort( ) Returns the number of the port to which the socket is connected. It returns –1 if the socket is not connected to a port. boolean isBound( ) Returns true if the socket is bound to an address. Returns false otherwise. boolean isConnected( ) Returns true if the socket is connected to a server. Returns false otherwise. void setSoTimeout(int millis) throws SocketException Sets the time-out period to the number of milliseconds passed in millis. DatagramPacket DatagramPacket defines several constructors. Four are shown here: DatagramPacket(byte data [ ], int size) DatagramPacket(byte data [ ], int offset, int size) DatagramPacket(byte data [ ], int size, InetAddress ipAddress, int port) DatagramPacket(byte data [ ], int offset, int size, InetAddress ipAddress, int port) The first constructor specifies a buffer that will receive data and the size of a packet. It is used for receiving data over a DatagramSocket. The second form allows you to specify an offset into the buffer at which data will be stored. The third form specifies a target address and port, which are used by a DatagramSocket to determine where the data in the packet will be sent. The fourth form transmits packets beginning at the specified offset into the Part II The first creates a DatagramSocket bound to any unused port on the local computer. The second creates a DatagramSocket bound to the port specified by port. The third constructs a DatagramSocket bound to the specified port and InetAddress. The fourth constructs a DatagramSocket bound to the specified SocketAddress. SocketAddress is an abstract class that is implemented by the concrete class InetSocketAddress. InetSocketAddress encapsulates an IP address with a port number. All can throw a SocketException if an error occurs while creating the socket. DatagramSocket defines many methods. Two of the most important are send( ) and receive( ), which are shown here: 684 PART II The Java Library data. Think of the first two forms as building an "in box," and the second two forms as stuffing and addressing an envelope. DatagramPacket defines several methods, including those shown here, that give access to the address and port number of a packet, as well as the raw data and its length. In general, the get methods are used on packets that are received and the set methods are used on packets that will be sent. InetAddress getAddress( ) Returns the address of the source (for datagrams being received) or destination (for datagrams being sent). byte[ ] getData( ) Returns the byte array of data contained in the datagram. Mostly used to retrieve data from the datagram after it has been received. int getLength( ) Returns the length of the valid data contained in the byte array that would be returned from the getData( ) method. This may not equal the length of the whole byte array. int getOffset( ) Returns the starting index of the data. int getPort( ) Returns the port number. void setAddress(InetAddress ipAddress) Sets the address to which a packet will be sent. The address is specified by ipAddress. void setData(byte[ ] data) Sets the data to data, the offset to zero, and the length to number of bytes in data. void setData(byte[ ] data, int idx, int size) Sets the data to data, the offset to idx, and the length to size. void setLength(int size) Sets the length of the packet to size. void setPort(int port) Sets the port to port. A Datagram Example The following example implements a very simple networked communications client and server. Messages are typed into the window at the server and written across the network to the client side, where they are displayed. // Demonstrate datagrams. import java.net.*; class WriteServer { public static int serverPort = 998; public static int clientPort = 999; public static int buffer_size = 1024; public static DatagramSocket ds; public static byte buffer[] = new byte[buffer_size]; public static void TheServer() throws Exception { int pos=0; while (true) { Networking 685 int c = System.in.read(); switch (c) { case -1: System.out.println("Server Quits."); ds.close(); return; case '\r': break; case '\n': ds.send(new DatagramPacket(buffer,pos, InetAddress.getLocalHost(),clientPort)); pos=0; break; default: buffer[pos++] = (byte) c; } } } public static void TheClient() throws Exception { while(true) { DatagramPacket p = new DatagramPacket(buffer, buffer.length); ds.receive(p); System.out.println(new String(p.getData(), 0, p.getLength())); } } public static void main(String args[]) throws Exception { if(args.length == 1) { ds = new DatagramSocket(serverPort); TheServer(); } else { ds = new DatagramSocket(clientPort); TheClient(); } } } This sample program is restricted by the DatagramSocket constructor to running between two ports on the local machine. To use the program, run java WriteServer in one window; this will be the client. Then run java WriteServer 1 This will be the server. Anything that is typed in the server window will be sent to the client window after a newline is received. Part II Chapter 21 This page intentionally left blank CHAPTER 22 The Applet Class This chapter examines the Applet class, which provides the foundation for applets. The Applet class is contained in the java.applet package. Applet contains several methods that give you detailed control over the execution of your applet. In addition, java.applet also defines three interfaces: AppletContext, AudioClip, and AppletStub. Two Types of Applets It is important to state at the outset that there are two varieties of applets. The first are those based directly on the Applet class described in this chapter. These applets use the Abstract Window Toolkit (AWT) to provide the graphical user interface (or use no GUI at all). This style of applet has been available since Java was first created. The second type of applets are those based on the Swing class JApplet. Swing applets use the Swing classes to provide the GUI. Swing offers a richer and often easier-to-use user interface than does the AWT. Thus, Swing-based applets are now the most popular. However, traditional AWT-based applets are still used, especially when only a very simple user interface is required. Thus, both AWT- and Swing-based applets are valid. Because JApplet inherits Applet, all the features of Applet are also available in JApplet, and most of the information in this chapter applies to both types of applets. Therefore, even if you are interested in only Swing applets, the information in this chapter is still relevant and necessary. Understand, however, that when creating Swing-based applets, some additional constraints apply and these are described later in this book, when Swing is covered. NOTE For information on building applets when using Swing, see Chapter 30. Applet Basics Chapter 13 introduced the general form of an applet and the steps necessary to compile and run one. Let’s begin by reviewing this information. All applets are subclasses (either directly or indirectly) of Applet. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. The illustrations shown in this chapter were created with the standard applet viewer, called appletviewer, provided by the JDK. But you can use any applet viewer or browser you like. 687 688 PART II The Java Library Execution of an applet does not begin at main( ). Actually, few applets even have main( ) methods. Instead, execution of an applet is started and controlled with an entirely different mechanism, which will be explained shortly. Output to your applet’s window is not performed by System.out.println( ). Rather, in non-Swing applets, output is handled with various AWT methods, such as drawString( ), which outputs a string to a specified X,Y location. Input is also handled differently than in a console application. (Remember, Swing-based applets use the Swing classes to handle user interactions, and they are described later in this book.) Before an applet can be used, a deployment strategy must be chosen. There are two basic approaches. The first is to use the Java Network Launch Protocol (JNLP). This approach offers the most flexibility, especially as it relates to rich Internet applications. For real-world applets that you create, JNLP will often be the best choice. However, a detailed discussion of JNLP is beyond the scope of this book. (See the JDK documentation for the latest details on JNLP.) Fortunately, JNLP is not required for the example applets shown here. The second basic approach to deploying an applet is to specify the applet directly in an HTML file, without the use of JNLP. This is the original way that applets were launched when Java was created, and it is still used today—especially for simple applets. Furthermore, because of its inherent simplicity, it is the appropriate method for the applet examples described in this book. At the time of this writing, Oracle recommends the APPLET tag for this purpose. Therefore, the APPLET tag is used in this book. (Be aware that the APPLET tag is currently deprecated by the HTML specification. The alternative is the OBJECT tag. You should check the JDK documentation in this regard for the latest recommendations.) When an APPLET tag is encountered in the HTML file, the specified applet will be executed by a Java-enabled web browser. The use of the APPLET tag offers a secondary advantage when developing applets because it enables you to easily view and test the applet. To do so, simply include a comment at the head of your Java source code file that contains the APPLET tag. This way, your code is documented with the necessary HTML statements needed by your applet, and you can test the compiled applet by starting the applet viewer with your Java source code file specified as the target. Here is an example of such a comment: /* */ This comment contains an APPLET tag that will run an applet called MyApplet in a window that is 200 pixels wide and 60 pixels high. Because the inclusion of an APPLET command makes testing applets easier, all of the applets shown in this book will contain the appropriate APPLET tag embedded in a comment. The Applet Class The Applet class defines the methods shown in Table 22-1. Applet provides all necessary support for applet execution, such as starting and stopping. It also provides methods that load and display images, and methods that load and play audio clips. Applet extends the AWT class Panel. In turn, Panel extends Container, which extends Component. These classes provide support for Java’s window-based, graphical interface. Thus, Applet provides all of the necessary support for window-based activities. (The AWT is described in detail in following chapters.) The Applet Class 689 Method Description void destroy( ) Called by the browser just before an applet is terminated. Your applet will override this method if it needs to perform any cleanup prior to its destruction. AccessibleContext getAccessibleContext( ) Returns the accessibility context for the invoking object. AppletContext getAppletContext( ) Returns the context associated with the applet. String getAppletInfo( ) Overrides of this method should return a string that describes the applet. The default implementation returns null. AudioClip getAudioClip(URL url) Returns an AudioClip object that encapsulates the audio clip found at the location specified by url. AudioClip getAudioClip(URL url, String clipName) Returns an AudioClip object that encapsulates the audio clip found at the location specified by url and having the name specified by clipName. URL getCodeBase( ) Returns the URL associated with the invoking applet. URL getDocumentBase( ) Returns the URL of the HTML document that invokes the applet. Image getImage(URL url) Returns an Image object that encapsulates the image found at the location specified by url. Image getImage(URL url, String imageName) Returns an Image object that encapsulates the image found at the location specified by url and having the name specified by imageName. Locale getLocale( ) Returns a Locale object that is used by various locale-sensitive classes and methods. String getParameter(String paramName) Returns the parameter associated with paramName. null is returned if the specified parameter is not found. String[ ] [ ] getParameterInfo( ) Overrides of this method should return a String table that describes the parameters recognized by the applet. Each entry in the table must consist of three strings that contain the name of the parameter, a description of its type and/or range, and an explanation of its purpose. The default implementation returns null. void init( ) Called when an applet begins execution. It is the first method called for any applet. boolean isActive( ) Returns true if the applet has been started. It returns false if the applet has been stopped. boolean isValidateRoot( ) Returns true, which indicates that an applet is a validate root. (Added by JDK 7.) Table 22-1 The Methods Defined by Applet Part II Chapter 22 690 PART II The Java Library Method Description static final AudioClip newAudioClip(URL url) Returns an AudioClip object that encapsulates the audio clip found at the location specified by url. This method is similar to getAudioClip( ) except that it is static and can be executed without the need for an Applet object. void play(URL url) If an audio clip is found at the location specified by url, the clip is played. void play(URL url, String clipName) If an audio clip is found at the location specified by url with the name specified by clipName, the clip is played. void resize(Dimension dim) Resizes the applet according to the dimensions specified by dim. Dimension is a class stored inside java.awt. It contains two integer fields: width and height. void resize(int width, int height) Resizes the applet according to the dimensions specified by width and height. final void setStub(AppletStub stubObj) Makes stubObj the stub for the applet. This method is used by the run-time system and is not usually called by your applet. A stub is a small piece of code that provides the linkage between your applet and the browser. void showStatus(String str) Displays str in the status window of the browser or applet viewer. If the browser does not support a status window, then no action takes place. void start( ) Called by the browser when an applet should start (or resume) execution. It is automatically called after init( ) when an applet first begins. void stop( ) Called by the browser to suspend execution of the applet. Once stopped, an applet is restarted when the browser calls start( ). Table 22-1 The Methods Defined by Applet (continued) Applet Architecture As a general rule, an applet is a GUI-based program. As such, its architecture is different from the console-based programs shown in the first part of this book. If you are already familiar with GUI programming, you will be right at home writing applets. If not, then there are a few key concepts you must understand. First, applets are event driven. Although we won’t examine event handling until the following chapter, it is important to understand in a general way how the event-driven architecture impacts the design of an applet. An applet resembles a set of interrupt service routines. Here is how the process works. An applet waits until an event occurs. The runtime system notifies the applet about an event by calling an event handler that has been provided by the applet. Once this happens, the applet must take appropriate action and The Applet Class 691 then quickly return. This is a crucial point. For the most part, your applet should not enter a "mode" of operation in which it maintains control for an extended period. Instead, it must perform specific actions in response to events and then return control to the run-time system. In those situations in which your applet needs to perform a repetitive task on its own (for example, displaying a scrolling message across its window), you must start an additional thread of execution. (You will see an example later in this chapter.) Second, the user initiates interaction with an applet—not the other way around. As you know, in a console-based program, when the program needs input, it will prompt the user and then call some input method, such as readLine( ). This is not the way it works in an applet. Instead, the user interacts with the applet as he or she wants, when he or she wants. These interactions are sent to the applet as events to which the applet must respond. For example, when the user clicks the mouse inside the applet’s window, a mouse-clicked event is generated. If the user presses a key while the applet’s window has input focus, a keypress event is generated. As you will see in later chapters, applets can contain various controls, such as push buttons and check boxes. When the user interacts with one of these controls, an event is generated. While the architecture of an applet is not as easy to understand as that of a consolebased program, Java makes it as simple as possible. If you have written programs for Windows (or other GUI-based operating systems), you know how intimidating that environment can be. Fortunately, Java provides a much cleaner approach that is more quickly mastered. An Applet Skeleton All but the most trivial applets override a set of methods that provides the basic mechanism by which the browser or applet viewer interfaces to the applet and controls its execution. Four of these methods, init( ), start( ), stop( ), and destroy( ), apply to all applets and are defined by Applet. Default implementations for all of these methods are provided. Applets do not need to override those methods they do not use. However, only very simple applets will not need to define all of them. AWT-based applets (such as those discussed in this chapter) will also often override the paint( ) method, which is defined by the AWT Component class. This method is called when the applet’s output must be redisplayed. (Swing-based applets use a different mechanism to accomplish this task.) These five methods can be assembled into the skeleton shown here: // An Applet skeleton. import java.awt.*; import java.applet.*; /* */ public class AppletSkel extends Applet { // Called first. public void init() { // initialization } Part II Chapter 22 692 PART II The Java Library /* Called second, after init(). the applet is restarted. */ public void start() { // start or resume execution } Also called whenever // Called when the applet is stopped. public void stop() { // suspends execution } /* Called when applet is terminated. method executed. */ public void destroy() { // perform shutdown activities } This is the last // Called when an applet’s window must be restored. public void paint(Graphics g) { // redisplay contents of window } } Although this skeleton does not do anything, it can be compiled and run. When run, it generates the following window when viewed with an applet viewer: Applet Initialization and Termination It is important to understand the order in which the various methods shown in the skeleton are called. When an applet begins, the following methods are called, in this sequence: 1. init( ) 2. start( ) 3. paint( ) When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) 2. destroy( ) Let’s look more closely at these methods. Chapter 22 The Applet Class 693 init( ) The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet. start( ) paint( ) The paint( ) method is called each time your applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored. paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required. stop( ) The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page. destroy( ) The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ). Overriding update( ) In some situations, your applet may need to override another method defined by the AWT, called update( ). This method is called when your applet has requested that a portion of its window be redrawn. The default version of update( ) simply calls paint( ). However, you can override the update( ) method so that it performs more subtle repainting. In general, overriding update( ) is a specialized technique that is not applicable to all applets, and the examples in this chapter do not override update( ). Simple Applet Display Methods As we’ve mentioned, applets are displayed in a window, and AWT-based applets use the AWT to perform input and output. Although we will examine the methods, procedures, and techniques necessary to fully handle the AWT windowed environment in subsequent Part II The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ). 694 PART II The Java Library chapters, a few are described here, because we will use them to write sample applets. (Remember, Swing-based applets are described later in this book.) As described in Chapter 13, to output a string to an applet, use drawString( ), which is a member of the Graphics class. Typically, it is called from within either update( ) or paint( ). It has the following general form: void drawString(String message, int x, int y) Here, message is the string to be output beginning at x,y. In a Java window, the upper-left corner is location 0,0. The drawString( ) method will not recognize newline characters. If you want to start a line of text on another line, you must do so manually, specifying the precise X,Y location where you want the line to begin. (As you will see in later chapters, there are techniques that make this process easy.) To set the background color of an applet’s window, use setBackground( ). To set the foreground color (the color in which text is shown, for example), use setForeground( ). These methods are defined by Component, and they have the following general forms: void setBackground(Color newColor) void setForeground(Color newColor) Here, newColor specifies the new color. The class Color defines the constants shown here that can be used to specify colors: Color.black Color.magenta Color.blue Color.orange Color.cyan Color.pink Color.darkGray Color.red Color.gray Color.white Color.green Color.yellow Color.lightGray Uppercase versions of the constants are also defined. The following example sets the background color to green and the text color to red: setBackground(Color.green); setForeground(Color.red); A good place to set the foreground and background colors is in the init( ) method. Of course, you can change these colors as often as necessary during the execution of your applet. You can obtain the current settings for the background and foreground colors by calling getBackground( ) and getForeground( ), respectively. They are also defined by Component and are shown here: Color getBackground( ) Color getForeground( ) Chapter 22 The Applet Class 695 /* A simple applet that sets the foreground and background colors and outputs a string. */ import java.awt.*; import java.applet.*; /* */ public class Sample extends Applet{ String msg; // set the foreground and background colors. public void init() { setBackground(Color.cyan); setForeground(Color.red); msg = "Inside init( ) --"; } // Initialize the string to be displayed. public void start() { msg += " Inside start( ) --"; } // Display msg in applet window. public void paint(Graphics g) { msg += " Inside paint( )."; g.drawString(msg, 10, 30); } } This applet generates the window shown here: The methods stop( ) and destroy( ) are not overridden, because they are not needed by this simple applet. Requesting Repainting As a general rule, an applet writes to its window only when its update( ) or paint( ) method is called by the AWT. This raises an interesting question: How can the applet itself cause its window to be updated when its information changes? For example, if an applet is displaying Part II Here is a very simple applet that sets the background color to cyan, the foreground color to red, and displays a message that illustrates the order in which the init( ), start( ), and paint( ) methods are called when an applet starts up: 696 PART II The Java Library a moving banner, what mechanism does the applet use to update the window each time this banner scrolls? Remember, one of the fundamental architectural constraints imposed on an applet is that it must quickly return control to the run-time system. It cannot create a loop inside paint( ) that repeatedly scrolls the banner, for example. This would prevent control from passing back to the AWT. Given this constraint, it may seem that output to your applet’s window will be difficult at best. Fortunately, this is not the case. Whenever your applet needs to update the information displayed in its window, it simply calls repaint( ). The repaint( ) method is defined by the AWT. It causes the AWT run-time system to execute a call to your applet’s update( ) method, which, in its default implementation, calls paint( ). Thus, for another part of your applet to output to its window, simply store the output and then call repaint( ). The AWT will then execute a call to paint( ), which can display the stored information. For example, if part of your applet needs to output a string, it can store this string in a String variable and then call repaint( ). Inside paint( ), you will output the string using drawString( ). The repaint( ) method has four forms. Let’s look at each one, in turn. The simplest version of repaint( ) is shown here: void repaint( ) This version causes the entire window to be repainted. The following version specifies a region that will be repainted: void repaint(int left, int top, int width, int height) Here, the coordinates of the upper-left corner of the region are specified by left and top, and the width and height of the region are passed in width and height. These dimensions are specified in pixels. You save time by specifying a region to repaint. Window updates are costly in terms of time. If you need to update only a small portion of the window, it is more efficient to repaint only that region. Calling repaint( ) is essentially a request that your applet be repainted sometime soon. However, if your system is slow or busy, update( ) might not be called immediately. Multiple requests for repainting that occur within a short time can be collapsed by the AWT in a manner such that update( ) is only called sporadically. This can be a problem in many situations, including animation, in which a consistent update time is necessary. One solution to this problem is to use the following forms of repaint( ): void repaint(long maxDelay) void repaint(long maxDelay, int x, int y, int width, int height) Here, maxDelay specifies the maximum number of milliseconds that can elapse before update( ) is called. Beware, though. If the time elapses before update( ) can be called, it isn’t called. There’s no return value or exception thrown, so you must be careful. NOTE It is possible for a method other than paint( ) or update( ) to output to an applet’s window. To do so, it must obtain a graphics context by calling getGraphics( ) (defined by Component) and then use this context to output to the window. However, for most applications, it is better and easier to route window output through paint( ) and to call repaint( ) when the contents of the window change. Chapter 22 The Applet Class 697 A Simple Banner Applet To demonstrate repaint( ), a simple banner applet is developed. This applet scrolls a message, from right to left, across the applet’s window. Since the scrolling of the message is a repetitive task, it is performed by a separate thread, created by the applet when it is initialized. The banner applet is shown here: This applet creates a thread that scrolls the message contained in msg right to left across the applet’s window. */ import java.awt.*; import java.applet.*; /* */ public class SimpleBanner extends Applet implements Runnable { String msg = " A Simple Moving Banner."; Thread t = null; int state; volatile boolean stopFlag; // Set colors and initialize thread. public void init() { setBackground(Color.cyan); setForeground(Color.red); } // Start thread public void start() { t = new Thread(this); stopFlag = false; t.start(); } // Entry point for the thread that runs the banner. public void run() { // Redisplay banner for( ; ; ) { try { repaint(); Thread.sleep(250); if(stopFlag) break; } catch(InterruptedException e) {} } } Part II /* A simple banner applet. 698 PART II The Java Library // Pause the banner. public void stop() { stopFlag = true; t = null; } // Display the banner. public void paint(Graphics g) { char ch; ch = msg.charAt(0); msg = msg.substring(1, msg.length()); msg += ch; g.drawString(msg, 50, 30); } } Following is sample output: Let’s take a close look at how this applet operates. First, notice that SimpleBanner extends Applet, as expected, but it also implements Runnable. This is necessary, since the applet will be creating a second thread of execution that will be used to scroll the banner. Inside init( ), the foreground and background colors of the applet are set. After initialization, the run-time system calls start( ) to start the applet running. Inside start( ), a new thread of execution is created and assigned to the Thread variable t. Then, the boolean variable stopFlag, which controls the execution of the applet, is set to false. Next, the thread is started by a call to t.start( ). Remember that t.start( ) calls a method defined by Thread, which causes run( ) to begin executing. It does not cause a call to the version of start( ) defined by Applet. These are two separate methods. Inside run( ), a call to repaint( ) is made. This eventually causes the paint( ) method to be called, and the rotated contents of msg are displayed. Between each iteration, run( ) sleeps for a quarter of a second. The net effect is that the contents of msg are scrolled right to left in a constantly moving display. The stopFlag variable is checked on each iteration. When it is true, the run( ) method terminates. If a browser is displaying the applet when a new page is viewed, the stop( ) method is called, which sets stopFlag to true, causing run( ) to terminate. This is the mechanism used to stop the thread when its page is no longer in view. When the applet is brought back into view, start( ) is once again called, which starts a new thread to execute the banner. Chapter 22 The Applet Class 699 Using the Status Window // Using the Status Window. import java.awt.*; import java.applet.*; /* */ public class StatusWindow extends Applet { public void init() { setBackground(Color.cyan); } // Display msg in applet window. public void paint(Graphics g) { g.drawString("This is in the applet window.", 10, 20); showStatus("This is shown in the status window."); } } Sample output from this program is shown here: The HTML APPLET Tag As mentioned earlier, at the time of this writing, Oracle recommends that the APPLET tag be used to manually start an applet when JNLP is not used. An applet viewer will execute each APPLET tag that it finds in a separate window, while web browsers will allow many applets on a single page. So far, we have been using only a simplified form of the APPLET tag. Now it is time to take a closer look at it. Part II In addition to displaying information in its window, an applet can also output a message to the status window of the browser or applet viewer on which it is running. To do so, call showStatus( ) with the string that you want displayed. The status window is a good place to give the user feedback about what is occurring in the applet, suggest options, or possibly report some types of errors. The status window also makes an excellent debugging aid, because it gives you an easy way to output information about your applet. The following applet demonstrates showStatus( ): 700 PART II The Java Library The syntax for a fuller form of the APPLET tag is shown here. Bracketed items are optional. < APPLET [CODEBASE = codebaseURL] CODE = appletFile [ALT = alternateText] [NAME = appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment ] [VSPACE = pixels] [HSPACE = pixels] > [< PARAM NAME = AttributeName VALUE = AttributeValue>] [< PARAM NAME = AttributeName2 VALUE = AttributeValue>] ... [HTML Displayed in the absence of Java] Let’s take a look at each part now. CODEBASE CODEBASE is an optional attribute that specifies the base URL of the applet code, which is the directory that will be searched for the applet’s executable class file (specified by the CODE tag). The HTML document’s URL directory is used as the CODEBASE if this attribute is not specified. The CODEBASE does not have to be on the host from which the HTML document was read. CODE CODE is a required attribute that gives the name of the file containing your applet’s compiled .class file. This file is relative to the code base URL of the applet, which is the directory that the HTML file was in or the directory indicated by CODEBASE if set. ALT The ALT tag is an optional attribute used to specify a short text message that should be displayed if the browser recognizes the APPLET tag but can’t currently run Java applets. This is distinct from the alternate HTML you provide for browsers that don’t support applets. NAME NAME is an optional attribute used to specify a name for the applet instance. Applets must be named in order for other applets on the same page to find them by name and communicate with them. To obtain an applet by name, use getApplet( ), which is defined by the AppletContext interface. WIDTH and HEIGHT WIDTH and HEIGHT are required attributes that give the size (in pixels) of the applet display area. ALIGN ALIGN is an optional attribute that specifies the alignment of the applet. This attribute is treated the same as the HTML IMG tag with these possible values: LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and ABSBOTTOM. Chapter 22 The Applet Class 701 VSPACE and HSPACE These attributes are optional. VSPACE specifies the space, in pixels, above and below the applet. HSPACE specifies the space, in pixels, on each side of the applet. They’re treated the same as the IMG tag’s VSPACE and HSPACE attributes. PARAM NAME and VALUE Other valid APPLET attributes include ARCHIVE, which lets you specify one or more archive files, and OBJECT, which specifies a saved version of the applet. In general, an APPLET tag should include only a CODE or an OBJECT attribute, but not both. Passing Parameters to Applets As just discussed, the APPLET tag allows you to pass parameters to your applet. To retrieve a parameter, use the getParameter( ) method. It returns the value of the specified parameter in the form of a String object. Thus, for numeric and boolean values, you will need to convert their string representations into their internal formats. Here is an example that demonstrates passing parameters: // Use Parameters import java.awt.*; import java.applet.*; /* */ public class ParamDemo extends Applet { String fontName; int fontSize; float leading; boolean active; // Initialize the string to be displayed. public void start() { String param; fontName = getParameter("fontName"); if(fontName == null) fontName = "Not Found"; param = getParameter("fontSize"); try { if(param != null) fontSize = Integer.parseInt(param); else fontSize = 0; Part II The PARAM tag allows you to specify applet-specific arguments. Applets access their attributes with the getParameter( ) method. 702 PART II The Java Library } catch(NumberFormatException e) { fontSize = -1; } param = getParameter("leading"); try { if(param != null) leading = Float.valueOf(param).floatValue(); else leading = 0; } catch(NumberFormatException e) { leading = -1; } param = getParameter("accountEnabled"); if(param != null) active = Boolean.valueOf(param).booleanValue(); } // Display parameters. public void paint(Graphics g) { g.drawString("Font name: " + fontName, 0, 10); g.drawString("Font size: " + fontSize, 0, 26); g.drawString("Leading: " + leading, 0, 42); g.drawString("Account Active: " + active, 0, 58); } } Sample output from this program is shown here: As the program shows, you should test the return values from getParameter( ). If a parameter isn’t available, getParameter( ) will return null. Also, conversions to numeric types must be attempted in a try statement that catches NumberFormatException. Uncaught exceptions should never occur within an applet. Improving the Banner Applet It is possible to use a parameter to enhance the banner applet shown earlier. In the previous version, the message being scrolled was hard-coded into the applet. However, passing the message as a parameter allows the banner applet to display a different message each time it is executed. This improved version is shown here. Notice that the APPLET tag at the top of the file now specifies a parameter called message that is linked to a quoted string. // A parameterized banner import java.awt.*; import java.applet.*; /* */ Chapter 22 The Applet Class 703 public class ParamBanner extends Applet implements Runnable { String msg; Thread t = null; int state; volatile boolean stopFlag; // Start thread public void start() { msg = getParameter("message"); if(msg == null) msg = "Message not found."; msg = " " + msg; t = new Thread(this); stopFlag = false; t.start(); } // Entry point for the thread that runs the banner. public void run() { // Redisplay banner for( ; ; ) { try { repaint(); Thread.sleep(250); if(stopFlag) break; } catch(InterruptedException e) {} } } // Pause the banner. public void stop() { stopFlag = true; t = null; } // Display the banner. public void paint(Graphics g) { char ch; ch = msg.charAt(0); msg = msg.substring(1, msg.length()); msg += ch; g.drawString(msg, 50, 30); } } Part II // Set colors and initialize thread. public void init() { setBackground(Color.cyan); setForeground(Color.red); } 704 PART II The Java Library getDocumentBase( ) and getCodeBase( ) Often, you will create applets that will need to explicitly load media and text. Java will allow the applet to load data from the directory holding the HTML file that started the applet (the document base) and the directory from which the applet’s class file was loaded (the code base). These directories are returned as URL objects (described in Chapter 20) by getDocumentBase( ) and getCodeBase( ). They can be concatenated with a string that names the file you want to load. To actually load another file, you will use the showDocument( ) method defined by the AppletContext interface, discussed in the next section. The following applet illustrates these methods: // Display code and document bases. import java.awt.*; import java.applet.*; import java.net.*; /* */ public class Bases extends Applet { // Display code and document bases. public void paint(Graphics g) { String msg; URL url = getCodeBase(); // get code base msg = "Code base: " + url.toString(); g.drawString(msg, 10, 20); url = getDocumentBase(); // get document base msg = "Document base: " + url.toString(); g.drawString(msg, 10, 40); } } Sample output from this program is shown here: AppletContext and showDocument( ) One application of Java is to use active images and animation to provide a graphical means of navigating the Web that is more interesting than simple text-based links. To allow your applet to transfer control to another URL, you must use the showDocument( ) method The Applet Class 705 defined by the AppletContext interface. AppletContext is an interface that lets you get information from the applet’s execution environment. The methods defined by AppletContext are shown in Table 22-2. The context of the currently executing applet is obtained by a call to the getAppletContext( ) method defined by Applet. Within an applet, once you have obtained the applet’s context, you can bring another document into view by calling showDocument( ). This method has no return value and throws no exception if it fails, so use it carefully. There are two showDocument( ) methods. The method showDocument(URL) displays the document at the specified URL. The method showDocument(URL, String) displays the specified document at the specified location within the browser window. Valid arguments for where are "_self" (show in current frame), "_parent" (show in parent frame), "_top" (show in topmost frame), and "_blank" (show in new browser window). You can also specify a name, which causes the document to be shown in a new browser window by that name. Method Description Applet getApplet(String appletName) Returns the applet specified by appletName if it is within the current applet context. Otherwise, null is returned. Enumeration getApplets( ) Returns an enumeration that contains all of the applets within the current applet context. AudioClip getAudioClip(URL url) Returns an AudioClip object that encapsulates the audio clip found at the location specified by url. Image getImage(URL url) Returns an Image object that encapsulates the image found at the location specified by url. InputStream getStream(String key) Returns the stream linked to key. Keys are linked to streams by using the setStream( ) method. A null reference is returned if no stream is linked to key. Iterator getStreamKeys( ) Returns an iterator for the keys associated with the invoking object. The keys are linked to streams. See getStream( ) and setStream( ). void setStream(String key, InputStream strm) throws IOException Links the stream specified by strm to the key passed in key. The key is deleted from the invoking object if strm is null. void showDocument(URL url) Brings the document at the URL specified by url into view. This method may not be supported by applet viewers. void showDocument(URL url, String where) Brings the document at the URL specified by url into view. This method may not be supported by applet viewers. The placement of the document is specified by where as described in the text. void showStatus(String str) Displays str in the status window. Table 22-2 The Methods Defined by the AppletContext Interface Part II Chapter 22 706 PART II The Java Library The following applet demonstrates AppletContext and showDocument( ). Upon execution, it obtains the current applet context and uses that context to transfer control to a file called Test.html. This file must be in the same directory as the applet. Test.html can contain any valid hypertext that you like. /* Using an applet context, getCodeBase(), and showDocument() to display an HTML file. */ import java.awt.*; import java.applet.*; import java.net.*; /* */ public class ACDemo extends Applet { public void start() { AppletContext ac = getAppletContext(); URL url = getCodeBase(); // get url of this applet try { ac.showDocument(new URL(url+"Test.html")); } catch(MalformedURLException e) { showStatus("URL not found"); } } } The AudioClip Interface The AudioClip interface defines these methods: play( ) (play a clip from the beginning), stop( ) (stop playing the clip), and loop( ) (play the loop continuously). After you have loaded an audio clip using getAudioClip( ), you can use these methods to play it. The AppletStub Interface The AppletStub interface provides the means by which an applet and the browser (or applet viewer) communicate. Your code will not typically implement this interface. Outputting to the Console Although output to an applet’s window must be accomplished through GUI-based methods, such as drawString( ), it is still possible to use console output in your applet—especially for debugging purposes. In an applet, when you call a method such as System.out.println( ), the output is not sent to your applet’s window. Instead, it appears either in the console session in which you launched the applet viewer or in the Java console that is available in some browsers. Use of console output for purposes other than debugging is discouraged, since it violates the design principles of the graphical interface most users will expect. CHAPTER 23 Event Handling This chapter examines an important aspect of Java: the event. Event handling is fundamental to Java programming because it is integral to the creation of applets and other types of GUI-based programs. As explained in Chapter 22, applets are event-driven programs that use a graphical user interface to interact with the user. Furthermore, any program that uses a graphical user interface, such as a Java application written for Windows, is event driven. Thus, you cannot write these types of programs without a solid command of event handling. Events are supported by a number of packages, including java.util, java.awt, and java.awt.event. Most events to which your program will respond are generated when the user interacts with a GUI-based program. These are the types of events examined in this chapter. They are passed to your program in a variety of ways, with the specific method dependent upon the actual event. There are several types of events, including those generated by the mouse, the keyboard, and various GUI controls, such as a push button, scroll bar, or check box. This chapter begins with an overview of Java’s event handling mechanism. It then examines the main event classes and interfaces used by the AWT and develops several examples that demonstrate the fundamentals of event processing. This chapter also explains how to use adapter classes, inner classes, and anonymous inner classes to streamline event handling code. The examples provided in the remainder of this book make frequent use of these techniques. NOTE This chapter focuses on events related to GUI-based programs. However, events are also occasionally used for purposes not directly related to GUI-based programs. In all cases, the same basic event handling techniques apply. Two Event Handling Mechanisms Before beginning our discussion of event handling, an important point must be made: The way in which events are handled changed significantly between the original version of Java (1.0) and modern versions of Java, beginning with version 1.1. The 1.0 method of event handling is still supported, but it is not recommended for new programs. Also, many of the 707 708 PART II The Java Library methods that support the old 1.0 event model have been deprecated. The modern approach is the way that events should be handled by all new programs and thus is the method employed by programs in this book. The Delegation Event Model The modern approach to handling events is based on the delegation event model, which defines standard and consistent mechanisms to generate and process events. Its concept is quite simple: a source generates an event and sends it to one or more listeners. In this scheme, the listener simply waits until it receives an event. Once an event is received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. A user interface element is able to “delegate” the processing of an event to a separate piece of code. In the delegation event model, listeners must register with a source in order to receive an event notification. This provides an important benefit: notifications are sent only to listeners that want to receive them. This is a more efficient way to handle events than the design used by the old Java 1.0 approach. Previously, an event was propagated up the containment hierarchy until it was handled by a component. This required components to receive events that they did not process, and it wasted valuable time. The delegation event model eliminates this overhead. NOTE Java also allows you to process events without using the delegation event model. This can be done by extending an AWT component. This technique is discussed at the end of Chapter 25. However, the delegation event model is the preferred design for the reasons just cited. The following sections define events and describe the roles of sources and listeners. Events In the delegation model, an event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse. Many other user operations could also be cited as examples. Events may also occur that are not directly caused by interactions with a user interface. For example, an event may be generated when a timer expires, a counter exceeds a value, a software or hardware failure occurs, or an operation is completed. You are free to define events that are appropriate for your application. Event Sources A source is an object that generates an event. This occurs when the internal state of that object changes in some way. Sources may generate more than one type of event. A source must register listeners in order for the listeners to receive notifications about a specific type of event. Each type of event has its own registration method. Here is the general form: public void addTypeListener (TypeListener el ) Chapter 23 Event Handling 709 public void addTypeListener(TypeListener el ) throws java.util.TooManyListenersException Here, Type is the name of the event, and el is a reference to the event listener. When such an event occurs, the registered listener is notified. This is known as unicasting the event. A source must also provide a method that allows a listener to unregister an interest in a specific type of event. The general form of such a method is this: public void removeTypeListener(TypeListener el ) Here, Type is the name of the event, and el is a reference to the event listener. For example, to remove a keyboard listener, you would call removeKeyListener( ). The methods that add or remove listeners are provided by the source that generates events. For example, the Component class provides methods to add and remove keyboard and mouse event listeners. Event Listeners A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. The methods that receive and process events are defined in a set of interfaces found in java.awt.event. For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved. Any object may receive and process one or both of these events if it provides an implementation of this interface. Many other listener interfaces are discussed later in this and other chapters. Event Classes The classes that represent events are at the core of Java’s event handling mechanism. Thus, a discussion of event handling must begin with the event classes. It is important to understand, however, that Java defines several types of events and that not all event classes can be discussed in this chapter. The most widely used events are those defined by the AWT and those defined by Swing. This chapter focuses on the AWT events. (Most of these events also apply to Swing.) Several Swing-specific events are described in Chapter 30, when Swing is covered. At the root of the Java event class hierarchy is EventObject, which is in java.util. It is the superclass for all events. Its one constructor is shown here: EventObject(Object src) Here, src is the object that generates this event. Part II Here, Type is the name of the event, and el is a reference to the event listener. For example, the method that registers a keyboard event listener is called addKeyListener( ). The method that registers a mouse motion listener is called addMouseMotionListener( ). When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event. In all cases, notifications are sent only to listeners that register to receive them. Some sources may allow only one listener to register. The general form of such a method is this: 710 PART II The Java Library EventObject contains two methods: getSource( ) and toString( ). The getSource( ) method returns the source of the event. Its general form is shown here: Object getSource( ) As expected, toString( ) returns the string equivalent of the event. The class AWTEvent, defined within the java.awt package, is a subclass of EventObject. It is the superclass (either directly or indirectly) of all AWT-based events used by the delegation event model. Its getID( ) method can be used to determine the type of the event. The signature of this method is shown here: int getID( ) Additional details about AWTEvent are provided at the end of Chapter 25. At this point, it is important to know only that all of the other classes discussed in this section are subclasses of AWTEvent. To summarize: • EventObject is a superclass of all events. • AWTEvent is a superclass of all AWT events that are handled by the delegation event model. The package java.awt.event defines many types of events that are generated by various user interface elements. Table 23-1 shows several commonly used event classes and provides a brief description of when they are generated. Commonly used constructors and methods in each class are described in the following sections. Event Class Description ActionEvent Generated when a button is pressed, a list item is double-clicked, or a menu item is selected. AdjustmentEvent Generated when a scroll bar is manipulated. ComponentEvent Generated when a component is hidden, moved, resized, or becomes visible. ContainerEvent Generated when a component is added to or removed from a container. FocusEvent Generated when a component gains or loses keyboard focus. InputEvent Abstract superclass for all component input event classes. ItemEvent Generated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected. KeyEvent Generated when input is received from the keyboard. MouseEvent Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component. MouseWheelEvent Generated when the mouse wheel is moved. TextEvent Generated when the value of a text area or text field is changed. WindowEvent Generated when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. Table 23-1 Commonly Used Event Classes in java.awt.event Chapter 23 Event Handling 711 The ActionEvent Class ActionEvent(Object src, int type, String cmd) ActionEvent(Object src, int type, String cmd, int modifiers) ActionEvent(Object src, int type, String cmd, long when, int modifiers) Here, src is a reference to the object that generated this event. The type of the event is specified by type, and its command string is cmd. The argument modifiers indicates which modifier keys (alt, ctrl, meta, and/or shift) were pressed when the event was generated. The when parameter specifies when the event occurred. You can obtain the command name for the invoking ActionEvent object by using the getActionCommand( ) method, shown here: String getActionCommand( ) For example, when a button is pressed, an action event is generated that has a command name equal to the label on that button. The getModifiers( ) method returns a value that indicates which modifier keys (alt, ctrl, meta, and/or shift) were pressed when the event was generated. Its form is shown here: int getModifiers( ) The method getWhen( ) returns the time at which the event took place. This is called the event’s timestamp. The getWhen( ) method is shown here: long getWhen( ) The AdjustmentEvent Class An AdjustmentEvent is generated by a scroll bar. There are five types of adjustment events. The AdjustmentEvent class defines integer constants that can be used to identify them. The constants and their meanings are shown here: BLOCK_DECREMENT The user clicked inside the scroll bar to decrease its value. BLOCK_INCREMENT The user clicked inside the scroll bar to increase its value. TRACK The slider was dragged. UNIT_DECREMENT The button at the end of the scroll bar was clicked to decrease its value. UNIT_INCREMENT The button at the end of the scroll bar was clicked to increase its value. In addition, there is an integer constant, ADJUSTMENT_VALUE_CHANGED, that indicates that a change has occurred. Part II An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a menu item is selected. The ActionEvent class defines four integer constants that can be used to identify any modifiers associated with an action event: ALT_MASK, CTRL_MASK, META_MASK, and SHIFT_MASK. In addition, there is an integer constant, ACTION_PERFORMED, which can be used to identify action events. ActionEvent has these three constructors: 712 PART II The Java Library Here is one AdjustmentEvent constructor: AdjustmentEvent(Adjustable src, int id, int type, int data) Here, src is a reference to the object that generated this event. The id specifies the event. The type of the adjustment is specified by type, and its associated data is data. The getAdjustable( ) method returns the object that generated the event. Its form is shown here: Adjustable getAdjustable( ) The type of the adjustment event may be obtained by the getAdjustmentType( ) method. It returns one of the constants defined by AdjustmentEvent. The general form is shown here: int getAdjustmentType( ) The amount of the adjustment can be obtained from the getValue( ) method, shown here: int getValue( ) For example, when a scroll bar is manipulated, this method returns the value represented by that change. The ComponentEvent Class A ComponentEvent is generated when the size, position, or visibility of a component is changed. There are four types of component events. The ComponentEvent class defines integer constants that can be used to identify them. The constants and their meanings are shown here: COMPONENT_HIDDEN The component was hidden. COMPONENT_MOVED The component was moved. COMPONENT_RESIZED The component was resized. COMPONENT_SHOWN The component became visible. ComponentEvent has this constructor: ComponentEvent(Component src, int type) Here, src is a reference to the object that generated this event. The type of the event is specified by type. ComponentEvent is the superclass either directly or indirectly of ContainerEvent, FocusEvent, KeyEvent, MouseEvent, and WindowEvent, among others. The getComponent( ) method returns the component that generated the event. It is shown here: Component getComponent( ) The ContainerEvent Class A ContainerEvent is generated when a component is added to or removed from a container. There are two types of container events. The ContainerEvent class defines int constants that can be used to identify them: COMPONENT_ADDED and Chapter 23 Event Handling 713 COMPONENT_REMOVED. They indicate that a component has been added to or removed from the container. ContainerEvent is a subclass of ComponentEvent and has this constructor: ContainerEvent(Component src, int type, Component comp) Container getContainer( ) The getChild( ) method returns a reference to the component that was added to or removed from the container. Its general form is shown here: Component getChild( ) The FocusEvent Class A FocusEvent is generated when a component gains or loses input focus. These events are identified by the integer constants FOCUS_GAINED and FOCUS_LOST. FocusEvent is a subclass of ComponentEvent and has these constructors: FocusEvent(Component src, int type) FocusEvent(Component src, int type, boolean temporaryFlag) FocusEvent(Component src, int type, boolean temporaryFlag, Component other) Here, src is a reference to the component that generated this event. The type of the event is specified by type. The argument temporaryFlag is set to true if the focus event is temporary. Otherwise, it is set to false. (A temporary focus event occurs as a result of another user interface operation. For example, assume that the focus is in a text field. If the user moves the mouse to adjust a scroll bar, the focus is temporarily lost.) The other component involved in the focus change, called the opposite component, is passed in other. Therefore, if a FOCUS_GAINED event occurred, other will refer to the component that lost focus. Conversely, if a FOCUS_LOST event occurred, other will refer to the component that gains focus. You can determine the other component by calling getOppositeComponent( ), shown here: Component getOppositeComponent( ) The opposite component is returned. The isTemporary( ) method indicates if this focus change is temporary. Its form is shown here: boolean isTemporary( ) The method returns true if the change is temporary. Otherwise, it returns false. The InputEvent Class The abstract class InputEvent is a subclass of ComponentEvent and is the superclass for component input events. Its subclasses are KeyEvent and MouseEvent. Part II Here, src is a reference to the container that generated this event. The type of the event is specified by type, and the component that has been added to or removed from the container is comp. You can obtain a reference to the container that generated this event by using the getContainer ( ) method, shown here: 714 PART II The Java Library InputEvent defines several integer constants that represent any modifiers, such as the control key being pressed, that might be associated with the event. Originally, the InputEvent class defined the following eight values to represent the modifiers: ALT_MASK BUTTON2_MASK META_MASK ALT_GRAPH_MASK BUTTON3_MASK SHIFT_MASK BUTTON1_MASK CTRL_MASK However, because of possible conflicts between the modifiers used by keyboard events and mouse events, and other issues, the following extended modifier values were added: ALT_DOWN_MASK BUTTON2_DOWN_MASK META_DOWN_MASK ALT_GRAPH_DOWN_MASK BUTTON3_DOWN_MASK SHIFT_DOWN_MASK BUTTON1_DOWN_MASK CTRL_DOWN_MASK When writing new code, it is recommended that you use the new, extended modifiers rather than the original modifiers. To test if a modifier was pressed at the time an event is generated, use the isAltDown( ), isAltGraphDown( ), isControlDown( ), isMetaDown( ), and isShiftDown( ) methods. The forms of these methods are shown here: boolean isAltDown( ) boolean isAltGraphDown( ) boolean isControlDown( ) boolean isMetaDown( ) boolean isShiftDown( ) You can obtain a value that contains all of the original modifier flags by calling the getModifiers( ) method. It is shown here: int getModifiers( ) You can obtain the extended modifiers by calling getModifiersEx( ), which is shown here: int getModifiersEx( ) The ItemEvent Class An ItemEvent is generated when a check box or a list item is clicked or when a checkable menu item is selected or deselected. (Check boxes and list boxes are described later in this book.) There are two types of item events, which are identified by the following integer constants: DESELECTED The user deselected an item. SELECTED The user selected an item. Chapter 23 Event Handling 715 In addition, ItemEvent defines one integer constant, ITEM_STATE_CHANGED, that signifies a change of state. ItemEvent has this constructor: Here, src is a reference to the component that generated this event. For example, this might be a list or choice element. The type of the event is specified by type. The specific item that generated the item event is passed in entry. The current state of that item is in state. The getItem( ) method can be used to obtain a reference to the item that generated an event. Its signature is shown here: Object getItem( ) The getItemSelectable( ) method can be used to obtain a reference to the ItemSelectable object that generated an event. Its general form is shown here: ItemSelectable getItemSelectable( ) Lists and choices are examples of user interface elements that implement the ItemSelectable interface. The getStateChange( ) method returns the state change (that is, SELECTED or DESELECTED) for the event. It is shown here: int getStateChange( ) The KeyEvent Class A KeyEvent is generated when keyboard input occurs. There are three types of key events, which are identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED. The first two events are generated when any key is pressed or released. The last event occurs only when a character is generated. Remember, not all keypresses result in characters. For example, pressing shift does not generate a character. There are many other integer constants that are defined by KeyEvent. For example, VK_0 through VK_9 and VK_A through VK_Z define the ASCII equivalents of the numbers and letters. Here are some others: VK_ALT VK_DOWN VK_LEFT VK_RIGHT VK_CANCEL VK_ENTER VK_PAGE_DOWN VK_SHIFT VK_CONTROL VK_ESCAPE VK_PAGE_UP VK_UP The VK constants specify virtual key codes and are independent of any modifiers, such as control, shift, or alt. KeyEvent is a subclass of InputEvent. Here is one of its constructors: KeyEvent(Component src, int type, long when, int modifiers, int code, char ch) Part II ItemEvent(ItemSelectable src, int type, Object entry, int state) 716 PART II The Java Library Here, src is a reference to the component that generated the event. The type of the event is specified by type. The system time at which the key was pressed is passed in when. The modifiers argument indicates which modifiers were pressed when this key event occurred. The virtual key code, such as VK_UP, VK_A, and so forth, is passed in code. The character equivalent (if one exists) is passed in ch. If no valid character exists, then ch contains CHAR_UNDEFINED. For KEY_TYPED events, code will contain VK_UNDEFINED. The KeyEvent class defines several methods, but probably the most commonly used ones are getKeyChar( ), which returns the character that was entered, and getKeyCode( ), which returns the key code. Their general forms are shown here: char getKeyChar( ) int getKeyCode( ) If no valid character is available, then getKeyChar( ) returns CHAR_UNDEFINED. When a KEY_TYPED event occurs, getKeyCode( ) returns VK_UNDEFINED. The MouseEvent Class There are eight types of mouse events. The MouseEvent class defines the following integer constants that can be used to identify them: MOUSE_CLICKED The user clicked the mouse. MOUSE_DRAGGED The user dragged the mouse. MOUSE_ENTERED The mouse entered a component. MOUSE_EXITED The mouse exited from a component. MOUSE_MOVED The mouse moved. MOUSE_PRESSED The mouse was pressed. MOUSE_RELEASED The mouse was released. MOUSE_WHEEL The mouse wheel was moved. MouseEvent is a subclass of InputEvent. Here is one of its constructors: MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup) Here, src is a reference to the component that generated the event. The type of the event is specified by type. The system time at which the mouse event occurred is passed in when. The modifiers argument indicates which modifiers were pressed when a mouse event occurred. The coordinates of the mouse are passed in x and y. The click count is passed in clicks. The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform. Two commonly used methods in this class are getX( ) and getY( ). These return the X and Y coordinates of the mouse within the component when the event occurred. Their forms are shown here: int getX( ) int getY( ) Alternatively, you can use the getPoint( ) method to obtain the coordinates of the mouse. It is shown here: Point getPoint( ) Chapter 23 Event Handling 717 It returns a Point object that contains the X,Y coordinates in its integer members: x and y. The translatePoint( ) method changes the location of the event. Its form is shown here: void translatePoint(int x, int y) Here, the arguments x and y are added to the coordinates of the event. The getClickCount( ) method obtains the number of mouse clicks for this event. Its signature is shown here: The isPopupTrigger( ) method tests if this event causes a pop-up menu to appear on this platform. Its form is shown here: boolean isPopupTrigger( ) Also available is the getButton( ) method, shown here: int getButton( ) It returns a value that represents the button that caused the event. The return value will be one of these constants defined by MouseEvent: NOBUTTON BUTTON1 BUTTON2 BUTTON3 The NOBUTTON value indicates that no button was pressed or released. Also available are three methods that obtain the coordinates of the mouse relative to the screen rather than the component. They are shown here: Point getLocationOnScreen( ) int getXOnScreen( ) int getYOnScreen( ) The getLocationOnScreen( ) method returns a Point object that contains both the X and Y coordinate. The other two methods return the indicated coordinate. The MouseWheelEvent Class The MouseWheelEvent class encapsulates a mouse wheel event. It is a subclass of MouseEvent. Not all mice have wheels. If a mouse has a wheel, it is located between the left and right buttons. Mouse wheels are used for scrolling. MouseWheelEvent defines these two integer constants: WHEEL_BLOCK_SCROLL A page-up or page-down scroll event occurred. WHEEL_UNIT_SCROLL A line-up or line-down scroll event occurred. Here is one of the constructors defined by MouseWheelEvent: MouseWheelEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup, int scrollHow, int amount, int count) Part II int getClickCount( ) 718 PART II The Java Library Here, src is a reference to the object that generated the event. The type of the event is specified by type. The system time at which the mouse event occurred is passed in when. The modifiers argument indicates which modifiers were pressed when the event occurred. The coordinates of the mouse are passed in x and y. The number of clicks is passed in clicks. The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform. The scrollHow value must be either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_ SCROLL. The number of units to scroll is passed in amount. The count parameter indicates the number of rotational units that the wheel moved. MouseWheelEvent defines methods that give you access to the wheel event. To obtain the number of rotational units, call getWheelRotation( ), shown here: int getWheelRotation( ) It returns the number of rotational units. If the value is positive, the wheel moved counterclockwise. If the value is negative, the wheel moved clockwise. JDK 7 adds a method called getPreciseWheelRotation( ), which supports high-resolution wheels. It works like getWheelRotation( ), but returns a double. To obtain the type of scroll, call getScrollType( ), shown next: int getScrollType( ) It returns either WHEEL_UNIT_SCROLL or WHEEL_BLOCK_SCROLL. If the scroll type is WHEEL_UNIT_SCROLL, you can obtain the number of units to scroll by calling getScrollAmount( ). It is shown here: int getScrollAmount( ) The TextEvent Class Instances of this class describe text events. These are generated by text fields and text areas when characters are entered by a user or program. TextEvent defines the integer constant TEXT_VALUE_CHANGED. The one constructor for this class is shown here: TextEvent(Object src, int type) Here, src is a reference to the object that generated this event. The type of the event is specified by type. The TextEvent object does not include the characters currently in the text component that generated the event. Instead, your program must use other methods associated with the text component to retrieve that information. This operation differs from other event objects discussed in this section. For this reason, no methods are discussed here for the TextEvent class. Think of a text event notification as a signal to a listener that it should retrieve information from a specific text component. The WindowEvent Class There are ten types of window events. The WindowEvent class defines integer constants that can be used to identify them. The constants and their meanings are shown here: Event Handling WINDOW_ACTIVATED The window was activated. WINDOW_CLOSED The window has been closed. WINDOW_CLOSING The user requested that the window be closed. WINDOW_DEACTIVATED The window was deactivated. WINDOW_DEICONIFIED The window was deiconified. WINDOW_GAINED_FOCUS The window gained input focus. WINDOW_ICONIFIED The window was iconified. WINDOW_LOST_FOCUS The window lost input focus. WINDOW_OPENED The window was opened. WINDOW_STATE_CHANGED The state of the window changed. 719 WindowEvent is a subclass of ComponentEvent. It defines several constructors. The first is WindowEvent(Window src, int type) Here, src is a reference to the object that generated this event. The type of the event is type. The next three constructors offer more detailed control: WindowEvent(Window src, int type, Window other) WindowEvent(Window src, int type, int fromState, int toState) WindowEvent(Window src, int type, Window other, int fromState, int toState) Here, other specifies the opposite window when a focus or activation event occurs. The fromState specifies the prior state of the window, and toState specifies the new state that the window will have when a window state change occurs. A commonly used method in this class is getWindow( ). It returns the Window object that generated the event. Its general form is shown here: Window getWindow( ) WindowEvent also defines methods that return the opposite window (when a focus or activation event has occurred), the previous window state, and the current window state. These methods are shown here: Window getOppositeWindow( ) int getOldState( ) int getNewState( ) Sources of Events Table 23-2 lists some of the user interface components that can generate the events described in the previous section. In addition to these graphical user interface elements, any class derived from Component, such as Applet, can generate events. For example, you can receive key and mouse events from an applet. (You may also build your own components that generate events.) In this chapter, we will be handling only mouse and keyboard events, but the following two chapters will be handling events from the sources shown in Table 23-2. Part II Chapter 23 720 PART II The Java Library Event Source Description Button Generates action events when the button is pressed. Check box Generates item events when the check box is selected or deselected. Choice Generates item events when the choice is changed. List Generates action events when an item is double-clicked; generates item events when an item is selected or deselected. Menu item Generates action events when a menu item is selected; generates item events when a checkable menu item is selected or deselected. Scroll bar Generates adjustment events when the scroll bar is manipulated. Text components Generates text events when the user enters a character. Window Generates window events when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. Table 23-2 Event Source Examples Event Listener Interfaces As explained, the delegation event model has two parts: sources and listeners. Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument. Table 23-3 lists commonly used Interface Description ActionListener Defines one method to receive action events. AdjustmentListener Defines one method to receive adjustment events. ComponentListener Defines four methods to recognize when a component is hidden, moved, resized, or shown. ContainerListener Defines two methods to recognize when a component is added to or removed from a container. FocusListener Defines two methods to recognize when a component gains or loses keyboard focus. ItemListener Defines one method to recognize when the state of an item changes. KeyListener Defines three methods to recognize when a key is pressed, released, or typed. MouseListener Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released. MouseMotionListener Defines two methods to recognize when the mouse is dragged or moved. MouseWheelListener Defines one method to recognize when the mouse wheel is moved. TextListener Defines one method to recognize when a text value changes. WindowFocusListener Defines two methods to recognize when a window gains or loses input focus. WindowListener Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. Table 23-3 Commonly Used Event Listener Interfaces Chapter 23 Event Handling 721 listener interfaces and provides a brief description of the methods that they define. The following sections examine the specific methods that are contained in each interface. The ActionListener Interface This interface defines the actionPerformed( ) method that is invoked when an action event occurs. Its general form is shown here: The AdjustmentListener Interface This interface defines the adjustmentValueChanged( ) method that is invoked when an adjustment event occurs. Its general form is shown here: void adjustmentValueChanged(AdjustmentEvent ae) The ComponentListener Interface This interface defines four methods that are invoked when a component is resized, moved, shown, or hidden. Their general forms are shown here: void componentResized(ComponentEvent ce) void componentMoved(ComponentEvent ce) void componentShown(ComponentEvent ce) void componentHidden(ComponentEvent ce) The ContainerListener Interface This interface contains two methods. When a component is added to a container, componentAdded( ) is invoked. When a component is removed from a container, componentRemoved( ) is invoked. Their general forms are shown here: void componentAdded(ContainerEvent ce) void componentRemoved(ContainerEvent ce) The FocusListener Interface This interface defines two methods. When a component obtains keyboard focus, focusGained( ) is invoked. When a component loses keyboard focus, focusLost( ) is called. Their general forms are shown here: void focusGained(FocusEvent fe) void focusLost(FocusEvent fe) The ItemListener Interface This interface defines the itemStateChanged( ) method that is invoked when the state of an item changes. Its general form is shown here: void itemStateChanged(ItemEvent ie) Part II void actionPerformed(ActionEvent ae) 722 PART II The Java Library The KeyListener Interface This interface defines three methods. The keyPressed( ) and keyReleased( ) methods are invoked when a key is pressed and released, respectively. The keyTyped( ) method is invoked when a character has been entered. For example, if a user presses and releases the a key, three events are generated in sequence: key pressed, typed, and released. If a user presses and releases the home key, two key events are generated in sequence: key pressed and released. The general forms of these methods are shown here: void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke) The MouseListener Interface This interface defines five methods. If the mouse is pressed and released at the same point, mouseClicked( ) is invoked. When the mouse enters a component, the mouseEntered( ) method is called. When it leaves, mouseExited( ) is called. The mousePressed( ) and mouseReleased( ) methods are invoked when the mouse is pressed and released, respectively. The general forms of these methods are shown here: void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me) The MouseMotionListener Interface This interface defines two methods. The mouseDragged( ) method is called multiple times as the mouse is dragged. The mouseMoved( ) method is called multiple times as the mouse is moved. Their general forms are shown here: void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me) The MouseWheelListener Interface This interface defines the mouseWheelMoved( ) method that is invoked when the mouse wheel is moved. Its general form is shown here: void mouseWheelMoved(MouseWheelEvent mwe) The TextListener Interface This interface defines the textChanged( ) method that is invoked when a change occurs in a text area or text field. Its general form is shown here: void textChanged(TextEvent te) Chapter 23 Event Handling 723 The WindowFocusListener Interface This interface defines two methods: windowGainedFocus( ) and windowLostFocus( ). These are called when a window gains or loses input focus. Their general forms are shown here: void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we) This interface defines seven methods. The windowActivated( ) and windowDeactivated( ) methods are invoked when a window is activated or deactivated, respectively. If a window is iconified, the windowIconified( ) method is called. When a window is deiconified, the windowDeiconified( ) method is called. When a window is opened or closed, the windowOpened( ) or windowClosed( ) methods are called, respectively. The windowClosing( ) method is called when a window is being closed. The general forms of these methods are void windowActivated(WindowEvent we) void windowClosed(WindowEvent we) void windowClosing(WindowEvent we) void windowDeactivated(WindowEvent we) void windowDeiconified(WindowEvent we) void windowIconified(WindowEvent we) void windowOpened(WindowEvent we) Using the Delegation Event Model Now that you have learned the theory behind the delegation event model and have had an overview of its various components, it is time to see it in practice. Using the delegation event model is actually quite easy. Just follow these two steps: 1. Implement the appropriate interface in the listener so that it will receive the type of event desired. 2. Implement code to register and unregister (if necessary) the listener as a recipient for the event notifications. Remember that a source may generate several types of events. Each event must be registered separately. Also, an object may register to receive several types of events, but it must implement all of the interfaces that are required to receive these events. To see how the delegation model works in practice, we will look at examples that handle two commonly used event generators: the mouse and keyboard. Handling Mouse Events To handle mouse events, you must implement the MouseListener and the MouseMotionListener interfaces. (You may also want to implement MouseWheelListener, but we won’t be doing so, here.) The following applet demonstrates the process. It displays Part II The WindowListener Interface 724 PART II The Java Library the current coordinates of the mouse in the applet’s status window. Each time a button is pressed, the word "Down" is displayed at the location of the mouse pointer. Each time the button is released, the word "Up" is shown. If a button is clicked, the message "Mouse clicked" is displayed in the upper-left corner of the applet display area. As the mouse enters or exits the applet window, a message is displayed in the upper-left corner of the applet display area. When dragging the mouse, a * is shown, which tracks with the mouse pointer as it is dragged. Notice that the two variables, mouseX and mouseY, store the location of the mouse when a mouse pressed, released, or dragged event occurs. These coordinates are then used by paint( ) to display output at the point of these occurrences. // Demonstrate the mouse event handlers. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int mouseX = 0, mouseY = 0; // coordinates of mouse public void init() { addMouseListener(this); addMouseMotionListener(this); } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse clicked."; repaint(); } // Handle mouse entered. public void mouseEntered(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse entered."; repaint(); } Chapter 23 Event Handling 725 // Handle button pressed. public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); } // Handle button released. public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "*"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); } // Handle mouse moved. public void mouseMoved(MouseEvent me) { // show status showStatus("Moving mouse at " + me.getX() + ", " + me.getY()); } // Display msg in applet window at current X,Y location. public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); } } Part II // Handle mouse exited. public void mouseExited(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint(); } 726 PART II The Java Library Sample output from this program is shown here: Let's look closely at this example. The MouseEvents class extends Applet and implements both the MouseListener and MouseMotionListener interfaces. These two interfaces contain methods that receive and process the various types of mouse events. Notice that the applet is both the source and the listener for these events. This works because Component, which supplies the addMouseListener( ) and addMouseMotionListener( ) methods, is a superclass of Applet. Being both the source and the listener for events is a common situation for applets. Inside init( ), the applet registers itself as a listener for mouse events. This is done by using addMouseListener( ) and addMouseMotionListener( ), which, as mentioned, are members of Component. They are shown here: void addMouseListener(MouseListener ml) void addMouseMotionListener(MouseMotionListener mml) Here, ml is a reference to the object receiving mouse events, and mml is a reference to the object receiving mouse motion events. In this program, the same object is used for both. The applet then implements all of the methods defined by the MouseListener and MouseMotionListener interfaces. These are the event handlers for the various mouse events. Each method handles its event and then returns. Handling Keyboard Events To handle keyboard events, you use the same general architecture as that shown in the mouse event example in the preceding section. The difference, of course, is that you will be implementing the KeyListener interface. Before looking at an example, it is useful to review how key events are generated. When a key is pressed, a KEY_PRESSED event is generated. This results in a call to the keyPressed( ) event handler. When the key is released, a KEY_RELEASED event is generated and the keyReleased( ) handler is executed. If a character is generated by the keystroke, then a KEY_TYPED event is sent and the keyTyped( ) handler is invoked. Thus, each time the user presses a key, at least two and often three events are generated. If all you care about are actual characters, then you can ignore the information passed by the key press and release events. However, if your program needs to handle special keys, such as the arrow or function keys, then it must watch for them through the keyPressed( ) handler. The following program demonstrates keyboard input. It echoes keystrokes to the applet window and shows the pressed/released status of each key in the status window. Chapter 23 Event Handling 727 // Demonstrate the key event handlers. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ String msg = ""; int X = 10, Y = 20; // output coordinates public void init() { addKeyListener(this); } public void keyPressed(KeyEvent ke) { showStatus("Key Down"); } public void keyReleased(KeyEvent ke) { showStatus("Key Up"); } public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint(); } // Display keystrokes. public void paint(Graphics g) { g.drawString(msg, X, Y); } } Sample output is shown here: If you want to handle the special keys, such as the arrow or function keys, you need to respond to them within the keyPressed( ) handler. They are not available through keyTyped( ). Part II public class SimpleKey extends Applet implements KeyListener { 728 PART II The Java Library To identify the keys, you use their virtual key codes. For example, the next applet outputs the name of a few of the special keys: // Demonstrate some virtual key codes. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class KeyEvents extends Applet implements KeyListener { String msg = ""; int X = 10, Y = 20; // output coordinates public void init() { addKeyListener(this); } public void keyPressed(KeyEvent ke) { showStatus("Key Down"); int key = ke.getKeyCode(); switch(key) { case KeyEvent.VK_F1: msg += ""; break; case KeyEvent.VK_F2: msg += ""; break; case KeyEvent.VK_F3: msg += ""; break; case KeyEvent.VK_PAGE_DOWN: msg += ""; break; case KeyEvent.VK_PAGE_UP: msg += ""; break; case KeyEvent.VK_LEFT: msg += ""; break; case KeyEvent.VK_RIGHT: msg += ""; break; } repaint(); } Chapter 23 Event Handling 729 public void keyReleased(KeyEvent ke) { showStatus("Key Up"); } // Display keystrokes. public void paint(Graphics g) { g.drawString(msg, X, Y); } } Sample output is shown here: The procedures shown in the preceding keyboard and mouse event examples can be generalized to any type of event handling, including those events generated by controls. In later chapters, you will see many examples that handle other types of events, but they will all follow the same basic structure as the programs just described. Adapter Classes Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations. An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. You can define a new class to act as an event listener by extending one of the adapter classes and implementing only those events in which you are interested. For example, the MouseMotionAdapter class has two methods, mouseDragged( ) and mouseMoved( ), which are the methods defined by the MouseMotionListener interface. If you were interested in only mouse drag events, then you could simply extend MouseMotionAdapter and override mouseDragged( ). The empty implementation of mouseMoved( ) would handle the mouse motion events for you. Table 23-4 lists the commonly used adapter classes in java.awt.event and notes the interface that each implements. Part II public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint(); } 730 PART II The Java Library Adapter Class Listener Interface ComponentAdapter ComponentListener ContainerAdapter ContainerListener FocusAdapter FocusListener KeyAdapter KeyListener MouseAdapter MouseListener MouseMotionAdapter MouseMotionListener WindowAdapter WindowListener Table 23-4 Commonly Used Listener Interfaces Implemented by Adapter Classes The following example demonstrates an adapter. It displays a message in the status bar of an applet viewer or browser when the mouse is clicked or dragged. However, all other mouse events are silently ignored. The program has three classes. AdapterDemo extends Applet. Its init( ) method creates an instance of MyMouseAdapter and registers that object to receive notifications of mouse events. It also creates an instance of MyMouseMotionAdapter and registers that object to receive notifications of mouse motion events. Both of the constructors take a reference to the applet as an argument. MyMouseAdapter extends MouseAdapter and overrides the mouseClicked( ) method. The other mouse events are silently ignored by code inherited from the MouseAdapter class. MyMouseMotionAdapter extends MouseMotionAdapter and overrides the mouseDragged( ) method. The other mouse motion event is silently ignored by code inherited from the MouseMotionAdapter class. Note that both of the event listener classes save a reference to the applet. This information is provided as an argument to their constructors and is used later to invoke the showStatus( ) method. // Demonstrate an adapter. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class AdapterDemo extends Applet { public void init() { addMouseListener(new MyMouseAdapter(this)); addMouseMotionListener(new MyMouseMotionAdapter(this)); } } class MyMouseAdapter extends MouseAdapter { AdapterDemo adapterDemo; public MyMouseAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo; } Chapter 23 Event Handling 731 // Handle mouse clicked. public void mouseClicked(MouseEvent me) { adapterDemo.showStatus("Mouse clicked"); } } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { adapterDemo.showStatus("Mouse dragged"); } } As you can see by looking at the program, not having to implement all of the methods defined by the MouseMotionListener and MouseListener interfaces saves you a considerable amount of effort and prevents your code from becoming cluttered with empty methods. As an exercise, you might want to try rewriting one of the keyboard input examples shown earlier so that it uses a KeyAdapter. Inner Classes In Chapter 7, the basics of inner classes were explained. Here you will see why they are important. Recall that an inner class is a class defined within another class, or even within an expression. This section illustrates how inner classes can be used to simplify the code when using event adapter classes. To understand the benefit provided by inner classes, consider the applet shown in the following listing. It does not use an inner class. Its goal is to display the string "Mouse Pressed" in the status bar of the applet viewer or browser when the mouse is pressed. There are two top-level classes in this program. MousePressedDemo extends Applet, and MyMouseAdapter extends MouseAdapter. The init( ) method of MousePressedDemo instantiates MyMouseAdapter and provides this object as an argument to the addMouseListener( ) method. Notice that a reference to the applet is supplied as an argument to the MyMouseAdapter constructor. This reference is stored in an instance variable for later use by the mousePressed( ) method. When the mouse is pressed, it invokes the showStatus( ) method of the applet through the stored applet reference. In other words, showStatus( ) is invoked relative to the applet reference stored by MyMouseAdapter. // This applet does NOT use an inner class. import java.applet.*; import java.awt.event.*; /* */ Part II class MyMouseMotionAdapter extends MouseMotionAdapter { AdapterDemo adapterDemo; public MyMouseMotionAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo; } 732 PART II The Java Library public class MousePressedDemo extends Applet { public void init() { addMouseListener(new MyMouseAdapter(this)); } } class MyMouseAdapter extends MouseAdapter { MousePressedDemo mousePressedDemo; public MyMouseAdapter(MousePressedDemo mousePressedDemo) { this.mousePressedDemo = mousePressedDemo; } public void mousePressed(MouseEvent me) { mousePressedDemo.showStatus("Mouse Pressed."); } } The following listing shows how the preceding program can be improved by using an inner class. Here, InnerClassDemo is a top-level class that extends Applet. MyMouseAdapter is an inner class that extends MouseAdapter. Because MyMouseAdapter is defined within the scope of InnerClassDemo, it has access to all of the variables and methods within the scope of that class. Therefore, the mousePressed( ) method can call the showStatus( ) method directly. It no longer needs to do this via a stored reference to the applet. Thus, it is no longer necessary to pass MyMouseAdapter( ) a reference to the invoking object. // Inner class demo. import java.applet.*; import java.awt.event.*; /* */ public class InnerClassDemo extends Applet { public void init() { addMouseListener(new MyMouseAdapter()); } class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed"); } } } Anonymous Inner Classes An anonymous inner class is one that is not assigned a name. This section illustrates how an anonymous inner class can facilitate the writing of event handlers. Consider the applet shown in the following listing. As before, its goal is to display the string "Mouse Pressed" in the status bar of the applet viewer or browser when the mouse is pressed. Chapter 23 Event Handling 733 public class AnonymousInnerClassDemo extends Applet { public void init() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed"); } }); } } There is one top-level class in this program: AnonymousInnerClassDemo. The init( ) method calls the addMouseListener( ) method. Its argument is an expression that defines and instantiates an anonymous inner class. Let’s analyze this expression carefully. The syntax new MouseAdapter(){...} indicates to the compiler that the code between the braces defines an anonymous inner class. Furthermore, that class extends MouseAdapter. This new class is not named, but it is automatically instantiated when this expression is executed. Because this anonymous inner class is defined within the scope of AnonymousInnerClassDemo, it has access to all of the variables and methods within the scope of that class. Therefore, it can call the showStatus( ) method directly. As just illustrated, both named and anonymous inner classes solve some annoying problems in a simple yet effective way. They also allow you to create more efficient code. Part II // Anonymous inner class demo. import java.applet.*; import java.awt.event.*; /* */ This page intentionally left blank CHAPTER 24 Introducing the AWT: Working with Windows, Graphics, and Text The Abstract Window Toolkit (AWT) was introduced in Chapter 22, where it was used in several example applets. This chapter begins its in-depth examination. The AWT contains numerous classes and methods that allow you to create and manage windows. It is also the foundation upon which Swing is built. The AWT is quite large and a full description would easily fill an entire book. Therefore, it is not possible to describe in detail every AWT class, method, or instance variable. However, this and the following two chapters explain the basic techniques needed to use the AWT effectively when creating your own applets or stand-alone GUI-based applications. From there, you will be able to explore other parts of the AWT on your own. You will also be ready to move on to Swing. In this chapter, you will learn how to create and manage windows, manage fonts, output text, and utilize graphics. Chapter 25 describes the various controls, such as scroll bars and push buttons, supported by the AWT. It also explains further aspects of Java’s event handling mechanism. Chapter 26 examines the AWT’s imaging subsystem and animation. Although a common use of the AWT is in applets, it is also used to create stand-alone windows that run in a GUI environment, such as Windows. For the sake of convenience, most of the examples in this chapter are contained in applets. To run them, you need to use an applet viewer or a Java-compatible web browser. A few examples will demonstrate the creation of stand-alone, windowed programs. One other point before beginning. Today, most Java programs employ user interfaces based on Swing. Because Swing provides richer implementations than does the AWT of some common GUI controls, such as buttons, lists, and check boxes, it is easy to jump to the conclusion that the AWT is no longer important, that it has been superseded by Swing. This assumption is, however, quite wrong. As mentioned, Swing is built on top of the AWT. Thus, many aspects of the AWT are also aspects of Swing. Furthermore, many AWT classes are used either directly or indirectly by Swing. Finally, for some types of small programs (especially small applets) that make only minimal use of a GUI, using the AWT rather than Swing still makes sense. Therefore, even though most interfaces today will be based on Swing, a solid knowledge of the AWT is still required. Simply put, you can’t be a great Java programmer without knowing the AWT. 735 736 PART II The Java Library NOTE If you have not yet read Chapter 23, please do so now. It provides an overview of event handling, which is used by many of the examples in this chapter. AWT Classes The AWT classes are contained in the java.awt package. It is one of Java’s largest packages. Fortunately, because it is logically organized in a top-down, hierarchical fashion, it is easier to understand and use than you might at first believe. Table 24-1 lists some of the many AWT classes. Class Description AWTEvent Encapsulates AWT events. AWTEventMulticaster Dispatches events to multiple listeners. BorderLayout The border layout manager. Border layouts use five components: North, South, East, West, and Center. Button Creates a push button control. Canvas A blank, semantics-free window. CardLayout The card layout manager. Card layouts emulate index cards. Only the one on top is showing. Checkbox Creates a check box control. CheckboxGroup Creates a group of check box controls. CheckboxMenuItem Creates an on/off menu item. Choice Creates a pop-up list. Color Manages colors in a portable, platform-independent fashion. Component An abstract superclass for various AWT components. Container A subclass of Component that can hold other components. Cursor Encapsulates a bitmapped cursor. Dialog Creates a dialog window. Dimension Specifies the dimensions of an object. The width is stored in width, and the height is stored in height. EventQueue Queues events. FileDialog Creates a window from which a file can be selected. FlowLayout The flow layout manager. Flow layout positions components left to right, top to bottom. Font Encapsulates a type font. FontMetrics Encapsulates various information related to a font. This information helps you display text in a window. Frame Creates a standard window that has a title bar, resize corners, and a menu bar. Table 24-1 A Sampling of AWT Classes Introducing the AWT: Working with Windows, Graphics, and Text Class Description Graphics Encapsulates the graphics context. This context is used by the various output methods to display output in a window. GraphicsDevice Describes a graphics device such as a screen or printer. GraphicsEnvironment Describes the collection of available Font and GraphicsDevice objects. GridBagConstraints Defines various constraints relating to the GridBagLayout class. GridBagLayout The grid bag layout manager. Grid bag layout displays components subject to the constraints specified by GridBagConstraints. GridLayout The grid layout manager. Grid layout displays components in a twodimensional grid. Image Encapsulates graphical images. Insets Encapsulates the borders of a container. Label Creates a label that displays a string. List Creates a list from which the user can choose. Similar to the standard Windows list box. MediaTracker Manages media objects. Menu Creates a pull-down menu. MenuBar Creates a menu bar. MenuComponent An abstract class implemented by various menu classes. MenuItem Creates a menu item. MenuShortcut Encapsulates a keyboard shortcut for a menu item. Panel The simplest concrete subclass of Container. Point Encapsulates a Cartesian coordinate pair, stored in x and y. Polygon Encapsulates a polygon. PopupMenu Encapsulates a pop-up menu. PrintJob An abstract class that represents a print job. Rectangle Encapsulates a rectangle. Robot Supports automated testing of AWT-based applications. Scrollbar Creates a scroll bar control. ScrollPane A container that provides horizontal and/or vertical scroll bars for another component. SystemColor Contains the colors of GUI widgets such as windows, scroll bars, text, and others. TextArea Creates a multiline edit control. TextComponent A superclass for TextArea and TextField. TextField Creates a single-line edit control. Toolkit Abstract class implemented by the AWT. Window Creates a window with no frame, no menu bar, and no title. Table 24-1 A Sampling of AWT Classes (continued) 737 Part II Chapter 24 738 PART II The Java Library Although the basic structure of the AWT has been the same since Java 1.0, some of the original methods were deprecated and replaced by new ones. For backward-compatibility, Java still supports all the original 1.0 methods. However, because these methods are not for use with new code, this book does not describe them. Window Fundamentals The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. The two most common windows are those derived from Panel, which is used by applets, and those derived from Frame, which creates a standard application window. Much of the functionality of these windows is derived from their parent classes. Thus, a description of the class hierarchies relating to these two classes is fundamental to their understanding. Figure 24-1 shows the class hierarchy for Panel and Frame. Let’s look at each of these classes now. Component At the top of the AWT hierarchy is the Component class. Component is an abstract class that encapsulates all of the attributes of a visual component. Except for menus, all user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. It defines over a hundred public methods that are responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting. (You already used many of these methods when you created applets in Chapters 22 and 23.) A Component object is responsible for remembering the current foreground and background colors and the currently selected text font. Figure 24-1 The class hierarchy for Panel and Frame Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 739 Container The Container class is a subclass of Component. It has additional methods that allow other Component objects to be nested within it. Other Container objects can be stored inside of a Container (since they are themselves instances of Component). This makes for a multileveled containment system. A container is responsible for laying out (that is, positioning) any components that it contains. It does this through the use of various layout managers, which you will learn about in Chapter 25. The Panel class is a concrete subclass of Container. A Panel may be thought of as a recursively nestable, concrete screen component. Panel is the superclass for Applet. When screen output is directed to an applet, it is drawn on the surface of a Panel object. In essence, a Panel is a window that does not contain a title bar, menu bar, or border. This is why you don’t see these items when an applet is run inside a browser. When you run an applet using an applet viewer, the applet viewer provides the title and border. Other components can be added to a Panel object by its add( ) method (inherited from Container). Once these components have been added, you can position and resize them manually using the setLocation( ), setSize( ), setPreferredSize( ), or setBounds( ) methods defined by Component. Window The Window class creates a top-level window. A top-level window is not contained within any other object; it sits directly on the desktop. Generally, you won’t create Window objects directly. Instead, you will use a subclass of Window called Frame, described next. Frame Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. Canvas Although it is not part of the hierarchy for applet or frame windows, there is one other type of window that you will find valuable: Canvas. Canvas encapsulates a blank window upon which you can draw. You will see an example of Canvas later in this book. Working with Frame Windows After the applet, the type of window you will most often create is derived from Frame. You will use it to create child windows within applets, and top-level or child windows for standalone applications. As mentioned, it creates a standard-style window. Here are two of Frame’s constructors: Frame( ) throws HeadlessException Frame(String title) throws HeadlessException Part II Panel 740 PART II The Java Library The first form creates a standard window that does not contain a title. The second form creates a window with the title specified by title. Notice that you cannot specify the dimensions of the window. Instead, you must set the size of the window after it has been created. A HeadlessException is thrown if an attempt is made to create a Frame instance in an environment that does not support user interaction. There are several key methods you will use when working with Frame windows. They are examined here. Setting the Window’s Dimensions The setSize( ) method is used to set the dimensions of the window. Its signature is shown here: void setSize(int newWidth, int newHeight) void setSize(Dimension newSize) The new size of the window is specified by newWidth and newHeight, or by the width and height fields of the Dimension object passed in newSize. The dimensions are specified in terms of pixels. The getSize( ) method is used to obtain the current size of a window. One of its forms is shown here: Dimension getSize( ) This method returns the current size of the window contained within the width and height fields of a Dimension object. Hiding and Showing a Window After a frame window has been created, it will not be visible until you call setVisible( ). Its signature is shown here: void setVisible(boolean visibleFlag) The component is visible if the argument to this method is true. Otherwise, it is hidden. Setting a Window’s Title You can change the title in a frame window using setTitle( ), which has this general form: void setTitle(String newTitle) Here, newTitle is the new title for the window. Closing a Frame Window When using a frame window, your program must remove that window from the screen when it is closed, by calling setVisible(false). To intercept a window-close event, you must implement the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), you must remove the window from the screen. The example in the next section illustrates this technique. Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 741 While it is possible to simply create a window by creating an instance of Frame, you will seldom do so, because you will not be able to do much with it. For example, you will not be able to receive or process events that occur within it or easily output information to it. Most of the time, you will create a subclass of Frame. Doing so lets you override Frame’s methods and provide event handling. Creating a new frame window from within an applet is actually quite easy. First, create a subclass of Frame. Next, override any of the standard applet methods, such as init( ), start( ), and stop( ), to show or hide the frame as needed. Finally, implement the windowClosing( ) method of the WindowListener interface, calling setVisible(false) when the window is closed. Once you have defined a Frame subclass, you can create an object of that class. This causes a frame window to come into existence, but it will not be initially visible. You make it visible by calling setVisible( ). When created, the window is given a default height and width. You can set the size of the window explicitly by calling the setSize( ) method. The following applet creates a subclass of Frame called SampleFrame. A window of this subclass is instantiated within the init( ) method of AppletFrame. Notice that SampleFrame calls Frame’s constructor. This causes a standard frame window to be created with the title passed in title. This example overrides the applet’s start( ) and stop( ) methods so that they show and hide the child window, respectively. This causes the window to be removed automatically when you terminate the applet, when you close the window, or, if using a browser, when you move to another page. It also causes the child window to be shown when the browser returns to the applet. // Create a child frame window from within an applet. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ // Create a subclass of Frame. class SampleFrame extends Frame { SampleFrame(String title) { super(title); // create an object to handle window events MyWindowAdapter adapter = new MyWindowAdapter(this); // register it to receive those events addWindowListener(adapter); } public void paint(Graphics g) { g.drawString("This is in frame window", 10, 40); } } Part II Creating a Frame Window in an Applet 742 PART II The Java Library class MyWindowAdapter extends WindowAdapter { SampleFrame sampleFrame; public MyWindowAdapter(SampleFrame sampleFrame) { this.sampleFrame = sampleFrame; } public void windowClosing(WindowEvent we) { sampleFrame.setVisible(false); } } // Create frame window. public class AppletFrame extends Applet { Frame f; public void init() { f = new SampleFrame("A Frame Window"); f.setSize(250, 250); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); } } Sample output from this program is shown here: Handling Events in a Frame Window Since Frame is a subclass of Component, it inherits all the capabilities defined by Component. This means that you can use and manage a frame window just like you manage an applet’s main window. For example, you can override paint( ) to display output, call repaint( ) when you need to restore the window, and add event handlers. Whenever an event occurs in a window, the event handlers defined by that window will be called. Each window handles its Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 743 // Handle mouse events in both child and applet windows. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ // Create a subclass of Frame. class SampleFrame extends Frame implements MouseListener, MouseMotionListener { String msg = ""; int mouseX=10, mouseY=40; int movX=0, movY=0; SampleFrame(String title) { super(title); // register this object to receive its own mouse events addMouseListener(this); addMouseMotionListener(this); // create an object to handle window events MyWindowAdapter adapter = new MyWindowAdapter(this); // register it to receive those events addWindowListener(adapter); } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { } // Handle mouse entered. public void mouseEntered(MouseEvent evtObj) { // save coordinates mouseX = 10; mouseY = 54; msg = "Mouse just entered child."; repaint(); } // Handle mouse exited. public void mouseExited(MouseEvent evtObj) { // save coordinates mouseX = 10; mouseY = 54; msg = "Mouse just left child window."; repaint(); } Part II own events. For example, the following program creates a window that responds to mouse events. The main applet window also responds to mouse events. When you experiment with this program, you will see that mouse events are sent to the window in which the event occurs. 744 PART II The Java Library // Handle mouse pressed. public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); } // Handle mouse released. public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); movX = me.getX(); movY = me.getY(); msg = "*"; repaint(); } // Handle mouse moved. public void mouseMoved(MouseEvent me) { // save coordinates movX = me.getX(); movY = me.getY(); repaint(0, 0, 100, 60); } public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); g.drawString("Mouse at " + movX + ", " + movY, 10, 40); } } class MyWindowAdapter extends WindowAdapter { SampleFrame sampleFrame; public MyWindowAdapter(SampleFrame sampleFrame) { this.sampleFrame = sampleFrame; } Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 745 public void windowClosing(WindowEvent we) { sampleFrame.setVisible(false); } } SampleFrame f; String msg = ""; int mouseX=0, mouseY=10; int movX=0, movY=0; // Create a frame window. public void init() { f = new SampleFrame("Handle Mouse Events"); f.setSize(300, 200); f.setVisible(true); // register this object to receive its own mouse events addMouseListener(this); addMouseMotionListener(this); } // Remove frame window when stopping applet. public void stop() { f.setVisible(false); } // Show frame window when starting applet. public void start() { f.setVisible(true); } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { } // Handle mouse entered. public void mouseEntered(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 24; msg = "Mouse just entered applet window."; repaint(); } Part II // Applet window. public class WindowEvents extends Applet implements MouseListener, MouseMotionListener { 746 PART II The Java Library // Handle mouse exited. public void mouseExited(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 24; msg = "Mouse just left applet window."; repaint(); } // Handle button pressed. public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); } // Handle button released. public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); movX = me.getX(); movY = me.getY(); msg = "*"; repaint(); } // Handle mouse moved. public void mouseMoved(MouseEvent me) { // save coordinates movX = me.getX(); movY = me.getY(); repaint(0, 0, 100, 20); } // Display msg in applet window. public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); g.drawString("Mouse at " + movX + ", " + movY, 0, 10); } } Sample output from this program is shown here: Introducing the AWT: Working with Windows, Graphics, and Text 747 Part II Chapter 24 Creating a Windowed Program Although creating applets is a common use for Java’s AWT, it is also possible to create standalone AWT-based applications. To do this, simply create an instance of the window or windows you need inside main( ). For example, the following program creates a frame window that responds to mouse clicks and keystrokes: // Create an AWT-based application. import java.awt.*; import java.awt.event.*; import java.applet.*; // Create a frame window. public class AppWindow extends Frame { String keymsg = "This is a test."; String mousemsg = ""; int mouseX=30, mouseY=30; public AppWindow() { addKeyListener(new MyKeyAdapter(this)); addMouseListener(new MyMouseAdapter(this)); addWindowListener(new MyWindowAdapter()); } public void paint(Graphics g) { g.drawString(keymsg, 10, 40); g.drawString(mousemsg, mouseX, mouseY); } // Create the window. public static void main(String args[]) { AppWindow appwin = new AppWindow(); appwin.setSize(new Dimension(300, 200)); appwin.setTitle("An AWT-Based Application"); appwin.setVisible(true); } } 748 PART II The Java Library class MyKeyAdapter extends KeyAdapter { AppWindow appWindow; public MyKeyAdapter(AppWindow appWindow) { this.appWindow = appWindow; } public void keyTyped(KeyEvent ke) { appWindow.keymsg += ke.getKeyChar(); appWindow.repaint(); }; } class MyMouseAdapter extends MouseAdapter { AppWindow appWindow; public MyMouseAdapter(AppWindow appWindow) { this.appWindow = appWindow; } public void mousePressed(MouseEvent me) { appWindow.mouseX = me.getX(); appWindow.mouseY = me.getY(); appWindow.mousemsg = "Mouse Down at " + appWindow.mouseX + ", " + appWindow.mouseY; appWindow.repaint(); } } class MyWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } } Sample output from this program is shown here: Once created, a frame window takes on a life of its own. Notice that main( ) ends with the call to appwin.setVisible(true). However, the program keeps running until you close the window. In essence, when creating a windowed application, you will use main( ) to launch its top-level window. After that, your program will function as a GUI-based application, not like the console-based programs used earlier. Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 749 Displaying Information Within a Window Working with Graphics The AWT supports a rich assortment of graphics methods. All graphics are drawn relative to a window. This can be the main window of an applet, a child window of an applet, or a standalone application window. The origin of each window is at the top-left corner and is 0,0. Coordinates are specified in pixels. All output to a window takes place through a graphics context. A graphics context is encapsulated by the Graphics class and is obtained in two ways: • It is passed to a method, such as paint( ) or update( ), as an argument. • It is returned by the getGraphics( ) method of Component. For the sake of convenience the remainder of the examples in this chapter will demonstrate graphics in a main applet window. However, the same techniques will apply to any other window. The Graphics class defines a number of drawing functions. Each shape can be drawn edge-only or filled. Objects are drawn and filled in the currently selected graphics color, which is black by default. When a graphics object is drawn that exceeds the dimensions of the window, output is automatically clipped. Let’s take a look at several of the drawing methods. Drawing Lines Lines are drawn by means of the drawLine( ) method, shown here: void drawLine(int startX, int startY, int endX, int endY ) drawLine( ) displays a line in the current drawing color that begins at startX, startY and ends at endX, endY. The following applet draws several lines: // Draw lines import java.awt.*; import java.applet.*; /* */ public class Lines extends Applet { public void paint(Graphics g) { g.drawLine(0, 0, 100, 100); Part II In the most general sense, a window is a container for information. Although we have already output small amounts of text to a window in the preceding examples, we have not begun to take advantage of a window’s ability to present high-quality text and graphics. Indeed, much of the power of the AWT comes from its support for these items. For this reason, the remainder of this chapter discusses Java’s text-, graphics-, and font-handling capabilities. As you will see, they are both powerful and flexible. 750 PART II The Java Library g.drawLine(0, 100, 100, 0); g.drawLine(40, 25, 250, 180); g.drawLine(75, 90, 400, 400); g.drawLine(20, 150, 400, 40); g.drawLine(5, 290, 80, 19); } } Sample output from this program is shown here: Drawing Rectangles The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle, respectively. They are shown here: void drawRect(int top, int left, int width, int height) void fillRect(int top, int left, int width, int height) The upper-left corner of the rectangle is at top, left. The dimensions of the rectangle are specified by width and height. To draw a rounded rectangle, use drawRoundRect( ) or fillRoundRect( ), both shown here: void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) A rounded rectangle has rounded corners. The upper-left corner of the rectangle is at top, left. The dimensions of the rectangle are specified by width and height. The diameter of the rounding arc along the X axis is specified by xDiam. The diameter of the rounding arc along the Y axis is specified by yDiam. The following applet draws several rectangles: // Draw rectangles import java.awt.*; import java.applet.*; /* */ Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 751 public class Rectangles extends Applet { public void paint(Graphics g) { g.drawRect(10, 10, 60, 50); g.fillRect(100, 10, 60, 50); g.drawRoundRect(190, 10, 60, 50, 15, 15); g.fillRoundRect(70, 90, 140, 100, 30, 40); } } Part II Sample output from this program is shown here: Drawing Ellipses and Circles To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). These methods are shown here: void drawOval(int top, int left, int width, int height) void fillOval(int top, int left, int width, int height) The ellipse is drawn within a bounding rectangle whose upper-left corner is specified by top, left and whose width and height are specified by width and height. To draw a circle, specify a square as the bounding rectangle. The following program draws several ellipses: // Draw Ellipses import java.awt.*; import java.applet.*; /* */ public class Ellipses extends Applet { public void paint(Graphics g) { g.drawOval(10, 10, 50, 50); g.fillOval(100, 10, 75, 50); g.drawOval(190, 10, 90, 30); g.fillOval(70, 90, 140, 100); } } 752 PART II The Java Library Sample output from this program is shown here: Drawing Arcs Arcs can be drawn with drawArc( ) and fillArc( ), shown here: void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle) void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle) The arc is bounded by the rectangle whose upper-left corner is specified by top, left and whose width and height are specified by width and height. The arc is drawn from startAngle through the angular distance specified by sweepAngle. Angles are specified in degrees. Zero degrees is on the horizontal, at the three o’clock position. The arc is drawn counterclockwise if sweepAngle is positive, and clockwise if sweepAngle is negative. Therefore, to draw an arc from twelve o’clock to six o’clock, the start angle would be 90 and the sweep angle 180. The following applet draws several arcs: // Draw Arcs import java.awt.*; import java.applet.*; /* */ public class Arcs extends Applet { public void paint(Graphics g) { g.drawArc(10, 40, 70, 70, 0, 75); g.fillArc(100, 40, 70, 70, 0, 75); g.drawArc(10, 100, 70, 80, 0, 175); g.fillArc(100, 100, 70, 90, 0, 270); g.drawArc(200, 80, 80, 80, 0, 180); } } Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 753 Part II Sample output from this program is shown here: Drawing Polygons It is possible to draw arbitrarily shaped figures using drawPolygon( ) and fillPolygon( ), shown here: void drawPolygon(int x[ ], int y[ ], int numPoints) void fillPolygon(int x[ ], int y[ ], int numPoints) The polygon’s endpoints are specified by the coordinate pairs contained within the x and y arrays. The number of points defined by x and y is specified by numPoints. There are alternative forms of these methods in which the polygon is specified by a Polygon object. The following applet draws an hourglass shape: // Draw Polygon import java.awt.*; import java.applet.*; /* */ public class HourGlass extends Applet { public void paint(Graphics g) { int xpoints[] = {30, 200, 30, 200, 30}; int ypoints[] = {30, 30, 200, 200, 30}; int num = 5; g.drawPolygon(xpoints, ypoints, num); } } 754 PART II The Java Library Sample output from this program is shown here: Sizing Graphics Often, you will want to size a graphics object to fit the current size of the window in which it is drawn. To do so, first obtain the current dimensions of the window by calling getSize( ) on the window object. It returns the dimensions of the window encapsulated within a Dimension object. Once you have the current size of the window, you can scale your graphical output accordingly. To demonstrate this technique, here is an applet that will start as a 200x200-pixel square and grow by 25 pixels in width and height with each mouse click until the applet gets larger than 500x500. At that point, the next click will return it to 200x200, and the process starts over. Within the window, a rectangle is drawn around the inner border of the window; within that rectangle, an X is drawn so that it fills the window. This applet works in appletviewer, but it may not work in a browser window. // Resizing output to fit the current size of a window. import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class ResizeMe extends Applet { final int inc = 25; int max = 500; int min = 200; Dimension d; public ResizeMe() { addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent me) { int w = (d.width + inc) > max?min :(d.width + inc); int h = (d.height + inc) > max?min :(d.height + inc); setSize(new Dimension(w, h)); Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 755 } }); } public void paint(Graphics g) { d = getSize(); g.drawLine(0, 0, d.width-1, d.height-1); g.drawLine(0, d.height-1, d.width-1, 0); g.drawRect(0, 0, d.width-1, d.height-1); Working with Color Java supports color in a portable, device-independent fashion. The AWT color system allows you to specify any color you want. It then finds the best match for that color, given the limits of the display hardware currently executing your program or applet. Thus, your code does not need to be concerned with the differences in the way color is supported by various hardware devices. Color is encapsulated by the Color class. As you saw in Chapter 22, Color defines several constants (for example, Color.black) to specify a number of common colors. You can also create your own colors, using one of the color constructors. Three commonly used forms are shown here: Color(int red, int green, int blue) Color(int rgbValue) Color(float red, float green, float blue) The first constructor takes three integers that specify the color as a mix of red, green, and blue. These values must be between 0 and 255, as in this example: new Color(255, 100, 100); // light red The second color constructor takes a single integer that contains the mix of red, green, and blue packed into an integer. The integer is organized with red in bits 16 to 23, green in bits 8 to 15, and blue in bits 0 to 7. Here is an example of this constructor: int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00); Color darkRed = new Color(newRed); The final constructor, Color(float, float, float), takes three float values (between 0.0 and 1.0) that specify the relative mix of red, green, and blue. Once you have created a color, you can use it to set the foreground and/or background color by using the setForeground( ) and setBackground( ) methods described in Chapter 22. You can also select it as the current drawing color. Color Methods The Color class defines several methods that help manipulate colors. Several are examined here. Part II } } 756 PART II The Java Library Using Hue, Saturation, and Brightness The hue-saturation-brightness (HSB) color model is an alternative to red-green-blue (RGB) for specifying particular colors. Figuratively, hue is a wheel of color. The hue can be specified with a number between 0.0 and 1.0, which is used to obtain an angle into the color wheel. (The principal colors are approximately red, orange, yellow, green, blue, indigo, and violet.) Saturation is another scale ranging from 0.0 to 1.0, representing light pastels to intense hues. Brightness values also range from 0.0 to 1.0, where 1 is bright white and 0 is black. Color supplies two methods that let you convert between RGB and HSB. They are shown here: static int HSBtoRGB(float hue, float saturation, float brightness) static float[ ] RGBtoHSB(int red, int green, int blue, float values[ ]) HSBtoRGB( ) returns a packed RGB value compatible with the Color(int) constructor. RGBtoHSB( ) returns a float array of HSB values corresponding to RGB integers. If values is not null, then this array is given the HSB values and returned. Otherwise, a new array is created and the HSB values are returned in it. In either case, the array contains the hue at index 0, saturation at index 1, and brightness at index 2. getRed( ), getGreen( ), getBlue( ) You can obtain the red, green, and blue components of a color independently using getRed( ), getGreen( ), and getBlue( ), shown here: int getRed( ) int getGreen( ) int getBlue( ) Each of these methods returns the RGB color component found in the invoking Color object in the lower 8 bits of an integer. getRGB( ) To obtain a packed, RGB representation of a color, use getRGB( ), shown here: int getRGB( ) The return value is organized as described earlier. Setting the Current Graphics Color By default, graphics objects are drawn in the current foreground color. You can change this color by calling the Graphics method setColor( ) : void setColor(Color newColor) Here, newColor specifies the new drawing color. You can obtain the current color by calling getColor( ), shown here: Color getColor( ) A Color Demonstration Applet The following applet constructs several colors and draws various objects using these colors: Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 757 public class ColorDemo extends Applet { // draw lines public void paint(Graphics g) { Color c1 = new Color(255, 100, 100); Color c2 = new Color(100, 255, 100); Color c3 = new Color(100, 100, 255); g.setColor(c1); g.drawLine(0, 0, 100, 100); g.drawLine(0, 100, 100, 0); g.setColor(c2); g.drawLine(40, 25, 250, 180); g.drawLine(75, 90, 400, 400); g.setColor(c3); g.drawLine(20, 150, 400, 40); g.drawLine(5, 290, 80, 19); g.setColor(Color.red); g.drawOval(10, 10, 50, 50); g.fillOval(70, 90, 140, 100); g.setColor(Color.blue); g.drawOval(190, 10, 90, 30); g.drawRect(10, 10, 60, 50); g.setColor(Color.cyan); g.fillRect(100, 10, 60, 50); g.drawRoundRect(190, 10, 60, 50, 15, 15); } } Setting the Paint Mode The paint mode determines how objects are drawn in a window. By default, new output to a window overwrites any preexisting contents. However, it is possible to have new objects XORed onto the window by using setXORMode( ), as follows: void setXORMode(Color xorColor) Here, xorColor specifies the color that will be XORed to the window when an object is drawn. The advantage of XOR mode is that the new object is always guaranteed to be visible no matter what color the object is drawn over. Part II // Demonstrate color. import java.awt.*; import java.applet.*; /* */ 758 PART II The Java Library To return to overwrite mode, call setPaintMode( ), shown here: void setPaintMode( ) In general, you will want to use overwrite mode for normal output, and XOR mode for special purposes. For example, the following program displays cross hairs that track the mouse pointer. The cross hairs are XORed onto the window and are always visible, no matter what the underlying color is. // Demonstrate XOR mode. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class XOR extends Applet { int chsX=100, chsY=100; public XOR() { addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent me) { int x = me.getX(); int y = me.getY(); chsX = x-10; chsY = y-10; repaint(); } }); } public void paint(Graphics g) { g.drawLine(0, 0, 100, 100); g.drawLine(0, 100, 100, 0); g.setColor(Color.blue); g.drawLine(40, 25, 250, 180); g.drawLine(75, 90, 400, 400); g.setColor(Color.green); g.drawRect(10, 10, 60, 50); g.fillRect(100, 10, 60, 50); g.setColor(Color.red); g.drawRoundRect(190, 10, 60, 50, 15, 15); Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 759 g.fillRoundRect(70, 90, 140, 100, 30, 40); g.setColor(Color.cyan); g.drawLine(20, 150, 400, 40); g.drawLine(5, 290, 80, 19); // xor cross hairs g.setXORMode(Color.black); g.drawLine(chsX-10, chsY, chsX+10, chsY); g.drawLine(chsX, chsY-10, chsX, chsY+10); g.setPaintMode(); Sample output from this program is shown here: Working with Fonts The AWT supports multiple type fonts. Years ago, fonts emerged from the domain of traditional typesetting to become an important part of computer-generated documents and displays. The AWT provides flexibility by abstracting font-manipulation operations and allowing for dynamic selection of fonts. Fonts have a family name, a logical font name, and a face name. The family name is the general name of the font, such as Courier. The logical name specifies a category of font, such as Monospaced. The face name specifies a specific font, such as Courier Italic. Fonts are encapsulated by the Font class. Several of the methods defined by Font are listed in Table 24-2. Part II } } 760 PART II The Java Library Method Description static Font decode(String str) Returns a font given its name. boolean equals(Object FontObj) Returns true if the invoking object contains the same font as that specified by FontObj. Otherwise, it returns false. String getFamily( ) Returns the name of the font family to which the invoking font belongs. static Font getFont(String property) Returns the font associated with the system property specified by property. null is returned if property does not exist. static Font getFont(String property, Font defaultFont) Returns the font associated with the system property specified by property. The font specified by defaultFont is returned if property does not exist. String getFontName() Returns the face name of the invoking font. String getName( ) Returns the logical name of the invoking font. int getSize( ) Returns the size, in points, of the invoking font. int getStyle( ) Returns the style values of the invoking font. int hashCode( ) Returns the hash code associated with the invoking object. boolean isBold( ) Returns true if the font includes the BOLD style value. Otherwise, false is returned. boolean isItalic( ) Returns true if the font includes the ITALIC style value. Otherwise, false is returned. boolean isPlain( ) Returns true if the font includes the PLAIN style value. Otherwise, false is returned. String toString( ) Returns the string equivalent of the invoking font. Table 24-2 A Sampling of Methods Defined by Font The Font class defines these variables: Variable Meaning String name Name of the font float pointSize Size of the font in points int size Size of the font in points int style Font style Determining the Available Fonts When working with fonts, often you need to know which fonts are available on your machine. To obtain this information, you can use the getAvailableFontFamilyNames( ) method defined by the GraphicsEnvironment class. It is shown here: String[ ] getAvailableFontFamilyNames( ) Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 761 This method returns an array of strings that contains the names of the available font families. In addition, the getAllFonts( ) method is defined by the GraphicsEnvironment class. It is shown here: This method returns an array of Font objects for all of the available fonts. Since these methods are members of GraphicsEnvironment, you need a GraphicsEnvironment reference to call them. You can obtain this reference by using the getLocalGraphicsEnvironment( ) static method, which is defined by GraphicsEnvironment. It is shown here: static GraphicsEnvironment getLocalGraphicsEnvironment( ) Here is an applet that shows how to obtain the names of the available font families: // Display Fonts /* */ import java.applet.*; import java.awt.*; public class ShowFonts extends Applet { public void paint(Graphics g) { String msg = ""; String FontList[]; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); FontList = ge.getAvailableFontFamilyNames(); for(int i = 0; i < FontList.length; i++) msg += FontList[i] + " "; g.drawString(msg, 4, 16); } } Sample output from this program is shown next. However, when you run this program, you may see a different list of fonts than the one shown in this illustration. Part II Font[ ] getAllFonts( ) 762 PART II The Java Library Creating and Selecting a Font To select a new font, you must first construct a Font object that describes that font. One Font constructor has this general form: Font(String fontName, int fontStyle, int pointSize) Here, fontName specifies the name of the desired font. The name can be specified using either the logical or face name. All Java environments will support the following fonts: Dialog, DialogInput, Sans Serif, Serif, and Monospaced. Dialog is the font used by your system’s dialog boxes. Dialog is also the default if you don’t explicitly set a font. You can also use any other fonts supported by your particular environment, but be careful—these other fonts may not be universally available. The style of the font is specified by fontStyle. It may consist of one or more of these three constants: Font.PLAIN, Font.BOLD, and Font.ITALIC. To combine styles, OR them together. For example, Font.BOLD | Font.ITALIC specifies a bold, italics style. The size, in points, of the font is specified by pointSize. To use a font that you have created, you must select it using setFont( ), which is defined by Component. It has this general form: void setFont(Font fontObj) Here, fontObj is the object that contains the desired font. The following program outputs a sample of each standard font. Each time you click the mouse within its window, a new font is selected and its name is displayed. // Show fonts. import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class SampleFonts extends Applet { int next = 0; Font f; String msg; public void init() { f = new Font("Dialog", Font.PLAIN, 12); msg = "Dialog"; setFont(f); addMouseListener(new MyMouseAdapter(this)); } public void paint(Graphics g) { g.drawString(msg, 4, 20); } } Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 763 class MyMouseAdapter extends MouseAdapter { SampleFonts sampleFonts; public void mousePressed(MouseEvent me) { // Switch fonts with each mouse click. sampleFonts.next++; switch(sampleFonts.next) { case 0: sampleFonts.f = new Font("Dialog", Font.PLAIN, 12); sampleFonts.msg = "Dialog"; break; case 1: sampleFonts.f = new Font("DialogInput", Font.PLAIN, 12); sampleFonts.msg = "DialogInput"; break; case 2: sampleFonts.f = new Font("SansSerif", Font.PLAIN, 12); sampleFonts.msg = "SansSerif"; break; case 3: sampleFonts.f = new Font("Serif", Font.PLAIN, 12); sampleFonts.msg = "Serif"; break; case 4: sampleFonts.f = new Font("Monospaced", Font.PLAIN, 12); sampleFonts.msg = "Monospaced"; break; } if(sampleFonts.next == 4) sampleFonts.next = -1; sampleFonts.setFont(sampleFonts.f); sampleFonts.repaint(); } } Sample output from this program is shown here: Part II public MyMouseAdapter(SampleFonts sampleFonts) { this.sampleFonts = sampleFonts; } 764 PART II The Java Library Obtaining Font Information Suppose you want to obtain information about the currently selected font. To do this, you must first get the current font by calling getFont( ). This method is defined by the Graphics class, as shown here: Font getFont( ) Once you have obtained the currently selected font, you can retrieve information about it using various methods defined by Font. For example, this applet displays the name, family, size, and style of the currently selected font: // Display font info. import java.applet.*; import java.awt.*; /* */ public class FontInfo extends Applet { public void paint(Graphics g) { Font f = g.getFont(); String fontName = f.getName(); String fontFamily = f.getFamily(); int fontSize = f.getSize(); int fontStyle = f.getStyle(); String msg = "Family: " + fontName; msg += ", Font: " + fontFamily; msg += ", Size: " + fontSize + ", Style: "; if((fontStyle & Font.BOLD) == Font.BOLD) msg += "Bold "; if((fontStyle & Font.ITALIC) == Font.ITALIC) msg += "Italic "; if((fontStyle & Font.PLAIN) == Font.PLAIN) msg += "Plain "; g.drawString(msg, 4, 16); } } Managing Text Output Using FontMetrics As just explained, Java supports a number of fonts. For most fonts, characters are not all the same dimension—most fonts are proportional. Also, the height of each character, the length of descenders (the hanging parts of letters, such as y), and the amount of space between horizontal lines vary from font to font. Further, the point size of a font can be changed. That these (and other) attributes are variable would not be of too much consequence except that Java demands that you, the programmer, manually manage virtually all text output. Given that the size of each font may differ and that fonts may be changed while your program is executing, there must be some way to determine the dimensions and various other attributes of the currently selected font. For example, to write one line of text after Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 765 Height The top-to-bottom size of a line of text Baseline The line that the bottoms of characters are aligned to (not counting descent) Ascent The distance from the baseline to the top of a character Descent The distance from the baseline to the bottom of a character Leading The distance between the bottom of one line of text and the top of the next As you know, we have used the drawString( ) method in many of the previous examples. It paints a string in the current font and color, beginning at a specified location. However, this location is at the left edge of the baseline of the characters, not at the upper-left corner as is usual with other drawing methods. It is a common error to draw a string at the same coordinate that you would draw a box. For example, if you were to draw a rectangle at coordinate 0,0, you would see a full rectangle. If you were to draw the string “Typesetting” at 0,0, you would only see the tails (or descenders) of the y, p, and g. As you will see, by using font metrics, you can determine the proper placement of each string that you display. FontMetrics defines several methods that help you manage text output. Several commonly used ones are listed in Table 24-3. These methods help you properly display text in a window. Let’s look at some examples. Method Description int bytesWidth(byte b[ ], int start, int numBytes) Returns the width of numBytes characters held in array b, beginning at start. int charWidth(char c[ ], int start, int numChars) Returns the width of numChars characters held in array c, beginning at start. int charWidth(char c) Returns the width of c. int charWidth(int c) Returns the width of c. int getAscent( ) Returns the ascent of the font. int getDescent( ) Returns the descent of the font. Font getFont( ) Returns the font. int getHeight( ) Returns the height of a line of text. This value can be used to output multiple lines of text in a window. int getLeading( ) Returns the space between lines of text. int getMaxAdvance( ) Returns the width of the widest character. –1 is returned if this value is not available. Table 24-3 A Sampling of Methods Defined by FontMetrics Part II another implies that you have some way of knowing how tall the font is and how many pixels are needed between lines. To fill this need, the AWT includes the FontMetrics class, which encapsulates various information about a font. Let’s begin by defining the common terminology used when describing fonts: 766 PART II The Java Library Method Description int getMaxAscent( ) Returns the maximum ascent. int getMaxDescent( ) Returns the maximum descent. int[ ] getWidths( ) Returns the widths of the first 256 characters. int stringWidth(String str) Returns the width of the string specified by str. String toString( ) Returns the string equivalent of the invoking object. Table 24-3 A Sampling of Methods Defined by FontMetrics (continued) Displaying Multiple Lines of Text Perhaps the most common use of FontMetrics is to determine the spacing between lines of text. The second most common use is to determine the length of a string that is being displayed. Here, you will see how to accomplish these tasks. In general, to display multiple lines of text, your program must manually keep track of the current output position. Each time a newline is desired, the Y coordinate must be advanced to the beginning of the next line. Each time a string is displayed, the X coordinate must be set to the point at which the string ends. This allows the next string to be written so that it begins at the end of the preceding one. To determine the spacing between lines, you can use the value returned by getLeading( ). To determine the total height of the font, add the value returned by getAscent( ) to the value returned by getDescent( ). You can then use these values to position each line of text you output. However, in many cases, you will not need to use these individual values. Often, all that you will need to know is the total height of a line, which is the sum of the leading space and the font’s ascent and descent values. The easiest way to obtain this value is to call getHeight( ). Simply increment the Y coordinate by this value each time you want to advance to the next line when outputting text. To start output at the end of previous output on the same line, you must know the length, in pixels, of each string that you display. To obtain this value, call stringWidth( ). You can use this value to advance the X coordinate each time you display a line. The following applet shows how to output multiple lines of text in a window. It also displays multiple sentences on the same line. Notice the variables curX and curY. They keep track of the current text output position. // Demonstrate multiline output. import java.applet.*; import java.awt.*; /* */ public class MultiLine extends Applet { int curX=0, curY=0; // current position public void init() { Font f = new Font("SansSerif", Font.PLAIN, 12); setFont(f); } Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 767 public void paint(Graphics g) { FontMetrics fm = g.getFontMetrics(); nextLine("This is on line one.", g); nextLine("This is on line two.", g); sameLine(" This is on same line.", g); sameLine(" This, too.", g); nextLine("This is on line three.", g); curX = curY = 0; // Reset coordinates for each repaint. // Advance to next line. void nextLine(String s, Graphics g) { FontMetrics fm = g.getFontMetrics(); curY += fm.getHeight(); // advance to next line curX = 0; g.drawString(s, curX, curY); curX = fm.stringWidth(s); // advance to end of line } // Display on same line. void sameLine(String s, Graphics g) { FontMetrics fm = g.getFontMetrics(); g.drawString(s, curX, curY); curX += fm.stringWidth(s); // advance to end of line } } Sample output from this program is shown here: Centering Text Here is an example that centers text, left to right, top to bottom, in a window. It obtains the ascent, descent, and width of the string and computes the position at which it must be displayed to be centered. // Center text. import java.applet.*; import java.awt.*; /* */ Part II } 768 PART II The Java Library public class CenterText extends Applet { final Font f = new Font("SansSerif", Font.BOLD, 18); public void paint(Graphics g) { Dimension d = this.getSize(); g.setColor(Color.white); g.fillRect(0, 0, d.width,d.height); g.setColor(Color.black); g.setFont(f); drawCenteredString("This is centered.", d.width, d.height, g); g.drawRect(0, 0, d.width-1, d.height-1); } public void drawCenteredString(String s, int w, int h, Graphics g) { FontMetrics fm = g.getFontMetrics(); int x = (w - fm.stringWidth(s)) / 2; int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent()))/2); g.drawString(s, x, y); } } Following is a sample output from this program: Multiline Text Alignment When using a word processor, it is common for text to be aligned so that one or more of the edges of the text make a straight line. For example, most word processors can left-justify and/or right-justify text. Most can also center text. In the following program, you will see how to accomplish these actions. In the program, the string to be justified is broken into individual words. For each word, the program keeps track of its length in the current font and automatically advances to the next line if the word will not fit on the current line. Each completed line is displayed in the window in the currently selected alignment style. Each time you click the mouse in the applet’s window, the alignment style is changed. Sample output from this program is shown here: Introducing the AWT: Working with Windows, Graphics, and Text 769 Part II Chapter 24 // Demonstrate text alignment. import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; /* Text Layout */ public class TextLayout extends Applet { final int LEFT = 0; final int RIGHT = 1; final int CENTER = 2; final int LEFTRIGHT =3; int align; Dimension d; Font f; FontMetrics fm; int fontSize; int fh, bl; int space; String text; public void init() { setBackground(Color.white); text = getParameter("text"); try { 770 PART II The Java Library fontSize = Integer.parseInt(getParameter("fontSize"));} catch (NumberFormatException e) { fontSize=14; } align = LEFT; addMouseListener(new MyMouseAdapter(this)); } public void paint(Graphics g) { update(g); } public void update(Graphics g) { d = getSize(); g.setColor(getBackground()); g.fillRect(0,0,d.width, d.height); if(f==null) f = new Font(getParameter("fontname"), Font.PLAIN, fontSize); g.setFont(f); if(fm == null) { fm = g.getFontMetrics(); bl = fm.getAscent(); fh = bl + fm.getDescent(); space = fm.stringWidth(" "); } g.setColor(Color.black); StringTokenizer st = new StringTokenizer(text); int x = 0; int nextx; int y = 0; String word, sp; int wordCount = 0; String line = ""; while (st.hasMoreTokens()) { word = st.nextToken(); if(word.equals("

")) { drawString(g, line, wordCount, fm.stringWidth(line), y+bl); line = ""; wordCount = 0; x = 0; y = y + (fh * 2); } else { int w = fm.stringWidth(word); if(( nextx = (x+space+w)) > d.width ) { drawString(g, line, wordCount, fm.stringWidth(line), y+bl); line = ""; wordCount = 0; x = 0; y = y + fh; } if(x!=0) {sp = " ";} else {sp = "";} Chapter 24 Introducing the AWT: Working with Windows, Graphics, and Text 771 line = line + sp + word; x = x + space + w; wordCount++; } } drawString(g, line, wordCount, fm.stringWidth(line), y+bl); public void drawString(Graphics g, String line, int wc, int lineW, int y) { switch(align) { case LEFT: g.drawString(line, 0, y); break; case RIGHT: g.drawString(line, d.width-lineW,y); break; case CENTER: g.drawString(line, (d.width-lineW)/2, y); break; case LEFTRIGHT: if(lineW < (int)(d.width*.75)) { g.drawString(line, 0, y); } else { int toFill = (d.width - lineW)/wc; int nudge = d.width - lineW - (toFill*wc); int s = fm.stringWidth(" "); StringTokenizer st = new StringTokenizer(line); int x = 0; while(st.hasMoreTokens()) { String word = st.nextToken(); g.drawString(word, x, y); if(nudge>0) { x = x + fm.stringWidth(word) + space + toFill + 1; nudge--; } else { x = x + fm.stringWidth(word) + space + toFill; } } } break; } } } class MyMouseAdapter extends MouseAdapter { TextLayout tl; public MyMouseAdapter(TextLayout tl) { this.tl = tl; } public void mouseClicked(MouseEvent me) { tl.align = (tl.align + 1) % 4; tl.repaint(); } } Part II } 772 PART II The Java Library Let’s take a closer look at how this applet works. The applet first creates several constants that will be used to determine the alignment style, and then declares several variables. The init( ) method obtains the text that will be displayed. It then initializes the font size in a try-catch block, which will set the font size to 14 if the fontSize parameter is missing from the HTML. The text parameter is a long string of text, with the HTML tag

as a paragraph separator. The update( ) method is the engine for this example. It sets the font and gets the baseline and font height from a font metrics object. Next, it creates a StringTokenizer and uses it to retrieve the next token (a string separated by whitespace) from the string specified by text. If the next token is

, it advances the vertical spacing. Otherwise, update( ) checks to see if the length of this token in the current font will go beyond the width of the column. If the line is full of text or if there are no more tokens, the line is output by a custom version of drawString( ). The first three cases in drawString( ) are simple. Each aligns the string that is passed in line to the left or right edge or to the center of the column, depending upon the alignment style. The LEFTRIGHT case aligns both the left and right sides of the string. This means that we need to calculate the remaining whitespace (the difference between the width of the string and the width of the column) and distribute that space between each of the words. The last method in this class advances the alignment style each time you click the mouse on the applet’s window. CHAPTER 25 Using AWT Controls, Layout Managers, and Menus This chapter continues our exploration of the Abstract Window Toolkit (AWT). It begins with an examination of the standard controls and layout managers. It then discusses menus and the menu bar. The chapter also includes a discussion of two high-level components: the dialog box and the file dialog box. It concludes with another look at event handling. Controls are components that allow a user to interact with your application in various ways—for example, a commonly used control is the push button. A layout manager automatically positions components within a container. Thus, the appearance of a window is determined by a combination of the controls that it contains and the layout manager used to position them. In addition to the controls, a frame window can also include a standard-style menu bar. Each entry in a menu bar activates a drop-down menu of options from which the user can choose. A menu bar is always positioned at the top of a window. Although different in appearance, menu bars are handled in much the same way as are the other controls. While it is possible to manually position components within a window, doing so is quite tedious. The layout manager automates this task. For the first part of this chapter, which introduces the various controls, the default layout manager will be used. This displays components in a container using left-to-right, top-to-bottom organization. Once the controls have been covered, the layout managers will be examined. There you will see how to better manage the positioning of your controls. Control Fundamentals The AWT supports the following types of controls: • Labels • Push buttons • Check boxes • Choice lists 773 774 PART II The Java Library • Lists • Scroll bars • Text Editing These controls are subclasses of Component. Adding and Removing Controls To include a control in a window, you must add it to the window. To do this, you must first create an instance of the desired control and then add it to a window by calling add( ), which is defined by Container. The add( ) method has several forms. The following form is the one that is used for the first part of this chapter: Component add(Component compObj) Here, compObj is an instance of the control that you want to add. A reference to compObj is returned. Once a control has been added, it will automatically be visible whenever its parent window is displayed. Sometimes you will want to remove a control from a window when the control is no longer needed. To do this, call remove( ). This method is also defined by Container. Here is one of its forms: void remove(Component obj) Here, obj is a reference to the control you want to remove. You can remove all controls by calling removeAll( ). Responding to Controls Except for labels, which are passive, all controls generate events when they are accessed by the user. For example, when the user clicks on a push button, an event is sent that identifies the push button. In general, your program simply implements the appropriate interface and then registers an event listener for each control that you need to monitor. As explained in Chapter 23, once a listener has been installed, events are automatically sent to it. In the sections that follow, the appropriate interface for each control is specified. The HeadlessException Most of the AWT controls described in this chapter have constructors that can throw a HeadlessException when an attempt is made to instantiate a GUI component in a noninteractive environment (such as one in which no display, mouse, or keyboard is present). The HeadlessException was added by Java 1.4. You can use this exception to write code that can adapt to non-interactive environments. (Of course, this is not always possible.) This exception is not handled by the programs in this chapter because an interactive environment is required to demonstrate the AWT controls. Chapter 25 Using AWT Controls, Layout Managers, and Menus 775 Labels The easiest control to use is a label. A label is an object of type Label, and it contains a string, which it displays. Labels are passive controls that do not support any interaction with the user. Label defines the following constructors: The first version creates a blank label. The second version creates a label that contains the string specified by str. This string is left-justified. The third version creates a label that contains the string specified by str using the alignment specified by how. The value of how must be one of these three constants: Label.LEFT, Label.RIGHT, or Label.CENTER. You can set or change the text in a label by using the setText( ) method. You can obtain the current label by calling getText( ). These methods are shown here: void setText(String str) String getText( ) For setText( ), str specifies the new label. For getText( ), the current label is returned. You can set the alignment of the string within the label by calling setAlignment( ). To obtain the current alignment, call getAlignment( ). The methods are as follows: void setAlignment(int how) int getAlignment( ) Here, how must be one of the alignment constants shown earlier. The following example creates three labels and adds them to an applet window: // Demonstrate Labels import java.awt.*; import java.applet.*; /* */ public class LabelDemo extends Applet { public void init() { Label one = new Label("One"); Label two = new Label("Two"); Label three = new Label("Three"); // add labels to applet window add(one); add(two); add(three); } } Part II Label( ) throws HeadlessException Label(String str) throws HeadlessException Label(String str, int how) throws HeadlessException 776 PART II The Java Library Here is the window created by the LabelDemo applet. Notice that the labels are organized in the window by the default layout manager. Later, you will see how to control more precisely the placement of the labels. Using Buttons Perhaps the most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button. Button defines these two constructors: Button( ) throws HeadlessException Button(String str) throws HeadlessException The first version creates an empty button. The second creates a button that contains str as a label. After a button has been created, you can set its label by calling setLabel( ). You can retrieve its label by calling getLabel( ). These methods are as follows: void setLabel(String str) String getLabel( ) Here, str becomes the new label for the button. Handling Buttons Each time a button is pressed, an action event is generated. This is sent to any listeners that previously registered an interest in receiving action event notifications from that component. Each listener implements the ActionListener interface. That interface defines the actionPerformed( ) method, which is called when an event occurs. An ActionEvent object is supplied as the argument to this method. It contains both a reference to the button that generated the event and a reference to the action command string associated with the button. By default, the action command string is the label of the button. Usually, either the button reference or the action command string can be used to identify the button. (You will soon see examples of each approach.) Here is an example that creates three buttons labeled "Yes", "No", and "Undecided". Each time one is pressed, a message is displayed that reports which button has been pressed. In this version, the action command of the button (which, by default, is its label) Chapter 25 Using AWT Controls, Layout Managers, and Menus 777 is used to determine which button has been pressed. The label is obtained by calling the getActionCommand( ) method on the ActionEvent object passed to actionPerformed( ). public class ButtonDemo extends Applet implements ActionListener { String msg = ""; Button yes, no, maybe; public void init() { yes = new Button("Yes"); no = new Button("No"); maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); if(str.equals("Yes")) { msg = "You pressed Yes."; } else if(str.equals("No")) { msg = "You pressed No."; } else { msg = "You pressed Undecided."; } repaint(); } public void paint(Graphics g) { g.drawString(msg, 6, 100); } } Part II // Demonstrate Buttons import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ 778 PART II The Java Library Figure 25-1 Sample output from the ButtonDemo applet Sample output from the ButtonDemo program is shown in Figure 25-1. As mentioned, in addition to comparing button action command strings, you can also determine which button has been pressed by comparing the object obtained from the getSource( ) method to the button objects that you added to the window. To do this, you must keep a list of the objects when they are added. The following applet shows this approach: // Recognize Button objects. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class ButtonList extends Applet implements ActionListener { String msg = ""; Button bList[] = new Button[3]; public void init() { Button yes = new Button("Yes"); Button no = new Button("No"); Button maybe = new Button("Undecided"); // store bList[0] bList[1] bList[2] references = (Button) = (Button) = (Button) to buttons as added add(yes); add(no); add(maybe); // register to receive action events for(int i = 0; i < 3; i++) { bList[i].addActionListener(this); } } public void actionPerformed(ActionEvent ae) { Chapter 25 Using AWT Controls, Layout Managers, and Menus 779 for(int i = 0; i < 3; i++) { if(ae.getSource() == bList[i]) { msg = "You pressed " + bList[i].getLabel(); } } repaint(); } } In this version, the program stores each button reference in an array when the buttons are added to the applet window. (Recall that the add( ) method returns a reference to the button when it is added.) Inside actionPerformed( ), this array is then used to determine which button has been pressed. For simple programs, it is usually easier to recognize buttons by their labels. However, in situations in which you will be changing the label inside a button during the execution of your program, or using buttons that have the same label, it may be easier to determine which button has been pushed by using its object reference. It is also possible to set the action command string associated with a button to something other than its label by calling setActionCommand( ). This method changes the action command string, but does not affect the string used to label the button. Thus, setting the action command enables the action command and the label of a button to differ. Applying Check Boxes A check box is a control that is used to turn an option on or off. It consists of a small box that can either contain a check mark or not. There is a label associated with each check box that describes what option the box represents. You change the state of a check box by clicking on it. Check boxes can be used individually or as part of a group. Check boxes are objects of the Checkbox class. Checkbox supports these constructors: Checkbox( ) throws HeadlessException Checkbox(String str) throws HeadlessException Checkbox(String str, boolean on) throws HeadlessException Checkbox(String str, boolean on, CheckboxGroup cbGroup) throws HeadlessException Checkbox(String str, CheckboxGroup cbGroup, boolean on) throws HeadlessException The first form creates a check box whose label is initially blank. The state of the check box is unchecked. The second form creates a check box whose label is specified by str. The state of the check box is unchecked. The third form allows you to set the initial state of the check box. If on is true, the check box is initially checked; otherwise, it is cleared. The fourth and fifth forms create a check box whose label is specified by str and whose group is specified by cbGroup. If this check box is not part of a group, then cbGroup must be null. (Check box groups are described in the next section.) The value of on determines the initial state of the check box. Part II public void paint(Graphics g) { g.drawString(msg, 6, 100); } 780 PART II The Java Library To retrieve the current state of a check box, call getState( ). To set its state, call setState( ). You can obtain the current label associated with a check box by calling getLabel( ). To set the label, call setLabel( ). These methods are as follows: boolean getState( ) void setState(boolean on) String getLabel( ) void setLabel(String str) Here, if on is true, the box is checked. If it is false, the box is cleared. The string passed in str becomes the new label associated with the invoking check box. Handling Check Boxes Each time a check box is selected or deselected, an item event is generated. This is sent to any listeners that previously registered an interest in receiving item event notifications from that component. Each listener implements the ItemListener interface. That interface defines the itemStateChanged( ) method. An ItemEvent object is supplied as the argument to this method. It contains information about the event (for example, whether it was a selection or deselection). The following program creates four check boxes. The initial state of the first box is checked. The status of each check box is displayed. Each time you change the state of a check box, the status display is updated. // Demonstrate check boxes. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class CheckboxDemo extends Applet implements ItemListener { String msg = ""; Checkbox winXP, win7, solaris, mac; public void init() { winXP = new Checkbox("Windows XP", null, true); win7 = new Checkbox("Windows 7"); solaris = new Checkbox("Solaris"); mac = new Checkbox("Mac OS"); add(winXP); add(win7); add(solaris); add(mac); Chapter 25 Using AWT Controls, Layout Managers, and Menus 781 winXP.addItemListener(this); win7.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current state: "; g.drawString(msg, 6, 80); msg = " Windows XP: " + winXP.getState(); g.drawString(msg, 6, 100); msg = " Windows 7: " + win7.getState(); g.drawString(msg, 6, 120); msg = " Solaris: " + solaris.getState(); g.drawString(msg, 6, 140); msg = " Mac OS: " + mac.getState(); g.drawString(msg, 6, 160); } } Sample output is shown in Figure 25-2. Figure 25-2 Sample output from the CheckboxDemo applet Part II public void itemStateChanged(ItemEvent ie) { repaint(); } 782 PART II The Java Library CheckboxGroup It is possible to create a set of mutually exclusive check boxes in which one and only one check box in the group can be checked at any one time. These check boxes are often called radio buttons, because they act like the station selector on a car radio—only one station can be selected at any one time. To create a set of mutually exclusive check boxes, you must first define the group to which they will belong and then specify that group when you construct the check boxes. Check box groups are objects of type CheckboxGroup. Only the default constructor is defined, which creates an empty group. You can determine which check box in a group is currently selected by calling getSelectedCheckbox( ). You can set a check box by calling setSelectedCheckbox( ). These methods are as follows: Checkbox getSelectedCheckbox( ) void setSelectedCheckbox(Checkbox which) Here, which is the check box that you want to be selected. The previously selected check box will be turned off. Here is a program that uses check boxes that are part of a group: // Demonstrate check box group. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class CBGroup extends Applet implements ItemListener { String msg = ""; Checkbox winXP, win7, solaris, mac; CheckboxGroup cbg; public void init() { cbg = new CheckboxGroup(); winXP = new Checkbox("Windows XP", cbg, true); win7 = new Checkbox("Windows 7", cbg, false); solaris = new Checkbox("Solaris", cbg, false); mac = new Checkbox("Mac OS", cbg, false); add(winXP); add(win7); add(solaris); add(mac); Chapter 25 Using AWT Controls, Layout Managers, and Menus 783 winXP.addItemListener(this); win7.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current selection: "; msg += cbg.getSelectedCheckbox().getLabel(); g.drawString(msg, 6, 100); } } Output generated by the CBGroup applet is shown in Figure 25-3. Notice that the check boxes are now circular in shape. Choice Controls The Choice class is used to create a pop-up list of items from which the user may choose. Thus, a Choice control is a form of menu. When inactive, a Choice component takes up only enough space to show the currently selected item. When the user clicks on it, the Figure 25-3 Sample output from the CBGroup applet Part II public void itemStateChanged(ItemEvent ie) { repaint(); } 784 PART II The Java Library whole list of choices pops up, and a new selection can be made. Each item in the list is a string that appears as a left-justified label in the order it is added to the Choice object. Choice defines only the default constructor, which creates an empty list. To add a selection to the list, call add( ). It has this general form: void add(String name) Here, name is the name of the item being added. Items are added to the list in the order in which calls to add( ) occur. To determine which item is currently selected, you may call either getSelectedItem( ) or getSelectedIndex( ). These methods are shown here: String getSelectedItem( ) int getSelectedIndex( ) The getSelectedItem( ) method returns a string containing the name of the item. getSelectedIndex( ) returns the index of the item. The first item is at index 0. By default, the first item added to the list is selected. To obtain the number of items in the list, call getItemCount( ). You can set the currently selected item using the select( ) method with either a zero-based integer index or a string that will match a name in the list. These methods are shown here: int getItemCount( ) void select(int index) void select(String name) Given an index, you can obtain the name associated with the item at that index by calling getItem( ), which has this general form: String getItem(int index) Here, index specifies the index of the desired item. Handling Choice Lists Each time a choice is selected, an item event is generated. This is sent to any listeners that previously registered an interest in receiving item event notifications from that component. Each listener implements the ItemListener interface. That interface defines the itemStateChanged( ) method. An ItemEvent object is supplied as the argument to this method. Here is an example that creates two Choice menus. One selects the operating system. The other selects the browser. // Demonstrate Choice lists. import java.awt.*; import java.awt.event.*; import java.applet.*; /* Chapter 25 Using AWT Controls, Layout Managers, and Menus 785 */ public class ChoiceDemo extends Applet implements ItemListener { Choice os, browser; String msg = ""; // add items to os list os.add("Windows XP"); os.add("Windows 7"); os.add("Solaris"); os.add("Mac OS"); // add items to browser list browser.add("Internet Explorer"); browser.add("Firefox"); browser.add("Opera"); // add choice lists to window add(os); add(browser); // register to receive item events os.addItemListener(this); browser.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current selections. public void paint(Graphics g) { msg = "Current OS: "; msg += os.getSelectedItem(); g.drawString(msg, 6, 120); msg = "Current Browser: "; msg += browser.getSelectedItem(); g.drawString(msg, 6, 140); } } Sample output is shown in Figure 25-4. Part II public void init() { os = new Choice(); browser = new Choice(); 786 PART II The Java Library Figure 25-4 Sample output from the ChoiceDemo applet Using Lists The List class provides a compact, multiple-choice, scrolling selection list. Unlike the Choice object, which shows only the single selected item in the menu, a List object can be constructed to show any number of choices in the visible window. It can also be created to allow multiple selections. List provides these constructors: List( ) throws HeadlessException List(int numRows) throws HeadlessException List(int numRows, boolean multipleSelect) throws HeadlessException The first version creates a List control that allows only one item to be selected at any one time. In the second form, the value of numRows specifies the number of entries in the list that will always be visible (others can be scrolled into view as needed). In the third form, if multipleSelect is true, then the user may select two or more items at a time. If it is false, then only one item may be selected. To add a selection to the list, call add( ). It has the following two forms: void add(String name) void add(String name, int index) Here, name is the name of the item added to the list. The first form adds items to the end of the list. The second form adds the item at the index specified by index. Indexing begins at zero. You can specify –1 to add the item to the end of the list. For lists that allow only single selection, you can determine which item is currently selected by calling either getSelectedItem( ) or getSelectedIndex( ). These methods are shown here: String getSelectedItem( ) int getSelectedIndex( ) The getSelectedItem( ) method returns a string containing the name of the item. If more than one item is selected, or if no selection has yet been made, null is returned. getSelectedIndex( ) returns the index of the item. The first item is at index 0. If more than one item is selected, or if no selection has yet been made, –1 is returned. Chapter 25 Using AWT Controls, Layout Managers, and Menus 787 For lists that allow multiple selection, you must use either getSelectedItems( ) or getSelectedIndexes( ), shown here, to determine the current selections: getSelectedItems( ) returns an array containing the names of the currently selected items. getSelectedIndexes( ) returns an array containing the indexes of the currently selected items. To obtain the number of items in the list, call getItemCount( ). You can set the currently selected item by using the select( ) method with a zero-based integer index. These methods are shown here: int getItemCount( ) void select(int index) Given an index, you can obtain the name associated with the item at that index by calling getItem( ), which has this general form: String getItem(int index) Here, index specifies the index of the desired item. Handling Lists To process list events, you will need to implement the ActionListener interface. Each time a List item is double-clicked, an ActionEvent object is generated. Its getActionCommand( ) method can be used to retrieve the name of the newly selected item. Also, each time an item is selected or deselected with a single click, an ItemEvent object is generated. Its getStateChange( ) method can be used to determine whether a selection or deselection triggered this event. getItemSelectable( ) returns a reference to the object that triggered this event. Here is an example that converts the Choice controls in the preceding section into List components, one multiple choice and the other single choice: // Demonstrate Lists. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class ListDemo extends Applet implements ActionListener { List os, browser; String msg = ""; public void init() { os = new List(4, true); browser = new List(4, false); Part II String[ ] getSelectedItems( ) int[ ] getSelectedIndexes( ) 788 PART II The Java Library // add items to os list os.add("Windows XP"); os.add("Windows 7"); os.add("Solaris"); os.add("Mac OS"); // add items to browser list browser.add("Internet Explorer"); browser.add("Firefox"); browser.add("Opera"); browser.select(1); // add lists to window add(os); add(browser); // register to receive action events os.addActionListener(this); browser.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repaint(); } // Display current selections. public void paint(Graphics g) { int idx[]; msg = "Current OS: "; idx = os.getSelectedIndexes(); for(int i=0; i

*/ public class SBDemo extends Applet implements AdjustmentListener, MouseMotionListener { String msg = ""; Scrollbar vertSB, horzSB; Chapter 25 Using AWT Controls, Layout Managers, and Menus 791 public void init() { int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); vertSB = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, height); vertSB.setPreferredSize(new Dimension(20, 100)); add(vertSB); add(horzSB); // register to receive adjustment events vertSB.addAdjustmentListener(this); horzSB.addAdjustmentListener(this); addMouseMotionListener(this); } public void adjustmentValueChanged(AdjustmentEvent ae) { repaint(); } // Update scroll bars to reflect mouse dragging. public void mouseDragged(MouseEvent me) { int x = me.getX(); int y = me.getY(); vertSB.setValue(y); horzSB.setValue(x); repaint(); } // Necessary for MouseMotionListener public void mouseMoved(MouseEvent me) { } // Display current value of scroll bars. public void paint(Graphics g) { msg = "Vertical: " + vertSB.getValue(); msg += ", Horizontal: " + horzSB.getValue(); g.drawString(msg, 6, 160); // show current mouse drag position g.drawString("*", horzSB.getValue(), vertSB.getValue()); } } Sample output from the SBDemo applet is shown in Figure 25-6. Part II horzSB = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, width); horzSB.setPreferredSize(new Dimension(100, 20)); 792 PART II The Java Library Figure 25-6 Sample output from the SBDemo applet Using a TextField The TextField class implements a single-line text-entry area, usually called an edit control. Text fields allow the user to enter strings and to edit the text using the arrow keys, cut and paste keys, and mouse selections. TextField is a subclass of TextComponent. TextField defines the following constructors: TextField( ) throws HeadlessException TextField(int numChars) throws HeadlessException TextField(String str) throws HeadlessException TextField(String str, int numChars) throws HeadlessException The first version creates a default text field. The second form creates a text field that is numChars characters wide. The third form initializes the text field with the string contained in str. The fourth form initializes a text field and sets its width. TextField (and its superclass TextComponent) provides several methods that allow you to utilize a text field. To obtain the string currently contained in the text field, call getText( ). To set the text, call setText( ). These methods are as follows: String getText( ) void setText(String str) Here, str is the new string. The user can select a portion of the text in a text field. Also, you can select a portion of text under program control by using select( ). Your program can obtain the currently selected text by calling getSelectedText( ). These methods are shown here: String getSelectedText( ) void select(int startIndex, int endIndex) getSelectedText( ) returns the selected text. The select( ) method selects the characters beginning at startIndex and ending at endIndex –1. Chapter 25 Using AWT Controls, Layout Managers, and Menus 793 You can control whether the contents of a text field may be modified by the user by calling setEditable( ). You can determine editability by calling isEditable( ). These methods are shown here: isEditable( ) returns true if the text may be changed and false if not. In setEditable( ), if canEdit is true, the text may be changed. If it is false, the text cannot be altered. There may be times when you will want the user to enter text that is not displayed, such as a password. You can disable the echoing of the characters as they are typed by calling setEchoChar( ). This method specifies a single character that the TextField will display when characters are entered (thus, the actual characters typed will not be shown). You can check a text field to see if it is in this mode with the echoCharIsSet( ) method. You can retrieve the echo character by calling the getEchoChar( ) method. These methods are as follows: void setEchoChar(char ch) boolean echoCharIsSet( ) char getEchoChar( ) Here, ch specifies the character to be echoed. If ch is zero, then normal echoing is restored. Handling a TextField Since text fields perform their own editing functions, your program generally will not respond to individual key events that occur within a text field. However, you may want to respond when the user presses enter. When this occurs, an action event is generated. Here is an example that creates the classic user name and password screen: // Demonstrate text field. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class TextFieldDemo extends Applet implements ActionListener { TextField name, pass; public void init() { Label namep = new Label("Name: ", Label.RIGHT); Label passp = new Label("Password: ", Label.RIGHT); name = new TextField(12); pass = new TextField(8); pass.setEchoChar('?'); Part II boolean isEditable( ) void setEditable(boolean canEdit) 794 PART II The Java Library add(namep); add(name); add(passp); add(pass); // register to receive action events name.addActionListener(this); pass.addActionListener(this); } // User pressed Enter. public void actionPerformed(ActionEvent ae) { repaint(); } public void paint(Graphics g) { g.drawString("Name: " + name.getText(), 6, 60); g.drawString("Selected text in name: " + name.getSelectedText(), 6, 80); g.drawString("Password: " + pass.getText(), 6, 100); } } Sample output from the TextFieldDemo applet is shown in Figure 25-7. Using a TextArea Sometimes a single line of text input is not enough for a given task. To handle these situations, the AWT includes a simple multiline editor called TextArea. Following are the constructors for TextArea: TextArea( ) throws HeadlessException TextArea(int numLines, int numChars) throws HeadlessException TextArea(String str) throws HeadlessException TextArea(String str, int numLines, int numChars) throws HeadlessException TextArea(String str, int numLines, int numChars, int sBars) throws HeadlessException Figure 25-7 Sample output from the TextFieldDemo applet Chapter 25 Using AWT Controls, Layout Managers, and Menus 795 SCROLLBARS_BOTH SCROLLBARS_NONE SCROLLBARS_HORIZONTAL_ONLY SCROLLBARS_VERTICAL_ONLY TextArea is a subclass of TextComponent. Therefore, it supports the getText( ), setText( ), getSelectedText( ), select( ), isEditable( ), and setEditable( ) methods described in the preceding section. TextArea adds the following methods: void append(String str) void insert(String str, int index) void replaceRange(String str, int startIndex, int endIndex) The append( ) method appends the string specified by str to the end of the current text. insert( ) inserts the string passed in str at the specified index. To replace text, call replaceRange( ). It replaces the characters from startIndex to endIndex–1, with the replacement text passed in str. Text areas are almost self-contained controls. Your program incurs virtually no management overhead. Normally, your program simply obtains the current text when it is needed. You can, however, listen for TextEvents, if you choose. The following program creates a TextArea control: // Demonstrate TextArea. import java.awt.*; import java.applet.*; /* */ public class TextAreaDemo extends Applet { public void init() { String val = "Java 7 is the latest version of the most\n" + "widely-used computer language for Internet programming.\n" + "Building on a rich heritage, Java has advanced both\n" + "the art and science of computer language design.\n\n" + "One of the reasons for Java's ongoing success is its\n" + "constant, steady rate of evolution. Java has never stood\n" + "still. Instead, Java has consistently adapted to the\n" + "rapidly changing landscape of the networked world.\n" + "Moreover, Java has often led the way, charting the\n" + "course for others to follow."; TextArea text = new TextArea(val, 10, 30); add(text); } } Part II Here, numLines specifies the height, in lines, of the text area, and numChars specifies its width, in characters. Initial text can be specified by str. In the fifth form, you can specify the scroll bars that you want the control to have. sBars must be one of these values: 796 PART II The Java Library Here is sample output from the TextAreaDemo applet: Understanding Layout Managers All of the components that we have shown so far have been positioned by the default layout manager. As we mentioned at the beginning of this chapter, a layout manager automatically arranges your controls within a window by using some type of algorithm. If you have programmed for other GUI environments, such as Windows, then you may have laid out your controls by hand. While it is possible to lay out Java controls by hand, too, you generally won’t want to, for two main reasons. First, it is very tedious to manually lay out a large number of components. Second, sometimes the width and height information is not yet available when you need to arrange some control, because the native toolkit components haven’t been realized. This is a chicken-and-egg situation; it is pretty confusing to figure out when it is okay to use the size of a given component to position it relative to another. Each Container object has a layout manager associated with it. A layout manager is an instance of any class that implements the LayoutManager interface. The layout manager is set by the setLayout( ) method. If no call to setLayout( ) is made, then the default layout manager is used. Whenever a container is resized (or sized for the first time), the layout manager is used to position each of the components within it. The setLayout( ) method has the following general form: void setLayout(LayoutManager layoutObj) Here, layoutObj is a reference to the desired layout manager. If you wish to disable the layout manager and position components manually, pass null for layoutObj. If you do this, you will need to determine the shape and position of each component manually, using the setBounds( ) method defined by Component. Normally, you will want to use a layout manager. Each layout manager keeps track of a list of components that are stored by their names. The layout manager is notified each time you add a component to a container. Whenever the container needs to be resized, the layout manager is consulted via its minimumLayoutSize( ) and preferredLayoutSize( ) methods. Each component that is being managed by a layout manager contains the getPreferredSize( ) and getMinimumSize( ) methods. These return the preferred and minimum size required to display each component. The layout manager will Chapter 25 Using AWT Controls, Layout Managers, and Menus 797 honor these requests if at all possible, while maintaining the integrity of the layout policy. You may override these methods for controls that you subclass. Default values are provided otherwise. Java has several predefined LayoutManager classes, several of which are described next. You can use the layout manager that best fits your application. FlowLayout is the default layout manager. This is the layout manager that the preceding examples have used. FlowLayout implements a simple layout style, which is similar to how words flow in a text editor. The direction of the layout is governed by the container’s component orientation property, which, by default, is left to right, top to bottom. Therefore, by default, components are laid out line-by-line beginning at the upper-left corner. In all cases, when a line is filled, layout advances to the next line. A small space is left between each component, above and below, as well as left and right. Here are the constructors for FlowLayout: FlowLayout( ) FlowLayout(int how) FlowLayout(int how, int horz, int vert) The first form creates the default layout, which centers components and leaves five pixels of space between each component. The second form lets you specify how each line is aligned. Valid values for how are as follows: FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT FlowLayout.LEADING FlowLayout.TRAILING These values specify left, center, right, leading edge, and trailing edge alignment, respectively. The third constructor allows you to specify the horizontal and vertical space left between components in horz and vert, respectively. Here is a version of the CheckboxDemo applet shown earlier in this chapter, modified so that it uses left-aligned flow layout: // Use left-aligned flow layout. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class FlowLayoutDemo extends Applet implements ItemListener { String msg = ""; Checkbox winXP, win7, solaris, mac; Part II FlowLayout 798 PART II The Java Library public void init() { // set left-aligned flow layout setLayout(new FlowLayout(FlowLayout.LEFT)); winXP = new Checkbox("Windows XP", null, true); win7 = new Checkbox("Windows 7"); solaris = new Checkbox("Solaris"); mac = new Checkbox("Mac OS"); add(winXP); add(win7); add(solaris); add(mac); // register to receive item events winXP.addItemListener(this); win7.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } // Repaint when status of a check box changes. public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current state: "; g.drawString(msg, 6, 80); msg = " Windows XP: " + winXP.getState(); g.drawString(msg, 6, 100); msg = " Windows 7: " + win7.getState(); g.drawString(msg, 6, 120); msg = " Solaris: " + solaris.getState(); g.drawString(msg, 6, 140); msg = " Mac: " + mac.getState(); g.drawString(msg, 6, 160); } } Here is sample output generated by the FlowLayoutDemo applet. Compare this with the output from the CheckboxDemo applet, shown earlier in Figure 25-2. BorderLayout The BorderLayout class implements a common layout style for top-level windows. It has four narrow, fixed-width components at the edges and one large area in the center. The four sides are referred to as Chapter 25 Using AWT Controls, Layout Managers, and Menus 799 north, south, east, and west. The middle area is called the center. Here are the constructors defined by BorderLayout: BorderLayout( ) BorderLayout(int horz, int vert) BorderLayout.CENTER BorderLayout.SOUTH BorderLayout.EAST BorderLayout.WEST BorderLayout.NORTH When adding components, you will use these constants with the following form of add( ), which is defined by Container: void add(Component compObj, Object region) Here, compObj is the component to be added, and region specifies where the component will be added. Here is an example of a BorderLayout with a component in each layout area: // Demonstrate BorderLayout. import java.awt.*; import java.applet.*; import java.util.*; /* */ public class BorderLayoutDemo extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button("This is across the top."), BorderLayout.NORTH); add(new Label("The footer message might go here."), BorderLayout.SOUTH); add(new Button("Right"), BorderLayout.EAST); add(new Button("Left"), BorderLayout.WEST); String msg = "The reasonable man adapts " + "himself to the world;\n" + "the unreasonable one persists in " + "trying to adapt the world to himself.\n" + "Therefore all progress depends " + "on the unreasonable man.\n\n" + " - George Bernard Shaw\n\n"; add(new TextArea(msg), BorderLayout.CENTER); } } Part II The first form creates a default border layout. The second allows you to specify the horizontal and vertical space left between components in horz and vert, respectively. BorderLayout defines the following constants that specify the regions: 800 PART II The Java Library Sample output from the BorderLayoutDemo applet is shown here: Using Insets Sometimes you will want to leave a small amount of space between the container that holds your components and the window that contains it. To do this, override the getInsets( ) method that is defined by Container. This method returns an Insets object that contains the top, bottom, left, and right inset to be used when the container is displayed. These values are used by the layout manager to inset the components when it lays out the window. The constructor for Insets is shown here: Insets(int top, int left, int bottom, int right) The values passed in top, left, bottom, and right specify the amount of space between the container and its enclosing window. The getInsets( ) method has this general form: Insets getInsets( ) When overriding this method, you must return a new Insets object that contains the inset spacing you desire. Here is the preceding BorderLayout example modified so that it insets its components ten pixels from each border. The background color has been set to cyan to help make the insets more visible. // Demonstrate BorderLayout with insets. import java.awt.*; import java.applet.*; import java.util.*; /* */ public class InsetsDemo extends Applet { public void init() { // set background color so insets can be easily seen setBackground(Color.cyan); setLayout(new BorderLayout()); Chapter 25 Using AWT Controls, Layout Managers, and Menus 801 String msg = "The reasonable man adapts " + "himself to the world;\n" + "the unreasonable one persists in " + "trying to adapt the world to himself.\n" + "Therefore all progress depends " + "on the unreasonable man.\n\n" + " - George Bernard Shaw\n\n"; add(new TextArea(msg), BorderLayout.CENTER); } // add insets public Insets getInsets() { return new Insets(10, 10, 10, 10); } } Output from the InsetsDemo applet is shown here: GridLayout GridLayout lays out components in a two-dimensional grid. When you instantiate a GridLayout, you define the number of rows and columns. The constructors supported by GridLayout are shown here: GridLayout( ) GridLayout(int numRows, int numColumns) GridLayout(int numRows, int numColumns, int horz, int vert) The first form creates a single-column grid layout. The second form creates a grid layout with the specified number of rows and columns. The third form allows you to specify the horizontal and vertical space left between components in horz and vert, respectively. Either numRows or numColumns can be zero. Specifying numRows as zero allows for unlimitedlength columns. Specifying numColumns as zero allows for unlimited-length rows. Part II add(new Button("This is across the top."), BorderLayout.NORTH); add(new Label("The footer message might go here."), BorderLayout.SOUTH); add(new Button("Right"), BorderLayout.EAST); add(new Button("Left"), BorderLayout.WEST); 802 PART II The Java Library Here is a sample program that creates a 4×4 grid and fills it in with 15 buttons, each labeled with its index: // Demonstrate GridLayout import java.awt.*; import java.applet.*; /* */ public class GridLayoutDemo extends Applet { static final int n = 4; public void init() { setLayout(new GridLayout(n, n)); setFont(new Font("SansSerif", Font.BOLD, 24)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int k = i * n + j; if(k > 0) add(new Button("" + k)); } } } } Following is the output generated by the GridLayoutDemo applet: TIP You might try using this example as the starting point for a 15-square puzzle. CardLayout The CardLayout class is unique among the other layout managers in that it stores several different layouts. Each layout can be thought of as being on a separate index card in a deck that can be shuffled so that any card is on top at a given time. This can be useful for user interfaces with optional components that can be dynamically enabled and disabled upon Chapter 25 Using AWT Controls, Layout Managers, and Menus 803 user input. You can prepare the other layouts and have them hidden, ready to be activated when needed. CardLayout provides these two constructors: The first form creates a default card layout. The second form allows you to specify the horizontal and vertical space left between components in horz and vert, respectively. Use of a card layout requires a bit more work than the other layouts. The cards are typically held in an object of type Panel. This panel must have CardLayout selected as its layout manager. The cards that form the deck are also typically objects of type Panel. Thus, you must create a panel that contains the deck and a panel for each card in the deck. Next, you add to the appropriate panel the components that form each card. You then add these panels to the panel for which CardLayout is the layout manager. Finally, you add this panel to the window. Once these steps are complete, you must provide some way for the user to select between cards. One common approach is to include one push button for each card in the deck. When card panels are added to a panel, they are usually given a name. Thus, most of the time, you will use this form of add( ) when adding cards to a panel: void add(Component panelObj, Object name) Here, name is a string that specifies the name of the card whose panel is specified by panelObj. After you have created a deck, your program activates a card by calling one of the following methods defined by CardLayout: void first(Container deck) void last(Container deck) void next(Container deck) void previous(Container deck) void show(Container deck, String cardName) Here, deck is a reference to the container (usually a panel) that holds the cards, and cardName is the name of a card. Calling first( ) causes the first card in the deck to be shown. To show the last card, call last( ). To show the next card, call next( ). To show the previous card, call previous( ). Both next( ) and previous( ) automatically cycle back to the top or bottom of the deck, respectively. The show( ) method displays the card whose name is passed in cardName. The following example creates a two-level card deck that allows the user to select an operating system. Windows-based operating systems are displayed in one card. Mac OS and Solaris are displayed in the other card. // Demonstrate CardLayout. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ Part II CardLayout( ) CardLayout(int horz, int vert) 804 PART II The Java Library public class CardLayoutDemo extends Applet implements ActionListener, MouseListener { Checkbox winXP, win7, solaris, mac; Panel osCards; CardLayout cardLO; Button Win, Other; public void init() { Win = new Button("Windows"); Other = new Button("Other"); add(Win); add(Other); cardLO = new CardLayout(); osCards = new Panel(); osCards.setLayout(cardLO); // set panel layout to card layout winXP = new Checkbox("Windows XP", null, true); win7 = new Checkbox("Windows 7"); solaris = new Checkbox("Solaris"); mac = new Checkbox("Mac OS"); // add Windows check boxes to a panel Panel winPan = new Panel(); winPan.add(winXP); winPan.add(win7); // add other OS check boxes to a panel Panel otherPan = new Panel(); otherPan.add(solaris); otherPan.add(mac); // add panels to card deck panel osCards.add(winPan, "Windows"); osCards.add(otherPan, "Other"); // add cards to main applet panel add(osCards); // register to receive action events Win.addActionListener(this); Other.addActionListener(this); // register mouse events addMouseListener(this); } // Cycle through panels. public void mousePressed(MouseEvent me) { cardLO.next(osCards); } Chapter 25 Using AWT Controls, Layout Managers, and Menus 805 public void actionPerformed(ActionEvent ae) { if(ae.getSource() == Win) { cardLO.show(osCards, "Windows"); } else { cardLO.show(osCards, "Other"); } } } Here is the output generated by the CardLayoutDemo applet. Each card is activated by pushing its button. You can also cycle through the cards by clicking the mouse. GridBagLayout Although the preceding layouts are perfectly acceptable for many uses, some situations will require that you take a bit more control over how the components are arranged. A good way to do this is to use a grid bag layout, which is specified by the GridBagLayout class. What makes the grid bag useful is that you can specify the relative placement of components by specifying their positions within cells inside a grid. The key to the grid bag is that each component can be a different size, and each row in the grid can have a different number of columns. This is why the layout is called a grid bag. It’s a collection of small grids joined together. Part II // Provide empty implementations for the other MouseListener methods. public void mouseClicked(MouseEvent me) { } public void mouseEntered(MouseEvent me) { } public void mouseExited(MouseEvent me) { } public void mouseReleased(MouseEvent me) { } 806 PART II The Java Library The location and size of each component in a grid bag are determined by a set of constraints linked to it. The constraints are contained in an object of type GridBagConstraints. Constraints include the height and width of a cell, and the placement of a component, its alignment, and its anchor point within the cell. The general procedure for using a grid bag is to first create a new GridBagLayout object and to make it the current layout manager. Then, set the constraints that apply to each component that will be added to the grid bag. Finally, add the components to the layout manager. Although GridBagLayout is a bit more complicated than the other layout managers, it is still quite easy to use once you understand how it works. GridBagLayout defines only one constructor, which is shown here: GridBagLayout( ) GridBagLayout defines several methods, of which many are protected and not for general use. There is one method, however, that you must use: setConstraints( ). It is shown here: void setConstraints(Component comp, GridBagConstraints cons) Here, comp is the component for which the constraints specified by cons apply. This method sets the constraints that apply to each component in the grid bag. The key to successfully using GridBagLayout is the proper setting of the constraints, which are stored in a GridBagConstraints object. GridBagConstraints defines several fields that you can set to govern the size, placement, and spacing of a component. These are shown in Table 25-1. Several are described in greater detail in the following discussion. GridBagConstraints also defines several static fields that contain standard constraint values, such as GridBagConstraints.CENTER and GridBagConstraints.VERTICAL. Field Purpose int anchor Specifies the location of a component within a cell. The default is GridBagConstraints.CENTER. int fill Specifies how a component is resized if the component is smaller than its cell. Valid values are GridBagConstraints.NONE (the default), GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL, GridBagConstraints.BOTH. int gridheight Specifies the height of component in terms of cells. The default is 1. int gridwidth Specifies the width of component in terms of cells. The default is 1. int gridx Specifies the X coordinate of the cell to which the component will be added. The default value is GridBagConstraints.RELATIVE. int gridy Specifies the Y coordinate of the cell to which the component will be added. The default value is GridBagConstraints.RELATIVE. Insets insets Specifies the insets. Default insets are all zero. int ipadx Specifies extra horizontal space that surrounds a component within a cell. The default is 0. int ipady Specifies extra vertical space that surrounds a component within a cell. The default is 0. Table 25-1 Constraint Fields Defined by GridBagConstraints Using AWT Controls, Layout Managers, and Menus Field Purpose double weightx Specifies a weight value that determines the horizontal spacing between cells and the edges of the container that holds them. The default value is 0.0. The greater the weight, the more space that is allocated. If all values are 0.0, extra space is distributed evenly between the edges of the window. double weighty Specifies a weight value that determines the vertical spacing between cells and the edges of the container that holds them. The default value is 0.0. The greater the weight, the more space that is allocated. If all values are 0.0, extra space is distributed evenly between the edges of the window. 807 Table 25-1 Constraint Fields Defined by GridBagConstraints (continued) When a component is smaller than its cell, you can use the anchor field to specify where within the cell the component’s top-left corner will be located. There are three types of values that you can give to anchor. The first are absolute: GridBagConstraints.CENTER GridBagConstraints.SOUTH GridBagConstraints.EAST GridBagConstraints.SOUTHEAST GridBagConstraints.NORTH GridBagConstraints.SOUTHWEST GridBagConstraints.NORTHEAST GridBagConstraints.WEST GridBagConstraints.NORTHWEST As their names imply, these values cause the component to be placed at the specific locations. The second type of values that can be given to anchor is relative, which means the values are relative to the container’s orientation, which might differ for non-Western languages. The relative values are shown here: GridBagConstraints.FIRST_LINE_END GridBagConstraints.LINE_END GridBagConstraints.FIRST_LINE_START GridBagConstraints.LINE_START GridBagConstraints.LAST_LINE_END GridBagConstraints.PAGE_END GridBagConstraints.LAST_LINE_START GridBagConstraints.PAGE_START Their names describe the placement. The third type of values that can be given to anchor allows you to position components relative to the baseline of the row. These values are shown here: GridBagConstraints.BASELINE GridBagConstraints.BASELINE_LEADING GridBagConstraints.BASELINE_TRAILING GridBagConstraints.ABOVE_BASELINE GridBagConstraints.ABOVE_BASELINE_LEADING GridBagConstraints.ABOVE_BASELINE_ TRAILING GridBagConstraints.BELOW_BASELINE GridBagConstraints.BELOW_BASELINE_ LEADING GridBagConstraints. BELOW_BASELINE_TRAILING Part II Chapter 25 808 PART II The Java Library The horizontal position can be either centered, against the leading edge (LEADING), or against the trailing edge (TRAILING). The weightx and weighty fields are both quite important and quite confusing at first glance. In general, their values determine how much of the extra space within a container is allocated to each row and column. By default, both these values are zero. When all values within a row or a column are zero, extra space is distributed evenly between the edges of the window. By increasing the weight, you increase that row or column’s allocation of space proportional to the other rows or columns. The best way to understand how these values work is to experiment with them a bit. The gridwidth variable lets you specify the width of a cell in terms of cell units. The default is 1. To specify that a component use the remaining space in a row, use GridBagConstraints.REMAINDER. To specify that a component use the next-to-last cell in a row, use GridBagConstraints.RELATIVE. The gridheight constraint works the same way, but in the vertical direction. You can specify a padding value that will be used to increase the minimum size of a cell. To pad horizontally, assign a value to ipadx. To pad vertically, assign a value to ipady. Here is an example that uses GridBagLayout to demonstrate several of the points just discussed: // Use GridBagLayout. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class GridBagDemo extends Applet implements ItemListener { String msg = ""; Checkbox winXP, win7, solaris, mac; public void init() { GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); // Define check boxes. winXP = new Checkbox("Windows XP ", null, true); win7 = new Checkbox("Windows 7"); solaris = new Checkbox("Solaris"); mac = new Checkbox("Mac OS"); // Define the grid bag. // Use default row weight of 0 for first row. gbc.weightx = 1.0; // use a column weight of 1 Chapter 25 Using AWT Controls, Layout Managers, and Menus 809 gbc.ipadx = 200; // pad by 200 units gbc.insets = new Insets(4, 4, 0, 0); // inset slightly from top left gbc.anchor = GridBagConstraints.NORTHEAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(winXP, gbc); // Give second row a weight of 1. gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(solaris, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(mac, gbc); // Add the components. add(winXP); add(win7); add(solaris); add(mac); // Register to receive item events. winXP.addItemListener(this); win7.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } // Repaint when status of a check box changes. public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current state: "; g.drawString(msg, 6, 80); msg = " Windows XP: " + winXP.getState(); g.drawString(msg, 6, 100); msg = " Windows 7: " + win7.getState(); g.drawString(msg, 6, 120); msg = " Solaris: " + solaris.getState(); g.drawString(msg, 6, 140); msg = " Mac: " + mac.getState(); g.drawString(msg, 6, 160); } } Part II gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(win7, gbc); 810 PART II The Java Library The output produced by the program is shown here. In this layout, the operating system check boxes are positioned in a 2×2 grid. Each cell has a horizontal padding of 200. Each component is inset slightly (by 4 units) from the top left. The column weight is set to 1, which causes any extra horizontal space to be distributed evenly between the columns. The first row uses a default weight of 0; the second has a weight of 1. This means that any extra vertical space is added to the second row. GridBagLayout is a powerful layout manager. It is worth taking some time to experiment with and explore. Once you understand what the various settings do, you can use GridBagLayout to position components with a high degree of precision. Menu Bars and Menus A top-level window can have a menu bar associated with it. A menu bar displays a list of top-level menu choices. Each choice is associated with a drop-down menu. This concept is implemented in the AWT by the following classes: MenuBar, Menu, and MenuItem. In general, a menu bar contains one or more Menu objects. Each Menu object contains a list of MenuItem objects. Each MenuItem object represents something that can be selected by the user. Since Menu is a subclass of MenuItem, a hierarchy of nested submenus can be created. It is also possible to include checkable menu items. These are menu options of type CheckboxMenuItem and will have a check mark next to them when they are selected. To create a menu bar, first create an instance of MenuBar. This class defines only the default constructor. Next, create instances of Menu that will define the selections displayed on the bar. Following are the constructors for Menu: Menu( ) throws HeadlessException Menu(String optionName) throws HeadlessException Menu(String optionName, boolean removable) throws HeadlessException Here, optionName specifies the name of the menu selection. If removable is true, the menu can be removed and allowed to float free. Otherwise, it will remain attached to the menu bar. (Removable menus are implementation-dependent.) The first form creates an empty menu. Individual menu items are of type MenuItem. It defines these constructors: MenuItem( ) throws HeadlessException MenuItem(String itemName) throws HeadlessException MenuItem(String itemName, MenuShortcut keyAccel) throws HeadlessException Chapter 25 Using AWT Controls, Layout Managers, and Menus 811 Here, itemName is the name shown in the menu, and keyAccel is the menu shortcut for this item. You can disable or enable a menu item by using the setEnabled( ) method. Its form is shown here: void setEnabled(boolean enabledFlag) boolean isEnabled( ) isEnabled( ) returns true if the menu item on which it is called is enabled. Otherwise, it returns false. You can change the name of a menu item by calling setLabel( ). You can retrieve the current name by using getLabel( ). These methods are as follows: void setLabel(String newName) String getLabel( ) Here, newName becomes the new name of the invoking menu item. getLabel( ) returns the current name. You can create a checkable menu item by using a subclass of MenuItem called CheckboxMenuItem. It has these constructors: CheckboxMenuItem( ) throws HeadlessException CheckboxMenuItem(String itemName) throws HeadlessException CheckboxMenuItem(String itemName, boolean on) throws HeadlessException Here, itemName is the name shown in the menu. Checkable items operate as toggles. Each time one is selected, its state changes. In the first two forms, the checkable entry is unchecked. In the third form, if on is true, the checkable entry is initially checked. Otherwise, it is cleared. You can obtain the status of a checkable item by calling getState( ). You can set it to a known state by using setState( ). These methods are shown here: boolean getState( ) void setState(boolean checked) If the item is checked, getState( ) returns true. Otherwise, it returns false. To check an item, pass true to setState( ). To clear an item, pass false. Once you have created a menu item, you must add the item to a Menu object by using add( ), which has the following general form: MenuItem add(MenuItem item) Here, item is the item being added. Items are added to a menu in the order in which the calls to add( ) take place. The item is returned. Once you have added all items to a Menu object, you can add that object to the menu bar by using this version of add( ) defined by MenuBar: Menu add(Menu menu) Here, menu is the menu being added. The menu is returned. Part II If the argument enabledFlag is true, the menu item is enabled. If false, the menu item is disabled. You can determine an item’s status by calling isEnabled( ). This method is shown here: 812 PART II The Java Library Menus generate events only when an item of type MenuItem or CheckboxMenuItem is selected. They do not generate events when a menu bar is accessed to display a drop-down menu, for example. Each time a menu item is selected, an ActionEvent object is generated. By default, the action command string is the name of the menu item. However, you can specify a different action command string by calling setActionCommand( ) on the menu item. Each time a check box menu item is checked or unchecked, an ItemEvent object is generated. Thus, you must implement the ActionListener and/or ItemListener interfaces in order to handle these menu events. The getItem( ) method of ItemEvent returns a reference to the item that generated this event. The general form of this method is shown here: Object getItem( ) Following is an example that adds a series of nested menus to a pop-up window. The item selected is displayed in the window. The state of the two check box menu items is also displayed. // Illustrate menus. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ // Create a subclass of Frame. class MenuFrame extends Frame { String msg = ""; CheckboxMenuItem debug, test; MenuFrame(String title) { super(title); // create menu bar and add it to frame MenuBar mbar = new MenuBar(); setMenuBar(mbar); // create the menu items Menu file = new Menu("File"); MenuItem item1, item2, item3, item4, item5; file.add(item1 = new MenuItem("New...")); file.add(item2 = new MenuItem("Open...")); file.add(item3 = new MenuItem("Close")); file.add(item4 = new MenuItem("-")); file.add(item5 = new MenuItem("Quit...")); mbar.add(file); Menu edit = new Menu("Edit"); MenuItem item6, item7, item8, item9; edit.add(item6 = new MenuItem("Cut")); edit.add(item7 = new MenuItem("Copy")); Chapter 25 Using AWT Controls, Layout Managers, and Menus 813 edit.add(item8 = new MenuItem("Paste")); edit.add(item9 = new MenuItem("-")); // these are checkable menu items debug = new CheckboxMenuItem("Debug"); edit.add(debug); test = new CheckboxMenuItem("Testing"); edit.add(test); mbar.add(edit); // create an object to handle action and item events MyMenuHandler handler = new MyMenuHandler(this); // register it to receive those events item1.addActionListener(handler); item2.addActionListener(handler); item3.addActionListener(handler); item4.addActionListener(handler); item5.addActionListener(handler); item6.addActionListener(handler); item7.addActionListener(handler); item8.addActionListener(handler); item9.addActionListener(handler); item10.addActionListener(handler); item11.addActionListener(handler); item12.addActionListener(handler); debug.addItemListener(handler); test.addItemListener(handler); // create an object to handle window events MyWindowAdapter adapter = new MyWindowAdapter(this); // register it to receive those events addWindowListener(adapter); } public void paint(Graphics g) { g.drawString(msg, 10, 200); if(debug.getState()) g.drawString("Debug is on.", 10, 220); else g.drawString("Debug is off.", 10, 220); if(test.getState()) g.drawString("Testing is on.", 10, 240); else Part II Menu sub = new Menu("Special"); MenuItem item10, item11, item12; sub.add(item10 = new MenuItem("First")); sub.add(item11 = new MenuItem("Second")); sub.add(item12 = new MenuItem("Third")); edit.add(sub); 814 PART II The Java Library g.drawString("Testing is off.", 10, 240); } } class MyWindowAdapter extends WindowAdapter { MenuFrame menuFrame; public MyWindowAdapter(MenuFrame menuFrame) { this.menuFrame = menuFrame; } public void windowClosing(WindowEvent we) { menuFrame.setVisible(false); } } class MyMenuHandler implements ActionListener, ItemListener { MenuFrame menuFrame; public MyMenuHandler(MenuFrame menuFrame) { this.menuFrame = menuFrame; } // Handle action events. public void actionPerformed(ActionEvent ae) { String msg = "You selected "; String arg = ae.getActionCommand(); if(arg.equals("New...")) msg += "New."; else if(arg.equals("Open...")) msg += "Open."; else if(arg.equals("Close")) msg += "Close."; else if(arg.equals("Quit...")) msg += "Quit."; else if(arg.equals("Edit")) msg += "Edit."; else if(arg.equals("Cut")) msg += "Cut."; else if(arg.equals("Copy")) msg += "Copy."; else if(arg.equals("Paste")) msg += "Paste."; else if(arg.equals("First")) msg += "First."; else if(arg.equals("Second")) msg += "Second."; else if(arg.equals("Third")) msg += "Third."; else if(arg.equals("Debug")) msg += "Debug."; else if(arg.equals("Testing")) msg += "Testing."; Chapter 25 Using AWT Controls, Layout Managers, and Menus 815 menuFrame.msg = msg; menuFrame.repaint(); } // Handle item events. public void itemStateChanged(ItemEvent ie) { menuFrame.repaint(); } // Create frame window. public class MenuDemo extends Applet { Frame f; public void init() { f = new MenuFrame("Menu Demo"); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); setSize(new Dimension(width, height)); f.setSize(width, height); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } } Sample output from the MenuDemo applet is shown in Figure 25-8. Figure 25-8 Sample output from the MenuDemo applet Part II } 816 PART II The Java Library There is one other menu-related class that you might find interesting: PopupMenu. It works just like Menu, but produces a menu that can be displayed at a specific location. PopupMenu provides a flexible, useful alternative for some types of menuing situations. Dialog Boxes Often, you will want to use a dialog box to hold a set of related controls. Dialog boxes are primarily used to obtain user input and are often child windows of a top-level window. Dialog boxes don’t have menu bars, but in other respects, they function like frame windows. (You can add controls to them, for example, in the same way that you add controls to a frame window.) Dialog boxes may be modal or modeless. When a modal dialog box is active, all input is directed to it until it is closed. This means that you cannot access other parts of your program until you have closed the dialog box. When a modeless dialog box is active, input focus can be directed to another window in your program. Thus, other parts of your program remain active and accessible. Dialog boxes are of type Dialog. Two commonly used constructors are shown here: Dialog(Frame parentWindow, boolean mode) Dialog(Frame parentWindow, String title, boolean mode) Here, parentWindow is the owner of the dialog box. If mode is true, the dialog box is modal. Otherwise, it is modeless. The title of the dialog box can be passed in title. Generally, you will subclass Dialog, adding the functionality required by your application. Following is a modified version of the preceding menu program that displays a modeless dialog box when the New option is chosen. Notice that when the dialog box is closed, dispose( ) is called. This method is defined by Window, and it frees all system resources associated with the dialog box window. // Demonstrate Dialog box. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ // Create a subclass of Dialog. class SampleDialog extends Dialog implements ActionListener { SampleDialog(Frame parent, String title) { super(parent, title, false); setLayout(new FlowLayout()); setSize(300, 200); add(new Label("Press this button:")); Button b; add(b = new Button("Cancel")); b.addActionListener(this); } Chapter 25 Using AWT Controls, Layout Managers, and Menus 817 public void actionPerformed(ActionEvent ae) { dispose(); } public void paint(Graphics g) { g.drawString("This is in the dialog box", 10, 70); } // Create a subclass of Frame. class MenuFrame extends Frame { String msg = ""; CheckboxMenuItem debug, test; MenuFrame(String title) { super(title); // create menu bar and add it to frame MenuBar mbar = new MenuBar(); setMenuBar(mbar); // create the menu items Menu file = new Menu("File"); MenuItem item1, item2, item3, item4; file.add(item1 = new MenuItem("New...")); file.add(item2 = new MenuItem("Open...")); file.add(item3 = new MenuItem("Close")); file.add(new MenuItem("-")); file.add(item4 = new MenuItem("Quit...")); mbar.add(file); Menu edit = new Menu("Edit"); MenuItem item5, item6, item7; edit.add(item5 = new MenuItem("Cut")); edit.add(item6 = new MenuItem("Copy")); edit.add(item7 = new MenuItem("Paste")); edit.add(new MenuItem("-")); Menu sub = new Menu("Special", true); MenuItem item8, item9, item10; sub.add(item8 = new MenuItem("First")); sub.add(item9 = new MenuItem("Second")); sub.add(item10 = new MenuItem("Third")); edit.add(sub); // these are checkable menu items debug = new CheckboxMenuItem("Debug"); edit.add(debug); test = new CheckboxMenuItem("Testing"); edit.add(test); mbar.add(edit); Part II } 818 PART II The Java Library // create an object to handle action and item events MyMenuHandler handler = new MyMenuHandler(this); // register it to receive those events item1.addActionListener(handler); item2.addActionListener(handler); item3.addActionListener(handler); item4.addActionListener(handler); item5.addActionListener(handler); item6.addActionListener(handler); item7.addActionListener(handler); item8.addActionListener(handler); item9.addActionListener(handler); item10.addActionListener(handler); debug.addItemListener(handler); test.addItemListener(handler); // create an object to handle window events MyWindowAdapter adapter = new MyWindowAdapter(this); // register it to receive those events addWindowListener(adapter); } public void paint(Graphics g) { g.drawString(msg, 10, 200); if(debug.getState()) g.drawString("Debug is on.", 10, 220); else g.drawString("Debug is off.", 10, 220); if(test.getState()) g.drawString("Testing is on.", 10, 240); else g.drawString("Testing is off.", 10, 240); } } class MyWindowAdapter extends WindowAdapter { MenuFrame menuFrame; public MyWindowAdapter(MenuFrame menuFrame) { this.menuFrame = menuFrame; } public void windowClosing(WindowEvent we) { menuFrame.dispose(); } } class MyMenuHandler implements ActionListener, ItemListener { MenuFrame menuFrame; Chapter 25 Using AWT Controls, Layout Managers, and Menus 819 // Handle action events. public void actionPerformed(ActionEvent ae) { String msg = "You selected "; String arg = ae.getActionCommand(); // Activate a dialog box when New is selected. if(arg.equals("New...")) { msg += "New."; SampleDialog d = new SampleDialog(menuFrame, "New Dialog Box"); d.setVisible(true); } // Try defining other dialog boxes for these options. else if(arg.equals("Open...")) msg += "Open."; else if(arg.equals("Close")) msg += "Close."; else if(arg.equals("Quit...")) msg += "Quit."; else if(arg.equals("Edit")) msg += "Edit."; else if(arg.equals("Cut")) msg += "Cut."; else if(arg.equals("Copy")) msg += "Copy."; else if(arg.equals("Paste")) msg += "Paste."; else if(arg.equals("First")) msg += "First."; else if(arg.equals("Second")) msg += "Second."; else if(arg.equals("Third")) msg += "Third."; else if(arg.equals("Debug")) msg += "Debug."; else if(arg.equals("Testing")) msg += "Testing."; menuFrame.msg = msg; menuFrame.repaint(); } public void itemStateChanged(ItemEvent ie) { menuFrame.repaint(); } } // Create frame window. public class DialogDemo extends Applet { Frame f; Part II public MyMenuHandler(MenuFrame menuFrame) { this.menuFrame = menuFrame; } 820 PART II The Java Library public void init() { f = new MenuFrame("Menu Demo"); int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); setSize(width, height); f.setSize(width, height); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } } Here is sample output from the DialogDemo applet: TIP On your own, try defining dialog boxes for the other options presented by the menus. FileDialog Java provides a built-in dialog box that lets the user specify a file. To create a file dialog box, instantiate an object of type FileDialog. This causes a file dialog box to be displayed. Chapter 25 Using AWT Controls, Layout Managers, and Menus 821 Usually, this is the standard file dialog box provided by the operating system. Here are three FileDialog constructors: Here, parent is the owner of the dialog box. The boxName parameter specifies the name displayed in the box’s title bar. If boxName is omitted, the title of the dialog box is empty. If how is FileDialog.LOAD, then the box is selecting a file for reading. If how is FileDialog.SAVE, the box is selecting a file for writing. If how is omitted, the box is selecting a file for reading. FileDialog provides methods that allow you to determine the name of the file and its path as selected by the user. Here are two examples: String getDirectory( ) String getFile( ) These methods return the directory and the filename, respectively. The following program activates the standard file dialog box: /* Demonstrate File Dialog box. This is an application, not an applet. */ import java.awt.*; import java.awt.event.*; // Create a subclass of Frame. class SampleFrame extends Frame { SampleFrame(String title) { super(title); // remove the window when closed addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); } } // Demonstrate FileDialog. class FileDialogDemo { public static void main(String args[]) { // create a frame that owns the dialog Frame f = new SampleFrame("File Dialog Demo"); f.setVisible(true); f.setSize(100, 100); FileDialog fd = new FileDialog(f, "File Dialog"); fd.setVisible(true); } } Part II FileDialog(Frame parent) FileDialog(Frame parent, String boxName) FileDialog(Frame parent, String boxName, int how) 822 PART II The Java Library The output generated by this program is shown here. (The precise configuration of the dialog box may vary.) One last point: Beginning with JDK 7, you can use FileDialog to select a list of files. This functionality is supported by the setMultipleMode( ), isMultipleMode( ), and getFiles( ) methods. Handling Events by Extending AWT Components The delegation event model was introduced in Chapter 23, and all of the programs in this book so far have used that design. But Java also allows you to handle events by subclassing AWT components. Doing so allows you to handle events in much the same way as they were handled under the original 1.0 version of Java. Of course, this technique is discouraged, because it has the same disadvantages of the Java 1.0 event model, the main one being inefficiency. Handling events by extending AWT components is described in this section for completeness. However, this technique is not used in any other sections of this book. When extending an AWT component, you must call the enableEvents( ) method of Component. Its general form is shown here: protected final void enableEvents(long eventMask) The eventMask argument is a bit mask that defines the events to be delivered to this component. The AWTEvent class defines int constants for making this mask. Several are shown here: Chapter 25 Using AWT Controls, Layout Managers, and Menus ACTION_EVENT_MASK KEY_EVENT_MASK ADJUSTMENT_EVENT_MASK MOUSE_EVENT_MASK COMPONENT_EVENT_MASK MOUSE_MOTION_EVENT_MASK CONTAINER_EVENT_MASK MOUSE_WHEEL_EVENT_MASK FOCUS_EVENT_MASK TEXT_EVENT_MASK INPUT_METHOD_EVENT_MASK WINDOW_EVENT_MASK 823 You must also override the appropriate method from one of your superclasses in order to process the event. Be sure to also call the superclass version of the method. Table 25-2 lists several commonly used methods and the classes that provide them. The following sections provide simple programs that show how to extend several AWT components. Extending Button The following program creates an applet that displays a button labeled "Test Button". When the button is pressed, the string "action event: " is displayed on the status line of the applet viewer or browser, followed by a count of the number of button presses. The program has one top-level class named ButtonDemo2 that extends Applet. A static integer variable named i is defined and initialized to zero. This records the number of button pushes. The init( ) method instantiates MyButton and adds it to the applet. MyButton is an inner class that extends Button. Its constructor uses super to pass the label of the button to the superclass constructor. It calls enableEvents( ) so that action events may be received by this object. When an action event is generated, processActionEvent( ) is called. That method displays a string on the status line and calls processActionEvent( ) Class Processing Methods Button processActionEvent( ) Checkbox processItemEvent( ) CheckboxMenuItem processItemEvent( ) Choice processItemEvent( ) Component processComponentEvent( ), processFocusEvent( ), processKeyEvent( ), processMouseEvent( ), processMouseMotionEvent( ), processMouseWheelEvent( ) List processActionEvent( ), processItemEvent( ) MenuItem processActionEvent( ) Scrollbar processAdjustmentEvent( ) TextComponent processTextEvent( ) Table 25-2 Commonly Used Event Processing Methods Part II ITEM_EVENT_MASK 824 PART II The Java Library for the superclass. Because MyButton is an inner class, it has direct access to the showStatus( ) method of ButtonDemo2. /* * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class ButtonDemo2 extends Applet { MyButton myButton; static int i = 0; public void init() { myButton = new MyButton("Test Button"); add(myButton); } class MyButton extends Button { public MyButton(String label) { super(label); enableEvents(AWTEvent.ACTION_EVENT_MASK); } protected void processActionEvent(ActionEvent ae) { showStatus("action event: " + i++); super.processActionEvent(ae); } } } Extending Checkbox The following program creates an applet that displays three check boxes labeled "Item 1", "Item 2", and "Item 3". When a check box is selected or deselected, a string containing the name and state of that check box is displayed on the status line of the applet viewer or browser. The program has one top-level class named CheckboxDemo2 that extends Applet. Its init( ) method creates three instances of MyCheckbox and adds these to the applet. MyCheckbox is an inner class that extends Checkbox. Its constructor uses super to pass the label of the check box to the superclass constructor. It calls enableEvents( ) so that item events may be received by this object. When an item event is generated, processItemEvent( ) is called. That method displays a string on the status line and calls processItemEvent( ) for the superclass. /* * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; Chapter 25 Using AWT Controls, Layout Managers, and Menus 825 class MyCheckbox extends Checkbox { public MyCheckbox(String label) { super(label); enableEvents(AWTEvent.ITEM_EVENT_MASK); } protected void processItemEvent(ItemEvent ie) { showStatus("Checkbox name/state: " + getLabel() + "/" + getState()); super.processItemEvent(ie); } } } Extending a Check Box Group The following program reworks the preceding check box example so that the check boxes form a check box group. Thus, only one of the check boxes may be selected at any time. /* * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class CheckboxGroupDemo2 extends Applet { CheckboxGroup cbg; MyCheckbox myCheckbox1, myCheckbox2, myCheckbox3; public void init() { cbg = new CheckboxGroup(); myCheckbox1 = new MyCheckbox("Item 1", cbg, true); add(myCheckbox1); myCheckbox2 = new MyCheckbox("Item 2", cbg, false); add(myCheckbox2); myCheckbox3 = new MyCheckbox("Item 3", cbg, false); add(myCheckbox3); } class MyCheckbox extends Checkbox { public MyCheckbox(String label, CheckboxGroup cbg, boolean flag) { super(label, cbg, flag); enableEvents(AWTEvent.ITEM_EVENT_MASK); } Part II public class CheckboxDemo2 extends Applet { MyCheckbox myCheckbox1, myCheckbox2, myCheckbox3; public void init() { myCheckbox1 = new MyCheckbox("Item 1"); add(myCheckbox1); myCheckbox2 = new MyCheckbox("Item 2"); add(myCheckbox2); myCheckbox3 = new MyCheckbox("Item 3"); add(myCheckbox3); } 826 PART II The Java Library protected void processItemEvent(ItemEvent ie) { showStatus("Checkbox name/state: " + getLabel() + "/" + getState()); super.processItemEvent(ie); } } } Extending Choice The following program creates an applet that displays a choice list with items labeled "Red", "Green", and "Blue". When an entry is selected, a string that contains the name of the color is displayed on the status line of the applet viewer or browser. There is one top-level class named ChoiceDemo2 that extends Applet. Its init( ) method creates a choice element and adds it to the applet. MyChoice is an inner class that extends Choice. It calls enableEvents( ) so that item events may be received by this object. When an item event is generated, processItemEvent( ) is called. That method displays a string on the status line and calls processItemEvent( ) for the superclass. /* * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class ChoiceDemo2 extends Applet { MyChoice choice; public void init() { choice = new MyChoice(); choice.add("Red"); choice.add("Green"); choice.add("Blue"); add(choice); } class MyChoice extends Choice { public MyChoice() { enableEvents(AWTEvent.ITEM_EVENT_MASK); } protected void processItemEvent(ItemEvent ie) { showStatus("Choice selection: " + getSelectedItem()); super.processItemEvent(ie); } } } Extending List The following program modifies the preceding example so that it uses a list instead of a choice menu. There is one top-level class named ListDemo2 that extends Applet. Its init( ) method creates a list element and adds it to the applet. MyList is an inner class that extends List. It calls enableEvents( ) so that both action and item events may be received by this object. When an entry is selected or deselected, processItemEvent( ) is called. When an Chapter 25 Using AWT Controls, Layout Managers, and Menus 827 entry is double-clicked, processActionEvent( ) is also called. Both methods display a string and then hand control to the superclass. public class ListDemo2 extends Applet { MyList list; public void init() { list = new MyList(); list.add("Red"); list.add("Green"); list.add("Blue"); add(list); } class MyList extends List { public MyList() { enableEvents(AWTEvent.ITEM_EVENT_MASK | AWTEvent.ACTION_EVENT_MASK); } protected void processActionEvent(ActionEvent ae) { showStatus("Action event: " + ae.getActionCommand()); super.processActionEvent(ae); } protected void processItemEvent(ItemEvent ie) { showStatus("Item event: " + getSelectedItem()); super.processItemEvent(ie); } } } Extending Scrollbar The following program creates an applet that displays a scroll bar. When this control is manipulated, a string is displayed on the status line of the applet viewer or browser. That string includes the value represented by the scroll bar. There is one top-level class named ScrollbarDemo2 that extends Applet. Its init( ) method creates a scroll bar element and adds it to the applet. MyScrollbar is an inner class that extends Scrollbar. It calls enableEvents( ) so that adjustment events may be received by this object. When the scroll bar is manipulated, processAdjustmentEvent( ) is called. When an entry is selected, processAdjustmentEvent( ) is called. It displays a string and then hands control to the superclass. /* * * */ import java.awt.*; import java.awt.event.*; Part II /* * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; 828 PART II The Java Library import java.applet.*; public class ScrollbarDemo2 extends Applet { MyScrollbar myScrollbar; public void init() { myScrollbar = new MyScrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 100); myScrollbar.setPreferredSize(new Dimension(100, 20)); add(myScrollbar); } class MyScrollbar extends Scrollbar { public MyScrollbar(int style, int initial, int thumb, int min, int max) { super(style, initial, thumb, min, max); enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK); } protected void processAdjustmentEvent(AdjustmentEvent ae) { showStatus("Adjustment event: " + ae.getValue()); setValue(getValue()); super.processAdjustmentEvent(ae); } } } A Word About Overriding paint( ) Before concluding our examination of AWT controls, a short word about overriding paint( ) is in order. Although not relevant to the simple AWT examples shown in this book, when overriding paint( ), there are times when it is necessary to call the superclass implementation of paint( ). Therefore, for some programs, you will need to use this paint( ) skeleton: public void paint(Graphics g) { // code to repaint this window // Call superclass paint() super.paint(g); } In Java, there are two general types of components: heavyweight and lightweight. A heavyweight component has its own native window, which is called its peer. A lightweight component is implemented completely in Java code and uses the window provided by an ancestor. The AWT controls described and used in this chapter are all heavyweight. However, if a container holds any lightweight components (that is, has lightweight child components), your override of paint( ) for that container must call super.paint( ). By calling super.paint( ), you ensure that any lightweight child components, such as lightweight controls, get properly painted. If you are unsure of a child component’s type, you can call isLightweight( ), defined by Component, to find out. It returns true if the component is lightweight, and false otherwise. CHAPTER 26 Images This chapter examines the AWT’s Image class and the java.awt.image package. Together, they provide support for imaging (the display and manipulation of graphical images). An image is simply a rectangular graphical object. Images are a key component of web design. In fact, the inclusion of the tag in the Mosaic browser at NCSA (National Center for Supercomputer Applications) is what caused the Web to begin to grow explosively in 1993. This tag was used to include an image inline with the flow of hypertext. Java expands upon this basic concept, allowing images to be managed under program control. Because of its importance, Java provides extensive support for imaging. Images are objects of the Image class, which is part of the java.awt package. Images are manipulated using the classes found in the java.awt.image package. There are a large number of imaging classes and interfaces defined by java.awt.image, and it is not possible to examine them all. Instead, we will focus on those that form the foundation of imaging. Here are the java.awt.image classes discussed in this chapter: CropImageFilter MemoryImageSource FilteredImageSource PixelGrabber ImageFilter RGBImageFilter These are the interfaces that we will use: ImageConsumer ImageObserver ImageProducer Also examined is the MediaTracker class, which is part of java.awt. File Formats Originally, web images could only be in GIF format. The GIF image format was created by CompuServe in 1987 to make it possible for images to be viewed while online, so it was well suited to the Internet. GIF images can have only up to 256 colors each. This limitation 829 830 PART II The Java Library caused the major browser vendors to add support for JPEG images in 1995. The JPEG format was created by a group of photographic experts to store full-color-spectrum, continuous-tone images. These images, when properly created, can be of much higher fidelity as well as more highly compressed than a GIF encoding of the same source image. Another file format is PNG. It too is an alternative to GIF. In almost all cases, you will never care or notice which format is being used in your programs. The Java image classes abstract the differences behind a clean interface. Image Fundamentals: Creating, Loading, and Displaying There are three common operations that occur when you work with images: creating an image, loading an image, and displaying an image. In Java, the Image class is used to refer to images in memory and to images that must be loaded from external sources. Thus, Java provides ways for you to create a new image object and ways to load one. It also provides a means by which an image can be displayed. Let’s look at each. Creating an Image Object You might expect that you create a memory image using something like the following: Image test = new Image(200, 100); // Error -- won’t work Not so. Because images must eventually be painted on a window to be seen, the Image class doesn’t have enough information about its environment to create the proper data format for the screen. Therefore, the Component class in java.awt has a factory method called createImage( ) that is used to create Image objects. (Remember that all of the AWT components are subclasses of Component, so all support this method.) The createImage( ) method has the following two forms: Image createImage(ImageProducer imgProd) Image createImage(int width, int height) The first form returns an image produced by imgProd, which is an object of a class that implements the ImageProducer interface. (We will look at image producers later.) The second form returns a blank (that is, empty) image that has the specified width and height. Here is an example: Canvas c = new Canvas(); Image test = c.createImage(200, 100); This creates an instance of Canvas and then calls the createImage( ) method to actually make an Image object. At this point, the image is blank. Later you will see how to write data to it. Loading an Image The other way to obtain an image is to load one. One way to do this is to use the getImage( ) method defined by the Applet class. It has the following forms: Chapter 26: Images 831 Image getImage(URL url) Image getImage(URL url, String imageName) The first version returns an Image object that encapsulates the image found at the location specified by url. The second version returns an Image object that encapsulates the image found at the location specified by url and having the name specified by imageName. Once you have an image, you can display it by using drawImage( ), which is a member of the Graphics class. It has several forms. The one we will be using is shown here: boolean drawImage(Image imgObj, int left, int top, ImageObserver imgOb) This displays the image passed in imgObj with its upper-left corner specified by left and top. imgOb is a reference to a class that implements the ImageObserver interface. This interface is implemented by all AWT (and Swing) components. An image observer is an object that can monitor an image while it loads. ImageObserver is described in the next section. With getImage( ) and drawImage( ), it is actually quite easy to load and display an image. Here is a sample applet that loads and displays a single image. The file seattle.jpg is loaded, but you can substitute any GIF, JPG, or PNG file you like (just make sure it is available in the same directory with the HTML file that contains the applet). /* * * * */ import java.awt.*; import java.applet.*; public class SimpleImageLoad extends Applet { Image img; public void init() { img = getImage(getDocumentBase(), getParameter("img")); } public void paint(Graphics g) { g.drawImage(img, 0, 0, this); } } In the init( ) method, the img variable is assigned to the image returned by getImage( ). The getImage( ) method uses the string returned by getParameter("img") as the filename for the image. This image is loaded from a URL that is relative to the result of getDocumentBase( ), which is the URL of the HTML page this applet tag was in. The filename returned by getParameter("img") comes from the applet tag . This is the equivalent, if a little slower, of using the HTML tag . Figure 26-1 shows what it looks like when you run the program. Part II Displaying an Image 832 PART II The Java Library Figure 26-1 Sample output from SimpleImageLoad When this applet runs, it starts loading img in the init( ) method. Onscreen you can see the image as it loads from the network, because Applet’s implementation of the ImageObserver interface calls paint( ) every time more image data arrives. Seeing the image load is somewhat informative, but it might be better if you use the time it takes to load the image to do other things in parallel. That way, the fully formed image can simply appear on the screen in an instant, once it is fully loaded. You can use ImageObserver, described next, to monitor loading an image while you paint the screen with other information. ImageObserver ImageObserver is an interface used to receive notification as an image is being generated, and it defines only one method: imageUpdate( ). Using an image observer allows you to perform other actions, such as show a progress indicator or an attract screen, as you are informed of the progress of the download. This kind of notification is very useful when an image is being loaded over a slow network. The imageUpdate( ) method has this general form: boolean imageUpdate(Image imgObj, int flags, int left, int top, int width, int height) Here, imgObj is the image being loaded, and flags is an integer that communicates the status of the update report. The four integers left, top, width, and height represent a rectangle that contains different values depending on the values passed in flags. imageUpdate( ) should return false if it has completed loading, and true if there is more image to process. The flags parameter contains one or more bit flags defined as static variables inside the ImageObserver interface. These flags and the information they provide are listed in Table 26-1. The Applet class has an implementation of the imageUpdate( ) method for the ImageObserver interface that is used to repaint images as they are loaded. You can override this method in your class to change that behavior. Images Flag Meaning WIDTH The width parameter is valid and contains the width of the image. HEIGHT The height parameter is valid and contains the height of the image. PROPERTIES The properties associated with the image can now be obtained using imgObj.getProperty( ). SOMEBITS More pixels needed to draw the image have been received. The parameters left, top, width, and height define the rectangle containing the new pixels. FRAMEBITS A complete frame that is part of a multiframe image, which was previously drawn, has been received. This frame can be displayed. The left, top, width, and height parameters are not used. ALLBITS The image is now complete. The left, top, width, and height parameters are not used. ERROR An error has occurred to an image that was being tracked asynchronously. The image is incomplete and cannot be displayed. No further image information will be received. The ABORT flag will also be set to indicate that the image production was aborted. ABORT An image that was being tracked asynchronously was aborted before it was complete. However, if an error has not occurred, accessing any part of the image’s data will restart the production of the image. 833 Table 26-1 Bit Flags of the imageUpdate( ) flags Parameter Here is a simple example of an imageUpdate( ) method: public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) { if ((flags & ALLBITS) == 0) { System.out.println("Still processing the image."); return true; } else { System.out.println("Done processing the image."); return false; } } Double Buffering Not only are images useful for storing pictures, as we’ve just shown, but you can also use them as offscreen drawing surfaces. This allows you to render any image, including text and graphics, to an offscreen buffer that you can display at a later time. The advantage to doing this is that the image is seen only when it is complete. Drawing a complicated image could take several milliseconds or more, which can be seen by the user as flashing or flickering. This flashing is distracting and causes the user to perceive your rendering as slower than it actually is. Use of an offscreen image to reduce flicker is called double buffering, because the screen is considered a buffer for pixels, and the offscreen image is the second buffer, where you can prepare pixels for display. Part II Chapter 26: 834 PART II The Java Library Earlier in this chapter, you saw how to create a blank Image object. Now you will see how to draw on that image rather than the screen. As you recall from earlier chapters, you need a Graphics object in order to use any of Java’s rendering methods. Conveniently, the Graphics object that you can use to draw on an Image is available via the getGraphics( ) method. Here is a code fragment that creates a new image, obtains its graphics context, and fills the entire image with red pixels: Canvas c = new Canvas(); Image test = c.createImage(200, 100); Graphics gc = test.getGraphics(); gc.setColor(Color.red); gc.fillRect(0, 0, 200, 100); Once you have constructed and filled an offscreen image, it will still not be visible. To actually display the image, call drawImage( ). Here is an example that draws a timeconsuming image, to demonstrate the difference that double buffering can make in perceived drawing time: /* * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class DoubleBuffer extends Applet { int gap = 3; int mx, my; boolean flicker = true; Image buffer = null; int w, h; public void init() { Dimension d = getSize(); w = d.width; h = d.height; buffer = createImage(w, h); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent me) { mx = me.getX(); my = me.getY(); flicker = false; repaint(); } public void mouseMoved(MouseEvent me) { mx = me.getX(); my = me.getY(); flicker = true; repaint(); } }); } Chapter 26: Images 835 public void paint(Graphics g) { Graphics screengc = null; if (!flicker) { screengc = g; g = buffer.getGraphics(); } g.setColor(Color.red); for (int i=0; i * * */ import java.util.*; import java.applet.*; import java.awt.*; public class TrackedImageLoad extends Applet implements Runnable { MediaTracker tracker; int tracked; int frame_rate = 5; int current_img = 0; Thread motor; static final int MAXIMAGES = 10; Image img[] = new Image[MAXIMAGES]; String name[] = new String[MAXIMAGES]; volatile boolean stopFlag; public void init() { tracker = new MediaTracker(this); StringTokenizer st = new StringTokenizer(getParameter("img"), "+"); while(st.hasMoreTokens() && tracked <= MAXIMAGES) { name[tracked] = st.nextToken(); img[tracked] = getImage(getDocumentBase(), name[tracked] + ".jpg"); tracker.addImage(img[tracked], tracked); tracked++; } } public void paint(Graphics g) { String loaded = ""; int donecount = 0; for(int i=0; i= tracked) current_img = 0; } else { int x = w * donecount / tracked; g.setColor(Color.black); g.fillRect(0, h/3, x, 16); g.setColor(Color.white); g.fillRect(x, h/3, w-x, 16); g.setColor(Color.black); g.drawString(loaded, 10, h/2); } } public void start() { motor = new Thread(this); stopFlag = false; motor.start(); } public void stop() { stopFlag = true; } public void run() { motor.setPriority(Thread.MIN_PRIORITY); while (true) { repaint(); try { Thread.sleep(1000/frame_rate); } catch (InterruptedException e) { System.out.println("Interrupted"); return; } if(stopFlag) return; } } } This example creates a new MediaTracker in the init( ) method and then adds each of the named images as a tracked image with addImage( ). In the paint( ) method, it calls checkID( ) on each of the images that we’re tracking. If all of the images are loaded, they are displayed. If not, a simple bar chart of the number of images loaded is shown, with the names of the fully loaded images displayed underneath the bar. Figure 26-3 shows two Images 839 Part II Chapter 26: Figure 26-3 Sample output from TrackedImageLoad scenes from this applet running. One is the bar chart, displaying that three of the images have been loaded. The other is the Van Gogh self-portrait during the slide show. ImageProducer ImageProducer is an interface for objects that want to produce data for images. An object that implements the ImageProducer interface will supply integer or byte arrays that represent image data and produce Image objects. As you saw earlier, one form of the createImage( ) method takes an ImageProducer object as its argument. There are two image producers contained in java.awt.image: MemoryImageSource and FilteredImageSource. Here, we will examine MemoryImageSource and create a new Image object from data generated in an applet. MemoryImageSource MemoryImageSource is a class that creates a new Image from an array of data. It defines several constructors. Here is the one we will be using: MemoryImageSource(int width, int height, int pixel[ ], int offset, int scanLineWidth) 840 PART II The Java Library The MemoryImageSource object is constructed out of the array of integers specified by pixel, in the default RGB color model to produce data for an Image object. In the default color model, a pixel is an integer with Alpha, Red, Green, and Blue (0xAARRGGBB). The Alpha value represents a degree of transparency for the pixel. Fully transparent is 0 and fully opaque is 255. The width and height of the resulting image are passed in width and height. The starting point in the pixel array to begin reading data is passed in offset. The width of a scan line (which is often the same as the width of the image) is passed in scanLineWidth. The following short example generates a MemoryImageSource object using a variation on a simple algorithm (a bitwise-exclusive-OR of the x and y address of each pixel) from the book Beyond Photography, The Digital Darkroom by Gerard J. Holzmann (Prentice Hall, 1988). /* * * */ import java.applet.*; import java.awt.*; import java.awt.image.*; public class MemoryImageGenerator extends Applet { Image img; public void init() { Dimension d = getSize(); int w = d.width; int h = d.height; int pixels[] = new int[w * h]; int i = 0; for(int y=0; y * * */ import java.applet.*; import java.awt.* ; import java.awt.image.* ; public class HistoGrab extends Applet { Dimension d; Image img; int iw, ih; int pixels[]; int w, h; int hist[] = new int[256]; int max_hist = 0; public void init() { d = getSize(); w = d.width; h = d.height; try { img = getImage(getDocumentBase(), getParameter("img")); MediaTracker t = new MediaTracker(this); t.addImage(img, 0); t.waitForID(0); iw = img.getWidth(null); ih = img.getHeight(null); pixels = new int[iw * ih]; PixelGrabber pg = new PixelGrabber(img, 0, 0, iw, ih, pixels, 0, iw); pg.grabPixels(); } catch (InterruptedException e) { System.out.println("Interrupted"); return; } for (int i=0; i> int g = 0xff & (p >> int b = 0xff & (p); int y = (int) (.33 * hist[y]++; i++) { 16); 8); r + .56 * g + .11 * b); Chapter 26: Images 843 } for (int i=0; i<256; i++) { if (hist[i] > max_hist) max_hist = hist[i]; } } public void paint(Graphics g) { g.drawImage(img, 0, 0, null); int x = (w - 256) / 2; int lasty = h - h * hist[0] / max_hist; for (int i=0; i<256; i++, x++) { int y = h - h * hist[i] / max_hist; g.setColor(new Color(i, i, i)); g.fillRect(x, y, 1, h); g.setColor(Color.red); g.drawLine(x-1,lasty,x,y); lasty = y; } } } Figure 26-5 shows the image and histogram for a famous Vermeer painting. Figure 26-5 Sample output from HistoGrab Part II public void update() {} 844 PART II The Java Library ImageFilter Given the ImageProducer and ImageConsumer interface pair—and their concrete classes MemoryImageSource and PixelGrabber—you can create an arbitrary set of translation filters that takes a source of pixels, modifies them, and passes them on to an arbitrary consumer. This mechanism is analogous to the way concrete classes are created from the abstract I/O classes InputStream, OutputStream, Reader, and Writer (described in Chapter 19). This stream model for images is completed by the introduction of the ImageFilter class. Some subclasses of ImageFilter in the java.awt.image package are AreaAveragingScaleFilter, CropImageFilter, ReplicateScaleFilter, and RGBImageFilter. There is also an implementation of ImageProducer called FilteredImageSource, which takes an arbitrary ImageFilter and wraps it around an ImageProducer to filter the pixels it produces. An instance of FilteredImageSource can be used as an ImageProducer in calls to createImage( ), in much the same way that BufferedInputStreams can be passed off as InputStreams. In this chapter, we examine two filters: CropImageFilter and RGBImageFilter. CropImageFilter CropImageFilter filters an image source to extract a rectangular region. One situation in which this filter is valuable is where you want to use several small images from a single, larger source image. Loading twenty 2K images takes much longer than loading a single 40K image that has many frames of an animation tiled into it. If every subimage is the same size, then you can easily extract these images by using CropImageFilter to disassemble the block once your program starts. Here is an example that creates 16 images taken from a single image. The tiles are then scrambled by swapping a random pair from the 16 images 32 times. /* * * * */ import java.applet.*; import java.awt.*; import java.awt.image.*; public class TileImage extends Applet { Image img; Image cell[] = new Image[4*4]; int iw, ih; int tw, th; public void init() { try { img = getImage(getDocumentBase(), getParameter("img")); MediaTracker t = new MediaTracker(this); t.addImage(img, 0); t.waitForID(0); iw = img.getWidth(null); ih = img.getHeight(null); Images 845 tw = iw / 4; th = ih / 4; CropImageFilter f; FilteredImageSource fis; t = new MediaTracker(this); for (int y=0; y<4; y++) { for (int x=0; x<4; x++) { f = new CropImageFilter(tw*x, th*y, tw, th); fis = new FilteredImageSource(img.getSource(), f); int i = y*4+x; cell[i] = createImage(fis); t.addImage(cell[i], i); } } t.waitForAll(); for (int i=0; i<32; i++) { int si = (int)(Math.random() * 16); int di = (int)(Math.random() * 16); Image tmp = cell[si]; cell[si] = cell[di]; cell[di] = tmp; } } catch (InterruptedException e) { System.out.println("Interrupted"); } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { for (int y=0; y<4; y++) { for (int x=0; x<4; x++) { g.drawImage(cell[y*4+x], x * tw, y * th, null); } } } } Figure 26-6 shows a famous Picasso painting scrambled by the TileImage applet. RGBImageFilter The RGBImageFilter is used to convert one image to another, pixel by pixel, transforming the colors along the way. This filter could be used to brighten an image, to increase its contrast, or even to convert it to grayscale. To demonstrate RGBImageFilter, we have developed a somewhat complicated example that employs a dynamic plug-in strategy for image-processing filters. We’ve created an interface for generalized image filtering so that an applet can simply load these filters based on tags without having to know about all of the ImageFilters in advance. This example consists of the main applet class called ImageFilterDemo, the interface called Part II Chapter 26: 846 PART II The Java Library Figure 26-6 Sample output from TileImage PlugInFilter, and a utility class called LoadedImage, which encapsulates some of the MediaTracker methods we’ve been using in this chapter. Also included are three filters— Grayscale, Invert, and Contrast—which simply manipulate the color space of the source image using RGBImageFilters, and two more classes—Blur and Sharpen—which do more complicated "convolution" filters that change pixel data based on the pixels surrounding each pixel of source data. Blur and Sharpen are subclasses of an abstract helper class called Convolver. Let’s look at each part of our example. ImageFilterDemo.java The ImageFilterDemo class is the applet framework for our sample image filters. It employs a simple BorderLayout, with a Panel at the South position to hold the buttons that will represent each filter. A Label object occupies the North slot for informational messages about filter progress. The Center is where the image (which is encapsulated in the LoadedImage Canvas subclass, described later) is put. We parse the buttons/filters out of the filters tag, separating them with +’s using a StringTokenizer. The actionPerformed( ) method is interesting because it uses the label from a button as the name of a filter class that it tries to load with (PlugInFilter) Class.forName(a).newInstance( ). This method is robust and takes appropriate action if the button does not correspond to a proper class that implements PlugInFilter. Chapter 26: Images 847 public class ImageFilterDemo extends Applet implements ActionListener { Image img; PlugInFilter pif; Image fimg; Image curImg; LoadedImage lim; Label lab; Button reset; public void init() { setLayout(new BorderLayout()); Panel p = new Panel(); add(p, BorderLayout.SOUTH); reset = new Button("Reset"); reset.addActionListener(this); p.add(reset); StringTokenizer st = new StringTokenizer(getParameter("filters"), "+"); while(st.hasMoreTokens()) { Button b = new Button(st.nextToken()); b.addActionListener(this); p.add(b); } lab = new Label(""); add(lab, BorderLayout.NORTH); img = getImage(getDocumentBase(), getParameter("img")); lim = new LoadedImage(img); add(lim, BorderLayout.CENTER); } public void actionPerformed(ActionEvent ae) { String a = ""; try { a = ae.getActionCommand(); if (a.equals("Reset")) { lim.set(img); lab.setText("Normal"); } Part II /* * * * * */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; 848 PART II The Java Library else { pif = (PlugInFilter) Class.forName(a).newInstance(); fimg = pif.filter(this, img); lim.set(fimg); lab.setText("Filtered: " + a); } repaint(); } catch (ClassNotFoundException e) { lab.setText(a + " not found"); lim.set(img); repaint(); } catch (InstantiationException e) { lab.setText("couldn’t new " + a); } catch (IllegalAccessException e) { lab.setText("no access: " + a); } } } Figure 26-7 shows what the applet looks like when it is first loaded using the applet tag shown at the top of this source file. Figure 26-7 Sample normal output from ImageFilterDemo Chapter 26: Images 849 PlugInFilter.java PlugInFilter is a simple interface used to abstract image filtering. It has only one method, filter( ), which takes the applet and the source image and returns a new image that has been filtered in some way. interface PlugInFilter { java.awt.Image filter(java.applet.Applet a, java.awt.Image in); } LoadedImage is a convenient subclass of Canvas, which takes an image at construction time and synchronously loads it using MediaTracker. LoadedImage then behaves properly inside of LayoutManager control, because it overrides the getPreferredSize( ) and getMinimumSize( ) methods. Also, it has a method called set( ) that can be used to set a new Image to be displayed in this Canvas. That is how the filtered image is displayed after the plug-in is finished. import java.awt.*; public class LoadedImage extends Canvas { Image img; public LoadedImage(Image i) { set(i); } void set(Image i) { MediaTracker mt = new MediaTracker(this); mt.addImage(i, 0); try { mt.waitForAll(); } catch (InterruptedException e) { System.out.println("Interrupted"); return; } img = i; repaint(); } public void paint(Graphics g) { if (img == null) { g.drawString("no image", 10, 30); } else { g.drawImage(img, 0, 0, this); } } public Dimension getPreferredSize() { return new Dimension(img.getWidth(this), img.getHeight(this)); } Part II LoadedImage.java 850 PART II The Java Library public Dimension getMinimumSize() { return getPreferredSize(); } } Grayscale.java The Grayscale filter is a subclass of RGBImageFilter, which means that Grayscale can use itself as the ImageFilter parameter to FilteredImageSource’s constructor. Then all it needs to do is override filterRGB( ) to change the incoming color values. It takes the red, green, and blue values and computes the brightness of the pixel, using the NTSC (National Television Standards Committee) color-to-brightness conversion factor. It then simply returns a gray pixel that is the same brightness as the color source. import java.applet.*; import java.awt.*; import java.awt.image.*; class Grayscale extends RGBImageFilter implements PlugInFilter { public Image filter(Applet a, Image in) { return a.createImage(new FilteredImageSource(in.getSource(), this)); } public int filterRGB(int x, int y, int rgb) { int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; int k = (int) (.56 * g + .33 * r + .11 * b); return (0xff000000 | k << 16 | k << 8 | k); } } Invert.java The Invert filter is also quite simple. It takes apart the red, green, and blue channels and then inverts them by subtracting them from 255. These inverted values are packed back into a pixel value and returned. import java.applet.*; import java.awt.*; import java.awt.image.*; class Invert extends RGBImageFilter implements PlugInFilter { public Image filter(Applet a, Image in) { return a.createImage(new FilteredImageSource(in.getSource(), this)); } public int filterRGB(int x, int y, int rgb) { int r = 0xff - (rgb >> 16) & 0xff; int g = 0xff - (rgb >> 8) & 0xff; int b = 0xff - rgb & 0xff; return (0xff000000 | r << 16 | g << 8 | b); } } Images 851 Part II Chapter 26: Figure 26-8 Using the Invert filter with ImageFilterDemo Figure 26-8 shows the image after it has been run through the Invert filter. Contrast.java The Contrast filter is very similar to Grayscale, except its override of filterRGB( ) is slightly more complicated. The algorithm it uses for contrast enhancement takes the red, green, and blue values separately and boosts them by 1.2 times if they are already brighter than 128. If they are below 128, then they are divided by 1.2. The boosted values are properly clamped at 255 by the multclamp( ) method. import java.applet.*; import java.awt.*; import java.awt.image.*; public class Contrast extends RGBImageFilter implements PlugInFilter { public Image filter(Applet a, Image in) { return a.createImage(new FilteredImageSource(in.getSource(), this)); } private int multclamp(int in, double factor) { in = (int) (in * factor); return in > 255 ? 255 : in; } 852 PART II The Java Library double gain = 1.2; private int cont(int in) { return (in < 128) ? (int)(in/gain) : multclamp(in, gain); } public int filterRGB(int x, int y, int rgb) { int r = cont((rgb >> 16) & 0xff); int g = cont((rgb >> 8) & 0xff); int b = cont(rgb & 0xff); return (0xff000000 | r << 16 | g << 8 | b); } } Figure 26-9 shows the image after Contrast is pressed. Convolver.java The abstract class Convolver handles the basics of a convolution filter by implementing the ImageConsumer interface to move the source pixels into an array called imgpixels. It also creates a second array called newimgpixels for the filtered data. Convolution filters sample a small rectangle of pixels around each pixel in an image, called the convolution kernel. This area, 3 x 3 pixels in this demo, is used to decide how to change the center pixel in the area. NOTE The reason that the filter can’t modify the imgpixels array in place is that the next pixel on a scan line would try to use the original value for the previous pixel, which would have just been filtered away. Figure 26-9 Using the Contrast filter with ImageFilterDemo Chapter 26: Images 853 The two concrete subclasses, shown in the next section, simply implement the convolve( ) method, using imgpixels for source data and newimgpixels to store the result. import java.applet.*; import java.awt.*; import java.awt.image.*; abstract void convolve(); // filter goes here... public Image filter(Applet a, Image in) { imageReady = false; in.getSource().startProduction(this); waitForImage(); newimgpixels = new int[width*height]; try { convolve(); } catch (Exception e) { System.out.println("Convolver failed: " + e); e.printStackTrace(); } return a.createImage( new MemoryImageSource(width, height, newimgpixels, 0, width)); } synchronized void waitForImage() { try { while(!imageReady) wait(); } catch (Exception e) { System.out.println("Interrupted"); } } public void setProperties(java.util.Hashtable dummy) { } public void setColorModel(ColorModel dummy) { } public void setHints(int dummy) { } public synchronized void imageComplete(int dummy) { imageReady = true; notifyAll(); } public void setDimensions(int x, int y) { width = x; height = y; imgpixels = new int[x*y]; } Part II abstract class Convolver implements ImageConsumer, PlugInFilter { int width, height; int imgpixels[], newimgpixels[]; boolean imageReady = false; 854 PART II The Java Library public void setPixels(int x1, int y1, int w, int h, ColorModel model, byte pixels[], int off, int scansize) { int pix, x, y, x2, y2, sx, sy; x2 = x1+w; y2 = y1+h; sy = off; for(y=y1; y> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; rs += r; gs += g; bs += b; } } 856 PART II The Java Library Sharpen.java The Sharpen filter is also a subclass of Convolver and is (more or less) the inverse of Blur. It runs through every pixel in the source image array, imgpixels, and computes the average of the 3 x 3 box surrounding it, not counting the center. The corresponding output pixel in newimgpixels has the difference between the center pixel and the surrounding average added to it. This basically says that if a pixel is 30 brighter than its surroundings, make it another 30 brighter. If, however, it is 10 darker, then make it another 10 darker. This tends to accentuate edges while leaving smooth areas unchanged. public class Sharpen extends Convolver { private final int clamp(int c) { return (c > 255 ? 255 : (c < 0 ? 0 : c)); } public void convolve() { int r0=0, g0=0, b0=0; for(int y=1; y> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; if (j == 0 && k == 0) { r0 = r; g0 = g; b0 = b; } else { rs += r; gs += g; bs += b; } } } rs >>= 3; gs >>= 3; bs >>= 3; newimgpixels[y*width+x] = (0xff000000 | clamp(r0+r0-rs) << 16 | clamp(g0+g0-gs) << 8 | clamp(b0+b0-bs)); } } } } Images 857 Part II Chapter 26: Figure 26-11 Using the Sharpen filter with ImageFilterDemo Figure 26-11 shows the applet after Sharpen. Cell Animation Now that we have presented an overview of the image APIs, we can put together an interesting applet that will display a sequence of animation cells. The animation cells are taken from a single image that can arrange the cells in a grid specified via the rows and cols tags. Each cell in the image is snipped out in a way similar to that used in the TileImage example earlier. We obtain the sequence in which to display the cells from the sequence tag. This is a comma-separated list of cell numbers that is zero-based and proceeds across the grid from left to right, top to bottom. Once the applet has parsed the tags and loaded the source image, it cuts the image into a number of small subimages. Then, a thread is started that causes the images to be displayed according to the order described in sequence. The thread sleeps for enough time to maintain the framerate. Here is the source code: / Animation example. import java.applet.*; import java.awt.*; import java.awt.image.*; import java.util.*; public class Animation extends Applet implements Runnable { Image cell[]; 858 PART II The Java Library final int MAXSEQ = 64; int sequence[]; int nseq; int idx; int framerate; volatile boolean stopFlag; private int intDef(String s, int def) { int n = def; if (s != null) try { n = Integer.parseInt(s); } catch (NumberFormatException e) { System.out.println("Number Format Exception"); } return n; } public void init() { framerate = intDef(getParameter("framerate"), 5); int tilex = intDef(getParameter("cols"), 1); int tiley = intDef(getParameter("rows"), 1); cell = new Image[tilex*tiley]; StringTokenizer st = new StringTokenizer(getParameter("sequence"), ","); sequence = new int[MAXSEQ]; nseq = 0; while(st.hasMoreTokens() && nseq < MAXSEQ) { sequence[nseq] = intDef(st.nextToken(), 0); nseq++; } try { Image img = getImage(getDocumentBase(), getParameter("img")); MediaTracker t = new MediaTracker(this); t.addImage(img, 0); t.waitForID(0); int iw = img.getWidth(null); int ih = img.getHeight(null); int tw = iw / tilex; int th = ih / tiley; CropImageFilter f; FilteredImageSource fis; for (int y=0; y Figure 26-12 shows the applet running. Notice the source image that has been loaded below the applet using a normal tag. Part II public void paint(Graphics g) { g.drawImage(cell[sequence[idx]], 0, 0, null); } 860 PART II The Java Library Figure 26-12 Sample output of Animation Additional Imaging Classes In addition to the imaging classes described in this chapter, java.awt.image supplies several others that offer enhanced control over the imaging process and that support advanced imaging techniques. Also available is the imaging package called javax.imageio. This package supports plug-ins that handle various image formats. If sophisticated graphical output is of special interest to you, then you will want to explore the additional classes found in java.awt.image and javax.imageio. CHAPTER 27 The Concurrency Utilities From the start, Java has provided built-in support for multithreading and synchronization. For example, new threads can be created by implementing Runnable or by extending Thread; synchronization is available by use of the synchronized keyword; and interthread communication is supported by the wait( ) and notify( ) methods that are defined by Object. In general, this built-in support for multithreading was one of Java’s most important innovations and is still one of its major strengths. However, as conceptually pure as Java’s original support for multithreading is, it is not ideal for all applications—especially those that make intensive use of multiple threads. For example, the original multithreading support does not provide several high-level features, such as semaphores, thread pools, and execution managers, that facilitate the creation of intensively concurrent programs. It is important to explain at the outset that many Java programs make use of multithreading and are, therefore, “concurrent.” For example, many applets and servlets use multithreading. However, as it is used in this chapter, the term concurrent program refers to a program that makes extensive, integral use of concurrently executing threads. An example of such a program is one that uses separate threads to simultaneously compute the partial results of a larger computation. Another example is a program that coordinates the activities of several threads, each of which seeks access to information in a database. In this case, readonly accesses might be handled differently from those that require read/write capabilities. To begin to handle the needs of a concurrent program, JDK 5 added the concurrency utilities, also commonly referred to as the concurrent API. The original set of concurrency utilities supplied many features that had long been wanted by programmers who develop concurrent applications. For example, it offered synchronizers (such as the semaphore), thread pools, execution managers, locks, several concurrent collections, and a streamlined way to use threads to obtain computational results. Although the original concurrent API was impressive in its own right, it was significantly expanded by JDK 7. The most important addition is the Fork/Join Framework. The Fork/Join Framework facilitates the creation of programs that make use of multiple processors (such as those found in multicore systems). Thus, it streamlines the development of programs in 861 862 PART II The Java Library which two or more pieces execute with true simultaneity (that is, true parallel execution), not just time-slicing. As you can easily imagine, parallel execution can dramatically increase the speed of certain operations. Because multicore systems are becoming commonplace, the inclusion of the Fork/Join Framework is as timely as it is powerful. The original concurrent API was quite large, and the Fork/Join Framework increases its size substantially. As you might expect, many of the issues surrounding the concurrency utilities are quite complex. It is beyond the scope of this book to discuss all of its facets. The preceding notwithstanding, it is important for all programmers to have a general, working knowledge of the concurrent API. Even in programs that are not intensively parallel, features such as synchronizers, callable threads, and executors, are applicable to a wide variety of situations. Perhaps most importantly, because of the rise of multicore computers, solutions involving the Fork/Join Framework will become more common. For these reasons, this chapter presents an overview of the concurrency utilities and shows several examples of their use. It concludes with an in-depth examination of the Fork/Join Framework. The Concurrent API Packages The concurrency utilities are contained in the java.util.concurrent package and in its two subpackages: java.util.concurrent.atomic and java.util.concurrent.locks. A brief overview of their contents is given here. java.util.concurrent java.util.concurrent defines the core features that support alternatives to the built-in approaches to synchronization and interthread communication. It defines the following key features: • Synchronizers • Executors • Concurrent collections • The Fork/Join Framework Synchronizers offer high-level ways of synchronizing the interactions between multiple threads. The synchronizer classes defined by java.util.concurrent are Semaphore Implements the classic semaphore. CountDownLatch Waits until a specified number of events have occurred. CyclicBarrier Enables a group of threads to wait at a predefined execution point. Exchanger Exchanges data between two threads. Phaser Synchronizes threads that advance through multiple phases of an operation. (Added by JDK 7.) Notice that each synchronizer provides a solution to a specific type of synchronization problem. This enables each synchronizer to be optimized for its intended use. In the past, these types of synchronization objects had to be crafted by hand. The concurrent API standardizes them and makes them available to all Java programmers. The Concurrency Utilities 863 Executors manage thread execution. At the top of the executor hierarchy is the Executor interface, which is used to initiate a thread. ExecutorService extends Executor and provides methods that manage execution. There are three implementations of ExecutorService: ThreadPoolExecutor, ScheduledThreadPoolExecutor, and ForkJoinPool (added by JDK 7). java.util.concurrent also defines the Executors utility class, which includes a number of static methods that simplify the creation of various executors. Related to executors are the Future and Callable interfaces. A Future contains a value that is returned by a thread after it executes. Thus, its value becomes defined “in the future,” when the thread terminates. Callable defines a thread that returns a value. java.util.concurrent defines several concurrent collection classes, including ConcurrentHashMap, ConcurrentLinkedQueue, and CopyOnWriteArrayList. These offer concurrent alternatives to their related classes defined by the Collections Framework. The Fork/Join Framework supports parallel programming. Its main classes are ForkJoinTask, ForkJoinPool, RecursiveTask, and RecursiveAction. As mentioned, the Fork/Join Framework was added by JDK 7. Finally, to better handle thread timing, java.util.concurrent defines the TimeUnit enumeration. java.util.concurrent.atomic java.util.concurrent.atomic facilitates the use of variables in a concurrent environment. It provides a means of efficiently updating the value of a variable without the use of locks. This is accomplished through the use of classes, such as AtomicInteger and AtomicLong, and methods, such as compareAndSet( ), decrementAndGet( ), and getAndSet( ). These methods execute as a single, non-interruptible operation. java.util.concurrent.locks java.util.concurrent.locks provides an alternative to the use of synchronized methods. At the core of this alternative is the Lock interface, which defines the basic mechanism used to acquire and relinquish access to an object. The key methods are lock( ), tryLock( ), and unlock( ). The advantage to using these methods is greater control over synchronization. The remainder of this chapter takes a closer look at the constituents of the concurrent API. Using Synchronization Objects Synchronization objects are supported by the Semaphore, CountDownLatch, CyclicBarrier, Exchanger, and Phaser classes. Collectively, they enable you to handle several formerly difficult synchronization situations with ease. They are also applicable to a wide range of programs—even those that contain only limited concurrency. Because the synchronization objects will be of interest to nearly all Java programs, each is examined here in some detail. Semaphore The synchronization object that many readers will immediately recognize is Semaphore, which implements a classic semaphore. A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to Part II Chapter 27 864 PART II The Java Library the shared resource. Thus, to access the resource, a thread must be granted a permit from the semaphore. In general, to use a semaphore, the thread that wants access to the shared resource tries to acquire a permit. If the semaphore’s count is greater than zero, then the thread acquires a permit, which causes the semaphore’s count to be decremented. Otherwise, the thread will be blocked until a permit can be acquired. When the thread no longer needs access to the shared resource, it releases the permit, which causes the semaphore’s count to be incremented. If there is another thread waiting for a permit, then that thread will acquire a permit at that time. Java’s Semaphore class implements this mechanism. Semaphore has the two constructors shown here: Semaphore(int num) Semaphore(int num, boolean how) Here, num specifies the initial permit count. Thus, num specifies the number of threads that can access a shared resource at any one time. If num is one, then only one thread can access the resource at any one time. By default, waiting threads are granted a permit in an undefined order. By setting how to true, you can ensure that waiting threads are granted a permit in the order in which they requested access. To acquire a permit, call the acquire( ) method, which has these two forms: void acquire( ) throws InterruptedException void acquire(int num) throws InterruptedException The first form acquires one permit. The second form acquires num permits. Most often, the first form is used. If the permit cannot be granted at the time of the call, then the invoking thread suspends until the permit is available. To release a permit, call release( ), which has these two forms: void release( ) void release(int num) The first form releases one permit. The second form releases the number of permits specified by num. To use a semaphore to control access to a resource, each thread that wants to use that resource must first call acquire( ) before accessing the resource. When the thread is done with the resource, it must call release( ). Here is an example that illustrates the use of a semaphore: // A simple semaphore example. import java.util.concurrent.*; class SemDemo { public static void main(String args[]) { Semaphore sem = new Semaphore(1); new IncThread(sem, "A"); new DecThread(sem, "B"); Chapter 27 The Concurrency Utilities 865 } } // A thread of execution that increments count. class IncThread implements Runnable { String name; Semaphore sem; IncThread(Semaphore s, String n) { sem = s; name = n; new Thread(this).start(); } public void run() { System.out.println("Starting " + name); try { // First, get a permit. System.out.println(name + " is waiting for a permit."); sem.acquire(); System.out.println(name + " gets a permit."); // Now, access shared resource. for(int i=0; i < 5; i++) { Shared.count++; System.out.println(name + ": " + Shared.count); // Now, allow a context switch -- if possible. Thread.sleep(10); } } catch (InterruptedException exc) { System.out.println(exc); } // Release the permit. System.out.println(name + " releases the permit."); sem.release(); } } // A thread of execution that decrements count. class DecThread implements Runnable { String name; Semaphore sem; Part II // A shared resource. class Shared { static int count = 0; } 866 PART II The Java Library DecThread(Semaphore s, String n) { sem = s; name = n; new Thread(this).start(); } public void run() { System.out.println("Starting " + name); try { // First, get a permit. System.out.println(name + " is waiting for a permit."); sem.acquire(); System.out.println(name + " gets a permit."); // Now, access shared resource. for(int i=0; i < 5; i++) { Shared.count--; System.out.println(name + ": " + Shared.count); // Now, allow a context switch -- if possible. Thread.sleep(10); } } catch (InterruptedException exc) { System.out.println(exc); } // Release the permit. System.out.println(name + " releases the permit."); sem.release(); } } The output from the program is shown here. (The precise order in which the threads execute may vary.) Starting A A is waiting for a permit. A gets a permit. A: 1 Starting B B is waiting for a permit. A: 2 A: 3 A: 4 A: 5 A releases the permit. B gets a permit. B: 4 B: 3 B: 2 B: 1 B: 0 B releases the permit. The Concurrency Utilities 867 The program uses a semaphore to control access to the count variable, which is a static variable within the Shared class. Shared.count is incremented five times by the run( ) method of IncThread and decremented five times by DecThread. To prevent these two threads from accessing Shared.count at the same time, access is allowed only after a permit is acquired from the controlling semaphore. After access is complete, the permit is released. In this way, only one thread at a time will access Shared.count, as the output shows. In both IncThread and DecThread, notice the call to sleep( ) within run( ). It is used to “prove” that accesses to Shared.count are synchronized by the semaphore. In run( ), the call to sleep( ) causes the invoking thread to pause between each access to Shared.count. This would normally enable the second thread to run. However, because of the semaphore, the second thread must wait until the first has released the permit, which happens only after all accesses by the first thread are complete. Thus, Shared.count is first incremented five times by IncThread and then decremented five times by DecThread. The increments and decrements are not intermixed. Without the use of the semaphore, accesses to Shared.count by both threads would have occurred simultaneously, and the increments and decrements would be intermixed. To confirm this, try commenting out the calls to acquire( ) and release( ). When you run the program, you will see that access to Shared.count is no longer synchronized, and each thread accesses it as soon as it gets a timeslice. Although many uses of a semaphore are as straightforward as that shown in the preceding program, more intriguing uses are also possible. Here is an example. The following program reworks the producer/consumer program shown in Chapter 11 so that it uses two semaphores to regulate the producer and consumer threads, ensuring that each call to put( ) is followed by a corresponding call to get( ): // An implementation of a producer and consumer // that use semaphores to control synchronization. import java.util.concurrent.Semaphore; class Q { int n; // Start with consumer semaphore unavailable. static Semaphore semCon = new Semaphore(0); static Semaphore semProd = new Semaphore(1); void get() { try { semCon.acquire(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Got: " + n); semProd.release(); } void put(int n) { try { semProd.acquire(); Part II Chapter 27 868 PART II The Java Library } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } this.n = n; System.out.println("Put: " + n); semCon.release(); } } class Producer implements Runnable { Q q; Producer(Q q) { this.q = q; new Thread(this, "Producer").start(); } public void run() { for(int i=0; i < 20; i++) q.put(i); } } class Consumer implements Runnable { Q q; Consumer(Q q) { this.q = q; new Thread(this, "Consumer").start(); } public void run() { for(int i=0; i < 20; i++) q.get(); } } class ProdCon { public static void main(String args[]) { Q q = new Q(); new Consumer(q); new Producer(q); } } A portion of the output is shown here: Put: Got: Put: Got: Put: Got: 0 0 1 1 2 2 Chapter 27 869 3 3 4 4 5 5 As you can see, the calls to put( ) and get( ) are synchronized. That is, each call to put( ) is followed by a call to get( ) and no values are missed. Without the semaphores, multiple calls to put( ) would have occurred without matching calls to get( ), resulting in values being missed. (To prove this, remove the semaphore code and observe the results.) The sequencing of put( ) and get( ) calls is handled by two semaphores: semProd and semCon. Before put( ) can produce a value, it must acquire a permit from semProd. After it has set the value, it releases semCon. Before get( ) can consume a value, it must acquire a permit from semCon. After it consumes the value, it releases semProd. This “give and take” mechanism ensures that each call to put( ) must be followed by a call to get( ). Notice that semCon is initialized with no available permits. This ensures that put( ) executes first. The ability to set the initial synchronization state is one of the more powerful aspects of a semaphore. CountDownLatch Sometimes you will want a thread to wait until one or more events have occurred. To handle such a situation, the concurrent API supplies CountDownLatch. A CountDownLatch is initially created with a count of the number of events that must occur before the latch is released. Each time an event happens, the count is decremented. When the count reaches zero, the latch opens. CountDownLatch has the following constructor: CountDownLatch(int num) Here, num specifies the number of events that must occur in order for the latch to open. To wait on the latch, a thread calls await( ), which has the forms shown here: void await( ) throws InterruptedException boolean await(long wait, TimeUnit tu) throws InterruptedException The first form waits until the count associated with the invoking CountDownLatch reaches zero. The second form waits only for the period of time specified by wait. The units represented by wait are specified by tu, which is an object the TimeUnit enumeration. (TimeUnit is described later in this chapter.) It returns false if the time limit is reached, and true if the countdown reaches zero To signal an event, call the countDown( ) method, shown next: void countDown( ) Each call to countDown( ) decrements the count associated with the invoking object. Part II Put: Got: Put: Got: Put: Got: . . . The Concurrency Utilities 870 PART II The Java Library The following program demonstrates CountDownLatch. It creates a latch that requires five events to occur before it opens. // An example of CountDownLatch. import java.util.concurrent.CountDownLatch; class CDLDemo { public static void main(String args[]) { CountDownLatch cdl = new CountDownLatch(5); System.out.println("Starting"); new MyThread(cdl); try { cdl.await(); } catch (InterruptedException exc) { System.out.println(exc); } System.out.println("Done"); } } class MyThread implements Runnable { CountDownLatch latch; MyThread(CountDownLatch c) { latch = c; new Thread(this).start(); } public void run() { for(int i = 0; i<5; i++) { System.out.println(i); latch.countDown(); // decrement count } } } The output produced by the program is shown here: Starting 0 1 2 3 4 Done Inside main( ), a CountDownLatch called cdl is created with an initial count of five. Next, an instance of MyThread is created, which begins execution of a new thread. Notice that cdl is passed as a parameter to MyThread’s constructor and stored in the latch instance Chapter 27 The Concurrency Utilities 871 variable. Then, the main thread calls await( ) on cdl, which causes execution of the main thread to pause until cdl’s count has been decremented five times. Inside the run( ) method of MyThread, a loop is created that iterates five times. With each iteration, the countDown( ) method is called on latch, which refers to cdl in main( ). After the fifth iteration, the latch opens, which allows the main thread to resume. CountDownLatch is a powerful yet easy-to-use synchronization object that is appropriate whenever a thread must wait for one or more events to occur. A situation not uncommon in concurrent programming occurs when a set of two or more threads must wait at a predetermined execution point until all threads in the set have reached that point. To handle such a situation, the concurrent API supplies the CyclicBarrier class. It enables you to define a synchronization object that suspends until the specified number of threads has reached the barrier point. CyclicBarrier has the following two constructors: CyclicBarrier(int numThreads) CyclicBarrier(int numThreads, Runnable action) Here, numThreads specifies the number of threads that must reach the barrier before execution continues. In the second form, action specifies a thread that will be executed when the barrier is reached. Here is the general procedure that you will follow to use CyclicBarrier. First, create a CyclicBarrier object, specifying the number of threads that you will be waiting for. Next, when each thread reaches the barrier, have it call await( ) on that object. This will pause execution of the thread until all of the other threads also call await( ). Once the specified number of threads has reached the barrier, await( ) will return and execution will resume. Also, if you have specified an action, then that thread is executed. The await( ) method has the following two forms: int await( ) throws InterruptedException, BrokenBarrierException int await(long wait, TimeUnit tu) throws InterruptedException, BrokenBarrierException, TimeoutException The first form waits until all the threads have reached the barrier point. The second form waits only for the period of time specified by wait. The units represented by wait are specified by tu. Both forms return a value that indicates the order that the threads arrive at the barrier point. The first thread returns a value equal to the number of threads waited upon minus one. The last thread returns zero. Here is an example that illustrates CyclicBarrier. It waits until a set of three threads has reached the barrier. When that occurs, the thread specified by BarAction executes. // An example of CyclicBarrier. import java.util.concurrent.*; class BarDemo { public static void main(String args[]) { CyclicBarrier cb = new CyclicBarrier(3, new BarAction() ); Part II CyclicBarrier 872 PART II The Java Library System.out.println("Starting"); new MyThread(cb, "A"); new MyThread(cb, "B"); new MyThread(cb, "C"); } } // A thread of execution that uses a CyclicBarrier. class MyThread implements Runnable { CyclicBarrier cbar; String name; MyThread(CyclicBarrier c, String n) { cbar = c; name = n; new Thread(this).start(); } public void run() { System.out.println(name); try { cbar.await(); } catch (BrokenBarrierException exc) { System.out.println(exc); } catch (InterruptedException exc) { System.out.println(exc); } } } // An object of this class is called when the // CyclicBarrier ends. class BarAction implements Runnable { public void run() { System.out.println("Barrier Reached!"); } } The output is shown here. (The precise order in which the threads execute may vary.) Starting A B C Barrier Reached! A CyclicBarrier can be reused because it will release waiting threads each time the specified number of threads calls await( ). For example, if you change main( ) in the preceding program so that it looks like this: Chapter 27 The Concurrency Utilities 873 public static void main(String args[]) { CyclicBarrier cb = new CyclicBarrier(3, new BarAction() ); System.out.println("Starting"); MyThread(cb, MyThread(cb, MyThread(cb, MyThread(cb, MyThread(cb, MyThread(cb, "A"); "B"); "C"); "X"); "Y"); "Z"); } The following output will be produced. (The precise order in which the threads execute may vary.) Starting A B C Barrier Reached! X Y Z Barrier Reached! As the preceding example shows, the CyclicBarrier offers a streamlined solution to what was previously a complicated problem. Exchanger Perhaps the most interesting of the synchronization classes is Exchanger. It is designed to simplify the exchange of data between two threads. The operation of an Exchanger is astoundingly simple: it simply waits until two separate threads call its exchange( ) method. When that occurs, it exchanges the data supplied by the threads. This mechanism is both elegant and easy to use. Uses for Exchanger are easy to imagine. For example, one thread might prepare a buffer for receiving information over a network connection. Another thread might fill that buffer with the information from the connection. The two threads work together so that each time a new buffer is needed, an exchange is made. Exchanger is a generic class that is declared as shown here: Exchanger Here, V specifies the type of the data being exchanged. The only method defined by Exchanger is exchange( ), which has the two forms shown here: V exchange(V buffer) throws InterruptedException V exchange(V buffer, long wait, TimeUnit tu) throws InterruptedException, TimeoutException Part II new new new new new new 874 PART II The Java Library Here, buffer is a reference to the data to exchange. The data received from the other thread is returned. The second form of exchange( ) allows a time-out period to be specified. The key point about exchange( ) is that it won’t succeed until it has been called on the same Exchanger object by two separate threads. Thus, exchange( ) synchronizes the exchange of the data. Here is an example that demonstrates Exchanger. It creates two threads. One thread creates an empty buffer that will receive the data put into it by the second thread. Thus, the first thread exchanges an empty thread for a full one. // An example of Exchanger. import java.util.concurrent.Exchanger; class ExgrDemo { public static void main(String args[]) { Exchanger exgr = new Exchanger(); new UseString(exgr); new MakeString(exgr); } } // A Thread that constructs a string. class MakeString implements Runnable { Exchanger ex; String str; MakeString(Exchanger c) { ex = c; str = new String(); new Thread(this).start(); } public void run() { char ch = 'A'; for(int i = 0; i < 3; i++) { // Fill Buffer for(int j = 0; j < 5; j++) str += ch++; try { // Exchange a full buffer for an empty one. str = ex.exchange(str); } catch(InterruptedException exc) { System.out.println(exc); } } } } Chapter 27 The Concurrency Utilities 875 // A Thread that uses a string. class UseString implements Runnable { Exchanger ex; String str; UseString(Exchanger c) { ex = c; new Thread(this).start(); } for(int i=0; i < 3; i++) { try { // Exchange an empty buffer for a full one. str = ex.exchange(new String()); System.out.println("Got: " + str); } catch(InterruptedException exc) { System.out.println(exc); } } } } Here is the output produced by the program: Got: ABCDE Got: FGHIJ Got: KLMNO In the program, the main( ) method creates an Exchanger for strings. This object is then used to synchronize the exchange of strings between the MakeString and UseString classes. The MakeString class fills a string with data. The UseString exchanges an empty buffer for a full one. It then displays the contents of the newly constructed string. The exchange of empty and full buffers is synchronized by the exchange( ) method, which is called by both class’ run( ) method. Phaser JDK 7 adds a new synchronization class called Phaser. Its primary purpose is to enable the synchronization of threads that represent one or more phases of activity. For example, you might have a set of threads that implement three phases of an order-processing application. In the first phase, separate threads are used to validate customer information, check inventory, and confirm pricing. When that phase is complete, the second phase has two threads that compute shipping costs and all applicable tax. After that, a final phase confirms payment and determines estimated shipping time. In the past, to synchronize the multiple threads that comprise this scenario would require a bit of work on your part. With the inclusion of Phaser, the process is now much easier. To begin, it helps to know that a Phaser works a bit like a CyclicBarrier, described earlier, except that it supports multiple phases. As a result, Phaser lets you define a synchronization object that waits until a specific phase has completed. It then advances Part II public void run() { 876 PART II The Java Library to the next phase, again waiting until that phase concludes. It is important to understand that Phaser can also be used to synchronize only a single phase. In this regard, it acts much like a CyclicBarrier. However, its primary use is to synchronize multiple phases. Phaser defines four constructors. Here are the two used in this section: Phaser( ) Phaser(int numParties) The first creates a phaser that has a registration count of zero. The second sets the registration count to numParties. The term party is often applied to the objects that register with a phaser. Although often there is a one-to-correspondence between the number of registrants and the number of threads being synchronized, this is not required. In both cases, the current phase is zero. That is, when a Phaser is created, it is initially at phase zero. In general, here is how you use Phaser. First, create a new instance of Phaser. Next, register one or more parties with the phaser, either by calling register( ) or by specifying the number of parties in the constructor. For each registered party, have the phaser wait until all registered parties complete a phase. A party signals this by calling one of a variety of methods supplied by Phaser, such as arrive( ) or arriveAndAwaitAdvance( ). After all parties have arrived, the phase is complete, and the phaser can move on to the next phase (if there is one), or terminate. The following sections explain the process in detail. To register parties after a Phaser has been constructed, call register( ). It is shown here: int register() It returns the phase number of the phase to which it is registered. To signal that a party has completed a phase, it must call arrive( ) or some variation of arrive( ). When the number of arrivals equals the number of registered parties, the phase is completed and the Phaser moves on the next phase (if there is one). The arrive( ) method has this general form: int arrive( ) This method signals that a party (normally a thread of execution) has completed some task (or portion of a task). It returns the current phase number. If the phaser has been terminated, then it returns a negative value. The arrive( ) method does not suspend execution of the calling thread. This means that it does not wait for the phase to be completed. This method should be called only by a registered party. If you want to indicate the completion of a phase and then wait until all other registrants have also completed that phase, use arriveAndAwaitAdvance( ). It is shown here: int arriveAndAwaitAdvance( ) It waits until all parties have arrived. It returns the next phase number or a negative value if the phaser has been terminated. This method should be called only by a registered party. A thread can arrive and then deregister itself by calling arriveAndDeregister( ). It is shown here: int arriveAndDeregister( ) It returns the current phase number or a negative value if the phaser has been terminated. It does not wait until the phase is complete. This method should be called only by a registered party. Chapter 27 The Concurrency Utilities 877 To obtain the current phase number, call getPhase( ), which is shown here: final int getPhase( ) When a Phaser is created, the first phase will be 0, the second phase 1, the third phase 2, and so on. A negative value is returned if the invoking Phaser has been terminated. Here is an example that shows Phaser in action. It creates three threads, each of which have three phases. It uses a Phaser to synchronize each phase. import java.util.concurrent.*; class PhaserDemo { public static void main(String args[]) { Phaser phsr = new Phaser(1); int curPhase; System.out.println("Starting"); new MyThread(phsr, "A"); new MyThread(phsr, "B"); new MyThread(phsr, "C"); // Wait for all threads to complete phase one. curPhase = phsr.getPhase(); phsr.arriveAndAwaitAdvance(); System.out.println("Phase " + curPhase + " Complete"); // Wait for all threads to complete phase two. curPhase = phsr.getPhase(); phsr.arriveAndAwaitAdvance(); System.out.println("Phase " + curPhase + " Complete"); curPhase = phsr.getPhase(); phsr.arriveAndAwaitAdvance(); System.out.println("Phase " + curPhase + " Complete"); // Deregister the main thread. phsr.arriveAndDeregister(); if(phsr.isTerminated()) System.out.println("The Phaser is terminated"); } } // A thread of execution that uses a Phaser. class MyThread implements Runnable { Phaser phsr; String name; MyThread(Phaser p, String n) { phsr = p; name = n; phsr.register(); Part II // An example of Phaser. 878 PART II The Java Library new Thread(this).start(); } public void run() { System.out.println("Thread " + name + " Beginning Phase One"); phsr.arriveAndAwaitAdvance(); // Signal arrival. // Pause a bit to prevent jumbled output. This is for illustration // only. It is not required for the proper operation of the phaser. try { Thread.sleep(10); } catch(InterruptedException e) { System.out.println(e); } System.out.println("Thread " + name + " Beginning Phase Two"); phsr.arriveAndAwaitAdvance(); // Signal arrival. // Pause a bit to prevent jumbled output. This is for illustration // only. It is not required for the proper operation of the phaser. try { Thread.sleep(10); } catch(InterruptedException e) { System.out.println(e); } System.out.println("Thread " + name + " Beginning Phase Three"); phsr.arriveAndDeregister(); // Signal arrival and deregister. } } The output is shown here: Starting Thread A Beginning Phase Thread C Beginning Phase Thread B Beginning Phase Phase 0 Complete Thread B Beginning Phase Thread C Beginning Phase Thread A Beginning Phase Phase 1 Complete Thread C Beginning Phase Thread B Beginning Phase Thread A Beginning Phase Phase 2 Complete The Phaser is terminated One One One Two Two Two Three Three Three Let’s look closely at the key sections of the program. First, in main( ), a Phaser called phsr is created with an initial party count of 1 (which corresponds to the main thread). Then three threads are started by creating three MyThread objects. Notice that MyThread is passed a reference to phsr (the phaser). The MyThread objects use this phaser to The Concurrency Utilities 879 synchronize their activities. Next, main( ) calls getPhase( ) to obtain the current phase number (which is initially zero) and then calls arriveAndAwaitAdvance( ). This causes main( ) to suspend until phase zero has completed. This won’t happen until all MyThreads also call arriveAndAwaitAdvance( ). When this occurs, main( ) will resume execution, at which point it displays that phase zero has completed, and it moves on to phase two. This process repeats until all three phases have finished. Then, main( ) calls arriveAndDeregister( ). At that point, all three MyThreads have also deregistered. Since this results in there being no registered parties when the phaser advances to the next phase, the phaser is terminated. Now look at MyThread. First, notice that the constructor is passed a reference to the phaser that it will use and then registers with the new thread as a party on that phaser. Thus, each new MyThread becomes a party registered with the passed-in phaser. Also notice that each thread has three phases. In this example, each phase consists of a placeholder that simply displays the name of the thread and what it is doing. Obviously, in real-world code, the thread would be performing more meaningful actions. Between the first two phases, the thread calls arriveAndAwaitAdvance( ). Thus, each thread waits until all threads have completed the phase (and the main thread is ready). After all threads have arrived (including the main thread), the phaser moves on to the next phase. After the third phase, each thread deregisters itself with a call to arriveAndDeregister( ). As the comments in MyThread explain, the calls to sleep( ) are used for the purposes of illustration to ensure that the output is not jumbled because of the multithreading. They are not needed to make the phaser work properly. If you remove them, the output may look a bit jumbled, but the phases will still be synchronized correctly. One other point: Although the preceding example used three threads that were all of the same type, this is not a requirement. Each party that uses a phaser can be unique, with each performing some separate task. It is possible to take control of precisely what happens when a phase advance occurs. To do this, you must override the onAdvance( ) method. This method is called by the run time when a Phaser advances from one phase to the next. It shown here: protected boolean onAdvance(int phase, int numParties) Here, phase will contain the current phase number prior to being incremented and numParties will contain the number of registered parties. To terminate the phaser, onAdvance( ) must return true. To keep the phaser alive, onAdvance( ) must return false. The default version of onAdvance( ) returns true (thus terminating the phaser) when there are no registered parties. As a general rule, your override should also follow this practice. One reason to override onAdvance( ) is to enable a phaser to execute a specific number of phases and then stop. The following example gives you the flavor of this usage. It creates a class called MyPhaser that extends Phaser so that it will run a specified number of phases. It does this by overriding the onAdvance( ) method. The MyPhaser constructor accepts one argument, which specifies the number of phases to execute. Notice that MyPhaser automatically registers one party. This behavior is useful in this example, but the needs of your own applications may differ. // Extend Phaser and override onAdvance() so that only a specific // number of phases are executed. import java.util.concurrent.*; Part II Chapter 27 880 PART II The Java Library // Extend MyPhaser to allow only a specific number of phases // to be executed. class MyPhaser extends Phaser { int numPhases; MyPhaser(int parties, int phaseCount) { super(parties); numPhases = phaseCount - 1; } // Override onAdvance() to execute the specified // number of phases. protected boolean onAdvance(int p, int regParties) { // This println() statement is for illustration only. // Normally, onAdvance() will not display output. System.out.println("Phase " + p + " completed.\n"); // If all phases have completed, return true if(p == numPhases || regParties == 0) return true; // Otherwise, return false. return false; } } class PhaserDemo2 { public static void main(String args[]) { MyPhaser phsr = new MyPhaser(1, 4); System.out.println("Starting\n"); new MyThread(phsr, "A"); new MyThread(phsr, "B"); new MyThread(phsr, "C"); // Wait for the specified number of phases to complete. while(!phsr.isTerminated()) { phsr.arriveAndAwaitAdvance(); } System.out.println("The Phaser is terminated"); } } // A thread of execution that uses a Phaser. class MyThread implements Runnable { Phaser phsr; String name; MyThread(Phaser p, String n) { phsr = p; name = n; phsr.register(); Chapter 27 The Concurrency Utilities 881 new Thread(this).start(); } public void run() { while(!phsr.isTerminated()) { System.out.println("Thread " + name + " Beginning Phase " + phsr.getPhase()); // Pause a bit to prevent jumbled output. This is for illustration // only. It is not required for the proper operation of the phaser. try { Thread.sleep(10); } catch(InterruptedException e) { System.out.println(e); } } } } The output from the program is shown here: Starting Thread B Beginning Phase 0 Thread A Beginning Phase 0 Thread C Beginning Phase 0 Phase 0 completed. Thread A Beginning Phase 1 Thread B Beginning Phase 1 Thread C Beginning Phase 1 Phase 1 completed. Thread C Beginning Phase 2 Thread B Beginning Phase 2 Thread A Beginning Phase 2 Phase 2 completed. Thread C Beginning Phase 3 Thread B Beginning Phase 3 Thread A Beginning Phase 3 Phase 3 completed. The Phaser is terminated Inside main( ), one instance of Phaser is created. It is passed 4 as an argument, which means that it will execute four phases and then stop. Next, three threads are created and then the following loop is entered: // Wait for the specified number of phases to complete. while(!phsr.isTerminated()) { phsr.arriveAndAwaitAdvance(); } Part II phsr.arriveAndAwaitAdvance(); 882 PART II The Java Library This loop simply calls arriveAndAwaitAdvance( ) until the phaser is terminated. The phaser won’t terminate until the specified number of phases have been executed. In this case, the loop continues to execute until four phases have run. Next, notice that the threads also call arriveAndAwaitAdvance( ) within a loop that runs until the phaser is terminated. This means that they will execute until the specified number of phases has been completed. Now, look closely at the code for onAdvance( ). Each time onAdvance( ) is called, it is passed the current phase and the number of registered parties. If the current phase equals the specified phase, or if the number of registered parties is zero, onAdvance( ) returns true, thus stopping the phaser. This is accomplished with this line of code: // If all phases have completed, return true if(p == numPhases || regParties == 0) return true; As you can see, very little code is needed to accommodate the desired outcome. Before moving on, it useful to point out that you don’t necessarily need to explicitly extend Phaser as the previous example does to simply override onAdvance( ). In some cases, more compact code can be created by using an anonymous inner class to override onAdvance( ). Phaser has additional capabilities that may be of use in your applications. You can wait for a specific phase by calling awaitAdvance( ), which is shown here: int awaitAdvance(int phase) Here, phase indicates the phase number on which awaitAdvance( ) will wait until a transition to the next phase takes place. It will return immediately if the argument passed to phase is not equal to the current phase. It will also return immediately if the phaser is terminated. However, if phase is passed the current phase, then it will wait until the phase increments. This method should be called only by a registered party. There is also an interruptible version of this method called awaitAdvanceInterruptibly( ). To register more than one party, call bulkRegister( ). To obtain the number of registered parties, call getRegisteredParties( ). You can also obtain the number of arrived parties and unarrived parties by calling getArrivedParties( ) and getUnarrivedParties( ), respectively. To force the phaser to enter a terminated state, call forceTermination( ). Phaser also lets you create a tree of phasers. This is supported by two additional constructors, which let you specify the parent, and the getParent( ) method. Using an Executor The concurrent API supplies a feature called an executor that initiates and controls the execution of threads. As such, an executor offers an alternative to managing threads through the Thread class. At the core of an executor is the Executor interface. It defines the following method: void execute(Runnable thread) The thread specified by thread is executed. Thus, execute( ) starts the specified thread. The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. For example, ExecutorService defines shutdown( ), shown here, which stops the invoking ExecutorService. void shutdown( ) The Concurrency Utilities 883 ExecutorService also defines methods that execute threads that return results, that execute a set of threads, and that determine the shutdown status. We will look at several of these methods a little later. Also defined is the interface ScheduledExecutorService, which extends ExecutorService to support the scheduling of threads. The concurrent API defines three predefined executor classes: ThreadPoolExecutor and ScheduledThreadPoolExecutor, and ForkJoinPool. ThreadPoolExecutor implements the Executor and ExecutorService interfaces and provides support for a managed pool of threads. ScheduledThreadPoolExecutor also implements the ScheduledExecutorService interface to allow a pool of threads to be scheduled. ForkJoinPool implements the Executor and ExecutorService interfaces and is used by the Fork/Join Framework. It is described later in this chapter. A thread pool provides a set of threads that is used to execute various tasks. Instead of each task using its own thread, the threads in the pool are used. This reduces the overhead associated with creating many separate threads. Although you can use ThreadPoolExecutor and ScheduledThreadPoolExecutor directly, most often you will want to obtain an executor by calling one of the following static factory methods defined by the Executors utility class. Here are some examples: static ExecutorService newCachedThreadPool( ) static ExecutorService newFixedThreadPool(int numThreads) static ScheduledExecutorService newScheduledThreadPool(int numThreads) newCachedThreadPool( ) creates a thread pool that adds threads as needed but reuses threads if possible. newFixedThreadPool( ) creates a thread pool that consists of a specified number of threads. newScheduledThreadPool( ) creates a thread pool that supports thread scheduling. Each returns a reference to an ExecutorService that can be used to manage the pool. A Simple Executor Example Before going any further, a simple example that uses an executor will be of value. The following program creates a fixed thread pool that contains two threads. It then uses that pool to execute four tasks. Thus, four tasks share the two threads that are in the pool. After the tasks finish, the pool is shut down and the program ends. // A simple example that uses an Executor. import java.util.concurrent.*; class SimpExec { public static void main(String args[]) { CountDownLatch cdl = new CountDownLatch(5); CountDownLatch cdl2 = new CountDownLatch(5); CountDownLatch cdl3 = new CountDownLatch(5); CountDownLatch cdl4 = new CountDownLatch(5); ExecutorService es = Executors.newFixedThreadPool(2); System.out.println("Starting"); // Start the threads. Part II Chapter 27 884 PART II The Java Library es.execute(new es.execute(new es.execute(new es.execute(new MyThread(cdl, "A")); MyThread(cdl2, "B")); MyThread(cdl3, "C")); MyThread(cdl4, "D")); try { cdl.await(); cdl2.await(); cdl3.await(); cdl4.await(); } catch (InterruptedException exc) { System.out.println(exc); } es.shutdown(); System.out.println("Done"); } } class MyThread implements Runnable { String name; CountDownLatch latch; MyThread(CountDownLatch c, String n) { latch = c; name = n; new Thread(this); } public void run() { for(int i = 0; i < 5; i++) { System.out.println(name + ": " + i); latch.countDown(); } } } The output from the program is shown here. (The precise order in which the threads execute may vary.) Starting A: 0 A: 1 A: 2 A: 3 A: 4 C: 0 C: 1 C: 2 C: 3 C: 4 Chapter 27 The Concurrency Utilities 885 As the output shows, even though the thread pool contains only two threads, all four tasks are still executed. However, only two can run at the same time. The others must wait until one of the pooled threads is available for use. The call to shutdown( ) is important. If it were not present in the program, then the program would not terminate because the executor would remain active. To try this for yourself, simply comment out the call to shutdown( ) and observe the result. Using Callable and Future One of the most interesting features of the concurrent API is the Callable interface. This interface represents a thread that returns a value. An application can use Callable objects to compute results that are then returned to the invoking thread. This is a powerful mechanism because it facilitates the coding of many types of numerical computations in which partial results are computed simultaneously. It can also be used to run a thread that returns a status code that indicates the successful completion of the thread. Callable is a generic interface that is defined like this: interface Callable Here, V indicates the type of data returned by the task. Callable defines only one method, call( ), which is shown here: V call( ) throws Exception Inside call( ), you define the task that you want performed. After that task completes, you return the result. If the result cannot be computed, call( ) must throw an exception. A Callable task is executed by an ExecutorService, by calling its submit( ) method. There are three forms of submit( ), but only one is used to execute a Callable. It is shown here: Future submit(Callable task) Here, task is the Callable object that will be executed in its own thread. The result is returned through an object of type Future. Future is a generic interface that represents the value that will be returned by a Callable object. Because this value is obtained at some future time, the name Future is appropriate. Future is defined like this: interface Future Here, V specifies the type of the result. Part II D: 0 D: 1 D: 2 D: 3 D: 4 B: 0 B: 1 B: 2 B: 3 B: 4 Done 886 PART II The Java Library To obtain the returned value, you will call Future’s get( ) method, which has these two forms: V get( ) throws InterruptedException, ExecutionException V get(long wait, TimeUnit tu) throws InterruptedException, ExecutionException, TimeoutException The first form waits for the result indefinitely. The second form allows you to specify a timeout period in wait. The units of wait are passed in tu, which is an object of the TimeUnit enumeration, described later in this chapter. The following program illustrates Callable and Future by creating three tasks that perform three different computations. The first returns the summation of a value, the second computes the length of the hypotenuse of a right triangle given the length of its sides, and the third computes the factorial of a value. All three computations occur simultaneously. // An example that uses a Callable. import java.util.concurrent.*; class CallableDemo { public static void main(String args[]) { ExecutorService es = Executors.newFixedThreadPool(3); Future f; Future f2; Future f3; System.out.println("Starting"); f = es.submit(new Sum(10)); f2 = es.submit(new Hypot(3, 4)); f3 = es.submit(new Factorial(5)); try { System.out.println(f.get()); System.out.println(f2.get()); System.out.println(f3.get()); } catch (InterruptedException exc) { System.out.println(exc); } catch (ExecutionException exc) { System.out.println(exc); } es.shutdown(); System.out.println("Done"); } } Chapter 27 The Concurrency Utilities 887 // Following are three computational threads. class Sum implements Callable { int stop; public Integer call() { int sum = 0; for(int i = 1; i <= stop; i++) { sum += i; } return sum; } } class Hypot implements Callable { double side1, side2; Hypot(double s1, double s2) { side1 = s1; side2 = s2; } public Double call() { return Math.sqrt((side1*side1) + (side2*side2)); } } class Factorial implements Callable { int stop; Factorial(int v) { stop = v; } public Integer call() { int fact = 1; for(int i = 2; i <= stop; i++) { fact *= i; } return fact; } } The output is shown here: Starting 55 5.0 120 Done Part II Sum(int v) { stop = v; } 888 PART II The Java Library The TimeUnit Enumeration The concurrent API defines several methods that take an argument of type TimeUnit, which indicates a time-out period. TimeUnit is an enumeration that is used to specify the granularity (or resolution) of the timing. TimeUnit is defined within java.util.concurrent. It can be one of the following values: DAYS HOURS MINUTES SECONDS MICROSECONDS MILLISECONDS NANOSECONDS Although TimeUnit lets you specify any of these values in calls to methods that take a timing argument, there is no guarantee that the system is capable of the specified resolution. Here is an example that uses TimeUnit. The CallableDemo class, shown in the previous section, is modified as shown next to use the second form of get( ) that takes a TimeUnit argument. try { System.out.println(f.get(10, TimeUnit.MILLISECONDS)); System.out.println(f2.get(10, TimeUnit.MILLISECONDS)); System.out.println(f3.get(10, TimeUnit.MILLISECONDS)); } catch (InterruptedException exc) { System.out.println(exc); } catch (ExecutionException exc) { System.out.println(exc); } catch (TimeoutException exc) { System.out.println(exc); } In this version, no call to get( ) will wait more than 10 milliseconds. The TimeUnit enumeration defines various methods that convert between units. These are shown here: long convert(long tval, TimeUnit tu) long toMicros(long tval) long toMillis(long tval) long toNanos(long tval) long toSeconds(long tval) long toDays(long tval) long toHours(long tval) long toMinutes(long tval) The convert( ) method converts tval into the specified unit and returns the result. The to methods perform the indicated conversion and return the result. Chapter 27 The Concurrency Utilities 889 TimeUnit also defines the following timing methods: Here, sleep( ) pauses execution for the specified delay period, which is specified in terms of the invoking enumeration constant. It translates into a call to Thread.sleep( ). The timedJoin( ) method is a specialized version of Thread.join( ) in which thrd pauses for the time period specified by delay, which is described in terms of the invoking time unit. The timedWait( ) method is a specialized version of Object.wait( ) in which obj is waited on for the period of time specified by delay, which is described in terms of the invoking time unit. The Concurrent Collections As explained, the concurrent API defines several collection classes that have been engineered for concurrent operation. They include: ArrayBlockingQueue ConcurrentHashMap ConcurrentLinkedDeque (Added by JDK 7.) ConcurrentLinkedQueue ConcurrentSkipListMap ConcurrentSkipListSet CopyOnWriteArrayList CopyOnWriteArraySet DelayQueue LinkedBlockingDeque LinkedBlockingQueue LinkedTransferQueue (Added by JDK 7.) PriorityBlockingQueue SynchronousQueue These offer concurrent alternatives to their related classes defined by the Collections Framework. These collections work much like the other collections except that they provide concurrency support. Programmers familiar with the Collections Framework will have no trouble using these concurrent collections. Locks The java.util.concurrent.locks package provides support for locks, which are objects that offer an alternative to using synchronized to control access to a shared resource. In general, here is how a lock works. Before accessing a shared resource, the lock that protects that resource is acquired. When access to the resource is complete, the lock is released. If a second thread attempts to acquire the lock when it is in use by another thread, the second thread will suspend until the lock is released. In this way, conflicting access to a shared resource is prevented. Part II void sleep(long delay) throws InterruptedExecution void timedJoin(Thread thrd, long delay) throws InterruptedExecution void timedWait(Object obj, long delay) throws InterruptedExecution 890 PART II The Java Library Locks are particularly useful when multiple threads need to access the value of shared data. For example, an inventory application might have a thread that first confirms that an item is in stock and then decreases the number of items on hand as each sale occurs. If two or more of these threads are running, then without some form of synchronization, it would be possible for one thread to be in middle of a transaction when the second thread begins its transaction. The result could be that both threads would assume that adequate inventory exists, even if there is only sufficient inventory on hand to satisfy one sale. In this type of situation, a lock offers a convenient means of handling the needed synchronization. All locks implement the Lock interface. The methods defined by Lock are shown in Table 27-1. In general, to acquire a lock, call lock( ). If the lock is unavailable, lock( ) will wait. To release a lock, call unlock( ). To see if a lock is available, and to acquire it if it is, call tryLock( ). This method will not wait for the lock if it is unavailable. Instead, it returns true if the lock is acquired and false otherwise. The newCondition( ) method returns a Condition object associated with the lock. Using a Condition, you gain detailed control of the lock through methods such as await( ) and signal( ), which provide functionality similar to Object.wait( ) and Object.notify( ). java.util.concurrent.locks supplies an implementation of Lock called ReentrantLock. ReentrantLock implements a reentrant lock, which is a lock that can be repeatedly entered by the thread that currently holds the lock. Of course, in the case of a thread reentering a lock, all calls to lock( ) must be offset by an equal number of calls to unlock( ). Otherwise, a thread seeking to acquire the lock will suspend until the lock is not in use. The following program demonstrates the use of a lock. It creates two threads that access a shared resource called Shared.count. Before a thread can access Shared.count, it must obtain a lock. After obtaining the lock, Shared.count is incremented and then, before releasing the lock, the thread sleeps. This causes the second thread to attempt to obtain the lock. However, because the lock is still held by the first thread, the second thread must wait Method Description void lock( ) Waits until the invoking lock can be acquired. void lockInterruptibly( ) throws InterruptedException Waits until the invoking lock can be acquired, unless interrupted. Condition newCondition( ) Returns a Condition object that is associated with the invoking lock. boolean tryLock( ) Attempts to acquire the lock. This method will not wait if the lock is unavailable. Instead, it returns true if the lock has been acquired and false if the lock is currently in use by another thread. boolean tryLock(long wait, TimeUnit tu) throws InterruptedException Attempts to acquire the lock. If the lock is unavailable, this method will wait no longer than the period specified by wait, which is in tu units. It returns true if the lock has been acquired and false if the lock cannot be acquired within the specified period. void unlock( ) Releases the lock. Table 27-1 The Lock Methods Chapter 27 The Concurrency Utilities 891 until the first thread stops sleeping and releases the lock. The output shows that access to Shared.count is, indeed, synchronized by the lock. // A simple lock example. import java.util.concurrent.locks.*; class LockDemo { new LockThread(lock, "A"); new LockThread(lock, "B"); } } // A shared resource. class Shared { static int count = 0; } // A thread of execution that increments count. class LockThread implements Runnable { String name; ReentrantLock lock; LockThread(ReentrantLock lk, String n) { lock = lk; name = n; new Thread(this).start(); } public void run() { System.out.println("Starting " + name); try { // First, lock count. System.out.println(name + " is waiting to lock count."); lock.lock(); System.out.println(name + " is locking count."); Shared.count++; System.out.println(name + ": " + Shared.count); // Now, allow a context switch -- if possible. System.out.println(name + " is sleeping."); Thread.sleep(1000); } catch (InterruptedException exc) { System.out.println(exc); } finally { // Unlock Part II public static void main(String args[]) { ReentrantLock lock = new ReentrantLock(); 892 PART II The Java Library System.out.println(name + " is unlocking count."); lock.unlock(); } } } The output is shown here. (The precise order in which the threads execute may vary.) Starting A A is waiting to lock count. A is locking count. A: 1 A is sleeping. Starting B B is waiting to lock count. A is unlocking count. B is locking count. B: 2 B is sleeping. B is unlocking count. java.util.concurrent.locks also defines the ReadWriteLock interface. This interface specifies a lock that maintains separate locks for read and write access. This enables multiple locks to be granted for readers of a resource as long as the resource is not being written. ReentrantReadWriteLock provides an implementation of ReadWriteLock. Atomic Operations java.util.concurrent.atomic offers an alternative to the other synchronization features when reading or writing the value of some types of variables. This package offers methods that get, set, or compare the value of a variable in one uninterruptible (that is, atomic) operation. This means that no lock or other synchronization mechanism is required. Atomic operations are accomplished through the use of classes, such as AtomicInteger and AtomicLong, and methods such as get( ), set( ), compareAndSet( ), decrementAndGet( ), and getAndSet( ), which perform the action indicated by their names. Here is an example that demonstrates how access to a shared integer can be synchronized by the use of AtomicInteger: // A simple example of Atomic. import java.util.concurrent.atomic.*; class AtomicDemo { public static void main(String args[]) { new AtomThread("A"); new AtomThread("B"); new AtomThread("C"); } } Chapter 27 The Concurrency Utilities 893 class Shared { static AtomicInteger ai = new AtomicInteger(0); } AtomThread(String n) { name = n; new Thread(this).start(); } public void run() { System.out.println("Starting " + name); for(int i=1; i <= 3; i++) System.out.println(name + " got: " + Shared.ai.getAndSet(i)); } } In the program, a static AtomicInteger named ai is created by Shared. Then, three threads of type AtomThread are created. Inside run( ), Shared.ai is modified by calling getAndSet( ). This method returns the previous value and then sets the value to the one passed as an argument. The use of AtomicInteger prevents two threads from writing to ai at the same time. In general, the atomic operations offer a convenient (and possibly more efficient) alternative to the other synchronization mechanisms when only a single variable is involved. Parallel Programming via the Fork/Join Framework In recent years, an important new trend has emerged in software development: parallel programming. Parallel programming is the name commonly given to the techniques that take advantage of computers that contain two or more processors (multicore). As most readers will know, multicore computers are becoming commonplace. The advantage that multi-processor environments offer is the ability to significantly increase program performance. As a result, there has been a growing need for a mechanism that gives Java programmers a simple, yet effective way to make use of multiple processors in a clean, scalable manner. To answer this need, JDK 7 adds several new classes and interfaces that support parallel programming. They are commonly referred to as the Fork/Join Framework. It is one of most important additions that JDK 7 has made to the Java class library. The Fork/Join Framework is defined in the java.util.concurrent package. The Fork/Join Framework enhances multithreaded programming in two important ways. First, it simplifies the creation and use of multiple threads. Second, it automatically makes use of multiple processors. In other words, by using the Fork/Join Framework you enable your applications to automatically scale to make use of the number of available processors. These two features make the Fork/Join Framework the recommended approach to multithreading when parallel processing is desired. Part II // A thread of execution that increments count. class AtomThread implements Runnable { String name; 894 PART II The Java Library Before continuing, it is important to point out the distinction between traditional multithreading and parallel programming. In the past, most computers had a single CPU and multithreading was primarily used to take advantage of idle time, such as when a program is waiting for user input. Using this approach, one thread can execute while another is waiting. In other words, on a single-CPU system, multithreading is used to allow two or more tasks to share the CPU. This type of multithreading is typically supported by an object of type Thread (as described in Chapter 11). Although this type of multithreading will always remain quite useful, it was not optimized for situations in which two or more CPUs are available (multicore computers). When multiple CPUs are present, a second type of multithreading capability that supports true parallel execution is required. With two or more CPUs, it is possible to execute portions of a program simultaneously, with each part executing on its own CPU. This can be used to significantly speed up the execution of some types of operations, such as sorting, transforming, or searching a large array. In many cases, these types of operations can be broken down into smaller pieces (each acting on a portion of the array), and each piece can be run on its own CPU. As you can imagine, the gain in efficiency can be enormous. Simply put: Parallel programming will be part of nearly every programmer’s future because it offers a way to dramatically improve program performance. The Main Fork/Join Classes The Fork/Join Framework is packaged in java.util.concurrent. At the core of the Fork/Join Framework are the following four classes: ForkJoinTask An abstract class that defines a task ForkJoinPool Manages the execution of ForkJoinTasks RecursiveAction A subclass of ForkJoinTask for tasks that do not return values RecursiveTask A subclass of ForkJoinTask for tasks that return values Here is how they relate. A ForkJoinPool manages the execution of ForkJoinTasks. ForkJoinTask is an abstract class that is extended by two other abstract classes: RecursiveAction and RecursiveTask. Typically, your code will extend these classes to create a task. Before looking at the process in detail, an overview of the key aspects of each class will be helpful. ForkJoinTask ForkJoinTask is an abstract class that defines a task that can be managed by a ForkJoinPool. The type parameter V specifies the result type of the task. ForkJoinTask differs from Thread in that ForkJoinTask represents lightweight abstraction of a task, rather than a thread of execution. ForkJoinTasks are executed by threads managed by a thread pool of type ForkJoinPool. This mechanism allows a large number of tasks to be managed by a small number of actual threads. Thus, ForkJoinTasks are very efficient when compared to threads. ForkJoinTask defines many methods. At the core are fork( ) and join( ), shown here: final ForkJoinTask fork( ) final V join( ) The Concurrency Utilities 895 The fork( ) method submits the invoking task for asynchronous execution of the invoking task. This means that the thread that calls fork( ) continues to run. The fork( ) method returns this after the task is scheduled for execution. It can be executed only from within the computational portion of another ForkJoinTask, which is running within a ForkJoinPool. (You will see how to do this, shortly.) The join( ) method waits until the task on which it is called terminates. The result of the task is returned. Thus, through the use of fork( ) and join( ), you can start one or more new tasks and then wait for them to finish. Another important ForkJoinTask method is invoke( ). It combines the fork and join operations into a single call because it begins a task and then waits for it to end. It is shown here: final V invoke( ) The result of the invoking task is returned. You can invoke more than one task at a time by using invokeAll( ). Two of its forms are shown here: static void invokeAll(ForkJoinTask taskA, ForkJoinTask taskB) static void invokeAll(ForkJoinTask ... taskList) In the first case, taskA and taskB are executed. In the second case, all specified tasks are executed. In both cases, the calling thread waits until all of the specified tasks have terminated. The invokeAll( ) method can be executed only from within the computational portion of another ForkJoinTask, which is running within a ForkJoinPool. RecursiveAction A subclass of ForkJoinTask is RecursiveAction. This class encapsulates a task that does not return a result. Typically, your code will extend RecursiveAction to create a task that has a void return type. RecursiveAction specifies four methods, but only one is usually of interest: the abstract method called compute( ). When you extend RecursiveAction to create a concrete class, you will put the code that defines the task inside compute( ). The compute( ) method represents the computational portion of the task. The compute( ) method is defined by RecursiveAction like this: protected abstract void compute( ) Notice that compute( ) is protected. This means that it can be called only by other methods of its class or subclass. Also, because it is abstract, it must be implemented by a subclass (unless that subclass is also abstract). In general, RecursiveAction is used to implement a recursive, divide-and-conquer strategy for tasks that don’t return results. (See “The Divide-and-Conquer Strategy” later in this chapter.) RecursiveTask Another subclass of ForkJoinTask is RecursiveTask. This class encapsulates a task that returns a result. The result type is specified by V. Typically, your code will extend RecursiveTask to create a task that returns a value. Like RecursiveAction, it too specifies four methods, but often only the abstract compute( ) method is used, which represents the Part II Chapter 27 896 PART II The Java Library computational portion of the task. When you extend RecursiveTask to create a concrete class, put the code that represents the task inside compute( ). This code must also return the result of the task. The compute( ) method is defined by RecursiveTask like this: protected abstract V compute( ) Notice that compute( ) is protected. This means that it can be called only by other methods of its class or subclass. Also, because it is abstract, it must be implemented by a subclass. When implemented, it must return the result of the task. In general, RecursiveTask is used to implement a recursive, divide-and-conquer strategy for tasks that return results. (See “The Divide-and-Conquer Strategy” later in this chapter.) ForkJoinPool The execution of ForkJoinTasks takes place within a ForkJoinPool, which also manages the execution of the tasks. Therefore, in order to execute a ForkJoinTask, you must first have a ForkJoinPool. ForkJoinPool defines several constructors. Here are two commonly used ones: ForkJoinPool( ) ForkJoinPool(int pLevel) The first creates a default pool that supports a level of parallelism equal to the number of processors available in the system. The second lets you specify the level of parallelism. Its value must be greater than zero and not more than the limits of the implementation. The level of parallelism determines the number of threads that can execute concurrently. As a result, the level of parallelism effectively determines the number of tasks that can be executed simultaneously. (Of course, the number of tasks that can execute simultaneously cannot exceed the number of processors.) It is important to understand that the level of parallelism does not, however, limit the number of tasks that can be managed by the pool. A ForkJoinPool can manage many more tasks than its level of parallelism. Also, the level of parallelism is only a target. It is not a guarantee. After you have created an instance of ForkJoinPool, you can start a task in a number of different ways. The first task started is often thought of as the main task. Frequently, the main task begins subtasks that are also managed by the pool. One common way to begin a main task is to call invoke( ) on the ForkJoinPool. It is shown here: T invoke(ForkJoinTask task) This method begins the task specified by task, and it returns the result of the task. This means that the calling code waits until invoke( ) returns. To start a task without waiting for its completion, you can use execute( ). Here is one of its forms: void execute(ForkJoinTask task) In this case, task is started, but the calling code does not wait for its completion. Rather, the calling code continues execution asynchronously. Chapter 27 The Concurrency Utilities 897 The Divide-and-Conquer Strategy As a general rule, uses of the Fork/Join Framework will employ a divide-and-conquer strategy that is based on recursion. This is why the two subclasses of ForkJoinTask are called RecursiveAction and RecursiveTask. It is anticipated that you will extend one of these classes when creating your own fork/join task. The divide-and-conquer strategy is based on recursively dividing a task into smaller subtasks until the size of a subtask is small enough to be handled sequentially. For example, a task that applies a transform to each element in an array of N integers can be broken down into two subtasks in which each transforms half the elements in the array. That is, one subtask transforms the elements 0 to N/2, and the other transforms the elements N/2 to N. In turn, each subtask can be reduced to another set of subtasks, each transforming half of the remaining elements. This process of dividing the array will continue until a threshold is reached in which a sequential solution is faster than creating another division. The advantage of the divide-and-conquer strategy is that the processing can occur in parallel. Therefore, instead of cycling through an entire array using a single thread, pieces of the array can be processed simultaneously. Of course, the divide-and-conquer approach works in many cases in which an array (or collection) is not present, but the most common uses involve some type of array, collection, or grouping of data. One of the keys to best employing the divide-and-conquer strategy is correctly selecting the threshold at which sequential processing (rather than further division) is used. Typically, an optimal threshold is obtained through profiling the execution characteristics. However, very significant speed-ups will still occur even when a less-than-optimal threshold is used. It is, however, best to avoid overly large or overly small thresholds. At the time of this writing, the Java API documentation for ForkJoinTask states that, as a rule-of-thumb, a task should perform somewhere between 100 and 10,000 computational steps. It is also important to understand that the optimal threshold value is also affected by how much time the computation takes. If each computational step is fairly long, then smaller thresholds might be better. Conversely, if each computational step is quite short, then larger thresholds could yield better results. For applications that are to be run on a known system, with a known number of processors, you can use the number of processors to make informed decisions about the threshold value. However, for applications that will be running on a variety of systems, the capabilities of which are not known in advance, you can make no assumptions about the execution environment. Part II ForkJoinPool manages the execution of its threads using an approach called workstealing. Each worker thread maintains a queue of tasks. If one worker thread’s queue is empty, it will take a task from another worker thread. This adds to overall efficiency and helps maintain a balanced load. (Because of demands on CPU time by other processes in the system, even two worker threads with identical tasks in their respective queues may not complete at the same time.) One other point: ForkJoinPool uses daemon threads. A daemon thread is automatically terminated when all user threads have terminated. Thus, there is no need to explicitly shut down a ForkJoinPool. However, it is possible to do so by calling shutdown( ). 898 PART II The Java Library One other point: Although multiple processors may be available on a system, other tasks (and the operating system, itself) will be competing with your application for CPU time. Thus, it is important not to assume that your program will have unrestricted access to all CPUs. Furthermore, different runs of the same program may display different run time characteristics because of varying task loads. A Simple First Fork/Join Example At this point, a simple example that demonstrates the Fork/Join Framework and the divideand-conquer strategy will be helpful. Following is a program that transforms the elements in an array of double into their square roots. It does so via a subclass of RecursiveAction. // A simple example of the basic divide-and-conquer strategy. // In this case, RecursiveAction is used. import java.util.concurrent.*; import java.util.*; // A ForkJoinTask (via RecursiveAction) that transforms // the elements in an array of doubles into their square roots. class SqrtTransform extends RecursiveAction { // The threshold value is arbitrarily set at 1,000 in this example. // In real-world code, its optimal value can be determined by // profiling and experimentation. final int seqThreshold = 1000; // Array to be accessed. double[] data; // Determines what part of data to process. int start, end; SqrtTransform(double[] vals, int s, int e ) { data = vals; start = s; end = e; } // This is the method in which parallel computation will occur. protected void compute() { // If number of elements is below the sequential threshold, // then process sequentially. if((end - start) < seqThreshold) { // Transform each element into its square root. for(int i = start; i < end; i++) { data[i] = Math.sqrt(data[i]); } } else { // Otherwise, continue to break the data into smaller pieces. // Find the midpoint. int middle = (start + end) / 2; Chapter 27 The Concurrency Utilities 899 // Invoke new tasks, using the subdivided data. invokeAll(new SqrtTransform(data, start, middle), new SqrtTransform(data, middle, end)); } } // Demonstrate parallel execution. class ForkJoinDemo { public static void main(String args[]) { // Create a task pool. ForkJoinPool fjp = new ForkJoinPool(); double[] nums = new double[100000]; // Give nums some values. for(int i = 0; i < nums.length; i++) nums[i] = (double) i; System.out.println("A portion of the original sequence:"); for(int i=0; i < 10; i++) System.out.print(nums[i] + " "); System.out.println("\n"); SqrtTransform task = new SqrtTransform(nums, 0, nums.length); // Start the main ForkJoinTask. fjp.invoke(task); System.out.println("A portion of the transformed sequence" + " (to four decimal places):"); for(int i=0; i < 10; i++) System.out.format("%.4f ", nums[i]); System.out.println(); } } The output from the program is shown here: A portion of the original sequence: 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 A portion of the transformed sequence (to four decimal places): 0.0000 1.0000 1.4142 1.7321 2.0000 2.2361 2.4495 2.6458 2.8284 3.0000 As you can see, the values of the array elements have been transformed into their square roots. Let’s look closely at how this program works. First, notice that SqrtTransform is a class that extends RecursiveAction. As explained, RecursiveAction extends ForkJoinTask for tasks that do not return results. Next, notice the final variable seqThreshold. This is the value that determines when sequential processing will take place. This value is set (somewhat arbitrarily) to 1,000. Next, notice that a reference to the array to be processed is stored in data and that the fields start and end are used to indicate the boundaries of the elements to be accessed. Part II } 900 PART II The Java Library The main action of the program takes place in compute( ). It begins by checking if the number of elements to be processed is below the sequential processing threshold. If it is, then those elements are processed (by computing their square root in this example). If the sequential processing threshold has not been reached, then two new tasks are started by calling invokeAll( ). In this case, each subtask processes half the elements. As explained earlier, invokeAll( ) waits until both tasks return. After all of the recursive calls unwind, each element in the array will have been modified, with much of the action taking place in parallel (if multiple processors are available). Understanding the Impact of the Level of Parallelism Before moving on, it is important to understand the impact that the level of parallelism has on the performance of a fork/join task and how the parallelism and the threshold interact. The program shown in this section lets you experiment with different degrees of parallelism and threshold values. Assuming that you are using a multicore computer, then you can interactively observe the effect of these values. In the preceding example, because the default ForkJoinPool constructor was used, the default level of parallelism was used, which is equal to the number of processors in the system. However, you can specify the level of parallelism that you want. One way shown earlier is to specify it when you create a ForkJoinPool using this constructor: ForkJoinPool(int pLevel) Here, pLevel specifies the level of parallelism, which must be greater than zero and less than the implementation defined limit. The following program creates a fork/join task that transforms an array of doubles. The transformation is arbitrary, but it is designed to consume several CPU cycles. This was done to ensure that the effects of changing the threshold or the level of parallelism would be more clearly displayed. To use the program, specify the threshold value and the level of parallelism on the command line. The program then runs the tasks. It also displays the amount of time it takes the tasks to run. To do this, it uses System.nanoTime( ), which returns the value of the JVM’s high-resolution timer. // A simple program that lets you experiment with the effects of // changing the threshold and parallelism of a ForkJoinTask. import java.util.concurrent.*; // A ForkJoinTask (via RecursiveAction) that performs a // a transform on the elements of an array of doubles. class Transform extends RecursiveAction { // Sequential threshold, which is set by the constructor. int seqThreshold; // Array to be accessed. double[] data; // Determines what part of data to process. int start, end; Transform(double[] vals, int s, int e, int t ) { Chapter 27 The Concurrency Utilities 901 data = vals; start = s; end = e; seqThreshold = t; } // If number of elements is below the sequential threshold, // then process sequentially. if((end - start) < seqThreshold) { // The following code assigns an element at an even index the // square root of its original value. An element at an odd // index is assigned its cube root. This code is designed // to simply consume CPU time so that the effects of concurrent // execution are more readily observable. for(int i = start; i < end; i++) { if((data[i] % 2) == 0) data[i] = Math.sqrt(data[i]); else data[i] = Math.cbrt(data[i]); } } else { // Otherwise, continue to break the data into smaller pieces. // Find the midpoint. int middle = (start + end) / 2; // Invoke new tasks, using the subdivided data. invokeAll(new Transform(data, start, middle, seqThreshold), new Transform(data, middle, end, seqThreshold)); } } } // Demonstrate parallel execution. class FJExperiment { public static void main(String args[]) { int pLevel; int threshold; if(args.length != 2) { System.out.println("Usage: FJExperiment parallelism threshold "); return; } pLevel = Integer.parseInt(args[0]); threshold = Integer.parseInt(args[1]); // These variables are used to time the task. long beginT, endT; Part II // This is the method in which parallel computation will occur. protected void compute() { 902 PART II The Java Library // Create a task pool. Notice that the parallelism level is set. ForkJoinPool fjp = new ForkJoinPool(pLevel); double[] nums = new double[1000000]; for(int i = 0; i < nums.length; i++) nums[i] = (double) i; Transform task = new Transform(nums, 0, nums.length, threshold); // Starting timing. beginT = System.nanoTime(); // Start the main ForkJoinTask. fjp.invoke(task); // End timing. endT = System.nanoTime(); System.out.println("Level of parallelism: " + pLevel); System.out.println("Sequential threshold: " + threshold); System.out.println("Elapsed time: " + (endT - beginT) + " ns"); System.out.println(); } } To use the program, specify the level of parallelism followed by the threshold limit. You should try experimenting with different values for each, observing the results. Remember, to be effective, you must run the code on a computer with at least two processors. Also, understand that two different runs may (almost certainly will) produce different results because of the effect of other processes in the system consuming CPU time. To give you an idea of the difference that parallelism makes, try this experiment. First, execute the program like this: java FJExperiment 1 1000 This requests 1 level of parallelism (essentially sequential execution) with a threshold of 1,000. Here is a sample run produced on a dual-core computer: Level of parallelism: 1 Sequential threshold: 1000 Elapsed time: 259677487 ns Now, specify 2 levels of parallelism like this: java FJExperiment 2 1000 Here is sample output from this run produced by same dual-core computer: Level of parallelism: 2 Sequential threshold: 1000 Elapsed time: 169254472 ns Chapter 27 The Concurrency Utilities 903 As is evident, adding parallelism substantially decreases execution time, thus increasing the speed of the program. You should experiment with varying the threshold and parallelism on your own computer. The results may surprise you. There are two other methods that you might find useful when experimenting with the execution characteristics of a fork/join program. First, you can obtain the level of parallelism by calling getParallelism( ), which is defined by ForkJoinPool. It is shown here: It returns the parallelism level currently in effect. Recall that, by default, this will equal the number of available processors. Second, you can obtain the number of processors available in the system by calling availableProcessors( ), which is defined by the Runtime class. It is shown here: int availableProcessors( ) The value returned may change from one call to the next because of other system demands. An Example that Uses RecursiveTask The two preceding examples are based on RecursiveAction, which means that they concurrently execute tasks that do not return results. To create a task that returns a result, use RecursiveTask. In general, solutions are designed in the same manner as just shown. The key difference is that the compute( ) method returns a result. Thus, you must aggregate the results, so that when the first invocation finishes, it returns the overall result. Another difference is that you will typically start a subtask by calling fork( ) and join( ) explicitly (rather than implicitly by calling invokeAll( ), for example). The following program demonstrates RecursiveTask. It creates a task called Sum that returns the summation of the values in an array of double. In this example, the array consists of 5,000 elements. However, every other value is negative. Thus, the first values in the array are 0, –1, 2, –3, 4, and so on. // A simple example that uses RecursiveTask. import java.util.concurrent.*; // A RecursiveTask that computes the summation of an array of doubles. class Sum extends RecursiveTask { // The sequential threshold value. final int seqThresHold = 500; // Array to be accessed. double[] data; // Determines what part of data to process. int start, end; Sum(double[] vals, int s, int e ) { data = vals; start = s; end = e; } Part II int getParallelism( ) 904 PART II The Java Library // Find the summation of an array of doubles. protected Double compute() { double sum = 0; // If number of elements is below the sequential threshold, // then process sequentially. if((end - start) < seqThresHold) { // Sum the elements. for(int i = start; i < end; i++) sum += data[i]; } else { // Otherwise, continue to break the data into smaller pieces. // Find the midpoint. int middle = (start + end) / 2; // Invoke new tasks, using the subdivided data. Sum subTaskA = new Sum(data, start, middle); Sum subTaskB = new Sum(data, middle, end); // Start each subtask by forking. subTaskA.fork(); subTaskB.fork(); // Wait for the subtasks to return, and aggregate the results. sum = subTaskA.join() + subTaskB.join(); } // Return the final sum. return sum; } } // Demonstrate parallel execution. class RecurTaskDemo { public static void main(String args[]) { // Create a task pool. ForkJoinPool fjp = new ForkJoinPool(); double[] nums = new double[5000]; // Initialize nums with values that alternate between // positive and negative. for(int i=0; i < nums.length; i++) nums[i] = (double) (((i%2) == 0) ? i : -i) ; Sum task = new Sum(nums, 0, nums.length); // Start the ForkJoinTasks. Notice that, in this case, // invoke() returns a result. double summation = fjp.invoke(task); System.out.println("Summation " + summation); } } Chapter 27 The Concurrency Utilities 905 Here’s the output from the program: Summation -2500.0 There are a couple of interesting items in this program. First, notice that the two subtasks are executed by calling fork( ), as shown here: In this case, fork( ) is used because it starts a task but does not wait for it to finish. (Thus, it asynchronously runs the task.) The result of each task is obtained by calling join( ), as shown here: sum = subTaskA.join() + subTaskB.join(); This statement waits until each task ends. It then adds the results of each and assigns the total to sum. Thus, the summation of each subtask is added to the running total. Finally, compute( ) ends by returning sum, which will be the final total when the first invocation returns. There are other ways to approach the handling of the asynchronous execution of the subtasks. For example, the following sequence uses fork( ) to start subTaskA and uses invoke( ) to start and wait for subTaskB: subTaskA.fork(); sum = subTaskA.join() + subTaskB.invoke(); Another alternative is to have subTaskB call compute( ) directly, as shown here: subTaskA.fork(); sum = subTaskA.join() + subTaskB.compute(); Executing a Task Asynchronously The preceding programs have called invoke( ) on a ForkJoinPool to initiate a task. This approach is commonly used when the calling thread must wait until the task has completed (which is often the case) because invoke( ) does not return until the task has terminated. However, you can start a task asynchronously. In this approach, the calling thread continues to execute. Thus, both the calling thread and the task execute simultaneously. To start a task asynchronously, use execute( ), which is also defined by ForkJoinPool. It has the two forms shown here: void execute(ForkJoinTask task) void execute(Runnable task) In both forms, task specifies the task to run. Notice that the second form lets you specify a Runnable rather than a ForkJoinTask task. Thus, it forms a bridge between Java’s traditional approach to multithreading and the new Fork/Join Framework. It is important to remember that the threads used by a ForkJoinPool are daemon. Thus, they will end when the main thread ends. As a result, you may need to keep the main thread alive until the tasks have finished. Part II subTaskA.fork(); subTaskB.fork(); 906 PART II The Java Library Cancelling a Task A task can be cancelled by calling cancel( ), which is defined by ForkJoinTask. It has this general form: boolean cancel(boolean interuptOK) It returns true if the task on which it was called is cancelled. It returns false if the task was already cancelled, has already completed, or can’t be cancelled. At this time, the interruptOK parameter is not used by the default implementation. In general, cancel( ) is intended to be called from code outside the task because a task can easily cancel itself by returning. You can determine if a task has been cancelled by calling isCancelled( ), as shown here: final boolean isCancelled( ) It returns true if the invoking task has been cancelled prior to completion and false otherwise. Determining a Task’s Completion Status In addition to isCancelled( ), which was just described, ForkJoinTask includes two other methods that you can use to determine a task’s completion status. The first is isCompletedNormally( ), which is shown here: final boolean isCompletedNormally( ) It returns true if the invoking task completed normally, that is, if it did not throw an exception and it was not cancelled via a call to cancel( ). It returns false otherwise. The second is isCompletedAbnormally( ), which is shown here: final boolean isCompletedAbnormally( ) It returns true if the invoking task completed because it was cancelled or because it threw an exception. It returns false otherwise. Restarting a Task Normally, you cannot rerun a task. In other words, once a task completes, it cannot be restarted. However, you can reinitialize the state of the task (after it has completed) so it can be run again. This is done by calling reinitialize( ), as shown here: void reinitialize( ) This method resets the state of the invoking task. However, any modification made to any persistent data that is operated upon by the task will not be undone. For example, if the task modifies an array, then those modifications are not undone by calling reinitialize( ). Things to Explore The preceding discussion presented the fundamentals of the Fork/Join Framework and described the most commonly used methods. However, Fork/Join is a rich framework that includes additional capabilities that give you extended control over concurrency. Although it is far beyond the scope of this book to examine all of the issues and nuances surrounding parallel programming and the Fork/Join Framework, a sampling of the other features provided by ForkJoinTask and ForkJoinPool are mentioned here. Chapter 27 The Concurrency Utilities 907 As mentioned, methods such as invokeAll( ) and fork( ) can be called only from within a ForkJoinTask. This is usually an easy rule to abide by, but, in some cases, you may have code that can be executed from inside or outside a task. You can determine if your code is executing inside a task by calling inForkJoinPool( ). You can convert a Runnable or Callable object into a ForkJoinTask by using the adapt( ) method defined by ForkJoinTask. It has three forms, one for converting a Callable, one for a Runnable that does not return a result, and one for a Runnable that does return a result. In the case of a Callable, the call( ) method is run. In the case of Runnable, the run( ) method is run. You can obtain an approximate count of the number of tasks that are in the queue of the invoking thread by calling getQueuedTaskCount( ). You can obtain an approximate count of how many tasks the invoking thread has in its queue that are in excess of the number of other threads in the pool that might “steal” them, by calling getSurplusQueuedTaskCount( ). Remember, in the Fork/Join Framework, work-stealing is one way in which a high level of efficiency is obtained. Although this process is automatic, in some cases, the information may prove helpful in optimizing through-put. ForkJoinTask defines two methods that begin with the prefix quietly. They are shown here: final void quietlyJoin( ) Joins a task, but does not return a result or throw an exception final void quietlyInvoke( ) Invokes a task, but does not return a result or throw an exception. In essence, these methods are similar to their non-quiet counterparts except they don’t return values or throw exceptions. You can attempt to “un-invoke” (in other words, unschedule) a task by calling tryUnfork( ). ForkJoinTask implements Serializable. Thus, it can be serialized. However, serialization is not used during execution. A Sampling of Other ForkJoinPool Features One method that is quite useful when tuning fork/join applications is ForkJoinPool’s override of toString( ). It displays a “user-friendly” synopsis of the state of the pool. To see it in action, use this sequence to start and then wait for the task in the FJExperiment class of the task experimenter program shown earlier: // Asynchronously start the main ForkJoinTask. fjp.execute(task); // Display the state of the pool while waiting. while(!task.isDone()) { System.out.println(fjp); } When you run the program, you will see a series of messages on the screen that describe the state of the pool. Here is an example of one. Of course, your output may vary, based on the number of processors, threshold values, task load, and so on. java.util.concurrent.ForkJoinPool@141d683[Running, parallelism = 2, size = 2, active = 0, running = 2, steals = 0, tasks = 0, submissions = 1] You can determine if a pool is currently idle by calling isQuiescent( ). It returns true if the pool has no active threads and false otherwise. Part II A Sampling of Other ForkJoinTask Features 908 PART II The Java Library You can obtain the number of worker threads currently in the pool by calling getPoolSize( ). You can obtain an approximate count of the active threads in the pool by calling getActiveThreadCount( ). To shut down a pool, call shutdown( ). Currently active tasks will still be executed, but no new tasks can be started. To stop a pool immediately, call shutdownNow( ). In this case, an attempt is made to cancel currently active tasks. You can determine if a pool is shut down by calling isShutdown( ). It returns true if the pool has been shut down and false otherwise. To determine if the pool has been shut down and all tasks have been completed, call isTerminated( ). Some Fork/Join Tips Here are a few tips to help you avoid some of the more troublesome pitfalls associated with using the Fork/Join Framework. First, avoid using a sequential threshold that is too low. In general, erring on the high side is better than erring on the low side. If the threshold is too low, more time can be consumed generating and switching tasks than in processing the tasks. Second, usually it is best to use the default level of parallelism. If you specify a smaller number, it may significantly reduce the benefits of using the Fork/Join Framework. In general, a ForkJoinTask should not use synchronized methods or synchronized blocks of code. Also, you will not normally want to have the compute( ) method use other types of synchronization, such as a semaphore. (The new Phaser can, however, be used when appropriate because it is compatible with the fork/join mechanism.) Remember, the main idea behind a ForkJoinTask is the divide-and-conquer strategy. Such an approach does not normally lend itself to situations in which outside synchronization is needed. Also, avoid situations in which substantial blocking will occur through I/O. Therefore, in general, a ForkJoinTask will not perform I/O. Simply put, to best utilize the Fork/Join Framework, a task should perform a computation that can run without outside blocking or synchronization. One last point: Except under unusual circumstances, do not make assumptions about the execution environment that your code will run in. This means you should not assume that some specific number of processors will be available, or that the execution characteristics of your program won’t be affected by other processes running at the same time. The Concurrency Utilities Versus Java’s Traditional Approach Given the power and flexibility found in the new concurrency utilities, it is natural to ask the following question: Do they replace Java’s traditional approach to multithreading and synchronization? The answer is a resounding no! The original support for multithreading and the built-in synchronization features are still the mechanism that should be employed for many, many Java programs, applets, and servlets. For example, synchronized, wait( ), and notify( ) offer elegant solutions to a wide range of problems. However, when extra control is needed, the concurrency utilities are available to handle the chore. Furthermore, the new Fork/Join Framework offers a powerful way to integrate parallel programming techniques into your more sophisticated applications. CHAPTER 28 Regular Expressions and Other Packages When Java was originally released, it included a set of eight packages, called the core API. Each subsequent release added to the API. Today, the Java API contains a large number of packages. Many of the packages support areas of specialization that are beyond the scope of this book. However, four packages warrant an examination here: java.util.regex, java.lang.reflect, java.rmi, and java.text. They support regular expression processing, reflection, Remote Method Invocation (RMI), and text formatting, respectively. The regular expression package lets you perform sophisticated pattern matching operations. This chapter provides an in-depth discussion of this package along with extensive examples. Reflection is the ability of software to analyze itself. It is an essential part of the Java Beans technology that is covered in Chapter 29. Remote Method Invocation (RMI) allows you to build Java applications that are distributed among several machines. This chapter provides a simple client/server example that uses RMI. The text formatting capabilities of java.text have many uses. The one examined here formats date and time strings. The Core Java API Packages Table 28-1 lists all of the core API packages defined by Java (those in the java namespace) and summarizes their functions. Package Primary Function java.applet Supports construction of applets. java.awt Provides capabilities for graphical user interfaces. java.awt.color Supports color spaces and profiles. java.awt.datatransfer Transfers data to and from the system clipboard. java.awt.dnd Supports drag-and-drop operations. java.awt.event Handles events. Table 28-1 The Core Java API Packages 909 910 PART II The Java Library Package Primary Function java.awt.font Represents various types of fonts. java.awt.geom Allows you to work with geometric shapes. java.awt.im Allows input of Japanese, Chinese, and Korean characters to text editing components. java.awt.im.spi Supports alternative input devices. java.awt.image Processes images. java.awt.image.renderable Supports rendering-independent images. java.awt.print Supports general print capabilities. java.beans Allows you to build software components. java.beans.beancontext Provides an execution environment for Beans. java.io Inputs and outputs data. java.lang Provides core functionality. java.lang.annotation Supports annotations (metadata). java.lang.instrument Supports program instrumentation. java.lang.invoke Supports dynamic languages. java.lang.management Supports management of the execution environment. java.lang.ref Enables some interaction with the garbage collector. java.lang.reflect Analyzes code at run time. java.math Handles large integers and decimal numbers. java.net Supports networking. java.nio Top-level package for the NIO classes. Encapsulates buffers. java.nio.channels Encapsulates channels, which are used by the NIO system. java.nio.channels.spi Supports service providers for channels. java.nio.charset Encapsulates character sets. java.nio.charset.spi Supports service providers for character sets. java.nio.file Provides NIO support for files. java.nio.file.attribute Supports NIO file attributes. java.nio.file.spi Supports NIO service providers for files. java.rmi Provides remote method invocation. java.rmi.activation Activates persistent objects. java.rmi.dgc Manages distributed garbage collection. java.rmi.registry Maps names to remote object references. java.rmi.server Supports remote method invocation. java.security Handles certificates, keys, digests, signatures, and other security functions. java.security.acl Manages access control lists. Table 28-1 The Core Java API Packages (continued) Regular Expressions and Other Packages Package Primary Function java.security.cert Parses and manages certificates. java.security.interfaces Defines interfaces for DSA (Digital Signature Algorithm) keys. java.security.spec Specifies keys and algorithm parameters. java.sql Communicates with a SQL (Structured Query Language) database. java.text Formats, searches, and manipulates text. java.text.spi Supports service providers for text formatting classes in java.text. java.util Contains common utilities. java.util.concurrent Supports the concurrent utilities. java.util.concurrent.atomic Supports atomic (that is, indivisible) operations on variables without the use of locks. java.util.concurrent.locks Supports synchronization locks. java.util.jar Creates and reads JAR files. java.util.logging Supports logging of information related to a program’s execution. java.util.prefs Encapsulates information relating to user preference. java.util.regex Supports regular expression processing. java.util.spi Supports service providers for the utility classes in java.util. java.util.zip Reads and writes compressed and uncompressed ZIP files. 911 Table 28-1 The Core Java API Packages (continued) Regular Expression Processing The java.util.regex package supports regular expression processing. As the term is used here, a regular expression is a string of characters that describes a character sequence. This general description, called a pattern, can then be used to find matches in other character sequences. Regular expressions can specify wildcard characters, sets of characters, and various quantifiers. Thus, you can specify a regular expression that represents a general form that can match several different specific character sequences. There are two classes that support regular expression processing: Pattern and Matcher. These classes work together. Use Pattern to define a regular expression. Match the pattern against another sequence using Matcher. Pattern The Pattern class defines no constructors. Instead, a pattern is created by calling the compile( ) factory method. One of its forms is shown here: static Pattern compile(String pattern) Here, pattern is the regular expression that you want to use. The compile( ) method transforms the string in pattern into a pattern that can be used for pattern matching by the Matcher class. It returns a Pattern object that contains the pattern. Part II Chapter 28 912 PART II The Java Library Once you have created a Pattern object, you will use it to create a Matcher. This is done by calling the matcher( ) factory method defined by Pattern. It is shown here: Matcher matcher(CharSequence str) Here str is the character sequence that the pattern will be matched against. This is called the input sequence. CharSequence is an interface that defines a read-only set of characters. It is implemented by the String class, among others. Thus, you can pass a string to matcher( ). Matcher The Matcher class has no constructors. Instead, you create a Matcher by calling the matcher( ) factory method defined by Pattern, as just explained. Once you have created a Matcher, you will use its methods to perform various pattern matching operations. The simplest pattern matching method is matches( ), which simply determines whether the character sequence matches the pattern. It is shown here: boolean matches( ) It returns true if the sequence and the pattern match, and false otherwise. Understand that the entire sequence must match the pattern, not just a subsequence of it. To determine if a subsequence of the input sequence matches the pattern, use find( ). One version is shown here: boolean find( ) It returns true if there is a matching subsequence and false otherwise. This method can be called repeatedly, allowing it to find all matching subsequences. Each call to find( ) begins where the previous one left off. You can obtain a string containing the last matching sequence by calling group( ). One of its forms is shown here: String group( ) The matching string is returned. If no match exists, then an IllegalStateException is thrown. You can obtain the index within the input sequence of the current match by calling start( ). The index one past the end of the current match is obtained by calling end( ). These methods are shown here: int start( ) int end( ) Both throw IllegalStateException if no match exists. You can replace all occurrences of a matching sequence with another sequence by calling replaceAll( ), shown here: String replaceAll(String newStr) Here, newStr specifies the new character sequence that will replace the ones that match the pattern. The updated input sequence is returned as a string. Chapter 28 Regular Expressions and Other Packages 913 Before demonstrating Pattern and Matcher, it is necessary to explain how to construct a regular expression. Although no rule is complicated by itself, there are a large number of them, and a complete discussion is beyond the scope of this chapter. However, a few of the more commonly used constructs are described here. In general, a regular expression is comprised of normal characters, character classes (sets of characters), wildcard characters, and quantifiers. A normal character is matched as-is. Thus, if a pattern consists of "xy", then the only input sequence that will match it is "xy". Characters such as newline and tab are specified using the standard escape sequences, which begin with a \ . For example, a newline is specified by \n. In the language of regular expressions, a normal character is also called a literal. A character class is a set of characters. A character class is specified by putting the characters in the class between brackets. For example, the class [wxyz] matches w, x, y, or z. To specify an inverted set, precede the characters with a ^. For example, [^wxyz] matches any character except w, x, y, or z. You can specify a range of characters using a hyphen. For example, to specify a character class that will match the digits 1 through 9, use [1-9]. The wildcard character is the . (dot) and it matches any character. Thus, a pattern that consists of "." will match these (and other) input sequences: "A", "a", "x", and so on. A quantifier determines how many times an expression is matched. The quantifiers are shown here: + Match one or more. * Match zero or more. ? Match zero or one. For example, the pattern "x+" will match "x", "xx", and "xxx", among others. One other point: In general, if you specify an invalid expression, a PatternSyntaxException will be thrown. Demonstrating Pattern Matching The best way to understand how regular expression pattern matching operates is to work through some examples. The first, shown here, looks for a match with a literal pattern: // A simple pattern matching demo. import java.util.regex.*; class RegExpr { public static void main(String args[]) { Pattern pat; Matcher mat; boolean found; pat = Pattern.compile("Java"); mat = pat.matcher("Java"); found = mat.matches(); // check for a match Part II Regular Expression Syntax 914 PART II The Java Library System.out.println("Testing Java against Java."); if(found) System.out.println("Matches"); else System.out.println("No Match"); System.out.println(); System.out.println("Testing Java against Java 7."); mat = pat.matcher("Java 7"); // create a new matcher found = mat.matches(); // check for a match if(found) System.out.println("Matches"); else System.out.println("No Match"); } } The output from the program is shown here: Testing Java against Java. Matches Testing Java against Java 7. No Match Let’s look closely at this program. The program begins by creating the pattern that contains the sequence "Java". Next, a Matcher is created for that pattern that has the input sequence "Java". Then, the matches( ) method is called to determine if the input sequence matches the pattern. Because the sequence and the pattern are the same, matches( ) returns true. Next, a new Matcher is created with the input sequence "Java 7" and matches( ) is called again. In this case, the pattern and the input sequence differ, and no match is found. Remember, the matches( ) function returns true only when the input sequence precisely matches the pattern. It will not return true just because a subsequence matches. You can use find( ) to determine if the input sequence contains a subsequence that matches the pattern. Consider the following program: // Use find() to find a subsequence. import java.util.regex.*; class RegExpr2 { public static void main(String args[]) { Pattern pat = Pattern.compile("Java"); Matcher mat = pat.matcher("Java 7"); System.out.println("Looking for Java in Java 7."); if(mat.find()) System.out.println("subsequence found"); else System.out.println("No Match"); } } The output is shown here: Looking for Java in Java 7. subsequence found Chapter 28 Regular Expressions and Other Packages 915 In this case, find( ) finds the subsequence "Java". The find( ) method can be used to search the input sequence for repeated occurrences of the pattern because each call to find( ) picks up where the previous one left off. For example, the following program finds two occurrences of the pattern "test": class RegExpr3 { public static void main(String args[]) { Pattern pat = Pattern.compile("test"); Matcher mat = pat.matcher("test 1 2 3 test"); while(mat.find()) { System.out.println("test found at index " + mat.start()); } } } The output is shown here: test found at index 0 test found at index 11 As the output shows, two matches were found. The program uses the start( ) method to obtain the index of each match. Using Wildcards and Quantifiers Although the preceding programs show the general technique for using Pattern and Matcher, they don’t show their power. The real benefit of regular expression processing is not seen until wildcards and quantifiers are used. To begin, consider the following example that uses the + quantifier to match any arbitrarily long sequence of Ws: // Use a quantifier. import java.util.regex.*; class RegExpr4 { public static void main(String args[]) { Pattern pat = Pattern.compile("W+"); Matcher mat = pat.matcher("W WW WWW"); while(mat.find()) System.out.println("Match: " + mat.group()); } } The output from the program is shown here: Match: W Match: WW Match: WWW Part II // Use find() to find multiple subsequences. import java.util.regex.*; 916 PART II The Java Library As the output shows, the regular expression pattern "W+" matches any arbitrarily long sequence of Ws. The next program uses a wildcard to create a pattern that will match any sequence that begins with e and ends with d. To do this, it uses the dot wildcard character along with the + quantifier. // Use wildcard and quantifier. import java.util.regex.*; class RegExpr5 { public static void main(String args[]) { Pattern pat = Pattern.compile("e.+d"); Matcher mat = pat.matcher("extend cup end table"); while(mat.find()) System.out.println("Match: " + mat.group()); } } You might be surprised by the output produced by the program, which is shown here: Match: extend cup end Only one match is found, and it is the longest sequence that begins with e and ends with d. You might have expected two matches: "extend" and "end". The reason that the longer sequence is found is that by default, find( ) matches the longest sequence that fits the pattern. This is called greedy behavior. You can specify reluctant behavior by adding the ? quantifier to the pattern, as shown in this version of the program. It causes the shortest matching pattern to be obtained. // Use the ? quantifier. import java.util.regex.*; class RegExpr6 { public static void main(String args[]) { // Use reluctant matching behavior. Pattern pat = Pattern.compile("e.+?d"); Matcher mat = pat.matcher("extend cup end table"); while(mat.find()) System.out.println("Match: " + mat.group()); } } The output from the program is shown here: Match: extend Match: end As the output shows, the pattern "e.+?d" will match the shortest sequence that begins with e and ends with d. Thus, two matches are found. Chapter 28 Regular Expressions and Other Packages 917 Working with Classes of Characters // Use a character class. import java.util.regex.*; class RegExpr7 { public static void main(String args[]) { // Match lowercase words. Pattern pat = Pattern.compile("[a-z]+"); Matcher mat = pat.matcher("this is a test."); while(mat.find()) System.out.println("Match: " + mat.group()); } } The output is shown here: Match: Match: Match: Match: this is a test Using replaceAll( ) The replaceAll( ) method supplied by Matcher lets you perform powerful search and replace operations that use regular expressions. For example, the following program replaces all occurrences of sequences that begin with "Jon" with "Eric": // Use replaceAll(). import java.util.regex.*; class RegExpr8 { public static void main(String args[]) { String str = "Jon Jonathan Frank Ken Todd"; Pattern pat = Pattern.compile("Jon.*? "); Matcher mat = pat.matcher(str); System.out.println("Original sequence: " + str); str = mat.replaceAll("Eric "); System.out.println("Modified sequence: " + str); } } Part II Sometimes you will want to match any sequence that contains one or more characters, in any order, that are part of a set of characters. For example, to match whole words, you want to match any sequence of the letters of the alphabet. One of the easiest ways to do this is to use a character class, which defines a set of characters. Recall that a character class is created by putting the characters you want to match between brackets. For example, to match the lowercase characters a through z, use [a-z]. The following program demonstrates this technique: 918 PART II The Java Library The output is shown here: Original sequence: Jon Jonathan Frank Ken Todd Modified sequence: Eric Eric Frank Ken Todd Because the regular expression "Jon.*? " matches any string that begins with Jon followed by zero or more characters, ending in a space, it can be used to match and replace both Jon and Jonathan with the name Eric. Such a substitution is not possible without pattern matching capabilities. Using split( ) You can reduce an input sequence into its individual tokens by using the split( ) method defined by Pattern. One form of the split( ) method is shown here: String[ ] split(CharSequence str) It processes the input sequence passed in str, reducing it into tokens based on the delimiters specified by the pattern. For example, the following program finds tokens that are separated by spaces, commas, periods, and exclamation points: // Use split(). import java.util.regex.*; class RegExpr9 { public static void main(String args[]) { // Match lowercase words. Pattern pat = Pattern.compile("[ ,.!]"); String strs[] = pat.split("one two,alpha9 12!done."); for(int i=0; i < strs.length; i++) System.out.println("Next token: " + strs[i]); } } The output is shown here: Next Next Next Next Next token: token: token: token: token: one two alpha9 12 done As the output shows, the input sequence is reduced to its individual tokens. Notice that the delimiters are not included. Chapter 28 Regular Expressions and Other Packages 919 Two Pattern-Matching Options Although the pattern-matching techniques described in the foregoing offer the greatest flexibility and power, there are two alternatives which you might find useful in some circumstances. If you only need to perform a one-time pattern match, you can use the matches( ) method defined by Pattern. It is shown here: It returns true if pattern matches str and false otherwise. This method automatically compiles pattern and then looks for a match. If you will be using the same pattern repeatedly, then using matches( ) is less efficient than compiling the pattern and using the pattern-matching methods defined by Matcher, as described previously. You can also perform a pattern match by using the matches( ) method implemented by String. It is shown here: boolean matches(String pattern) If the invoking string matches the regular expression in pattern, then matches( ) returns true. Otherwise, it returns false. Exploring Regular Expressions The overview of regular expressions presented in this section only hints at their power. Since text parsing, manipulation, and tokenization are a large part of programming, you will likely find Java’s regular expression subsystem a powerful tool that you can use to your advantage. It is, therefore, wise to explore the capabilities of regular expressions. Experiment with several different types of patterns and input sequences. Once you understand how regular expression pattern matching works, you will find it useful in many of your programming endeavors. Reflection Reflection is the ability of software to analyze itself. This is provided by the java.lang.reflect package and elements in Class. Reflection is an important capability, especially when using components called Java Beans. It allows you to analyze a software component and describe its capabilities dynamically, at run time rather than at compile time. For example, by using reflection, you can determine what methods, constructors, and fields a class supports. Reflection was introduced in Chapter 12. It is examined further here. The package java.lang.reflect includes several interfaces. Of special interest is Member, which defines methods that allow you to get information about a field, constructor, or method of a class. There are also eight classes in this package. These are listed in Table 28-2. The following application illustrates a simple use of the Java reflection capabilities. It prints the constructors, fields, and methods of the class java.awt.Dimension. The program begins by using the forName( ) method of Class to get a class object for java.awt.Dimension. Part II static boolean matches(String pattern, CharSequence str) 920 PART II The Java Library Class Primary Function AccessibleObject Allows you to bypass the default access control checks. Array Allows you to dynamically create and manipulate arrays. Constructor Provides information about a constructor. Field Provides information about a field. Method Provides information about a method. Modifier Provides information about class and member access modifiers. Proxy Supports dynamic proxy classes. ReflectPermission Allows reflection of private or protected members of a class. Table 28-2 Classes Defined in java.lang.reflect Once this is obtained, getConstructors( ), getFields( ), and getMethods( ) are used to analyze this class object. They return arrays of Constructor, Field, and Method objects that provide the information about the object. The Constructor, Field, and Method classes define several methods that can be used to obtain information about an object. You will want to explore these on your own. However, each supports the toString( ) method. Therefore, using Constructor, Field, and Method objects as arguments to the println( ) method is straightforward, as shown in the program. // Demonstrate reflection. import java.lang.reflect.*; public class ReflectionDemo1 { public static void main(String args[]) { try { Class c = Class.forName("java.awt.Dimension"); System.out.println("Constructors:"); Constructor constructors[] = c.getConstructors(); for(int i = 0; i < constructors.length; i++) { System.out.println(" " + constructors[i]); } System.out.println("Fields:"); Field fields[] = c.getFields(); for(int i = 0; i < fields.length; i++) { System.out.println(" " + fields[i]); } System.out.println("Methods:"); Method methods[] = c.getMethods(); for(int i = 0; i < methods.length; i++) { System.out.println(" " + methods[i]); } } catch(Exception e) { System.out.println("Exception: " + e); } } } Chapter 28 Regular Expressions and Other Packages 921 Constructors: public java.awt.Dimension(int,int) public java.awt.Dimension() public java.awt.Dimension(java.awt.Dimension) Fields: public int java.awt.Dimension.width public int java.awt.Dimension.height Methods: public int java.awt.Dimension.hashCode() public boolean java.awt.Dimension.equals(java.lang.Object) public java.lang.String java.awt.Dimension.toString() public java.awt.Dimension java.awt.Dimension.getSize() public void java.awt.Dimension.setSize(double,double) public void java.awt.Dimension.setSize(java.awt.Dimension) public void java.awt.Dimension.setSize(int,int) public double java.awt.Dimension.getHeight() public double java.awt.Dimension.getWidth() public java.lang.Object java.awt.geom.Dimension2D.clone() public void java.awt.geom. Dimension2D.setSize(java.awt.geom.Dimension2D) public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public final void java.lang.Object.wait() throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll() The next example uses Java’s reflection capabilities to obtain the public methods of a class. The program begins by instantiating class A. The getClass( ) method is applied to this object reference, and it returns the Class object for class A. The getDeclaredMethods( ) method returns an array of Method objects that describe only the methods declared by this class. Methods inherited from superclasses such as Object are not included. Each element of the methods array is then processed. The getModifiers( ) method returns an int containing flags that describe which modifiers apply for this element. The Modifier class provides a set of isX methods, shown in Table 28-3, that can be used to examine this value. For example, the static method isPublic( ) returns true if its argument includes the public modifier. Otherwise, it returns false. In the following program, if the method supports public access, its name is obtained by the getName( ) method and is then printed. // Show public methods. import java.lang.reflect.*; public class ReflectionDemo2 { public static void main(String args[]) { Part II Here is the output from this program. (The precise order may differ slightly from that shown.) 922 PART II The Java Library try { A a = new A(); Class c = a.getClass(); System.out.println("Public Methods:"); Method methods[] = c.getDeclaredMethods(); for(int i = 0; i < methods.length; i++) { int modifiers = methods[i].getModifiers(); if(Modifier.isPublic(modifiers)) { System.out.println(" " + methods[i].getName()); } } } catch(Exception e) { System.out.println("Exception: " + e); } } } class A { public void a1() { } public void a2() { } protected void a3() { } private void a4() { } } Here is the output from this program: Public Methods: a1 a2 Beginning with JDK 7, Modifier also includes a set of static methods that return the type of modifiers that can be applied to a specific type of program element. These methods are static int classModifiers( ) static int constructorModifiers( ) static int fieldModifiers( ) static int interfaceModifiers( ) static int methodModifiers( ) For example, methodModifiers( ) returns the modifiers that can be applied to a method. Each method returns flags, packed into an int, that indicate which modifiers are legal. The modifier values are defined by constants in Modifier, which include PROTECTED, PUBLIC, PRIVATE, STATIC, FINAL, and so on. Regular Expressions and Other Packages Method Description static boolean isAbstract(int val) Returns true if val has the abstract flag set and false otherwise. static boolean isFinal(int val) Returns true if val has the final flag set and false otherwise. static boolean isInterface(int val) Returns true if val has the interface flag set and false otherwise. static boolean isNative(int val) Returns true if val has the native flag set and false otherwise. static boolean isPrivate(int val) Returns true if val has the private flag set and false otherwise. static boolean isProtected(int val) Returns true if val has the protected flag set and false otherwise. static boolean isPublic(int val) Returns true if val has the public flag set and false otherwise. static boolean isStatic(int val) Returns true if val has the static flag set and false otherwise. static boolean isStrict(int val) Returns true if val has the strict flag set and false otherwise. static boolean isSynchronized(int val) Returns true if val has the synchronized flag set and false otherwise. static boolean isTransient(int val) Returns true if val has the transient flag set and false otherwise. static boolean isVolatile(int val) Returns true if val has the volatile flag set and false otherwise. 923 Table 28-3 The “is” Methods Defined by Modifier That Determine Modifiers Remote Method Invocation (RMI) Remote Method Invocation (RMI) allows a Java object that executes on one machine to invoke a method of a Java object that executes on another machine. This is an important feature, because it allows you to build distributed applications. While a complete discussion of RMI is outside the scope of this book, the following simplified example describes the basic principles involved. A Simple Client/Server Application Using RMI This section provides step-by-step directions for building a simple client/server application by using RMI. The server receives a request from a client, processes it, and returns a result. In this example, the request specifies two numbers. The server adds these together and returns the sum. Part II Chapter 28 924 PART II The Java Library Step One: Enter and Compile the Source Code This application uses four source files. The first file, AddServerIntf.java, defines the remote interface that is provided by the server. It contains one method that accepts two double arguments and returns their sum. All remote interfaces must extend the Remote interface, which is part of java.rmi. Remote defines no members. Its purpose is simply to indicate that an interface uses remote methods. All remote methods can throw a RemoteException. import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1, double d2) throws RemoteException; } The second source file, AddServerImpl.java, implements the remote interface. The implementation of the add( ) method is straightforward. Remote objects typically extend UnicastRemoteObject, which provides functionality that is needed to make objects available from remote machines. import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl() throws RemoteException { } public double add(double d1, double d2) throws RemoteException { return d1 + d2; } } The third source file, AddServer.java, contains the main program for the server machine. Its primary function is to update the RMI registry on that machine. This is done by using the rebind( ) method of the Naming class (found in java.rmi). That method associates a name with an object reference. The first argument to the rebind( ) method is a string that names the server as "AddServer". Its second argument is a reference to an instance of AddServerImpl. import java.net.*; import java.rmi.*; public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl = new AddServerImpl(); Naming.rebind("AddServer", addServerImpl); } catch(Exception e) { System.out.println("Exception: " + e); } } } Regular Expressions and Other Packages 925 The fourth source file, AddClient.java, implements the client side of this distributed application. AddClient.java requires three command-line arguments. The first is the IP address or name of the server machine. The second and third arguments are the two numbers that are to be summed. The application begins by forming a string that follows the URL syntax. This URL uses the rmi protocol. The string includes the IP address or name of the server and the string "AddServer". The program then invokes the lookup( ) method of the Naming class. This method accepts one argument, the rmi URL, and returns a reference to an object of type AddServerIntf. All remote method invocations can then be directed to this object. The program continues by displaying its arguments and then invokes the remote add( ) method. The sum is returned from this method and is then printed. import java.rmi.*; public class AddClient { public static void main(String args[]) { try { String addServerURL = "rmi://" + args[0] + "/AddServer"; AddServerIntf addServerIntf = (AddServerIntf)Naming.lookup(addServerURL); System.out.println("The first number is: " + args[1]); double d1 = Double.valueOf(args[1]).doubleValue(); System.out.println("The second number is: " + args[2]); double d2 = Double.valueOf(args[2]).doubleValue(); System.out.println("The sum is: " + addServerIntf.add(d1, d2)); } catch(Exception e) { System.out.println("Exception: " + e); } } } After you enter all the code, use javac to compile the four source files that you created. Step Two: Manually Generate a Stub if Required In the context of RMI, a stub is a Java object that resides on the client machine. Its function is to present the same interfaces as the remote server. Remote method calls initiated by the client are actually directed to the stub. The stub works with the other parts of the RMI system to formulate a request that is sent to the remote machine. A remote method may accept arguments that are simple types or objects. In the latter case, the object may have references to other objects. All of this information must be sent to the remote machine. That is, an object passed as an argument to a remote method call must be serialized and sent to the remote machine. Recall from Chapter 19 that the serialization facilities also recursively process all referenced objects. If a response must be returned to the client, the process works in reverse. Note that the serialization and deserialization facilities are also used if objects are returned to a client. Prior to Java 5, stubs needed to be built manually by using rmic. This step is not required for modern versions of Java. However, if you are working in a legacy environment, then you can use the rmic compiler, as shown here, to build a stub: rmic AddServerImpl Part II Chapter 28 926 PART II The Java Library This command generates the file AddServerImpl_Stub.class. When using rmic, be sure that CLASSPATH is set to include the current directory. Step Three: Install Files on the Client and Server Machines Copy AddClient.class, AddServerImpl_Stub.class (if needed), and AddServerIntf.class to a directory on the client machine. Copy AddServerIntf.class, AddServerImpl.class, AddServerImpl_Stub.class (if needed), and AddServer.class to a directory on the server machine. NOTE RMI has techniques for dynamic class loading, but they are not used by the example at hand. Instead, all of the files that are used by the client and server applications must be installed manually on those machines. Step Four: Start the RMI Registry on the Server Machine The JDK provides a program called rmiregistry, which executes on the server machine. It maps names to object references. First, check that the CLASSPATH environment variable includes the directory in which your files are located. Then, start the RMI Registry from the command line, as shown here: start rmiregistry When this command returns, you should see that a new window has been created. You need to leave this window open until you are done experimenting with the RMI example. Step Five: Start the Server The server code is started from the command line, as shown here: java AddServer Recall that the AddServer code instantiates AddServerImpl and registers that object with the name "AddServer". Step Six: Start the Client The AddClient software requires three arguments: the name or IP address of the server machine and the two numbers that are to be summed together. You may invoke it from the command line by using one of the two formats shown here: java AddClient server1 8 9 java AddClient 11.12.13.14 8 9 In the first line, the name of the server is provided. The second line uses its IP address (11.12.13.14). You can try this example without actually having a remote server. To do so, simply install all of the programs on the same machine, start rmiregistry, start AddServer, and then execute AddClient using this command line: java AddClient 127.0.0.1 8 9 Here, the address 127.0.0.1 is the “loop back” address for the local machine. Using this address allows you to exercise the entire RMI mechanism without actually having to install the server on a remote computer. In either case, sample output from this program is shown here: Chapter 28 Regular Expressions and Other Packages 927 The first number is: 8 The second number is: 9 The sum is: 17.0 NOTE When working with RMI in the real world, it may be necessary for the server to install a security manager. The package java.text allows you to format, search, and manipulate text. Chapter 33 illustrates its NumberFormat class, which is used to format numeric data. This section examines two more of its most commonly used classes: those that format date and time information. DateFormat Class DateFormat is an abstract class that provides the ability to format and parse dates and times. The getDateInstance( ) method returns an instance of DateFormat that can format date information. It is available in these forms: static final DateFormat getDateInstance( ) static final DateFormat getDateInstance(int style) static final DateFormat getDateInstance(int style, Locale locale) The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the date to be presented. The argument locale is one of the static references defined by Locale (refer to Chapter 18 for details). If the style and/or locale is not specified, defaults are used. One of the most commonly used methods in this class is format( ). It has several overloaded forms, one of which is shown here: final String format(Date d) The argument is a Date object that is to be displayed. The method returns a string containing the formatted information. The following listing illustrates how to format date information. It begins by creating a Date object. This captures the current date and time information. Then it outputs the date information by using different styles and locales. // Demonstrate date formats. import java.text.*; import java.util.*; public class DateFormatDemo { public static void main(String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA); System.out.println("Korea: " + df.format(date)); Part II Text Formatting 928 PART II The Java Library df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("United States: " + df.format(date)); } } Sample output from this program is shown here: Japan: Korea: United United 11/01/01 2011. 1. 1 Kingdom: 01 January 2011 States: Saturday, January 1, 2011 The getTimeInstance( ) method returns an instance of DateFormat that can format time information. It is available in these versions: static final DateFormat getTimeInstance( ) static final DateFormat getTimeInstance(int style) static final DateFormat getTimeInstance(int style, Locale locale) The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the time to be presented. The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used. The following listing illustrates how to format time information. It begins by creating a Date object. This captures the current date and time information. Then it outputs the time information by using different styles and locales. // Demonstrate time formats. import java.text.*; import java.util.*; public class TimeFormatDemo { public static void main(String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA); System.out.println("Canada: " + df.format(date)); } } Sample output from this program is shown here: Japan: 20:25 United Kingdom: 20:25:14 CDT Canada: 8:25:14 o'clock PM CDT Chapter 28 Regular Expressions and Other Packages 929 The DateFormat class also has a getDateTimeInstance( ) method that can format both date and time information. You may wish to experiment with it on your own. SimpleDateFormat Class SimpleDateFormat is a concrete subclass of DateFormat. It allows you to define your own formatting patterns that are used to display date and time information. One of its constructors is shown here: The argument formatString describes how date and time information is displayed. An example of its use is given here: SimpleDateFormat sdf = SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz"); The symbols used in the formatting string determine the information that is displayed. Table 28-4 lists these symbols and gives a description of each. Symbol Description a AM or PM d Day of month (1–31) h Hour in AM/PM (1–12) k Hour in day (1–24) m Minute in hour (0–59) s Second in minute (0–59) u Day of week, with Monday being 1 w Week of year (1–52) y Year z Time zone D Day of year (1–366) E Day of week (for example, Thursday) F Day of week in month G Era (for example, AD or BC) H Hour in day (0–23) K Hour in AM/PM (0–11) M Month S Millisecond in second W Week of month (1–5) X Time zone in ISO 8601 format Y Week year Z Time zone in RFC 822 format Table 28-4 Formatting String Symbols for SimpleDateFormat Part II SimpleDateFormat(String formatString) 930 PART II The Java Library In most cases, the number of times a symbol is repeated determines how that data is presented. Text information is displayed in an abbreviated form if the pattern letter is repeated less than four times. Otherwise, the unabbreviated form is used. For example, a zzzz pattern can display Pacific Daylight Time, and a zzz pattern can display PDT. For numbers, the number of times a pattern letter is repeated determines how many digits are presented. For example, hh:mm:ss can present 01:51:15, but h:m:s displays the same time value as 1:51:15. Finally, M or MM causes the month to be displayed as one or two digits. However, three or more repetitions of M cause the month to be displayed as a text string. The following program shows how this class is used: // Demonstrate SimpleDateFormat. import java.text.*; import java.util.*; public class SimpleDateFormatDemo { public static void main(String args[]) { Date date = new Date(); SimpleDateFormat sdf; sdf = new SimpleDateFormat("hh:mm:ss"); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz"); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("E MMM dd yyyy"); System.out.println(sdf.format(date)); } } Sample output from this program is shown here: 12:46:49 01 Jan 2011 12:46:49 CST Sat Jan 01 2011 PART III CHAPTER 29 Java Beans CHAPTER 30 Introducing Swing CHAPTER 31 Exploring Swing CHAPTER 32 Servlets Software Development Using Java This page intentionally left blank CHAPTER 29 Java Beans This chapter provides an overview of Java Beans. Beans are important because they allow you to build complex systems from software components. These components may be provided by you or supplied by one or more different vendors. Java Beans defines an architecture that specifies how these building blocks can operate together. To better understand the value of Beans, consider the following. Hardware designers have a wide variety of components that can be integrated together to construct a system. Resistors, capacitors, and inductors are examples of simple building blocks. Integrated circuits provide more advanced functionality. All of these different parts can be reused. It is not necessary or possible to rebuild these capabilities each time a new system is needed. Also, the same pieces can be used in different types of circuits. This is possible because the behavior of these components is understood and documented. The software industry has also been seeking the benefits of reusability and interoperability of a component-based approach. To realize these benefits, a component architecture is needed that allows programs to be assembled from software building blocks, perhaps provided by different vendors. It must also be possible for a designer to select a component, understand its capabilities, and incorporate it into an application. When a new version of a component becomes available, it should be easy to incorporate this functionality into existing code. Fortunately, Java Beans provides just such an architecture. What Is a Java Bean? A Java Bean is a software component that has been designed to be reusable in a variety of different environments. There is no restriction on the capability of a Bean. It may perform a simple function, such as obtaining an inventory value, or a complex function, such as forecasting the performance of a stock portfolio. A Bean may be visible to an end user. One example of this is a button on a graphical user interface. A Bean may also be invisible to a user. Software to decode a stream of multimedia information in real time is an example of this type of building block. Finally, a Bean may be designed to work autonomously on a user’s workstation or to work in cooperation with a set of other distributed components. 933 934 PART III Software Development Using Java Software to generate a pie chart from a set of data points is an example of a Bean that can execute locally. However, a Bean that provides real-time price information from a stock or commodities exchange would need to work in cooperation with other distributed software to obtain its data. Advantages of Java Beans The following list enumerates some of the benefits that Java Bean technology provides for a component developer: • A Bean obtains all the benefits of Java’s “write-once, run-anywhere” paradigm. • The properties, events, and methods of a Bean that are exposed to another application can be controlled. • Auxiliary software can be provided to help configure a Bean. This software is only needed when the design-time parameters for that component are being set. It does not need to be included in the run-time environment. • The configuration settings of a Bean can be saved in persistent storage and restored at a later time. • A Bean may register to receive events from other objects and can generate events that are sent to other objects. Introspection At the core of Java Beans is introspection. This is the process of analyzing a Bean to determine its capabilities. This is an essential feature of the Java Beans API because it allows another application, such as a design tool, to obtain information about a component. Without introspection, the Java Beans technology could not operate. There are two ways in which the developer of a Bean can indicate which of its properties, events, and methods should be exposed. With the first method, simple naming conventions are used. These allow the introspection mechanisms to infer information about a Bean. In the second way, an additional class that extends the BeanInfo interface is provided that explicitly supplies this information. Both approaches are examined here. Design Patterns for Properties A property is a subset of a Bean’s state. The values assigned to the properties determine the behavior and appearance of that component. A property is set through a setter method. A property is obtained by a getter method. There are two types of properties: simple and indexed. Simple Properties A simple property has a single value. It can be identified by the following design patterns, where N is the name of the property and T is its type: public T getN( ) public void setN(T arg) A read/write property has both of these methods to access its values. A read-only property has only a get method. A write-only property has only a set method. Chapter 29 Java Beans 935 Here are three read/write simple properties along with their getter and setter methods: private double depth, height, width; public double getDepth( ) { return depth; } public void setDepth(double d) { depth = d; } public double getWidth( ) { return width; } public void setWidth(double w) { width = w; } Indexed Properties An indexed property consists of multiple values. It can be identified by the following design patterns, where N is the name of the property and T is its type: public T getN(int index); public void setN(int index, T value); public T[ ] getN( ); public void setN(T values[ ]); Here is an indexed property called data along with its getter and setter methods: private double data[ ]; public double getData(int index) { return data[index]; } public void setData(int index, double value) { data[index] = value; } public double[ ] getData( ) { return data; } public void setData(double[ ] values) { data = new double[values.length]; System.arraycopy(values, 0, data, 0, values.length); } Part III public double getHeight( ) { return height; } public void setHeight(double h) { height = h; } 936 PART III Software Development Using Java Design Patterns for Events Beans use the delegation event model that was discussed earlier in this book. Beans can generate events and send them to other objects. These can be identified by the following design patterns, where T is the type of the event: public void addTListener(TListener eventListener) public void addTListener(TListener eventListener) throws java.util.TooManyListenersException public void removeTListener(TListener eventListener) These methods are used to add or remove a listener for the specified event. The version of AddTListener( ) that does not throw an exception can be used to multicast an event, which means that more than one listener can register for the event notification. The version that throws TooManyListenersException unicasts the event, which means that the number of listeners is restricted to one. In either case, removeTListener( ) is used to remove the listener. For example, assuming an event interface type called TemperatureListener, a Bean that monitors temperature might supply the following methods: public void addTemperatureListener(TemperatureListener tl) { ... } public void removeTemperatureListener(TemperatureListener tl) { ... } Methods and Design Patterns Design patterns are not used for naming nonproperty methods. The introspection mechanism finds all of the public methods of a Bean. Protected and private methods are not presented. Using the BeanInfo Interface As the preceding discussion shows, design patterns implicitly determine what information is available to the user of a Bean. The BeanInfo interface enables you to explicitly control what information is available. The BeanInfo interface defines several methods, including these: PropertyDescriptor[ ] getPropertyDescriptors( ) EventSetDescriptor[ ] getEventSetDescriptors( ) MethodDescriptor[ ] getMethodDescriptors( ) They return arrays of objects that provide information about the properties, events, and methods of a Bean. The classes PropertyDescriptor, EventSetDescriptor, and MethodDescriptor are defined within the java.beans package, and they describe the indicated elements. By implementing these methods, a developer can designate exactly what is presented to a user, bypassing introspection based on design patterns. When creating a class that implements BeanInfo, you must call that class bnameBeanInfo, where bname is the name of the Bean. For example, if the Bean is called MyBean, then the information class must be called MyBeanBeanInfo. Chapter 29 Java Beans 937 To simplify the use of BeanInfo, JavaBeans supplies the SimpleBeanInfo class. It provides default implementations of the BeanInfo interface, including the three methods just shown. You can extend this class and override one or more of the methods to explicitly control what aspects of a Bean are exposed. If you don’t override a method, then design-pattern introspection will be used. For example, if you don’t override getPropertyDescriptors( ), then design patterns are used to discover a Bean’s properties. You will see SimpleBeanInfo in action later in this chapter. A Bean that has a bound property generates an event when the property is changed. The event is of type PropertyChangeEvent and is sent to objects that previously registered an interest in receiving such notifications. A class that handles this event must implement the PropertyChangeListener interface. A Bean that has a constrained property generates an event when an attempt is made to change its value. It also generates an event of type PropertyChangeEvent. It too is sent to objects that previously registered an interest in receiving such notifications. However, those other objects have the ability to veto the proposed change by throwing a PropertyVetoException. This capability allows a Bean to operate differently according to its run-time environment. A class that handles this event must implement the VetoableChangeListener interface. Persistence Persistence is the ability to save the current state of a Bean, including the values of a Bean’s properties and instance variables, to nonvolatile storage and to retrieve them at a later time. The object serialization capabilities provided by the Java class libraries are used to provide persistence for Beans. The easiest way to serialize a Bean is to have it implement the java.io.Serializable interface, which is simply a marker interface. Implementing java.io.Serializable makes serialization automatic. Your Bean need take no other action. Automatic serialization can also be inherited. Therefore, if any superclass of a Bean implements java.io.Serializable, then automatic serialization is obtained. There is one important restriction: any class that implements java.io.Serializable must supply a parameterless constructor. When using automatic serialization, you can selectively prevent a field from being saved through the use of the transient keyword. Thus, data members of a Bean specified as transient will not be serialized. If a Bean does not implement java.io.Serializable, you must provide serialization yourself, such as by implementing java.io.Externalizable. Otherwise, containers cannot save the configuration of your component. Customizers A Bean developer can provide a customizer that helps another developer configure the Bean. A customizer can provide a step-by-step guide through the process that must be followed to use the component in a specific context. Online documentation can also be provided. A Bean developer has great flexibility to develop a customizer that can differentiate his or her product in the marketplace. Part III Bound and Constrained Properties 938 PART III Software Development Using Java Interface Description AppletInitializer Methods in this interface are used to initialize Beans that are also applets. BeanInfo This interface allows a designer to specify information about the properties, events, and methods of a Bean. Customizer This interface allows a designer to provide a graphical user interface through which a Bean may be configured. DesignMode Methods in this interface determine if a Bean is executing in design mode. ExceptionListener A method in this interface is invoked when an exception has occurred. PropertyChangeListener A method in this interface is invoked when a bound property is changed. PropertyEditor Objects that implement this interface allow designers to change and display property values. VetoableChangeListener A method in this interface is invoked when a constrained property is changed. Visibility Methods in this interface allow a Bean to execute in environments where a graphical user interface is not available. Table 29-1 The Interfaces in java.beans The Java Beans API The Java Beans functionality is provided by a set of classes and interfaces in the java.beans package. This section provides a brief overview of its contents. Table 29-1 lists the interfaces in java.beans and provides a brief description of their functionality. Table 29-2 lists the classes in java.beans. Class Description BeanDescriptor This class provides information about a Bean. It also allows you to associate a customizer with a Bean. Beans This class is used to obtain information about a Bean. DefaultPersistenceDelegate A concrete subclass of PersistenceDelegate. Encoder Encodes the state of a set of Beans. Can be used to write this information to a stream. EventHandler Supports dynamic event listener creation. EventSetDescriptor Instances of this class describe an event that can be generated by a Bean. Expression Encapsulates a call to a method that returns a result. FeatureDescriptor This is the superclass of the PropertyDescriptor, EventSetDescriptor, and MethodDescriptor classes. Table 29-2 The Classes in java.beans Java Beans Class Description IndexedPropertyChangeEvent A subclass of PropertyChangeEvent that represents a change to an indexed property. IndexedPropertyDescriptor Instances of this class describe an indexed property of a Bean. IntrospectionException An exception of this type is generated if a problem occurs when analyzing a Bean. Introspector This class analyzes a Bean and constructs a BeanInfo object that describes the component. MethodDescriptor Instances of this class describe a method of a Bean. ParameterDescriptor Instances of this class describe a method parameter. 939 PersistenceDelegate Handles the state information of an object. PropertyChangeEvent This event is generated when bound or constrained properties are changed. It is sent to objects that registered an interest in these events and that implement either the PropertyChangeListener or VetoableChangeListener interfaces. PropertyChangeListenerProxy Extends EventListenerProxy and implements PropertyChangeListener. PropertyChangeSupport Beans that support bound properties can use this class to notify PropertyChangeListener objects. PropertyDescriptor Instances of this class describe a property of a Bean. PropertyEditorManager This class locates a PropertyEditor object for a given type. PropertyEditorSupport This class provides functionality that can be used when writing property editors. PropertyVetoException An exception of this type is generated if a change to a constrained property is vetoed. SimpleBeanInfo This class provides functionality that can be used when writing BeanInfo classes. Statement Encapsulates a call to a method. VetoableChangeListenerProxy Extends EventListenerProxy and implements VetoableChangeListener. VetoableChangeSupport Beans that support constrained properties can use this class to notify VetoableChangeListener objects. XMLDecoder Used to read a Bean from an XML document. XMLEncoder Used to write a Bean to an XML document. Table 29-2 The Classes in java.beans (continued) Although it is beyond the scope of this chapter to discuss all of the classes, four are of particular interest: Introspector, PropertyDescriptor, EventSetDescriptor, and MethodDescriptor. Each is briefly examined here. Part III Chapter 29 940 PART III Software Development Using Java Introspector The Introspector class provides several static methods that support introspection. Of most interest is getBeanInfo( ). This method returns a BeanInfo object that can be used to obtain information about the Bean. The getBeanInfo( ) method has several forms, including the one shown here: static BeanInfo getBeanInfo(Class bean) throws IntrospectionException The returned object contains information about the Bean specified by bean. PropertyDescriptor The PropertyDescriptor class describes a Bean property. It supports several methods that manage and describe properties. For example, you can determine if a property is bound by calling isBound( ). To determine if a property is constrained, call isConstrained( ). You can obtain the name of a property by calling getName( ). EventSetDescriptor The EventSetDescriptor class represents a Bean event. It supports several methods that obtain the methods that a Bean uses to add or remove event listeners, and to otherwise manage events. For example, to obtain the method used to add listeners, call getAddListenerMethod( ). To obtain the method used to remove listeners, call getRemoveListenerMethod( ). To obtain the type of a listener, call getListenerType( ). You can obtain the name of an event by calling getName( ). MethodDescriptor The MethodDescriptor class represents a Bean method. To obtain the name of the method, call getName( ). You can obtain information about the method by calling getMethod( ), shown here: Method getMethod( ) An object of type Method that describes the method is returned. A Bean Example This chapter concludes with an example that illustrates various aspects of Bean programming, including introspection and using a BeanInfo class. It also makes use of the Introspector, PropertyDescriptor, and EventSetDescriptor classes. The example uses three classes. The first is a Bean called Colors, shown here: // A simple Bean. import java.awt.*; import java.awt.event.*; import java.io.Serializable; public class Colors extends Canvas implements Serializable { transient private Color color; // not persistent private boolean rectangular; // is persistent Chapter 29 Java Beans 941 public Colors() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { change(); } }); rectangular = false; setSize(200, 100); change(); } public boolean getRectangular() { return rectangular; } public void change() { color = randomColor(); repaint(); } private Color randomColor() { int r = (int)(255*Math.random()); int g = (int)(255*Math.random()); int b = (int)(255*Math.random()); return new Color(r, g, b); } public void paint(Graphics g) { Dimension d = getSize(); int h = d.height; int w = d.width; g.setColor(color); if(rectangular) { g.fillRect(0, 0, w-1, h-1); } else { g.fillOval(0, 0, w-1, h-1); } } } The Colors Bean displays a colored object within a frame. The color of the component is determined by the private Color variable color, and its shape is determined by the private boolean variable rectangular. The constructor defines an anonymous inner class that extends MouseAdapter and overrides its mousePressed( ) method. The change( ) method is invoked in response to mouse presses. It selects a random color and then repaints the component. The getRectangular( ) and setRectangular( ) methods provide access to the one property of this Bean. The change( ) method calls randomColor( ) to choose a color and then calls Part III public void setRectangular(boolean flag) { this.rectangular = flag; repaint(); } 942 PART III Software Development Using Java repaint( ) to make the change visible. Notice that the paint( ) method uses the rectangular and color variables to determine how to present the Bean. The next class is ColorsBeanInfo. It is a subclass of SimpleBeanInfo that provides explicit information about Colors. It overrides getPropertyDescriptors( ) in order to designate which properties are presented to a Bean user. In this case, the only property exposed is rectangular. The method creates and returns a PropertyDescriptor object for the rectangular property. The PropertyDescriptor constructor that is used is shown here: PropertyDescriptor(String property, Class beanCls) throws IntrospectionException Here, the first argument is the name of the property, and the second argument is the class of the Bean. // A Bean information class. import java.beans.*; public class ColorsBeanInfo extends SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() { try { PropertyDescriptor rectangular = new PropertyDescriptor("rectangular", Colors.class); PropertyDescriptor pd[] = {rectangular}; return pd; } catch(Exception e) { System.out.println("Exception caught. " + e); } return null; } } The final class is called IntrospectorDemo. It uses introspection to display the properties and events that are available within the Colors Bean. // Show properties and events. import java.awt.*; import java.beans.*; public class IntrospectorDemo { public static void main(String args[]) { try { Class c = Class.forName("Colors"); BeanInfo beanInfo = Introspector.getBeanInfo(c); System.out.println("Properties:"); PropertyDescriptor propertyDescriptor[] = beanInfo.getPropertyDescriptors(); for(int i = 0; i < propertyDescriptor.length; i++) { System.out.println("\t" + propertyDescriptor[i].getName()); } Chapter 29 Java Beans 943 System.out.println("Events:"); EventSetDescriptor eventSetDescriptor[] = beanInfo.getEventSetDescriptors(); for(int i = 0; i < eventSetDescriptor.length; i++) { System.out.println("\t" + eventSetDescriptor[i].getName()); } } catch(Exception e) { System.out.println("Exception caught. " + e); } } } Properties: rectangular Events: mouseWheel mouse mouseMotion component hierarchyBounds focus hierarchy propertyChange inputMethod key Notice two things in the output. First, because ColorsBeanInfo overrides getPropertyDescriptors( ) such that the only property returned is rectangular, only the rectangular property is displayed. However, because getEventSetDescriptors( ) is not overridden by ColorsBeanInfo, design-pattern introspection is used, and all events are found, including those in Colors’ superclass, Canvas. Remember, if you don’t override one of the “get” methods defined by SimpleBeanInfo, then the default, design-pattern introspection is used. To observe the difference that ColorsBeanInfo makes, erase its class file and then run IntrospectorDemo again. This time it will report more properties. Part III The output from this program is the following: This page intentionally left blank CHAPTER 30 Introducing Swing In Part II, you saw how to build user interfaces with the AWT classes. Although the AWT is still a crucial part of Java, its component set is no longer widely used to create graphical user interfaces. Today, most programmers use Swing for this purpose. Swing is a set of classes that provides more powerful and flexible GUI components than does the AWT. Simply put, Swing provides the look and feel of the modern Java GUI. Coverage of Swing is divided between two chapters. This chapter introduces Swing. It begins by describing Swing’s core concepts. It then shows the general form of a Swing program, including both applications and applets. It concludes by explaining how painting is accomplished in Swing. The following chapter presents several commonly used Swing components. It is important to understand that the number of classes and interfaces in the Swing packages is quite large, and they can’t all be covered in this book. (In fact, full coverage of Swing requires an entire book of its own.) However, these two chapters will give you a basic understanding of this important topic. NOTE For a comprehensive introduction to Swing, see my book Swing: A Beginner's Guide published by McGraw-Hill Professional (2007). The Origins of Swing Swing did not exist in the early days of Java. Rather, it was a response to deficiencies present in Java’s original GUI subsystem: the Abstract Window Toolkit. The AWT defines a basic set of controls, windows, and dialog boxes that support a usable, but limited graphical interface. One reason for the limited nature of the AWT is that it translates its various visual components into their corresponding, platform-specific equivalents, or peers. This means that the look and feel of a component is defined by the platform, not by Java. Because the AWT components use native code resources, they are referred to as heavyweight. The use of native peers led to several problems. First, because of variations between operating systems, a component might look, or even act, differently on different platforms. This potential variability threatened the overarching philosophy of Java: write once, run 945 946 PART III Software Development Using Java anywhere. Second, the look and feel of each component was fixed (because it is defined by the platform) and could not be (easily) changed. Third, the use of heavyweight components caused some frustrating restrictions. For example, a heavyweight component was always rectangular and opaque. Not long after Java’s original release, it became apparent that the limitations and restrictions present in the AWT were sufficiently serious that a better approach was needed. The solution was Swing. Introduced in 1997, Swing was included as part of the Java Foundation Classes (JFC). Swing was initially available for use with Java 1.1 as a separate library. However, beginning with Java 1.2, Swing (and the rest of the JFC) was fully integrated into Java. Swing Is Built on the AWT Before moving on, it is necessary to make one important point: although Swing eliminates a number of the limitations inherent in the AWT, Swing does not replace it. Instead, Swing is built on the foundation of the AWT. This is why the AWT is still a crucial part of Java. Swing also uses the same event handling mechanism as the AWT. Therefore, a basic understanding of the AWT and of event handling is required to use Swing. (The AWT is covered in Chapters 24 and 25. Event handling is described in Chapter 23.) Two Key Swing Features As just explained, Swing was created to address the limitations present in the AWT. It does this through two key features: lightweight components and a pluggable look and feel. Together they provide an elegant, yet easy-to-use solution to the problems of the AWT. More than anything else, it is these two features that define the essence of Swing. Each is examined here. Swing Components Are Lightweight With very few exceptions, Swing components are lightweight. This means that they are written entirely in Java and do not map directly to platform-specific peers. Thus, lightweight components are more efficient and more flexible. Furthermore, because lightweight components do not translate into native peers, the look and feel of each component is determined by Swing, not by the underlying operating system. This means that each component will work in a consistent manner across all platforms. Swing Supports a Pluggable Look and Feel Swing supports a pluggable look and feel (PLAF). Because each Swing component is rendered by Java code rather than by native peers, the look and feel of a component is under the control of Swing. This fact means that it is possible to separate the look and feel of a component from the logic of the component, and this is what Swing does. Separating out the look and feel provides a significant advantage: it becomes possible to change the way that a component is rendered without affecting any of its other aspects. In other words, it is possible to “plug in” a new look and feel for any given component without creating any side effects in the code that uses that component. Moreover, it becomes possible to define entire Chapter 30 Introducing Swing 947 The MVC Connection In general, a visual component is a composite of three distinct aspects: • The way that the component looks when rendered on the screen • The way that the component reacts to the user • The state information associated with the component No matter what architecture is used to implement a component, it must implicitly contain these three parts. Over the years, one component architecture has proven itself to be exceptionally effective: Model-View-Controller, or MVC for short. The MVC architecture is successful because each piece of the design corresponds to an aspect of a component. In MVC terminology, the model corresponds to the state information associated with the component. For example, in the case of a check box, the model contains a field that indicates if the box is checked or unchecked. The view determines how the component is displayed on the screen, including any aspects of the view that are affected by the current state of the model. The controller determines how the component reacts to the user. For example, when the user clicks a check box, the controller reacts by changing the model to reflect the user’s choice (checked or unchecked). This then results in the view being updated. By separating a component into a model, a view, and a controller, the specific implementation of each can be changed without affecting the other two. For instance, different view implementations can render the same component in different ways without affecting the model or the controller. Although the MVC architecture and the principles behind it are conceptually sound, the high level of separation between the view and the controller is not beneficial for Swing components. Instead, Swing uses a modified version of MVC that combines the view and the controller into a single logical entity called the UI delegate. For this reason, Swing’s approach is called either the Model-Delegate architecture or the Separable Model architecture. Therefore, although Swing’s component architecture is based on MVC, it does not use a classical implementation of it. Part III sets of look-and-feels that represent different GUI styles. To use a specific style, its look and feel is simply “plugged in.” Once this is done, all components are automatically rendered using that style. Pluggable look-and-feels offer several important advantages. It is possible to define a look and feel that is consistent across all platforms. Conversely, it is possible to create a look and feel that acts like a specific platform. For example, if you know that an application will be running only in a Windows environment, it is possible to specify the Windows look and feel. It is also possible to design a custom look and feel. Finally, the look and feel can be changed dynamically at run time. Java 7 provides look-and-feels, such as metal, Motif, and Nimbus, that are available to all Swing users. The metal look and feel is also called the Java look and feel. It is platformindependent and available in all Java execution environments. It is also the default look and feel. Windows environments also have access to the Windows and Windows Classic look and feel. This book uses the default Java look and feel (metal) because it is platform independent. 948 PART III Software Development Using Java Swing’s pluggable look and feel is made possible by its Model-Delegate architecture. Because the view (look) and controller (feel) are separate from the model, the look and feel can be changed without affecting how the component is used within a program. Conversely, it is possible to customize the model without affecting the way that the component appears on the screen or responds to user input. To support the Model-Delegate architecture, most Swing components contain two objects. The first represents the model. The second represents the UI delegate. Models are defined by interfaces. For example, the model for a button is defined by the ButtonModel interface. UI delegates are classes that inherit ComponentUI. For example, the UI delegate for a button is ButtonUI. Normally, your programs will not interact directly with the UI delegate. Components and Containers A Swing GUI consists of two key items: components and containers. However, this distinction is mostly conceptual because all containers are also components. The difference between the two is found in their intended purpose: As the term is commonly used, a component is an independent visual control, such as a push button or slider. A container holds a group of components. Thus, a container is a special type of component that is designed to hold other components. Furthermore, in order for a component to be displayed, it must be held within a container. Thus, all Swing GUIs will have at least one container. Because containers are components, a container can also hold other containers. This enables Swing to define what is called a containment hierarchy, at the top of which must be a top-level container. Let’s look a bit more closely at components and containers. Components In general, Swing components are derived from the JComponent class. (The only exceptions to this are the four top-level containers, described in the next section.) JComponent provides the functionality that is common to all components. For example, JComponent supports the pluggable look and feel. JComponent inherits the AWT classes Container and Component. Thus, a Swing component is built on and compatible with an AWT component. All of Swing’s components are represented by classes defined within the package javax.swing. The following table shows the class names for Swing components (including those used as containers). JApplet JButton JCheckBox JCheckBoxMenuItem JColorChooser JComboBox JComponent JDesktopPane JDialog JEditorPane JFileChooser JFormattedTextField JFrame JInternalFrame JLabel JLayer JLayeredPane JList JMenu JMenuBar JMenuItem JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JRadioButtonMenuItem JRootPane JScrollBar JScrollPane JSeparator JSlider JSpinner JSplitPane JTabbedPane Chapter 30 Introducing Swing JTable JTextArea JTextField JTextPane JTogglebutton JToolBar JToolTip JTree JViewport JWindow 949 Notice that all component classes begin with the letter J. For example, the class for a label is JLabel; the class for a push button is JButton; and the class for a scroll bar is JScrollBar. Swing defines two types of containers. The first are top-level containers: JFrame, JApplet, JWindow, and JDialog. These containers do not inherit JComponent. They do, however, inherit the AWT classes Component and Container. Unlike Swing’s other components, which are lightweight, the top-level containers are heavyweight. This makes the top-level containers a special case in the Swing component library. As the name implies, a top-level container must be at the top of a containment hierarchy. A top-level container is not contained within any other container. Furthermore, every containment hierarchy must begin with a top-level container. The one most commonly used for applications is JFrame. The one used for applets is JApplet. The second type of containers supported by Swing are lightweight containers. Lightweight containers do inherit JComponent. An example of a lightweight container is JPanel, which is a general-purpose container. Lightweight containers are often used to organize and manage groups of related components because a lightweight container can be contained within another container. Thus, you can use lightweight containers such as JPanel to create subgroups of related controls that are contained within an outer container. The Top-Level Container Panes Each top-level container defines a set of panes. At the top of the hierarchy is an instance of JRootPane. JRootPane is a lightweight container whose purpose is to manage the other panes. It also helps manage the optional menu bar. The panes that comprise the root pane are called the glass pane, the content pane, and the layered pane. The glass pane is the top-level pane. It sits above and completely covers all other panes. By default, it is a transparent instance of JPanel. The glass pane enables you to manage mouse events that affect the entire container (rather than an individual control) or to paint over any other component, for example. In most cases, you won’t need to use the glass pane directly, but it is there if you need it. The layered pane is an instance of JLayeredPane. The layered pane allows components to be given a depth value. This value determines which component overlays another. (Thus, the layered pane lets you specify a Z-order for a component, although this is not something that you will usually need to do.) The layered pane holds the content pane and the (optional) menu bar. Although the glass pane and the layered panes are integral to the operation of a top-level container and serve important purposes, much of what they provide occurs behind the scene. The pane with which your application will interact the most is the content pane, because this is the pane to which you will add visual components. In other words, when you add a component, such as a button, to a top-level container, you will add it to the content pane. By default, the content pane is an opaque instance of JPanel. Part III Containers 950 PART III Software Development Using Java The Swing Packages Swing is a very large subsystem and makes use of many packages. At the time of this writing, these are the packages defined by Swing. javax.swing javax.swing.plaf.basic javax.swing.text javax.swing.border javax.swing.plaf.metal javax.swing.text.html javax.swing.colorchooser javax.swing.plaf.multi javax.swing.text.html.parser javax.swing.event javax.swing.plaf.nimbus javax.swing.text.rtf javax.swing.filechooser javax.swing.plaf.synth javax.swing.tree javax.swing.plaf javax.swing.table javax.swing.undo The main package is javax.swing. This package must be imported into any program that uses Swing. It contains the classes that implement the basic Swing components, such as push buttons, labels, and check boxes. A Simple Swing Application Swing programs differ from both the console-based programs and the AWT-based programs shown earlier in this book. For example, they use a different set of components and a different container hierarchy than does the AWT. Swing programs also have special requirements that relate to threading. The best way to understand the structure of a Swing program is to work through an example. There are two types of Java programs in which Swing is typically used. The first is a desktop application. The second is the applet. This section shows how to create a Swing application. The creation of a Swing applet is described later in this chapter. Although quite short, the following program shows one way to write a Swing application. In the process, it demonstrates several key features of Swing. It uses two Swing components: JFrame and JLabel. JFrame is the top-level container that is commonly used for Swing applications. JLabel is the Swing component that creates a label, which is a component that displays information. The label is Swing’s simplest component because it is passive. That is, a label does not respond to user input. It just displays output. The program uses a JFrame container to hold an instance of a JLabel. The label displays a short text message. // A simple Swing application. import javax.swing.*; class SwingDemo { SwingDemo() { // Create a new JFrame container. JFrame jfrm = new JFrame("A Simple Swing Application"); // Give the frame an initial size. jfrm.setSize(275, 100); Chapter 30 Introducing Swing 951 // Terminate the program when the user closes the application. jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a text-based label. JLabel jlab = new JLabel(" Swing means powerful GUIs."); // Add the label to the content pane. jfrm.add(jlab); // Display the frame. jfrm.setVisible(true); public static void main(String args[]) { // Create the frame on the event dispatching thread. SwingUtilities.invokeLater(new Runnable() { public void run() { new SwingDemo(); } }); } } Swing programs are compiled and run in the same way as other Java applications. Thus, to compile this program, you can use this command line: javac SwingDemo.java To run the program, use this command line: java SwingDemo When the program is run, it will produce the window shown in Figure 30-1. Because the SwingDemo program illustrates several core Swing concepts, we will examine it carefully, line by line. The program begins by importing javax.swing. As mentioned, this package contains the components and models defined by Swing. For example, javax.swing defines classes that implement labels, buttons, text controls, and menus. It will be included in all programs that use Swing. Next, the program declares the SwingDemo class and a constructor for that class. The constructor is where most of the action of the program occurs. It begins by creating a JFrame, using this line of code: JFrame jfrm = new JFrame("A Simple Swing Application"); Figure 30-1 The window produced by the SwingDemo program Part III } 952 PART III Software Development Using Java This creates a container called jfrm that defines a rectangular window complete with a title bar; close, minimize, maximize, and restore buttons; and a system menu. Thus, it creates a standard, top-level window. The title of the window is passed to the constructor. Next, the window is sized using this statement: jfrm.setSize(275, 100); The setSize( ) method (which is inherited by JFrame from the AWT class Component) sets the dimensions of the window, which are specified in pixels. Its general form is shown here: void setSize(int width, int height) In this example, the width of the window is set to 275 and the height is set to 100. By default, when a top-level window is closed (such as when the user clicks the close box), the window is removed from the screen, but the application is not terminated. While this default behavior is useful in some situations, it is not what is needed for most applications. Instead, you will usually want the entire application to terminate when its top-level window is closed. There are a couple of ways to achieve this. The easiest way is to call setDefaultCloseOperation( ), as the program does: jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); After this call executes, closing the window causes the entire application to terminate. The general form of setDefaultCloseOperation( ) is shown here: void setDefaultCloseOperation(int what) The value passed in what determines what happens when the window is closed. There are several other options in addition to JFrame.EXIT_ON_CLOSE. They are shown here: DISPOSE_ON_CLOSE HIDE_ON_CLOSE DO_NOTHING_ON_CLOSE Their names reflect their actions. These constants are declared in WindowConstants, which is an interface declared in javax.swing that is implemented by JFrame. The next line of code creates a Swing JLabel component: JLabel jlab = new JLabel(" Swing means powerful GUIs."); JLabel is the simplest and easiest-to-use component because it does not accept user input. It simply displays information, which can consist of text, an icon, or a combination of the two. The label created by the program contains only text, which is passed to its constructor. The next line of code adds the label to the content pane of the frame: jfrm.add(jlab); As explained earlier, all top-level containers have a content pane in which components are stored. Thus, to add a component to a frame, you must add it to the frame’s content pane. This is accomplished by calling add( ) on the JFrame reference (jfrm in this case). The general form of add( ) is shown here: Chapter 30 Introducing Swing 953 Component add(Component comp) The add( ) method is inherited by JFrame from the AWT class Container. By default, the content pane associated with a JFrame uses border layout. The version of add( ) just shown adds the label to the center location. Other versions of add( ) enable you to specify one of the border regions. When a component is added to the center, its size is adjusted automatically to fit the size of the center. Before continuing, an important historical point needs to be made. Prior to JDK 5, when adding a component to the content pane, you could not invoke the add( ) method directly on a JFrame instance. Instead, you needed to call add( ) on the content pane of the JFrame object. The content pane can be obtained by calling getContentPane( ) on a JFrame instance. The getContentPane( ) method is shown here: Container getContentPane( ) jfrm.getContentPane().add(jlab); // old-style Here, getContentPane( ) first obtains a reference to content pane, and then add( ) adds the component to the container linked to this pane. This same procedure was also required to invoke remove( ) to remove a component and setLayout( ) to set the layout manager for the content pane. You will see explicit calls to getContentPane( ) frequently throughout pre-5.0 code. Today, the use of getContentPane( ) is no longer necessary. You can simply call add( ), remove( ), and setLayout( ) directly on JFrame because these methods have been changed so that they operate on the content pane automatically. The last statement in the SwingDemo constructor causes the window to become visible: jfrm.setVisible(true); The setVisible( ) method is inherited from the AWT Component class. If its argument is true, the window will be displayed. Otherwise, it will be hidden. By default, a JFrame is invisible, so setVisible(true) must be called to show it. Inside main( ), a SwingDemo object is created, which causes the window and the label to be displayed. Notice that the SwingDemo constructor is invoked using these lines of code: SwingUtilities.invokeLater(new Runnable() { public void run() { new SwingDemo(); } }); This sequence causes a SwingDemo object to be created on the event dispatching thread rather than on the main thread of the application. Here’s why. In general, Swing programs are event-driven. For example, when a user interacts with a component, an event is generated. An event is passed to the application by calling an event handler defined by the application. However, the handler is executed on the event dispatching thread provided by Swing and not on the main thread of the application. Thus, although event handlers are defined by your program, they are called on a thread that was not created by your program. Part III It returns a Container reference to the content pane. The add( ) method was then called on that reference to add a component to a content pane. Thus, in the past, you had to use the following statement to add jlab to jfrm: 954 PART III Software Development Using Java To avoid problems (including the potential for deadlock), all Swing GUI components must be created and updated from the event dispatching thread, not the main thread of the application. However, main( ) is executed on the main thread. Thus, main( ) cannot directly instantiate a SwingDemo object. Instead, it must create a Runnable object that executes on the event dispatching thread and have this object create the GUI. To enable the GUI code to be created on the event dispatching thread, you must use one of two methods that are defined by the SwingUtilities class. These methods are invokeLater( ) and invokeAndWait( ). They are shown here: static void invokeLater(Runnable obj) static void invokeAndWait(Runnable obj) throws InterruptedException, InvocationTargetException Here, obj is a Runnable object that will have its run( ) method called by the event dispatching thread. The difference between the two methods is that invokeLater( ) returns immediately, but invokeAndWait( ) waits until obj.run( ) returns. You can use one of these methods to call a method that constructs the GUI for your Swing application, or whenever you need to modify the state of the GUI from code not executed by the event dispatching thread. You will normally want to use invokeLater( ), as the preceding program does. However, when constructing the initial GUI for an applet, you will need to use invokeAndWait( ). Event Handling The preceding example showed the basic form of a Swing program, but it left out one important part: event handling. Because JLabel does not take input from the user, it does not generate events, so no event handling was needed. However, the other Swing components do respond to user input and the events generated by those interactions need to be handled. Events can also be generated in ways not directly related to user input. For example, an event is generated when a timer goes off. Whatever the case, event handling is a large part of any Swing-based application. The event handling mechanism used by Swing is the same as that used by the AWT. This approach is called the delegation event model, and it is described in Chapter 23. In many cases, Swing uses the same events as does the AWT, and these events are packaged in java.awt.event. Events specific to Swing are stored in javax.swing.event. Although events are handled in Swing in the same way as they are with the AWT, it is still useful to work through a simple example. The following program handles the event generated by a Swing push button. Sample output is shown in Figure 30-2. Figure 30-2 Output from the EventDemo program Chapter 30 Introducing Swing 955 // Handle an event in a Swing program. import java.awt.*; import java.awt.event.*; import javax.swing.*; class EventDemo { JLabel jlab; EventDemo() { // Create a new JFrame container. JFrame jfrm = new JFrame("An Event Example"); // Specify FlowLayout for the layout manager. jfrm.setLayout(new FlowLayout()); // Terminate the program when the user closes the application. jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Make two buttons. JButton jbtnAlpha = new JButton("Alpha"); JButton jbtnBeta = new JButton("Beta"); // Add action listener for Alpha. jbtnAlpha.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jlab.setText("Alpha was pressed."); } }); // Add action listener for Beta. jbtnBeta.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jlab.setText("Beta was pressed."); } }); // Add the buttons to the content pane. jfrm.add(jbtnAlpha); jfrm.add(jbtnBeta); // Create a text-based label. jlab = new JLabel("Press a button."); // Add the label to the content pane. jfrm.add(jlab); Part III // Give the frame an initial size. jfrm.setSize(220, 90); 956 PART III Software Development Using Java // Display the frame. jfrm.setVisible(true); } public static void main(String args[]) { // Create the frame on the event dispatching thread. SwingUtilities.invokeLater(new Runnable() { public void run() { new EventDemo(); } }); } } First, notice that the program now imports both the java.awt and java.awt.event packages. The java.awt package is needed because it contains the FlowLayout class, which supports the standard flow layout manager used to lay out components in a frame. (See Chapter 25 for coverage of layout managers.) The java.awt.event package is needed because it defines the ActionListener interface and the ActionEvent class. The EventDemo constructor begins by creating a JFrame called jfrm. It then sets the layout manager for the content pane of jfrm to FlowLayout. Recall that, by default, the content pane uses BorderLayout as its layout manager. However, for this example, FlowLayout is more convenient. Notice that FlowLayout is assigned using this statement: jfrm.setLayout(new FlowLayout()); As explained, in the past you had to explicitly call getContentPane( ) to set the layout manager for the content pane. This requirement was removed as of JDK 5. After setting the size and default close operation, EventDemo( ) creates two push buttons, as shown here: JButton jbtnAlpha = new JButton("Alpha"); JButton jbtnBeta = new JButton("Beta"); The first button will contain the text "Alpha" and the second will contain the text "Beta". Swing push buttons are instances of JButton. JButton supplies several constructors. The one used here is JButton(String msg) The msg parameter specifies the string that will be displayed inside the button. When a push button is pressed, it generates an ActionEvent. Thus, JButton provides the addActionListener( ) method, which is used to add an action listener. (JButton also provides removeActionListener( ) to remove a listener, but this method is not used by the program.) As explained in Chapter 23, the ActionListener interface defines only one method: actionPerformed( ). It is shown again here for your convenience: void actionPerformed(ActionEvent ae) This method is called when a button is pressed. In other words, it is the event handler that is called when a button press event has occurred. Next, event listeners for the button’s action events are added by the code shown here: Chapter 30 Introducing Swing 957 // Add action listener for Alpha. jbtnAlpha.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jlab.setText("Alpha was pressed."); } }); // Add action listener for Beta. jbtnBeta.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jlab.setText("Beta was pressed."); } }); jfrm.add(jbtnAlpha); jfrm.add(jbtnBeta); Finally, jlab is added to the content pane and window is made visible. When you run the program, each time you press a button, a message is displayed in the label that indicates which button was pressed. One last point: Remember that all event handlers, such as actionPerformed( ), are called on the event dispatching thread. Therefore, an event handler must return quickly in order to avoid slowing down the application. If your application needs to do something time consuming as the result of an event, it must use a separate thread. Create a Swing Applet The second type of program that commonly uses Swing is the applet. Swing-based applets are similar to AWT-based applets, but with an important difference: A Swing applet extends JApplet rather than Applet. JApplet is derived from Applet. Thus, JApplet includes all of the functionality found in Applet and adds support for Swing. JApplet is a top-level Swing container, which means that it is not derived from JComponent. Because JApplet is a toplevel container, it includes the various panes described earlier. This means that all components are added to JApplet’s content pane in the same way that components are added to JFrame's content pane. Swing applets use the same four lifecycle methods as described in Chapter 22: init( ), start( ), stop( ), and destroy( ). Of course, you need override only those methods that are needed by your applet. Painting is accomplished differently in Swing than it is in the AWT, and a Swing applet will not normally override the paint( ) method. (Painting in Swing is described later in this chapter.) One other point: All interaction with components in a Swing applet must take place on the event dispatching thread, as described in the previous section. This threading issue applies to all Swing programs. Here is an example of a Swing applet. It provides the same functionality as the previous application, but does so in applet form. Figure 30-3 shows the program when executed by appletviewer. Part III Here, anonymous inner classes are used to provide the event handlers for the two buttons. Each time a button is pressed, the string displayed in jlab is changed to reflect which button was pressed. Next, the buttons are added to the content pane of jfrm: 958 PART III Software Development Using Java Figure 30-3 Output from the example Swing applet // A simple Swing-based applet import javax.swing.*; import java.awt.*; import java.awt.event.*; /* This HTML can be used to launch the applet: */ public class MySwingApplet extends JApplet { JButton jbtnAlpha; JButton jbtnBeta; JLabel jlab; // Initialize the applet. public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { makeGUI(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can’t create because of "+ exc); } } // This applet does not need to override start(), stop(), // or destroy(). // Set up and initialize the GUI. private void makeGUI() { // Set the applet to use flow layout. setLayout(new FlowLayout()); Chapter 30 Introducing Swing 959 // Make two buttons. jbtnAlpha = new JButton("Alpha"); jbtnBeta = new JButton("Beta"); // Add action listener for Alpha. jbtnAlpha.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { jlab.setText("Alpha was pressed."); } }); // Add the buttons to the content pane. add(jbtnAlpha); add(jbtnBeta); // Create a text-based label. jlab = new JLabel("Press a button."); // Add the label to the content pane. add(jlab); } } There are two important things to notice about this applet. First, MySwingApplet extends JApplet. As explained, all Swing-based applets extend JApplet rather than Applet. Second, the init( ) method initializes the Swing components on the event dispatching thread by setting up a call to makeGUI( ). Notice that this is accomplished through the use of invokeAndWait( ) rather than invokeLater( ). Applets must use invokeAndWait( ) because the init( ) method must not return until the entire initialization process has been completed. In essence, the start( ) method cannot be called until after initialization, which means that the GUI must be fully constructed. Inside makeGUI( ), the two buttons and label are created, and the action listeners are added to the buttons. Finally, the components are added to the content pane. Although this example is quite simple, this same general approach must be used when building any Swing GUI that will be used by an applet. Painting in Swing Although the Swing component set is quite powerful, you are not limited to using it because Swing also lets you write directly into the display area of a frame, panel, or one of Swing’s other components, such as JLabel. Although many (perhaps most) uses of Swing will not involve drawing directly to the surface of a component, it is available for those applications that need this capability. To write output directly to the surface of a component, you will Part III // Add action listener for Beta. jbtnBeta.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { jlab.setText("Beta was pressed."); } }); 960 PART III Software Development Using Java use one or more drawing methods defined by the AWT, such as drawLine( ) or drawRect( ). Thus, most of the techniques and methods described in Chapter 24 also apply to Swing. However, there are also some very important differences, and the process is discussed in detail in this section. Painting Fundamentals Swing’s approach to painting is built on the original AWT-based mechanism, but Swing’s implementation offers more finally grained control. Before examining the specifics of Swing-based painting, it is useful to review the AWT-based mechanism that underlies it. The AWT class Component defines a method called paint( ) that is used to draw output directly to the surface of a component. For the most part, paint( ) is not called by your program. (In fact, only in the most unusual cases should it ever be called by your program.) Rather, paint( ) is called by the run-time system whenever a component must be rendered. This situation can occur for several reasons. For example, the window in which the component is displayed can be overwritten by another window and then uncovered. Or, the window might be minimized and then restored. The paint( ) method is also called when a program begins running. When writing AWT-based code, an application will override paint( ) when it needs to write output directly to the surface of the component. Because JComponent inherits Component, all Swing’s lightweight components inherit the paint( ) method. However, you will not override it to paint directly to the surface of a component. The reason is that Swing uses a bit more sophisticated approach to painting that involves three distinct methods: paintComponent( ), paintBorder( ), and paintChildren( ). These methods paint the indicated portion of a component and divide the painting process into its three distinct, logical actions. In a lightweight component, the original AWT method paint( ) simply executes calls to these methods, in the order just shown. To paint to the surface of a Swing component, you will create a subclass of the component and then override its paintComponent( ) method. This is the method that paints the interior of the component. You will not normally override the other two painting methods. When overriding paintComponent( ), the first thing you must do is call super.paintComponent( ), so that the superclass portion of the painting process takes place. (The only time this is not required is when you are taking complete, manual control over how a component is displayed.) After that, write the output that you want to display. The paintComponent( ) method is shown here: protected void paintComponent(Graphics g) The parameter g is the graphics context to which output is written. To cause a component to be painted under program control, call repaint( ). It works in Swing just as it does for the AWT. The repaint( ) method is defined by Component. Calling it causes the system to call paint( ) as soon as it is possible to do so. Because painting is a time-consuming operation, this mechanism allows the run-time system to defer painting momentarily until some higher-priority task has completed, for example. Of course, in Swing the call to paint( ) results in a call to paintComponent( ). Therefore, to output to the surface of a component, your program will store the output until paintComponent( ) is called. Inside the overridden paintComponent( ), you will draw the stored output. Chapter 30 Introducing Swing 961 Compute the Paintable Area When drawing to the surface of a component, you must be careful to restrict your output to the area that is inside the border. Although Swing automatically clips any output that will exceed the boundaries of a component, it is still possible to paint into the border, which will then get overwritten when the border is drawn. To avoid this, you must compute the paintable area of the component. This is the area defined by the current size of the component minus the space used by the border. Therefore, before you paint to a component, you must obtain the width of the border and then adjust your drawing accordingly. To obtain the border width, call getInsets( ), shown here: Insets getInsets( ) This method is defined by Container and overridden by JComponent. It returns an Insets object that contains the dimensions of the border. The inset values can be obtained by using these fields: int top; int left; int right; These values are then used to compute the drawing area given the width and the height of the component. You can obtain the width and height of the component by calling getWidth( ) and getHeight( ) on the component. They are shown here: int getWidth( ) int getHeight( ) By subtracting the value of the insets, you can compute the usable width and height of the component. A Paint Example Here is a program that puts into action the preceding discussion. It creates a class called PaintPanel that extends JPanel. The program then uses an object of that class to display lines whose endpoints have been generated randomly. Sample output is shown in Figure 10-4. Figure 30-4 Sample output from the PaintPanel program Part III int bottom; 962 PART III Software Development Using Java // Paint lines to a panel. import import import import java.awt.*; java.awt.event.*; javax.swing.*; java.util.*; // This class extends JPanel. It overrides // the paintComponent() method so that random // lines are plotted in the panel. class PaintPanel extends JPanel { Insets ins; // holds the panel’s insets Random rand; // used to generate random numbers // Construct a panel. PaintPanel() { // Put a border around the panel. setBorder( BorderFactory.createLineBorder(Color.RED, 5)); rand = new Random(); } // Override the paintComponent() method. protected void paintComponent(Graphics g) { // Always call the superclass method first. super.paintComponent(g); int x, y, x2, y2; // Get the height and width of the component. int height = getHeight(); int width = getWidth(); // Get the insets. ins = getInsets(); // Draw ten lines whose endpoints are randomly generated. for(int i=0; i < 10; i++) { // Obtain random coordinates that define // the endpoints of each line. x = rand.nextInt(width-ins.left); y = rand.nextInt(height-ins.bottom); x2 = rand.nextInt(width-ins.left); y2 = rand.nextInt(height-ins.bottom); // Draw the line. g.drawLine(x, y, x2, y2); } } } Chapter 30 Introducing Swing 963 // Demonstrate painting directly onto a panel. class PaintDemo { JLabel jlab; PaintPanel pp; PaintDemo() { // Create a new JFrame container. JFrame jfrm = new JFrame("Paint Demo"); // Give the frame an initial size. jfrm.setSize(200, 150); // Terminate the program when the user closes the application. jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add the panel to the content pane. Because the default // border layout is used, the panel will automatically be // sized to fit the center region. jfrm.add(pp); // Display the frame. jfrm.setVisible(true); } public static void main(String args[]) { // Create the frame on the event dispatching thread. SwingUtilities.invokeLater(new Runnable() { public void run() { new PaintDemo(); } }); } } Let’s examine this program closely. The PaintPanel class extends JPanel. JPanel is one of Swing’s lightweight containers, which means that it is a component that can be added to the content pane of a JFrame. To handle painting, PaintPanel overrides the paintComponent( ) method. This enables PaintPanel to write directly to the surface of the component when painting takes place. The size of the panel is not specified because the program uses the default border layout and the panel is added to the center. This results in the panel being sized to fill the center. If you change the size of the window, the size of the panel will be adjusted accordingly. Notice that the constructor also specifies a 5-pixel wide, red border. This is accomplished by setting the border by using the setBorder( ) method, shown here: void setBorder(Border border) Part III // Create the panel that will be painted. pp = new PaintPanel(); 964 PART III Software Development Using Java Border is the Swing interface that encapsulates a border. You can obtain a border by calling one of the factory methods defined by the BorderFactory class. The one used in the program is createLineBorder( ), which creates a simple line border. It is shown here: static Border createLineBorder(Color clr, int width) Here, clr specifies the color of the border and width specifies its width in pixels. Inside the override of paintComponent( ), notice that it first calls super.paintComponent( ). As explained, this is necessary to ensure that the component is properly drawn. Next the width and height of the panel are obtained along with the insets. These values are used to ensure the lines lie within the drawing area of the panel. The drawing area is the overall width and height of a component less the border width. The computations are designed to work with differently sized PaintPanels and borders. To prove this, try changing the size of the window. The lines will still all lie within the borders of the panel. The PaintDemo class creates a PaintPanel and then adds the panel to the content pane. When the application is first displayed, the overridden paintComponent( ) method is called, and the lines are drawn. Each time you resize or hide and restore the window, a new set of lines are drawn. In all cases, the lines fall within the paintable area. CHAPTER 31 Exploring Swing The previous chapter described several of the core concepts relating to Swing and showed the general form of both a Swing application and a Swing applet. This chapter continues the discussion of Swing by presenting an overview of several Swing components, such as buttons, check boxes, trees, and tables. The Swing components provide rich functionality and allow a high level of customization. Because of space limitations, it is not possible to describe all of their features and attributes. Rather, the purpose of this overview is to give you a feel for the capabilities of the Swing component set. The Swing component classes described in this chapter are shown here: JButton JCheckBox JComboBox JLabel JList JRadioButton JScrollPane JTabbedPane JTable JTextField JToggleButton JTree These components are all lightweight, which means that they are all derived from JComponent. Also discussed is the ButtonGroup class, which encapsulates a mutually exclusive set of Swing buttons, and ImageIcon, which encapsulates a graphics image. Both are defined by Swing and packaged in javax.swing. One other point: The Swing components are demonstrated in applets because the code for an applet is more compact than it is for a desktop application. However, the same techniques apply to both applets and applications. JLabel and ImageIcon JLabel is Swing’s easiest-to-use component. It creates a label and was introduced in the preceding chapter. Here, we will look at JLabel a bit more closely. JLabel can be used to display text and/or an icon. It is a passive component in that it does not respond to user input. JLabel defines several constructors. Here are three of them: JLabel(Icon icon) JLabel(String str) JLabel(String str, Icon icon, int align) 965 966 PART III Software Development Using Java Here, str and icon are the text and icon used for the label. The align argument specifies the horizontal alignment of the text and/or icon within the dimensions of the label. It must be one of the following values: LEFT, RIGHT, CENTER, LEADING, or TRAILING. These constants are defined in the SwingConstants interface, along with several others used by the Swing classes. Notice that icons are specified by objects of type Icon, which is an interface defined by Swing. The easiest way to obtain an icon is to use the ImageIcon class. ImageIcon implements Icon and encapsulates an image. Thus, an object of type ImageIcon can be passed as an argument to the Icon parameter of JLabel’s constructor. There are several ways to provide the image, including reading it from a file or downloading it from a URL. Here is the ImageIcon constructor used by the example in this section: ImageIcon(String filename) It obtains the image in the file named filename. The icon and text associated with the label can be obtained by the following methods: Icon getIcon( ) String getText( ) The icon and text associated with a label can be set by these methods: void setIcon(Icon icon) void setText(String str) Here, icon and str are the icon and text, respectively. Therefore, using setText( ) it is possible to change the text inside a label during program execution. The following applet illustrates how to create and display a label containing both an icon and a string. It begins by creating an ImageIcon object for the file france.gif, which depicts the flag for France. This is used as the second argument to the JLabel constructor. The first and last arguments for the JLabel constructor are the label text and the alignment. Finally, the label is added to the content pane. // Demonstrate JLabel and ImageIcon. import java.awt.*; import javax.swing.*; /* */ public class JLabelDemo extends JApplet { public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); Chapter 31 Exploring Swing 967 } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Create an icon. ImageIcon ii = new ImageIcon("france.gif"); // Create a label. JLabel jl = new JLabel("France", ii, JLabel.CENTER); // Add the label to the content pane. add(jl); } } Part III Output from the label example is shown here: JTextField JTextField is the simplest Swing text component. It is also probably its most widely used text component. JTextField allows you to edit one line of text. It is derived from JTextComponent, which provides the basic functionality common to Swing text components. JTextField uses the Document interface for its model. Three of JTextField’s constructors are shown here: JTextField(int cols) JTextField(String str, int cols) JTextField(String str) Here, str is the string to be initially presented, and cols is the number of columns in the text field. If no string is specified, the text field is initially empty. If the number of columns is not specified, the text field is sized to fit the specified string. JTextField generates events in response to user interaction. For example, an ActionEvent is fired when the user presses enter. A CaretEvent is fired each time the caret (i.e., the cursor) changes position. (CaretEvent is packaged in javax.swing.event.) Other events are also possible. In many cases, your program will not need to handle these events. Instead, you will simply obtain the string currently in the text field when it is needed. To obtain the text currently in the text field, call getText( ). 968 PART III Software Development Using Java The following example illustrates JTextField. It creates a JTextField and adds it to the content pane. When the user presses enter, an action event is generated. This is handled by displaying the text in the status window. // Demonstrate JTextField. import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class JTextFieldDemo extends JApplet { JTextField jtf; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Change to flow layout. setLayout(new FlowLayout()); // Add text field to content pane. jtf = new JTextField(15); add(jtf); jtf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { // Show text when user presses ENTER. showStatus(jtf.getText()); } }); } } Output from the text field example is shown here: Chapter 31 Exploring Swing 969 The Swing Buttons Swing defines four types of buttons: JButton, JToggleButton, JCheckBox, and JRadioButton. All are subclasses of the AbstractButton class, which extends JComponent. Thus, all buttons share a set of common traits. AbstractButton contains many methods that allow you to control the behavior of buttons. For example, you can define different icons that are displayed for the button when it is disabled, pressed, or selected. Another icon can be used as a rollover icon, which is displayed when the mouse is positioned over a button. The following methods set these icons: void setDisabledIcon(Icon di) void setPressedIcon(Icon pi) void setSelectedIcon(Icon si) void setRolloverIcon(Icon ri) String getText( ) void setText(String str) Here, str is the text to be associated with the button. The model used by all buttons is defined by the ButtonModel interface. A button generates an action event when it is pressed. Other events are possible. Each of the concrete button classes is examined next. JButton The JButton class provides the functionality of a push button. You have already seen a simple form of it in the preceding chapter. JButton allows an icon, a string, or both to be associated with the push button. Three of its constructors are shown here: JButton(Icon icon) JButton(String str) JButton(String str, Icon icon) Here, str and icon are the string and icon used for the button. When the button is pressed, an ActionEvent is generated. Using the ActionEvent object passed to the actionPerformed( ) method of the registered ActionListener, you can obtain the action command string associated with the button. By default, this is the string displayed inside the button. However, you can set the action command by calling setActionCommand( ) on the button. You can obtain the action command by calling getActionCommand( ) on the event object. It is declared like this: String getActionCommand( ) The action command identifies the button. Thus, when using two or more buttons within the same application, the action command gives you an easy way to determine which button was pressed. In the preceding chapter, you saw an example of a text-based button. The following demonstrates an icon-based button. It displays four push buttons and a label. Each button Part III Here, di, pi, si, and ri are the icons to be used for the indicated purpose. The text associated with a button can be read and written via the following methods: 970 PART III Software Development Using Java displays an icon that represents the flag of a country. When a button is pressed, the name of that country is displayed in the label. // Demonstrate an icon-based JButton. import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class JButtonDemo extends JApplet implements ActionListener { JLabel jlab; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Change to flow layout. setLayout(new FlowLayout()); // Add buttons to content pane. ImageIcon france = new ImageIcon("france.gif"); JButton jb = new JButton(france); jb.setActionCommand("France"); jb.addActionListener(this); add(jb); ImageIcon germany = new ImageIcon("germany.gif"); jb = new JButton(germany); jb.setActionCommand("Germany"); jb.addActionListener(this); add(jb); ImageIcon italy = new ImageIcon("italy.gif"); jb = new JButton(italy); jb.setActionCommand("Italy"); jb.addActionListener(this); add(jb); Chapter 31 Exploring Swing 971 ImageIcon japan = new ImageIcon("japan.gif"); jb = new JButton(japan); jb.setActionCommand("Japan"); jb.addActionListener(this); add(jb); // Create and add the label to content pane. jlab = new JLabel("Choose a Flag"); add(jlab); } // Handle button events. public void actionPerformed(ActionEvent ae) { jlab.setText("You selected " + ae.getActionCommand()); } } JToggleButton A useful variation on the push button is called a toggle button. A toggle button looks just like a push button, but it acts differently because it has two states: pushed and released. That is, when you press a toggle button, it stays pressed rather than popping back up as a regular push button does. When you press the toggle button a second time, it releases (pops up). Therefore, each time a toggle button is pushed, it toggles between its two states. Toggle buttons are objects of the JToggleButton class. JToggleButton implements AbstractButton. In addition to creating standard toggle buttons, JToggleButton is a superclass for two other Swing components that also represent two-state controls. These are JCheckBox and JRadioButton, which are described later in this chapter. Thus, JToggleButton defines the basic functionality of all two-state components. JToggleButton defines several constructors. The one used by the example in this section is shown here: JToggleButton(String str) This creates a toggle button that contains the text passed in str. By default, the button is in the off position. Other constructors enable you to create toggle buttons that contain images, or images and text. JToggleButton uses a model defined by a nested class called JToggleButton.ToggleButtonModel. Normally, you won’t need to interact directly with the model to use a standard toggle button. Like JButton, JToggleButton generates an action event each time it is pressed. Unlike JButton, however, JToggleButton also generates an item event. This event is used by those Part III Output from the button example is shown here: 972 PART III Software Development Using Java components that support the concept of selection. When a JToggleButton is pressed in, it is selected. When it is popped out, it is deselected. To handle item events, you must implement the ItemListener interface. Recall from Chapter 23, that each time an item event is generated, it is passed to the itemStateChanged( ) method defined by ItemListener. Inside itemStateChanged( ), the getItem( ) method can be called on the ItemEvent object to obtain a reference to the JToggleButton instance that generated the event. It is shown here: Object getItem( ) A reference to the button is returned. You will need to cast this reference to JToggleButton. The easiest way to determine a toggle button’s state is by calling the isSelected( ) method (inherited from AbstractButton) on the button that generated the event. It is shown here: boolean isSelected( ) It returns true if the button is selected and false otherwise. Here is an example that uses a toggle button. Notice how the item listener works. It simply calls isSelected( ) to determine the button’s state. // Demonstrate JToggleButton. import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class JToggleButtonDemo extends JApplet { JLabel jlab; JToggleButton jtbn; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Change to flow layout. setLayout(new FlowLayout()); Chapter 31 Exploring Swing 973 // Create a label. jlab = new JLabel("Button is off."); // Make a toggle button. jtbn = new JToggleButton("On/Off"); // Add an item listener for the toggle button. jtbn.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if(jtbn.isSelected()) jlab.setText("Button is on."); else jlab.setText("Button is off."); } }); } } The output from the toggle button example is shown here: Check Boxes The JCheckBox class provides the functionality of a check box. Its immediate superclass is JToggleButton, which provides support for two-state buttons, as just described. JCheckBox defines several constructors. The one used here is JCheckBox(String str) It creates a check box that has the text specified by str as a label. Other constructors let you specify the initial selection state of the button and specify an icon. When the user selects or deselects a check box, an ItemEvent is generated. You can obtain a reference to the JCheckBox that generated the event by calling getItem( ) on the ItemEvent passed to the itemStateChanged( ) method defined by ItemListener. The easiest way to determine the selected state of a check box is to call isSelected( ) on the JCheckBox instance. The following example illustrates check boxes. It displays four check boxes and a label. When the user clicks a check box, an ItemEvent is generated. Inside the itemStateChanged( ) method, getItem( ) is called to obtain a reference to the JCheckBox object that generated Part III // Add the toggle button and label to the content pane. add(jtbn); add(jlab); 974 PART III Software Development Using Java the event. Next, a call to isSelected( ) determines if the box was selected or cleared. The getText( ) method gets the text for that check box and uses it to set the text inside the label. // Demonstrate JCheckbox. import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class JCheckBoxDemo extends JApplet implements ItemListener { JLabel jlab; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Change to flow layout. setLayout(new FlowLayout()); // Add check boxes to the content pane. JCheckBox cb = new JCheckBox("C"); cb.addItemListener(this); add(cb); cb = new JCheckBox("C++"); cb.addItemListener(this); add(cb); cb = new JCheckBox("Java"); cb.addItemListener(this); add(cb); cb = new JCheckBox("Perl"); cb.addItemListener(this); add(cb); // Create the label and add it to the content pane. Chapter 31 Exploring Swing 975 jlab = new JLabel("Select languages"); add(jlab); } // Handle item events for the check boxes. public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox)ie.getItem(); if(cb.isSelected()) jlab.setText(cb.getText() + " is selected"); else jlab.setText(cb.getText() + " is cleared"); } } Output from this example is shown here: Radio buttons are a group of mutually exclusive buttons, in which only one button can be selected at any one time. They are supported by the JRadioButton class, which extends JToggleButton. JRadioButton provides several constructors. The one used in the example is shown here: JRadioButton(String str) Here, str is the label for the button. Other constructors let you specify the initial selection state of the button and specify an icon. In order for their mutually exclusive nature to be activated, radio buttons must be configured into a group. Only one of the buttons in the group can be selected at any time. For example, if a user presses a radio button that is in a group, any previously selected button in that group is automatically deselected. A button group is created by the ButtonGroup class. Its default constructor is invoked for this purpose. Elements are then added to the button group via the following method: void add(AbstractButton ab) Here, ab is a reference to the button to be added to the group. A JRadioButton generates action events, item events, and change events each time the button selection changes. Most often, it is the action event that is handled, which means that you will normally implement the ActionListener interface. Recall that the only method defined by ActionListener is actionPerformed( ). Inside this method, you can use a number of different ways to determine which button was selected. First, you can check its action command by calling getActionCommand( ). By default, the action command is the same as the button label, but you can set the action command to something else by calling setActionCommand( ) on the radio button. Second, you can call getSource( ) on the ActionEvent object and check that reference against the buttons. Finally, you can simply check each radio button to find out which one is currently selected by calling isSelected( ) on each button. Remember, each time an action event occurs, it means that the button being selected has changed and that one and only one button will be selected. Part III Radio Buttons 976 PART III Software Development Using Java The following example illustrates how to use radio buttons. Three radio buttons are created. The buttons are then added to a button group. As explained, this is necessary to cause their mutually exclusive behavior. Pressing a radio button generates an action event, which is handled by actionPerformed( ). Within that handler, the getActionCommand( ) method gets the text that is associated with the radio button and uses it to set the text within a label. // Demonstrate JRadioButton import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class JRadioButtonDemo extends JApplet implements ActionListener { JLabel jlab; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Change to flow layout. setLayout(new FlowLayout()); // Create radio buttons and add them to content pane. JRadioButton b1 = new JRadioButton("A"); b1.addActionListener(this); add(b1); JRadioButton b2 = new JRadioButton("B"); b2.addActionListener(this); add(b2); JRadioButton b3 = new JRadioButton("C"); b3.addActionListener(this); add(b3); Chapter 31 Exploring Swing 977 // Define a button group. ButtonGroup bg = new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); // Create a label and add it to the content pane. jlab = new JLabel("Select One"); add(jlab); } // Handle button selection. public void actionPerformed(ActionEvent ae) { jlab.setText("You selected " + ae.getActionCommand()); } } Part III Output from the radio button example is shown here: JTabbedPane JTabbedPane encapsulates a tabbed pane. It manages a set of components by linking them with tabs. Selecting a tab causes the component associated with that tab to come to the forefront. Tabbed panes are very common in the modern GUI, and you have no doubt used them many times. Given the complex nature of a tabbed pane, they are surprisingly easy to create and use. JTabbedPane defines three constructors. We will use its default constructor, which creates an empty control with the tabs positioned across the top of the pane. The other two constructors let you specify the location of the tabs, which can be along any of the four sides. JTabbedPane uses the SingleSelectionModel model. Tabs are added by calling addTab( ). Here is one of its forms: void addTab(String name, Component comp) Here, name is the name for the tab, and comp is the component that should be added to the tab. Often, the component added to a tab is a JPanel that contains a group of related components. This technique allows a tab to hold a set of components. The general procedure to use a tabbed pane is outlined here: 1. Create an instance of JTabbedPane. 2. Add each tab by calling addTab( ). 3. Add the tabbed pane to the content pane. 978 PART III Software Development Using Java The following example illustrates a tabbed pane. The first tab is titled "Cities" and contains four buttons. Each button displays the name of a city. The second tab is titled "Colors" and contains three check boxes. Each check box displays the name of a color. The third tab is titled "Flavors" and contains one combo box. This enables the user to select one of three flavors. // Demonstrate JTabbedPane. import javax.swing.*; /* */ public class JTabbedPaneDemo extends JApplet { public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { JTabbedPane jtp = new JTabbedPane(); jtp.addTab("Cities", new CitiesPanel()); jtp.addTab("Colors", new ColorsPanel()); jtp.addTab("Flavors", new FlavorsPanel()); add(jtp); } } // Make the panels that will be added to the tabbed pane. class CitiesPanel extends JPanel { public CitiesPanel() { JButton b1 = new JButton("New York"); add(b1); JButton b2 = new JButton("London"); add(b2); JButton b3 = new JButton("Hong Kong"); add(b3); JButton b4 = new JButton("Tokyo"); add(b4); } } Chapter 31 Exploring Swing 979 class ColorsPanel extends JPanel { public ColorsPanel() { JCheckBox cb1 = new JCheckBox("Red"); add(cb1); JCheckBox cb2 = new JCheckBox("Green"); add(cb2); JCheckBox cb3 = new JCheckBox("Blue"); add(cb3); } } public FlavorsPanel() { JComboBox jcb = new JComboBox(); jcb.addItem("Vanilla"); jcb.addItem("Chocolate"); jcb.addItem("Strawberry"); add(jcb); } } Output from the tabbed pane example is shown in the following three illustrations: JScrollPane JScrollPane is a lightweight container that automatically handles the scrolling of another component. The component being scrolled can be either an individual component, such as a table, or a group of components contained within another lightweight container, such as a JPanel. In either case, if the object being scrolled is larger than the viewable area, horizontal and/or vertical scroll bars are automatically provided, and the component can be scrolled through the pane. Because JScrollPane automates scrolling, it usually eliminates the need to manage individual scroll bars. Part III class FlavorsPanel extends JPanel { 980 PART III Software Development Using Java The viewable area of a scroll pane is called the viewport. It is a window in which the component being scrolled is displayed. Thus, the viewport displays the visible portion of the component being scrolled. The scroll bars scroll the component through the viewport. In its default behavior, a JScrollPane will dynamically add or remove a scroll bar as needed. For example, if the component is taller than the viewport, a vertical scroll bar is added. If the component will completely fit within the viewport, the scroll bars are removed. JScrollPane defines several constructors. The one used in this chapter is shown here: JScrollPane(Component comp) The component to be scrolled is specified by comp. Scroll bars are automatically displayed when the content of the pane exceeds the dimensions of the viewport. Here are the steps to follow to use a scroll pane: 1. Create the component to be scrolled. 2. Create an instance of JScrollPane, passing to it the object to scroll. 3. Add the scroll pane to the content pane. The following example illustrates a scroll pane. First, a JPanel object is created, and 400 buttons are added to it, arranged into 20 columns. This panel is then added to a scroll pane, and the scroll pane is added to the content pane. Because the panel is larger than the viewport, vertical and horizontal scroll bars appear automatically. You can use the scroll bars to scroll the buttons into view. // Demonstrate JScrollPane. import java.awt.*; import javax.swing.*; /* */ public class JScrollPaneDemo extends JApplet { public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { Chapter 31 Exploring Swing 981 // Add 400 buttons to a panel. JPanel jp = new JPanel(); jp.setLayout(new GridLayout(20, 20)); int b = 0; for(int i = 0; i < 20; i++) { for(int j = 0; j < 20; j++) { jp.add(new JButton("Button " + b)); ++b; } } // Create the scroll pane. JScrollPane jsp = new JScrollPane(jp); // Add the scroll pane to the content pane. // Because the default border layout is used, // the scroll pane will be added to the center. add(jsp, BorderLayout.CENTER); Output from the scroll pane example is shown here: JList In Swing, the basic list class is called JList. It supports the selection of one or more items from a list. Although the list often consists of strings, it is possible to create a list of just about any object that can be displayed. JList is so widely used in Java that it is highly unlikely that you have not seen one before. In the past, the items in a JList were represented as Object references. However, with the release of JDK 7, JList was made generic and is now declared like this: class JList Here, E represents the type of the items in the list. Part III } } 982 PART III Software Development Using Java JList provides several constructors. The one used here is JList(E[ ] items) This creates a JList that contains the items in the array specified by items. JList is based on two models. The first is ListModel. This interface defines how access to the list data is achieved. The second model is the ListSelectionModel interface, which defines methods that determine what list item or items are selected. Although a JList will work properly by itself, most of the time you will wrap a JList inside a JScrollPane. This way, long lists will automatically be scrollable, which simplifies GUI design. It also makes it easy to change the number of entries in a list without having to change the size of the JList component. A JList generates a ListSelectionEvent when the user makes or changes a selection. This event is also generated when the user deselects an item. It is handled by implementing ListSelectionListener. This listener specifies only one method, called valueChanged( ), which is shown here: void valueChanged(ListSelectionEvent le) Here, le is a reference to the object that generated the event. Although ListSelectionEvent does provide some methods of its own, normally you will interrogate the JList object itself to determine what has occurred. Both ListSelectionEvent and ListSelectionListener are packaged in javax.swing.event. By default, a JList allows the user to select multiple ranges of items within the list, but you can change this behavior by calling setSelectionMode( ), which is defined by JList. It is shown here: void setSelectionMode(int mode) Here, mode specifies the selection mode. It must be one of these values defined by ListSelectionModel: SINGLE_SELECTION SINGLE_INTERVAL_SELECTION MULTIPLE_INTERVAL_SELECTION The default, multiple-interval selection, lets the user select multiple ranges of items within a list. With single-interval selection, the user can select one range of items. With single selection, the user can select only a single item. Of course, a single item can be selected in the other two modes, too. It’s just that they also allow a range to be selected. You can obtain the index of the first item selected, which will also be the index of the only selected item when using single-selection mode, by calling getSelectedIndex( ), shown here: int getSelectedIndex( ) Indexing begins at zero. So, if the first item is selected, this method will return 0. If no item is selected, –1 is returned. Instead of obtaining the index of a selection, you can obtain the value associated with the selection by calling getSelectedValue( ): E getSelectedValue( ) Chapter 31 Exploring Swing 983 It returns a reference to the first selected value. If no value has been selected, it returns null. The following applet demonstrates a simple JList, which holds a list of cities. Each time a city is selected in the list, a ListSelectionEvent is generated, which is handled by the valueChanged( ) method defined by ListSelectionListener. It responds by obtaining the index of the selected item and displaying the name of the selected city in a label. // Demonstrate JList. import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; /* public class JListDemo extends JApplet { JList jlst; JLabel jlab; JScrollPane jscrlp; // Create an array of cities. String Cities[] = { "New York", "Chicago", "Houston", "Denver", "Los Angeles", "Seattle", "London", "Paris", "New Delhi", "Hong Kong", "Tokyo", "Sydney" }; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Change to flow layout. setLayout(new FlowLayout()); // Create a JList. jlst = new JList(Cities); // Set the list selection mode to single selection. jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); Part III */ 984 PART III Software Development Using Java // Add the list to a scroll pane. jscrlp = new JScrollPane(jlst); // Set the preferred size of the scroll pane. jscrlp.setPreferredSize(new Dimension(120, 90)); // Make a label that displays the selection. jlab = new JLabel("Choose a City"); // Add selection listener for the list. jlst.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent le) { // Get the index of the changed item. int idx = jlst.getSelectedIndex(); // Display selection, if item was selected. if(idx != -1) jlab.setText("Current selection: " + Cities[idx]); else // Otherwise, reprompt. jlab.setText("Choose a City"); } }); // Add the list and label to the content pane. add(jscrlp); add(jlab); } } Output from the list example is shown here: JComboBox Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class. A combo box normally displays one entry, but it will also display a dropdown list that allows a user to select a different entry. You can also create a combo box that lets the user enter a selection into the text field. In the past, the items in a JComboBox were represented as Object references. However, with the release of JDK 7, JComboBox was made generic and is now declared like this: class JComboBox Chapter 31 Exploring Swing 985 Here, E represents the type of the items in the combo box. The JComboBox constructor used by the example is shown here: JComboBox(E[ ] items) Here, items is an array that initializes the combo box. Other constructors are available. JComboBox uses the ComboBoxModel. Mutable combo boxes (those whose entries can be changed) use the MutableComboBoxModel. In addition to passing an array of items to be displayed in the drop-down list, items can be dynamically added to the list of choices via the addItem( ) method, shown here: Here, obj is the object to be added to the combo box. This method must be used only with mutable combo boxes. JComboBox generates an action event when the user selects an item from the list. JComboBox also generates an item event when the state of selection changes, which occurs when an item is selected or deselected. Thus, changing a selection means that two item events will occur: one for the deselected item and another for the selected item. Often, it is sufficient to simply listen for action events, but both event types are available for your use. One way to obtain the item selected in the list is to call getSelectedItem( ) on the combo box. It is shown here: Object getSelectedItem( ) You will need to cast the returned value into the type of object stored in the list. The following example demonstrates the combo box. The combo box contains entries for “France,” “Germany,” “Italy,” and “Japan.” When a country is selected, an icon-based label is updated to display the flag for that country. You can see how little code is required to use this powerful component. // Demonstrate JComboBox. import java.awt.*; import java.awt.event.*; import javax.swing.*; /* */ public class JComboBoxDemo extends JApplet { JLabel jlab; ImageIcon france, germany, italy, japan; JComboBox jcb; String flags[] = { "France", "Germany", "Italy", "Japan" }; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { Part III void addItem(E obj) 986 PART III Software Development Using Java makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Change to flow layout. setLayout(new FlowLayout()); // Instantiate a combo box and add it to the content pane. jcb = new JComboBox(flags); add(jcb); // Handle selections. jcb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { String s = (String) jcb.getSelectedItem(); jlab.setIcon(new ImageIcon(s + ".gif")); } }); // Create a label and add it to the content pane. jlab = new JLabel(new ImageIcon("france.gif")); add(jlab); } } Output from the combo box example is shown here: Trees A tree is a component that presents a hierarchical view of data. The user has the ability to expand or collapse individual subtrees in this display. Trees are implemented in Swing by the JTree class. A sampling of its constructors is shown here: JTree(Object obj [ ]) JTree(Vector v) JTree(TreeNode tn) Exploring Swing 987 In the first form, the tree is constructed from the elements in the array obj. The second form constructs the tree from the elements of vector v. In the third form, the tree whose root node is specified by tn specifies the tree. Although JTree is packaged in javax.swing, its support classes and interfaces are packaged in javax.swing.tree. This is because the number of classes and interfaces needed to support JTree is quite large. JTree relies on two models: TreeModel and TreeSelectionModel. A JTree generates a variety of events, but three relate specifically to trees: TreeExpansionEvent, TreeSelectionEvent, and TreeModelEvent. TreeExpansionEvent events occur when a node is expanded or collapsed. A TreeSelectionEvent is generated when the user selects or deselects a node within the tree. A TreeModelEvent is fired when the data or structure of the tree changes. The listeners for these events are TreeExpansionListener, TreeSelectionListener, and TreeModelListener, respectively. The tree event classes and listener interfaces are packaged in javax.swing.event. The event handled by the sample program shown in this section is TreeSelectionEvent. To listen for this event, implement TreeSelectionListener. It defines only one method, called valueChanged( ), which receives the TreeSelectionEvent object. You can obtain the path to the selected object by calling getPath( ), shown here, on the event object: TreePath getPath( ) It returns a TreePath object that describes the path to the changed node. The TreePath class encapsulates information about a path to a particular node in a tree. It provides several constructors and methods. In this book, only the toString( ) method is used. It returns a string that describes the path. The TreeNode interface declares methods that obtain information about a tree node. For example, it is possible to obtain a reference to the parent node or an enumeration of the child nodes. The MutableTreeNode interface extends TreeNode. It declares methods that can insert and remove child nodes or change the parent node. The DefaultMutableTreeNode class implements the MutableTreeNode interface. It represents a node in a tree. One of its constructors is shown here: DefaultMutableTreeNode(Object obj) Here, obj is the object to be enclosed in this tree node. The new tree node doesn’t have a parent or children. To create a hierarchy of tree nodes, the add( ) method of DefaultMutableTreeNode can be used. Its signature is shown here: void add(MutableTreeNode child) Here, child is a mutable tree node that is to be added as a child to the current node. JTree does not provide any scrolling capabilities of its own. Instead, a JTree is typically placed within a JScrollPane. This way, a large tree can be scrolled through a smaller viewport. Here are the steps to follow to use a tree: 1. Create an instance of JTree. 2. Create a JScrollPane and specify the tree as the object to be scrolled. 3. Add the tree to the scroll pane. 4. Add the scroll pane to the content pane. Part III Chapter 31 988 PART III Software Development Using Java The following example illustrates how to create a tree and handle selections. The program creates a DefaultMutableTreeNode instance labeled "Options". This is the top node of the tree hierarchy. Additional tree nodes are then created, and the add( ) method is called to connect these nodes to the tree. A reference to the top node in the tree is provided as the argument to the JTree constructor. The tree is then provided as the argument to the JScrollPane constructor. This scroll pane is then added to the content pane. Next, a label is created and added to the content pane. The tree selection is displayed in this label. To receive selection events from the tree, a TreeSelectionListener is registered for the tree. Inside the valueChanged( ) method, the path to the current selection is obtained and displayed. // Demonstrate JTree. import java.awt.*; import javax.swing.event.*; import javax.swing.*; import javax.swing.tree.*; /* */ public class JTreeDemo extends JApplet { JTree tree; JLabel jlab; public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Create top node of tree. DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options"); // Create subtree of "A". DefaultMutableTreeNode a = new DefaultMutableTreeNode("A"); top.add(a); DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1"); a.add(a1); Chapter 31 Exploring Swing 989 DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2"); a.add(a2); // Create subtree of "B" DefaultMutableTreeNode b = new DefaultMutableTreeNode("B"); top.add(b); DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1"); b.add(b1); DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2"); b.add(b2); DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3"); b.add(b3); // Create the tree. tree = new JTree(top); // Add the scroll pane to the content pane. add(jsp); // Add the label to the content pane. jlab = new JLabel(); add(jlab, BorderLayout.SOUTH); // Handle tree selection events. tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent tse) { jlab.setText("Selection is " + tse.getPath()); } }); } } Output from the tree example is shown here: The string presented in the text field describes the path from the top tree node to the selected node. Part III // Add the tree to a scroll pane. JScrollPane jsp = new JScrollPane(tree); 990 PART III Software Development Using Java JTable JTable is a component that displays rows and columns of data. You can drag the cursor on column boundaries to resize columns. You can also drag a column to a new position. Depending on its configuration, it is also possible to select a row, column, or cell within the table, and to change the data within a cell. JTable is a sophisticated component that offers many more options and features than can be discussed here. (It is perhaps Swing’s most complicated component.) However, in its default configuration, JTable still offers substantial functionality that is easy to use—especially if you simply want to use the table to present data in a tabular format. The brief overview presented here will give you a general understanding of this powerful component. Like JTree, JTable has many classes and interfaces associated with it. These are packaged in javax.swing.table. At its core, JTable is conceptually simple. It is a component that consists of one or more columns of information. At the top of each column is a heading. In addition to describing the data in a column, the heading also provides the mechanism by which the user can change the size of a column or change the location of a column within the table. JTable does not provide any scrolling capabilities of its own. Instead, you will normally wrap a JTable inside a JScrollPane. JTable supplies several constructors. The one used here is JTable(Object data[ ][ ], Object colHeads[ ]) Here, data is a two-dimensional array of the information to be presented, and colHeads is a one-dimensional array with the column headings. JTable relies on three models. The first is the table model, which is defined by the TableModel interface. This model defines those things related to displaying data in a two-dimensional format. The second is the table column model, which is represented by TableColumnModel. JTable is defined in terms of columns, and it is TableColumnModel that specifies the characteristics of a column. These two models are packaged in javax.swing.table. The third model determines how items are selected, and it is specified by the ListSelectionModel, which was described when JList was discussed. A JTable can generate several different events. The two most fundamental to a table’s operation are ListSelectionEvent and TableModelEvent. A ListSelectionEvent is generated when the user selects something in the table. By default, JTable allows you to select one or more complete rows, but you can change this behavior to allow one or more columns, or one or more individual cells to be selected. A TableModelEvent is fired when that table’s data changes in some way. Handling these events requires a bit more work than it does to handle the events generated by the previously described components and is beyond the scope of this book. However, if you simply want to use JTable to display data (as the following example does), then you don’t need to handle any events. Here are the steps required to set up a simple JTable that can be used to display data: 1. Create an instance of JTable. 2. Create a JScrollPane object, specifying the table as the object to scroll. 3. Add the table to the scroll pane. 4. Add the scroll pane to the content pane. Chapter 31 Exploring Swing 991 The following example illustrates how to create and use a simple table. A one-dimensional array of strings called colHeads is created for the column headings. A two-dimensional array of strings called data is created for the table cells. You can see that each element in the array is an array of three strings. These arrays are passed to the JTable constructor. The table is added to a scroll pane, and then the scroll pane is added to the content pane. The table displays the data in the data array. The default table configuration also allows the contents of a cell to be edited. Changes affect the underlying array, which is data in this case. // Demonstrate JTable. import java.awt.*; import javax.swing.*; /* */ public void init() { try { SwingUtilities.invokeAndWait( new Runnable() { public void run() { makeGUI(); } } ); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } } private void makeGUI() { // Initialize column headings. String[] colHeads = { "Name", "Extension", "ID#" }; // Initialize data. Object[][] data = { { "Gail", "4567", "865" }, { "Ken", "7566", "555" }, { "Viviane", "5634", "587" }, { "Melanie", "7345", "922" }, { "Anne", "1237", "333" }, { "John", "5656", "314" }, { "Matt", "5672", "217" }, { "Claire", "6741", "444" }, { "Erwin", "9023", "519" }, { "Ellen", "1134", "532" }, { "Jennifer", "5689", "112" }, { "Ed", "9030", "133" }, { "Helen", "6751", "145" } }; Part III public class JTableDemo extends JApplet { 992 PART III Software Development Using Java // Create the table. JTable table = new JTable(data, colHeads); // Add the table to a scroll pane. JScrollPane jsp = new JScrollPane(table); // Add the scroll pane to the content pane. add(jsp); } } Output from this example is shown here: Continuing Your Exploration of Swing Swing defines a very large GUI toolkit. It has many more features that you will want to explore on your own. For example, Swing provides toolbars, tooltips, and progress bars. It also provides a complete menu subsystem. Swing’s pluggable look and feel lets you substitute another appearance and behavior for an element. You can define your own models for the various components, and you can change the way that cells are edited and rendered when working with tables and trees. The best way to become familiar with Swing’s capabilities is to experiment with it. CHAPTER 32 Servlets This chapter presents an overview of servlets. Servlets are small programs that execute on the server side of a web connection. Just as applets dynamically extend the functionality of a web browser, servlets dynamically extend the functionality of a web server. The topic of servlets is quite large, and it is beyond the scope of this chapter to cover it all. Instead, we will focus on the core concepts, interfaces, and classes, and develop several examples. Background In order to understand the advantages of servlets, you must have a basic understanding of how web browsers and servers cooperate to provide content to a user. Consider a request for a static web page. A user enters a Uniform Resource Locator (URL) into a browser. The browser generates an HTTP request to the appropriate web server. The web server maps this request to a specific file. That file is returned in an HTTP response to the browser. The HTTP header in the response indicates the type of the content. The Multipurpose Internet Mail Extensions (MIME) are used for this purpose. For example, ordinary ASCII text has a MIME type of text/plain. The Hypertext Markup Language (HTML) source code of a web page has a MIME type of text/html. Now consider dynamic content. Assume that an online store uses a database to store information about its business. This would include items for sale, prices, availability, orders, and so forth. It wishes to make this information accessible to customers via web pages. The contents of those web pages must be dynamically generated to reflect the latest information in the database. In the early days of the Web, a server could dynamically construct a page by creating a separate process to handle each client request. The process would open connections to one or more databases in order to obtain the necessary information. It communicated with the web server via an interface known as the Common Gateway Interface (CGI). CGI allowed the separate process to read data from the HTTP request and write data to the HTTP response. A variety of different languages were used to build CGI programs. These included C, C++, and Perl. 993 994 PART III Software Development Using Java However, CGI suffered serious performance problems. It was expensive in terms of processor and memory resources to create a separate process for each client request. It was also expensive to open and close database connections for each client request. In addition, the CGI programs were not platform-independent. Therefore, other techniques were introduced. Among these are servlets. Servlets offer several advantages in comparison with CGI. First, performance is significantly better. Servlets execute within the address space of a web server. It is not necessary to create a separate process to handle each client request. Second, servlets are platform-independent because they are written in Java. Third, the Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. Finally, the full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already. The Life Cycle of a Servlet Three methods are central to the life cycle of a servlet. These are init( ), service( ), and destroy( ). They are implemented by every servlet and are invoked at specific times by the server. Let us consider a typical user scenario to understand when these methods are called. First, assume that a user enters a Uniform Resource Locator (URL) to a web browser. The browser then generates an HTTP request for this URL. This request is then sent to the appropriate server. Second, this HTTP request is received by the web server. The server maps this request to a particular servlet. The servlet is dynamically retrieved and loaded into the address space of the server. Third, the server invokes the init( ) method of the servlet. This method is invoked only when the servlet is first loaded into memory. It is possible to pass initialization parameters to the servlet so it may configure itself. Fourth, the server invokes the service( ) method of the servlet. This method is called to process the HTTP request. You will see that it is possible for the servlet to read data that has been provided in the HTTP request. It may also formulate an HTTP response for the client. The servlet remains in the server’s address space and is available to process any other HTTP requests received from clients. The service( ) method is called for each HTTP request. Finally, the server may decide to unload the servlet from its memory. The algorithms by which this determination is made are specific to each server. The server calls the destroy( ) method to relinquish any resources such as file handles that are allocated for the servlet. Important data may be saved to a persistent store. The memory allocated for the servlet and its objects can then be garbage collected. Servlet Development Options To create servlets, you will need access to a servlet container/server. Two popular ones are Glassfish and Tomcat. Glassfish is from Oracle and is provided by the Java EE SDK. It is supported by NetBeans. Tomcat is an open-source product maintained by the Apache Software Foundation. It can also be used by NetBeans. Both Tomcat and Glassfish can also be used with other IDEs, such as Eclipse. The examples and descriptions in this chapter use Tomcat for reasons that will soon be apparent. Chapter 32 Servlets 995 REMEMBER The instructions for developing and deploying servlets in this chapter are based on Tomcat and use only command-line tools. If you are using an IDE and different servlet container/server, consult the documentation for your environment. Using Tomcat Tomcat contains the class libraries, documentation, and run time support that you will need to create and test servlets. At the time of this writing, several versions of Tomcat are available for use, including 5.5.x, 6.0.x, and 7.0.x. All will work with the examples in this chapter. However, the instructions that follow use 7.0.4, which supports servlet specification 3.0. You can download Tomcat from tomcat.apache.org. Tomcat versions 6.0.x and 7.0.x support both 32-bit and 64-bit Windows. You should choose a version appropriate to your environment. The examples in this chapter assume a 64-bit Windows environment. Assuming that a 64-bit version of Tomcat 7.0.4 was unpacked from the root directly, the default location is C:\apache-tomcat-7.0.4-windows-x64\apache-tomcat-7.0.4\ This is the location assumed by the examples in this book. If you load Tomcat in a different location (or use a different version of Tomcat), you will need to make appropriate changes to the examples. You may need to set the environmental variable JAVA_HOME to the toplevel directory in which the Java Development Kit is installed. NOTE All of the directories shown in this section assume Tomcat 7.0.4. If you install a different version of Tomcat, then you will need to adjust the directory names and paths to match those used by the version you installed. Once installed, you start Tomcat by selecting startup.bat from the bin directly under the apache-tomcat-7.0.4 directory. To stop Tomcat, execute shutdown.bat, also in the bin directory. Part III Although IDEs such as NetBeans and Eclipse are very useful and can streamline the creation of servlets, they are not used in this chapter. The way you develop and deploy servlets differs among IDEs, and it is simply not possible for this book to address each environment. Furthermore, many readers will be using the command-line tools rather than an IDE. Therefore, if you are using an IDE, you must refer to the instructions for that environment for information concerning the development and deployment of servlets. For this reason, the instructions given here and elsewhere in this chapter assume that only the command-line tools are employed. Thus, they will work for nearly any reader. Tomcat is used in this chapter because, in the opinion of this author, it makes it relatively easy to run the example servlets using only command-line tools and a text editor. It is also widely available in various programming environments. Furthermore, since only command-line tools are used, you don’t need to download and install an IDE just to experiment with servlets. Understand, however, that even if you are developing in an environment that uses Glassfish, the concepts presented here still apply. It is just that the mechanics of preparing a servlet for testing will be slightly different. 996 PART III Software Development Using Java The classes and interfaces needed to build servlets are contained in servlet-api.jar, which is in the following directory: C:\apache-tomcat-7.0.4-windows-x64\apache-tomcat-7.0.4\lib To make servlet-api.jar accessible, update your CLASSPATH environment variable so that it includes C:\apache-tomcat-7.0.4-windows-x64\apache-tomcat-7.0.4\lib\servlet-api.jar Alternatively, you can specify this file when you compile the servlets. For example, the following command compiles the first servlet example: javac HelloServlet.java -classpath "C:\apache-tomcat-7.0.4-windowsx64\apache-tomcat-7.0.4\lib\servlet-api.jar" Once you have compiled a servlet, you must enable Tomcat to find it. For our purposes, this means putting it into a directory under Tomcat’s webapps directory and entering its name into a web.xml file. To keep things simple, the examples in this chapter use the directory and web.xml file that Tomcat supplies for its own example servlets. This way, you won’t have to create any files or directories just to experiment with the sample servlets. Here is the procedure that you will follow. First, copy the servlet’s class file into the following directory: C:\apache-tomcat-7.0.4-windows-x64\apache-tomcat-7.0.4\webapps\ examples\WEB-INF\classes Next, add the servlet’s name and mapping to the web.xml file in the following directory: C:\apache-tomcat-7.0.4-windows-x64\apache-tomcat-7.0.4\webapps\ examples\WEB-INF For instance, assuming the first example, called HelloServlet, you will add the following lines in the section that defines the servlets: HelloServlet HelloServlet Next, you will add the following lines to the section that defines the servlet mappings: HelloServlet /servlet/HelloServlet Follow this same general procedure for all of the examples. A Simple Servlet To become familiar with the key servlet concepts, we will begin by building and testing a simple servlet. The basic steps are the following: Chapter 32 Servlets 997 1. Create and compile the servlet source code. Then, copy the servlet’s class file to the proper directory, and add the servlet’s name and mappings to the proper web.xml file. 2. Start Tomcat. 3. Start a web browser and request the servlet. Let us examine each of these steps in detail. Create and Compile the Servlet Source Code To begin, create a file named HelloServlet.java that contains the following program: import java.io.*; import javax.servlet.*; public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("Hello!"); pw.close(); } } Let’s look closely at this program. First, note that it imports the javax.servlet package. This package contains the classes and interfaces required to build servlets. You will learn more about these later in this chapter. Next, the program defines HelloServlet as a subclass of GenericServlet. The GenericServlet class provides functionality that simplifies the creation of a servlet. For example, it provides versions of init( ) and destroy( ), which may be used as is. You need supply only the service( ) method. Inside HelloServlet, the service( ) method (which is inherited from GenericServlet) is overridden. This method handles requests from a client. Notice that the first argument is a ServletRequest object. This enables the servlet to read data that is provided via the client request. The second argument is a ServletResponse object. This enables the servlet to formulate a response for the client. The call to setContentType( ) establishes the MIME type of the HTTP response. In this program, the MIME type is text/html. This indicates that the browser should interpret the content as HTML source code. Next, the getWriter( ) method obtains a PrintWriter. Anything written to this stream is sent to the client as part of the HTTP response. Then println( ) is used to write some simple HTML source code as the HTTP response. Compile this source code and place the HelloServlet.class file in the proper Tomcat directory as described in the previous section. Also, add HelloServlet to the web.xml file, as described earlier. Part III public class HelloServlet extends GenericServlet { 998 PART III Software Development Using Java Start Tomcat Start Tomcat as explained earlier. Tomcat must be running before you try to execute a servlet. Start a Web Browser and Request the Servlet Start a web browser and enter the URL shown here: http://localhost:8080/examples/servlets/servlet/HelloServlet Alternatively, you may enter the URL shown here: http://127.0.0.1:8080/examples/servlets/servlet/HelloServlet This can be done because 127.0.0.1 is defined as the IP address of the local machine. You will observe the output of the servlet in the browser display area. It will contain the string Hello! in bold type. The Servlet API Two packages contain the classes and interfaces that are required to build the servlets described in this chapter. These are javax.servlet and javax.servlet.http. They constitute the Servlet API. Keep in mind that these packages are not part of the Java core packages. Therefore, they are not included with Java SE. Instead, they are provided by Tomcat. They are also provided by Java EE. The Servlet API has been in a process of ongoing development and enhancement. The current servlet specification is version 3.0, and that is the one used in this book. However, because changes happen fast in the world of Java, you will want to check for any additions or alterations. This chapter discusses the core of the Servlet API, which will be available to most readers. The javax.servlet Package The javax.servlet package contains a number of interfaces and classes that establish the framework in which servlets operate. The following table summarizes the core interfaces that are provided in this package. The most significant of these is Servlet. All servlets must implement this interface or extend a class that implements the interface. The ServletRequest and ServletResponse interfaces are also very important. Interface Description Servlet Declares life cycle methods for a servlet. ServletConfig Allows servlets to get initialization parameters. ServletContext Enables servlets to log events and access information about their environment. ServletRequest Used to read data from a client request. ServletResponse Used to write data to a client response. Chapter 32 Servlets 999 The following table summarizes the core classes that are provided in the javax.servlet package: Class Description GenericServlet Implements the Servlet and ServletConfig interfaces. ServletInputStream Provides an input stream for reading requests from a client. ServletOutputStream Provides an output stream for writing responses to a client. ServletException Indicates a servlet error occurred. UnavailableException Indicates a servlet is unavailable. Let us examine these interfaces and classes in more detail. All servlets must implement the Servlet interface. It declares the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet. A method is also provided that allows a servlet to obtain any initialization parameters. The methods defined by Servlet are shown in Table 32-1. The init( ), service( ), and destroy( ) methods are the life cycle methods of the servlet. These are invoked by the server. The getServletConfig( ) method is called by the servlet to obtain initialization parameters. A servlet developer overrides the getServletInfo( ) method to provide a string with useful information (for example, author, version, date, copyright). This method is also invoked by the server. Method Description void destroy( ) Called when the servlet is unloaded. ServletConfig getServletConfig( ) Returns a ServletConfig object that contains any initialization parameters. String getServletInfo( ) Returns a string describing the servlet. void init(ServletConfig sc) throws ServletException Called when the servlet is initialized. Initialization parameters for the servlet can be obtained from sc. A ServletException should be thrown if the servlet cannot be initialized. void service(ServletRequest req, ServletResponse res) throws ServletException, IOException Called to process a request from a client. The request from the client can be read from req. The response to the client can be written to res. An exception is generated if a servlet or IO problem occurs. Table 32-1 The Methods Defined by Servlet Part III The Servlet Interface 1000 PART III Software Development Using Java The ServletConfig Interface The ServletConfig interface allows a servlet to obtain configuration data when it is loaded. The methods declared by this interface are summarized here: Method Description ServletContext getServletContext( ) Returns the context for this servlet. String getInitParameter(String param) Returns the value of the initialization parameter named param. Enumeration getInitParameterNames( ) Returns an enumeration of all initialization parameter names. String getServletName( ) Returns the name of the invoking servlet. The ServletContext Interface The ServletContext interface enables servlets to obtain information about their environment. Several of its methods are summarized in Table 32-2. The ServletRequest Interface The ServletRequest interface enables a servlet to obtain information about a client request. Several of its methods are summarized in Table 32-3. The ServletResponse Interface The ServletResponse interface enables a servlet to formulate a response for a client. Several of its methods are summarized in Table 32-4. Method Description Object getAttribute(String attr) Returns the value of the server attribute named attr. String getMimeType(String file) Returns the MIME type of file. String getRealPath(String vpath) Returns the real path that corresponds to the virtual path vpath. String getServerInfo( ) Returns information about the server. void log(String s) Writes s to the servlet log. void log(String s, Throwable e) Writes s and the stack trace for e to the servlet log. void setAttribute(String attr, Object val) Sets the attribute specified by attr to the value passed in val. Table 32-2 Various Methods Defined by ServletContext Chapter 32 1001 Description Object getAttribute(String attr) Returns the value of the attribute named attr. String getCharacterEncoding( ) Returns the character encoding of the request. Int getContentLength( ) Returns the size of the request. The value –1 is returned if the size is unavailable. String getContentType( ) Returns the type of the request. A null value is returned if the type cannot be determined. ServletInputStream getInputStream( ) throws IOException Returns a ServletInputStream that can be used to read binary data from the request. An IllegalStateException is thrown if getReader( ) has already been invoked for this request. String getParameter(String pname) Returns the value of the parameter named pname. Enumeration getParameterNames( ) Returns an enumeration of the parameter names for this request. String[ ] getParameterValues(String name) Returns an array containing values associated with the parameter specified by name. String getProtocol( ) Returns a description of the protocol. BufferedReader getReader( ) throws IOException Returns a buffered reader that can be used to read text from the request. An IllegalStateException is thrown if getInputStream( ) has already been invoked for this request. String getRemoteAddr( ) Returns the string equivalent of the client IP address. String getRemoteHost( ) Returns the string equivalent of the client host name. String getScheme( ) Returns the transmission scheme of the URL used for the request (for example, "http", "ftp"). String getServerName( ) Returns the name of the server. int getServerPort( ) Returns the port number. Table 32-3 Various Methods Defined by ServletRequest Method Description String getCharacterEncoding( ) Returns the character encoding for the response. ServletOutputStream getOutputStream( ) throws IOException Returns a ServletOutputStream that can be used to write binary data to the response. An IllegalStateException is thrown if getWriter( ) has already been invoked for this request. PrintWriter getWriter( ) throws IOException Returns a PrintWriter that can be used to write character data to the response. An IllegalStateException is thrown if getOutputStream( ) has already been invoked for this request. void setContentLength(int size) Sets the content length for the response to size. void setContentType(String type) Sets the content type for the response to type. Table 32-4 Various Methods Defined by ServletResponse Part III Method Servlets 1002 PART III Software Development Using Java The GenericServlet Class The GenericServlet class provides implementations of the basic life cycle methods for a servlet. GenericServlet implements the Servlet and ServletConfig interfaces. In addition, a method to append a string to the server log file is available. The signatures of this method are shown here: void log(String s) void log(String s, Throwable e) Here, s is the string to be appended to the log, and e is an exception that occurred. The ServletInputStream Class The ServletInputStream class extends InputStream. It is implemented by the servlet container and provides an input stream that a servlet developer can use to read the data from a client request. It defines the default constructor. In addition, a method is provided to read bytes from the stream. It is shown here: int readLine(byte[ ] buffer, int offset, int size) throws IOException Here, buffer is the array into which size bytes are placed starting at offset. The method returns the actual number of bytes read or –1 if an end-of-stream condition is encountered. The ServletOutputStream Class The ServletOutputStream class extends OutputStream. It is implemented by the servlet container and provides an output stream that a servlet developer can use to write data to a client response. A default constructor is defined. It also defines the print( ) and println( ) methods, which output data to the stream. The Servlet Exception Classes javax.servlet defines two exceptions. The first is ServletException, which indicates that a servlet problem has occurred. The second is UnavailableException, which extends ServletException. It indicates that a servlet is unavailable. Reading Servlet Parameters The ServletRequest interface includes methods that allow you to read the names and values of parameters that are included in a client request. We will develop a servlet that illustrates their use. The example contains two files. A web page is defined in PostParameters.html, and a servlet is defined in PostParametersServlet.java. The HTML source code for PostParameters.html is shown in the following listing. It defines a table that contains two labels and two text fields. One of the labels is Employee and the other is Phone. There is also a submit button. Notice that the action parameter of the form tag specifies a URL. The URL identifies the servlet to process the HTTP POST request. Chapter 32 Servlets 1003 The source code for PostParametersServlet.java is shown in the following listing. The service( ) method is overridden to process client requests. The getParameterNames( ) method returns an enumeration of the parameter names. These are processed in a loop. You can see that the parameter name and value are output to the client. The parameter value is obtained via the getParameter( ) method. import java.io.*; import java.util.*; import javax.servlet.*; public class PostParametersServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { // Get print writer. PrintWriter pw = response.getWriter(); // Get enumeration of parameter names. Enumeration e = request.getParameterNames(); // Display parameter names and values. while(e.hasMoreElements()) { String pname = (String)e.nextElement(); pw.print(pname + " = "); String pvalue = request.getParameter(pname); pw.println(pvalue); } pw.close(); } } Part III
Employee
Phone
1004 PART III Software Development Using Java Compile the servlet. Next, copy it to the appropriate directory, and update the web.xml file, as previously described. Then, perform these steps to test this example: 1. Start Tomcat (if it is not already running). 2. Display the web page in a browser. 3. Enter an employee name and phone number in the text fields. 4. Submit the web page. After following these steps, the browser will display a response that is dynamically generated by the servlet. The javax.servlet.http Package The preceding examples have used the classes and interfaces defined in javax.servlet, such as ServletRequest, ServletResponse, and GenericServlet, to illustrate the basic functionality of servlets. However, when working with HTTP, you will normally use the interfaces and classes in javax.servlet.http. As you will see, its functionality makes it easy to build servlets that work with HTTP requests and responses. The following table summarizes the core interfaces that are provided in this package: Interface Description HttpServletRequest Enables servlets to read data from an HTTP request. HttpServletResponse Enables servlets to write data to an HTTP response. HttpSession Allows session data to be read and written. HttpSessionBindingListener Informs an object that it is bound to or unbound from a session. The following table summarizes the core classes that are provided in this package. The most important of these is HttpServlet. Servlet developers typically extend this class in order to process HTTP requests. Class Description Cookie Allows state information to be stored on a client machine. HttpServlet Provides methods to handle HTTP requests and responses. HttpSessionEvent Encapsulates a session-changed event. HttpSessionBindingEvent Indicates when a listener is bound to or unbound from a session value, or that a session attribute changed. The HttpServletRequest Interface The HttpServletRequest interface enables a servlet to obtain information about a client request. Several of its methods are shown in Table 32-5. Servlets 1005 Method Description String getAuthType( ) Returns authentication scheme. Cookie[ ] getCookies( ) Returns an array of the cookies in this request. long getDateHeader(String field) Returns the value of the date header field named field. String getHeader(String field) Returns the value of the header field named field. Enumeration getHeaderNames( ) Returns an enumeration of the header names. int getIntHeader(String field) Returns the int equivalent of the header field named field. String getMethod( ) Returns the HTTP method for this request. String getPathInfo( ) Returns any path information that is located after the servlet path and before a query string of the URL. String getPathTranslated( ) Returns any path information that is located after the servlet path and before a query string of the URL after translating it to a real path. String getQueryString( ) Returns any query string in the URL. String getRemoteUser( ) Returns the name of the user who issued this request. String getRequestedSessionId( ) Returns the ID of the session. String getRequestURI( ) Returns the URI. StringBuffer getRequestURL( ) Returns the URL. String getServletPath( ) Returns that part of the URL that identifies the servlet. HttpSession getSession( ) Returns the session for this request. If a session does not exist, one is created and then returned. HttpSession getSession(boolean new) If new is true and no session exists, creates and returns a session for this request. Otherwise, returns the existing session for this request. boolean isRequestedSessionIdFromCookie( ) Returns true if a cookie contains the session ID. Otherwise, returns false. boolean isRequestedSessionIdFromURL( ) Returns true if the URL contains the session ID. Otherwise, returns false. boolean isRequestedSessionIdValid( ) Returns true if the requested session ID is valid in the current session context. Table 32-5 Various Methods Defined by HttpServletRequest The HttpServletResponse Interface The HttpServletResponse interface enables a servlet to formulate an HTTP response to a client. Several constants are defined. These correspond to the different status codes that can be assigned to an HTTP response. For example, SC_OK indicates that the HTTP request succeeded, and SC_NOT_FOUND indicates that the requested resource is not available. Several methods of this interface are summarized in Table 32-6. Part III Chapter 32 1006 PART III Software Development Using Java Method Description void addCookie(Cookie cookie) Adds cookie to the HTTP response. boolean containsHeader(String field) Returns true if the HTTP response header contains a field named field. String encodeURL(String url) Determines if the session ID must be encoded in the URL identified as url. If so, returns the modified version of url. Otherwise, returns url. All URLs generated by a servlet should be processed by this method. String encodeRedirectURL(String url) Determines if the session ID must be encoded in the URL identified as url. If so, returns the modified version of url. Otherwise, returns url. All URLs passed to sendRedirect( ) should be processed by this method. void sendError(int c) throws IOException Sends the error code c to the client. void sendError(int c, String s) throws IOException Sends the error code c and message s to the client. void sendRedirect(String url) throws IOException Redirects the client to url. void setDateHeader(String field, long msec) Adds field to the header with date value equal to msec (milliseconds since midnight, January 1, 1970, GMT). void setHeader(String field, String value) Adds field to the header with value equal to value. void setIntHeader(String field, int value) Adds field to the header with value equal to value. void setStatus(int code) Sets the status code for this response to code. Table 32-6 Various Methods Defined by HttpServletResponse The HttpSession Interface The HttpSession interface enables a servlet to read and write the state information that is associated with an HTTP session. Several of its methods are summarized in Table 32-7. All of these methods throw an IllegalStateException if the session has already been invalidated. The HttpSessionBindingListener Interface The HttpSessionBindingListener interface is implemented by objects that need to be notified when they are bound to or unbound from an HTTP session. The methods that are invoked when an object is bound or unbound are void valueBound(HttpSessionBindingEvent e) void valueUnbound(HttpSessionBindingEvent e) Here, e is the event object that describes the binding. Servlets 1007 Method Description Object getAttribute(String attr) Returns the value associated with the name passed in attr. Returns null if attr is not found. Enumeration getAttributeNames( ) Returns an enumeration of the attribute names associated with the session. long getCreationTime( ) Returns the time (in milliseconds since midnight, January 1, 1970, GMT) when this session was created. String getId( ) Returns the session ID. long getLastAccessedTime( ) Returns the time (in milliseconds since midnight, January 1, 1970, GMT) when the client last made a request for this session. void invalidate( ) Invalidates this session and removes it from the context. boolean isNew( ) Returns true if the server created the session and it has not yet been accessed by the client. void removeAttribute(String attr) Removes the attribute specified by attr from the session. void setAttribute(String attr, Object val) Associates the value passed in val with the attribute name passed in attr. Table 32-7 Various Methods Defined by HttpSession The Cookie Class The Cookie class encapsulates a cookie. A cookie is stored on a client and contains state information. Cookies are valuable for tracking user activities. For example, assume that a user visits an online store. A cookie can save the user’s name, address, and other information. The user does not need to enter this data each time he or she visits the store. A servlet can write a cookie to a user’s machine via the addCookie( ) method of the HttpServletResponse interface. The data for that cookie is then included in the header of the HTTP response that is sent to the browser. The names and values of cookies are stored on the user’s machine. Some of the information that is saved for each cookie includes the following: • The name of the cookie • The value of the cookie • The expiration date of the cookie • The domain and path of the cookie The expiration date determines when this cookie is deleted from the user’s machine. If an expiration date is not explicitly assigned to a cookie, it is deleted when the current browser session ends. Part III Chapter 32 1008 PART III Software Development Using Java The domain and path of the cookie determine when it is included in the header of an HTTP request. If the user enters a URL whose domain and path match these values, the cookie is then supplied to the web server. Otherwise, it is not. There is one constructor for Cookie. It has the signature shown here: Cookie(String name, String value) Here, the name and value of the cookie are supplied as arguments to the constructor. The methods of the Cookie class are summarized in Table 32-8. The HttpServlet Class The HttpServlet class extends GenericServlet. It is commonly used when developing servlets that receive and process HTTP requests. The methods defined by the HttpServlet class are summarized in Table 32-9. Method Description Object clone( ) Returns a copy of this object. String getComment( ) Returns the comment. String getDomain( ) Returns the domain. int getMaxAge( ) Returns the maximum age (in seconds). String getName( ) Returns the name. String getPath( ) Returns the path. boolean getSecure( ) Returns true if the cookie is secure. Otherwise, returns false. String getValue( ) Returns the value. int getVersion( ) Returns the version. boolean isHttpOnly( ) Returns true if the cookie has the HttpOnly attribute. void setComment(String c) Sets the comment to c. void setDomain(String d) Sets the domain to d. void setHttpOnly(boolean httpOnly) If httpOnly is true, then the HttpOnly attribute is added to the cookie. If httpOnly is false, the HttpOnly attribute is removed. void setMaxAge(int secs) Sets the maximum age of the cookie to secs. This is the number of seconds after which the cookie is deleted. void setPath(String p) Sets the path to p. void setSecure(boolean secure) Sets the security flag to secure. void setValue(String v) Sets the value to v. void setVersion(int v) Sets the version to v. Table 32-8 The Methods Defined by Cookie Servlets 1009 Method Description void doDelete(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Handles an HTTP DELETE request. void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Handles an HTTP GET request. void doHead(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Handles an HTTP HEAD request. void doOptions(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Handles an HTTP OPTIONS request. void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Handles an HTTP POST request. void doPut(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Handles an HTTP PUT request. void doTrace(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Handles an HTTP TRACE request. long getLastModified(HttpServletRequest req) Returns the time (in milliseconds since midnight, January 1, 1970, GMT) when the requested resource was last modified. void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException Called by the server when an HTTP request arrives for this servlet. The arguments provide access to the HTTP request and response, respectively. Table 32-9 The Methods Defined by HttpServlet The HttpSessionEvent Class HttpSessionEvent encapsulates session events. It extends EventObject and is generated when a change occurs to the session. It defines this constructor: HttpSessionEvent(HttpSession session) Here, session is the source of the event. HttpSessionEvent defines one method, getSession( ), which is shown here: HttpSession getSession( ) It returns the session in which the event occurred. Part III Chapter 32 1010 PART III Software Development Using Java The HttpSessionBindingEvent Class The HttpSessionBindingEvent class extends HttpSessionEvent. It is generated when a listener is bound or unbound in an HttpSession object. It is also generated when an attribute is bound or unbound. Here are its constructors: HttpSessionBindingEvent(HttpSession session, String name) HttpSessionBindingEvent(HttpSession session, String name, Object val) Here, session is the source of the event, and name is the name associated with the object that is being bound or unbound. If an attribute is being bound or unbound, its value is passed in val. The getName( ) method obtains the name that is being bound or unbound. It is shown here: String getName( ) The getSession( ) method, shown next, obtains the session to which the listener is being bound or unbound: HttpSession getSession( ) The getValue( ) method obtains the value of the attribute that is being bound or unbound. It is shown here: Object getValue( ) Handling HTTP Requests and Responses The HttpServlet class provides specialized methods that handle the various types of HTTP requests. A servlet developer typically overrides one of these methods. These methods are doDelete( ), doGet( ), doHead( ), doOptions( ), doPost( ), doPut( ), and doTrace( ). A complete description of the different types of HTTP requests is beyond the scope of this book. However, the GET and POST requests are commonly used when handling form input. Therefore, this section presents examples of these cases. Handling HTTP GET Requests Here we will develop a servlet that handles an HTTP GET request. The servlet is invoked when a form on a web page is submitted. The example contains two files. A web page is defined in ColorGet.html, and a servlet is defined in ColorGetServlet.java. The HTML source code for ColorGet.html is shown in the following listing. It defines a form that contains a select element and a submit button. Notice that the action parameter of the form tag specifies a URL. The URL identifies a servlet to process the HTTP GET request.
Color:

The source code for ColorGetServlet.java is shown in the following listing. The doGet( ) method is overridden to process any HTTP GET requests that are sent to this servlet. It uses the getParameter( ) method of HttpServletRequest to obtain the selection that was made by the user. A response is then formulated. public class ColorGetServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String color = request.getParameter("color"); response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("The selected color is: "); pw.println(color); pw.close(); } } Compile the servlet. Next, copy it to the appropriate directory, and update the web.xml file, as previously described. Then, perform these steps to test this example: 1. Start Tomcat, if it is not already running. 2. Display the web page in a browser. 3. Select a color. 4. Submit the web page. After completing these steps, the browser will display the response that is dynamically generated by the servlet. One other point: Parameters for an HTTP GET request are included as part of the URL that is sent to the web server. Assume that the user selects the red option and submits the form. The URL sent from the browser to the server is http://localhost:8080/examples/servlets/servlet/ColorGetServlet?color=Red The characters to the right of the question mark are known as the query string. Part III import java.io.*; import javax.servlet.*; import javax.servlet.http.*; 1012 PART III Software Development Using Java Handling HTTP POST Requests Here we will develop a servlet that handles an HTTP POST request. The servlet is invoked when a form on a web page is submitted. The example contains two files. A web page is defined in ColorPost.html, and a servlet is defined in ColorPostServlet.java. The HTML source code for ColorPost.html is shown in the following listing. It is identical to ColorGet.html except that the method parameter for the form tag explicitly specifies that the POST method should be used, and the action parameter for the form tag specifies a different servlet.
Color:

The source code for ColorPostServlet.java is shown in the following listing. The doPost( ) method is overridden to process any HTTP POST requests that are sent to this servlet. It uses the getParameter( ) method of HttpServletRequest to obtain the selection that was made by the user. A response is then formulated. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ColorPostServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String color = request.getParameter("color"); response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("The selected color is: "); pw.println(color); pw.close(); } } Compile the servlet and perform the same steps as described in the previous section to test it. Chapter 32 Servlets 1013 NOTE Parameters for an HTTP POST request are not included as part of the URL that is sent to the web server. In this example, the URL sent from the browser to the server is http:// localhost:8080/examples/servlets/servlet/ColorPostServlet. The parameter names and values are sent in the body of the HTTP request. Using Cookies Now, let’s develop a servlet that illustrates how to use cookies. The servlet is invoked when a form on a web page is submitted. The example contains three files as summarized here: Description AddCookie.html Allows a user to specify a value for the cookie named MyCookie. AddCookieServlet.java Processes the submission of AddCookie.html. GetCookiesServlet.java Displays cookie values. The HTML source code for AddCookie.html is shown in the following listing. This page contains a text field in which a value can be entered. There is also a submit button on the page. When this button is pressed, the value in the text field is sent to AddCookieServlet via an HTTP POST request.
Enter a value for MyCookie:
The source code for AddCookieServlet.java is shown in the following listing. It gets the value of the parameter named "data". It then creates a Cookie object that has the name "MyCookie" and contains the value of the "data" parameter. The cookie is then added to the header of the HTTP response via the addCookie( ) method. A feedback message is then written to the browser. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AddCookieServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Part III File 1014 PART III Software Development Using Java // Get parameter from HTTP request. String data = request.getParameter("data"); // Create cookie. Cookie cookie = new Cookie("MyCookie", data); // Add cookie to HTTP response. response.addCookie(cookie); // Write output to browser. response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("MyCookie has been set to"); pw.println(data); pw.close(); } } The source code for GetCookiesServlet.java is shown in the following listing. It invokes the getCookies( ) method to read any cookies that are included in the HTTP GET request. The names and values of these cookies are then written to the HTTP response. Observe that the getName( ) and getValue( ) methods are called to obtain this information. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class GetCookiesServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get cookies from header of HTTP request. Cookie[] cookies = request.getCookies(); // Display these cookies. response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println(""); for(int i = 0; i < cookies.length; i++) { String name = cookies[i].getName(); String value = cookies[i].getValue(); pw.println("name = " + name + "; value = " + value); } pw.close(); } } Compile the servlets. Next, copy them to the appropriate directory, and update the web.xml file, as previously described. Then, perform these steps to test this example: 1. Start Tomcat, if it is not already running. Chapter 32 Servlets 1015 2. Display AddCookie.html in a browser. 3. Enter a value for MyCookie. 4. Submit the web page. After completing these steps, you will observe that a feedback message is displayed by the browser. Next, request the following URL via the browser: http://localhost:8080/examples/servlets/servlet/GetCookiesServlet Session Tracking HTTP is a stateless protocol. Each request is independent of the previous one. However, in some applications, it is necessary to save state information so that information can be collected from several interactions between a browser and a server. Sessions provide such a mechanism. A session can be created via the getSession( ) method of HttpServletRequest. An HttpSession object is returned. This object can store a set of bindings that associate names with objects. The setAttribute( ), getAttribute( ), getAttributeNames( ), and removeAttribute( ) methods of HttpSession manage these bindings. Session state is shared by all servlets that are associated with a client. The following servlet illustrates how to use session state. The getSession( ) method gets the current session. A new session is created if one does not already exist. The getAttribute( ) method is called to obtain the object that is bound to the name "date". That object is a Date object that encapsulates the date and time when this page was last accessed. (Of course, there is no such binding when the page is first accessed.) A Date object encapsulating the current date and time is then created. The setAttribute( ) method is called to bind the name "date" to this object. import import import import java.io.*; java.util.*; javax.servlet.*; javax.servlet.http.*; public class DateServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the HttpSession object. HttpSession hs = request.getSession(true); Part III Observe that the name and value of the cookie are displayed in the browser. In this example, an expiration date is not explicitly assigned to the cookie via the setMaxAge( ) method of Cookie. Therefore, the cookie expires when the browser session ends. You can experiment by using setMaxAge( ) and observe that the cookie is then saved to the disk on the client machine. 1016 PART III Software Development Using Java // Get writer. response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.print(""); // Display date/time of last access. Date date = (Date)hs.getAttribute("date"); if(date != null) { pw.print("Last access: " + date + "
"); } // Display current date/time. date = new Date(); hs.setAttribute("date", date); pw.println("Current date: " + date); } } When you first request this servlet, the browser displays one line with the current date and time information. On subsequent invocations, two lines are displayed. The first line shows the date and time when the servlet was last accessed. The second line shows the current date and time. PART IV CHAPTER 33 Financial Applets and Servlets CHAPTER 34 Creating a Download Manager in Java APPENDIX Using Java’s Documentation Comments Applying Java This page intentionally left blank CHAPTER 33 Financial Applets and Servlets Despite all the large, sophisticated applications, such as word processors, databases, and accounting packages, that dominate much of the computing landscape, there has remained a class of programs that are both popular and small. These perform various financial calculations, such as the regular payments on a loan, the future value of an investment, or the remaining balance on a loan. None of these calculations are very complicated or require much code, yet they yield information that is quite useful. As you know, Java was initially designed to support the creation of small, portable programs. Originally, these programs took the form of applets, but a few years later, servlets were added. (Recall that applets run on the local machine, inside the browser, and servlets execute on the server.) Because of their small size, many of the common financial calculations are right-sized for applets and servlets. Furthermore, including a financial applet/servlet in a web page is an amenity that many users will appreciate. A user will return again and again to a page that offers the calculation that he or she desires. This chapter develops a number of applets that perform the financial calculations shown here: • Regular payments on a loan • Remaining balance on a loan • Future value of an investment • Initial investment needed to attain a desired future value • Annuity from an investment • Investment necessary for a desired annuity The chapter ends by showing how to convert the financial applets into servlets. 1019 1020 PART IV Applying Java Finding the Payments for a Loan Perhaps the most popular financial calculation is the one that computes the regular payments on a loan, such as a car or house loan. The payments on a loan are found by using the following formula: Payment = (intRate * (principal /payPerYear)) / (1 – ((intRate /payPerYear) + 1)– payPerYear * numYears) where intRate specifies the interest rate, principal contains the starting balance, payPerYear specifies the number of payments per year, and numYears specifies the length of the loan in years. The following applet called RegPay uses the preceding formula to compute the payments on a loan given the information entered by the user. Like all of the applets in this chapter, RegPay is a Swing-based applet. This means that it extends the JApplet class and uses the Swing classes to provide the user interface. Notice that it also implements the ActionListener interface. // A simple loan calculator applet. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; /* */ public class RegPay extends JApplet implements ActionListener { JTextField amountText, paymentText, periodText, rateText; JButton doIt; double principal; // original principal double intRate; // interest rate double numYears; // length of loan in years /* Number of payments per year. You could allow this value to be set by the user. */ final int payPerYear = 12; NumberFormat nf; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { makeGUI(); // initialize the GUI } }); } catch(Exception exc) { Chapter 33 Financial Applets and Servlets 1021 System.out.println("Can't create because of "+ exc); } } // Set up and initialize the GUI. private void makeGUI() { // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); JLabel heading = new JLabel("Compute Monthly Loan Payments"); JLabel JLabel JLabel JLabel amountLab = new JLabel("Principal "); periodLab = new JLabel("Years "); rateLab = new JLabel("Interest Rate "); paymentLab = new JLabel("Monthly Payments "); amountText = new JTextField(10); periodText = new JTextField(10); paymentText = new JTextField(10); rateText = new JTextField(10); // Payment field for display only. paymentText.setEditable(false); // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(amountLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(amountText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(periodLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(periodText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); Part IV doIt = new JButton("Compute"); 1022 PART IV Applying Java gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(paymentLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(paymentText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); // Add all the components. add(heading); add(amountLab); add(amountText); add(periodLab); add(periodText); add(rateLab); add(rateText); add(paymentLab); add(paymentText); add(doIt); // Register to receive action events. amountText.addActionListener(this); periodText.addActionListener(this); rateText.addActionListener(this); doIt.addActionListener(this); // Create a number format. nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } /* User pressed Enter on a text field or pressed Compute. Display the result if all fields are completed. */ public void actionPerformed(ActionEvent ae) { double result = 0.0; String amountStr = amountText.getText(); String periodStr = periodText.getText(); String rateStr = rateText.getText(); try { if(amountStr.length() != 0 && periodStr.length() != 0 && rateStr.length() != 0) { principal = Double.parseDouble(amountStr); numYears = Double.parseDouble(periodStr); intRate = Double.parseDouble(rateStr) / 100; Chapter 33 Financial Applets and Servlets 1023 result = compute(); paymentText.setText(nf.format(result)); } showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); paymentText.setText(""); } } // Compute the loan payment. double compute() { double numer; double denom; double b, e; numer = intRate * principal / payPerYear; e = -(payPerYear * numYears); b = (intRate / payPerYear) + 1.0; denom = 1.0 - Math.pow(b, e); return numer / denom; } The applet produced by this program is shown in Figure 33-1. To use the applet, simply enter the loan principal, the length of the loan in years, and the interest rate. The payments are assumed to be monthly. Once the information is entered, press Compute to calculate the monthly payment. The following sections examine the code to RegPay in detail. Because all the applets in this chapter use the same basic framework, much of the explanation presented here also applies to the other applets. Figure 33-1 The RegPay applet Part IV } 1024 PART IV Applying Java The RegPay Fields RegPay begins by declaring a number of instance variables that hold references to the text fields into which the user will enter the loan information. Next, it declares the doIt variable that will hold a reference to the Compute button. RegPay then declares three double variables that hold the loan values. The original principal is stored in principal, the interest rate is stored in intRate, and the length of the loan in years is stored in numYears. These values are entered by the user through the text fields. Next, the final integer variable payPerYear is declared and initialized to 12. Thus, the number of payments per year is hard-coded to monthly because this is the way that most loans are paid. As the comments suggest, you could allow the user to enter this value, but doing so will require another text field. The last instance variable declared by RegPay is nf, a reference to an object of type NumberFormat, which will describe the number format used for output. NumberFormat is stored in the java.text package. Although there are other ways to format numeric output, such as by using the Formatter class, NumberFormat is a good choice in this case, because the same format is used repeatedly, and this format can be set once, at the start of the program. The financial applets also offer a good opportunity to demonstrate its use. The init( ) Method Like all applets, the init( ) method is called when the applet first starts execution. This method simply invokes the makeGUI( ) method on the event-dispatching thread. As explained in Chapter 30, Swing-based applets must construct and interact with GUI components only through the event-dispatching thread. The makeGUI( ) Method The makeGUI( ) method sets up the user interface for the applet. It performs the following jobs: 1. It changes the layout manager to GridBagLayout. 2. It instantiates the various GUI components. 3. It adds the components to the grid bag. 4. It adds action listeners for the components. Let’s now look at makeGUI( ) line by line. The method begins with these lines of code: // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); This sequence creates a GridBagLayout layout manager that will be used by the applet. (For details on using GridBagLayout, see Chapter 25.) GrigBagLayout is used because it allows detailed control over the placement of controls within an applet. Next, makeGUI( ) creates the label components, text fields, and Compute button, as shown here: Chapter 33 Financial Applets and Servlets 1025 JLabel heading = new JLabel("Compute Monthly Loan Payments"); JLabel JLabel JLabel JLabel amountLab = new JLabel("Principal "); periodLab = new JLabel("Years "); rateLab = new JLabel("Interest Rate "); paymentLab = new JLabel("Monthly Payments "); amountText = new JTextField(10); periodText = new JTextField(10); paymentText = new JTextField(10); rateText = new JTextField(10); // Payment field for display only. paymentText.setEditable(false); doIt = new JButton("Compute"); // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(amountLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(amountText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(periodLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(periodText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(paymentLab, gbc); Part IV Notice that the text field that displays the monthly payment is set to read-only by calling setEditable(false). This causes the field to be grayed and no text can be entered into the field by the user. However, the contents of the text field can still be set by calling setText( ). Thus, when editing is disabled in a JTextField, the field can be used to display text, but the text cannot be changed by the user. Next, the grid bag constraints for each component are set by the following code sequence: 1026 PART IV Applying Java gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(paymentText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); Although this seems a bit complicated at first glance, it really isn’t. Just remember that each row in the grid is specified separately. Here is how the sequence works. First, the weight of each row, contained in gbc.weighty, is set to 1. This tells the grid bag to distribute extra space evenly when there is more vertical space than needed to hold the components. Next, the gbc.gridwidth is set to REMAINDER, and gbc.anchor is set to NORTH. The label referred to by heading is added by calling setConstraints( ) on gbag. This sequence sets the location of heading to the top of the grid (north) and gives it the remainder of the row. Thus, after this sequence executes, the heading will be at the top of the window and on a row by itself. Next, the four text fields and their labels are added. First, gbc.anchor is set to EAST. This causes each component to be aligned to the right. Next, gbc.gridWidth is set to RELATIVE, and the label is added. Then, gbc.gridWidth is set to REMAINDER, and the text field is added. Thus, each text field and label pair occupies one row. This process repeats until all four text field and label pairs have been added. Finally, the Compute button is added in the center. After the grid bag constraints have been set, the components are actually added to the window by the following code: // Add all the components. add(heading); add(amountLab); add(amountText); add(periodLab); add(periodText); add(rateLab); add(rateText); add(paymentLab); add(paymentText); add(doIt); Next, action listeners are registered for the three input text fields and the Compute button, as shown here: // Register to receive action events. amountText.addActionListener(this); periodText.addActionListener(this); rateText.addActionListener(this); doIt.addActionListener(this); Finally, a NumberFormat object is obtained and the format is set to two decimal digits: // Create a number format. nf = NumberFormat.getInstance(); Chapter 33 Financial Applets and Servlets 1027 nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); The call to the factory method getInstance( ) obtains a NumberFormat object suitable for the default locale. The calls to setMinimumFractionDigits( ) and setMaximumFractionDigits( ) set the minimum and maximum number of decimal digits to be displayed. Because both are set to two, this ensures that two decimal places will always be visible. The actionPerformed( ) Method The actionPerformed( ) method is called whenever the user presses enter when in a text field or clicks the Compute button. This method performs three main functions: it obtains the loan information entered by the user, it calls compute( ) to find the loan payments, and it displays the result. Let’s now examine actionPerformed( ) line by line. After declaring the result variable, actionPerformed( ) begins by obtaining the strings from the three user-input text fields using the following sequence: String amountStr = amountText.getText(); String periodStr = periodText.getText(); String rateStr = rateText.getText(); try { if(amountStr.length() != 0 && periodStr.length() != 0 && rateStr.length() != 0) { Recall that the user must enter the original loan amount, the number of years for the loan, and the interest rate. If all three text fields contain information, then the length of each string will be greater than zero. If the user has entered all the loan data, then the numeric values corresponding to those strings are obtained and stored in the appropriate instance variable. Next, compute( ) is called to compute the loan payment, and the result is displayed in the read-only text field referred to by paymentText, as shown here: principal = Double.parseDouble(amountStr); numYears = Double.parseDouble(periodStr); intRate = Double.parseDouble(rateStr) / 100; result = compute(); paymentText.setText(nf.format(result)); Notice the call to nf.format(result). This causes the value in result to be formatted as previously specified (with two decimal digits) and the resulting string is returned. This string is then used to set the text in the JTextField specified by paymentText. Part IV Next, it begins a try block and then verifies that all three fields actually contain information, as shown here: 1028 PART IV Applying Java If the user has entered a nonnumeric value into one of the text fields, then Double.parseDouble( ) will throw a NumberFormatException. If this happens, an error message will be displayed on the status line and the Payment text field will be emptied, as shown here: showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); paymentText.setText(""); } Otherwise, any previously reported error is removed. The compute( ) Method The calculation of the loan payment takes place in compute( ). It implements the formula shown earlier and operates on the values in principal, intRate, numYears, and payPerYear. It returns the result. NOTE The basic skeleton used by RegPay is used by all the applets shown in this chapter. Finding the Future Value of an Investment Another popular financial calculation finds the future value of an investment given the initial investment, the rate of return, the number of compounding periods per year, and the number of years the investment is held. For example, you might want to know what your retirement account will be worth in 12 years if it currently contains $98,000 and has an average annual rate of return of 6 percent. The FutVal applet developed here will supply the answer. To compute the future value, use the following formula: Future Value = principal * ((rateOfRet / compPerYear) + 1)compPerYear * numYears where rateOfRet specifies the rate of return, principal contains the initial value of the investment, compPerYear specifies the number of compounding periods per year, and numYears specifies the length of the investment in years. If you use an annualized rate of return for rateOfRet, then the number of compounding periods is 1. The following applet called FutVal uses the preceding formula to compute the future value of an investment. The applet produced by this program is shown in Figure 33-2. Aside from the computational differences within the compute( ) method, the applet is similar in operation to the RegPay applet described in the preceding section. // Compute the future value of an investment. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; /* */ Chapter 33 Financial Applets and Servlets 1029 public class FutVal extends JApplet implements ActionListener { JTextField amountText, futvalText, periodText, rateText, compText; JButton doIt; double principal; double rateOfRet; double numYears; int compPerYear; // // // // original principal rate of return length of investment in years number of compoundings per year NumberFormat nf; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { makeGUI(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can't create because of "+ exc); } } // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); JLabel heading = new JLabel("Future Value of an Investment"); JLabel JLabel JLabel JLabel amountLab = new JLabel("Principal "); periodLab = new JLabel("Years "); rateLab = new JLabel("Rate of Return "); futvalLab = new JLabel("Future Value of Investment "); JLabel compLab = new JLabel("Compounding Periods per Year "); amountText periodText futvalText rateText = compText = = new JTextField(10); = new JTextField(10); = new JTextField(10); new JTextField(10); new JTextField(10); // Future value field for display only. futvalText.setEditable(false); Part IV // Set up and initialize the GUI. private void makeGUI() { 1030 PART IV Applying Java doIt = new JButton("Compute"); // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(amountLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(amountText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(periodLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(periodText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(compLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(compText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(futvalLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(futvalText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); add(heading); add(amountLab); add(amountText); add(periodLab); add(periodText); add(rateLab); add(rateText); add(compLab); add(compText); add(futvalLab); add(futvalText); add(doIt); // Register to receive action events. amountText.addActionListener(this); Chapter 33 Financial Applets and Servlets 1031 periodText.addActionListener(this); rateText.addActionListener(this); compText.addActionListener(this); doIt.addActionListener(this); // Create a number format. nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } /* User pressed Enter on a text field or pressed Compute. Display the result if all fields are completed. */ public void actionPerformed(ActionEvent ae) { double result = 0.0; String String String String amountStr periodStr rateStr = compStr = = amountText.getText(); = periodText.getText(); rateText.getText(); compText.getText(); principal = Double.parseDouble(amountStr); numYears = Double.parseDouble(periodStr); rateOfRet = Double.parseDouble(rateStr) / 100; compPerYear = Integer.parseInt(compStr); result = compute(); futvalText.setText(nf.format(result)); } showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); futvalText.setText(""); } } // Compute the future value. double compute() { double b, e; b = (1 + rateOfRet/compPerYear); e = compPerYear * numYears; return principal * Math.pow(b, e); } } Part IV try { if(amountStr.length() != 0 && periodStr.length() != 0 && rateStr.length() != 0 && compStr.length() != 0) { 1032 PART IV Applying Java Figure 33-2 The FutVal applet Finding the Initial Investment Required to Achieve a Future Value Sometimes you will want to know how large an initial investment is required to achieve some future value. For example, if you are saving for your child’s college education and you know that you will need $75,000 in five years, how much money do you need to invest at 7 percent to reach that goal? The InitInv applet developed here can answer that question. The formula to compute an initial investment is shown here: Initial Investment = targetValue / (((rateOfRet /compPerYear) + 1) compPerYear * numYears) where rateOfRet specifies the rate of return, targetValue contains the starting balance, compPerYear specifies the number of compounding periods per year, and numYears specifies the length of the investment in years. If you use an annualized rate of return for rateOfRet, then the number of compounding periods is 1. The following applet called InitInv uses the preceding formula to compute the initial investment required to reach a desired future value. The applet produced by this program is shown in Figure 33-3. /* Compute the initial investment necessary for a specified future value. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; /* */ public class InitInv extends JApplet implements ActionListener { Chapter 33 Financial Applets and Servlets 1033 JTextField targetText, initialText, periodText, rateText, compText; JButton doIt; double targetValue; double rateOfRet; double numYears; int compPerYear; // // // // original targetValue rate of return length of loan in years number of compoundings per year NumberFormat nf; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { makeGUI(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can't create because of "+ exc); } } // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); JLabel heading = new JLabel("Initial Investment Needed for " + "Future Value"); JLabel JLabel JLabel JLabel targetLab = new JLabel("Desired Future Value "); periodLab = new JLabel("Years "); rateLab = new JLabel("Rate of Return "); compLab = new JLabel("Compounding Periods per Year "); JLabel initialLab = new JLabel("Initial Investment Required "); targetText = new JTextField(10); periodText = new JTextField(10); initialText = new JTextField(10); rateText = new JTextField(10); compText = new JTextField(10); // Initial value field for display only. initialText.setEditable(false); doIt = new JButton("Compute"); Part IV // Set up and initialize the GUI. private void makeGUI() { 1034 PART IV Applying Java // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(targetLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(targetText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(periodLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(periodText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(compLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(compText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(initialLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(initialText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); // Add all the components. add(heading); add(targetLab); add(targetText); add(periodLab); add(periodText); add(rateLab); add(rateText); add(compLab); add(compText); add(initialLab); add(initialText); add(doIt); // Register to receive action events. targetText.addActionListener(this); Chapter 33 Financial Applets and Servlets 1035 periodText.addActionListener(this); rateText.addActionListener(this); compText.addActionListener(this); doIt.addActionListener(this); // Create a number format. nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } /* User pressed Enter on a text field or pressed Compute. Display the result if all fields are completed. */ public void actionPerformed(ActionEvent ae) { double result = 0.0; String String String String targetStr periodStr rateStr = compStr = = targetText.getText(); = periodText.getText(); rateText.getText(); compText.getText(); targetValue = Double.parseDouble(targetStr); numYears = Double.parseDouble(periodStr); rateOfRet = Double.parseDouble(rateStr) / 100; compPerYear = Integer.parseInt(compStr); result = compute(); initialText.setText(nf.format(result)); } showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); initialText.setText(""); } } // Compute the required initial investment. double compute() { double b, e; b = (1 + rateOfRet/compPerYear); e = compPerYear * numYears; return targetValue / Math.pow(b, e); } } Part IV try { if(targetStr.length() != 0 && periodStr.length() != 0 && rateStr.length() != 0 && compStr.length() != 0) { 1036 PART IV Applying Java Figure 33-3 The InitInv applet Finding the Initial Investment Needed for a Desired Annuity Another common financial calculation computes the amount of money that you must invest so that a desired annuity, in terms of a regular withdrawal, can be paid. For example, you might decide that you need $5,000 per month at retirement and that you will need that amount for 20 years. The question is how much will you need to invest to secure that annuity? The answer can be found using the following formula: Initial Investment = ((regWD * wdPerYear) / rateOfRet) * (1 – (1 / (rateOfRet / wdPerYear + 1) wdPerYear * numYears)) where rateOfRet specifies the rate of return, regWD contains the desired regular withdrawal, wdPerYear specifies the number of withdrawals per year, and numYears specifies the length of the annuity in years. The Annuity applet shown here computes the initial investment required to produce the desired annuity. The applet produced by this program is shown in Figure 33-4. /* Compute the initial investment necessary for a desired annuity. In other words, it finds the initial amount needed to allow the regular withdrawals of a desired amount over a period of time. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; /* */ public class Annuity extends JApplet implements ActionListener { JTextField regWDText, initialText, periodText, rateText, numWDText; JButton doIt; Chapter 33 double regWDAmount; double rateOfRet; double numYears; int numPerYear; // // // // Financial Applets and Servlets 1037 amount of each withdrawal rate of return length of time in years number of withdrawals per year NumberFormat nf; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { makeGUI(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can't create because of "+ exc); } } // Set up and initialize the GUI. private void makeGUI() { JLabel heading = new JLabel("Initial Investment Needed for " + "Regular Withdrawals"); JLabel JLabel JLabel JLabel regWDLab = new JLabel("Desired Withdrawal "); periodLab = new JLabel("Years "); rateLab = new JLabel("Rate of Return "); numWDLab = new JLabel("Number of Withdrawals per Year "); JLabel initialLab = new JLabel("Initial Investment Required "); regWDText = new JTextField(10); periodText = new JTextField(10); initialText = new JTextField(10); rateText = new JTextField(10); numWDText = new JTextField(10); // Initial investment field for display only. initialText.setEditable(false); doIt = new JButton("Compute"); // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; Part IV // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); 1038 PART IV Applying Java gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(regWDLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(regWDText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(periodLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(periodText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(numWDLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(numWDText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(initialLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(initialText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); // Add all the components. add(heading); add(regWDLab); add(regWDText); add(periodLab); add(periodText); add(rateLab); add(rateText); add(numWDLab); add(numWDText); add(initialLab); add(initialText); add(doIt); // Register to receive text field action events. regWDText.addActionListener(this); periodText.addActionListener(this); rateText.addActionListener(this); numWDText.addActionListener(this); doIt.addActionListener(this); Chapter 33 Financial Applets and Servlets 1039 // Create a number format. nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } /* User pressed Enter on a text field or pressed Compute. Display the result if all fields are completed. */ public void actionPerformed(ActionEvent ae) { double result = 0.0; String String String String regWDStr = regWDText.getText(); periodStr = periodText.getText(); rateStr = rateText.getText(); numWDStr = numWDText.getText(); try { if(regWDStr.length() != 0 && periodStr.length() != 0 && rateStr.length() != 0 && numWDStr.length() != 0) { regWDAmount = Double.parseDouble(regWDStr); numYears = Double.parseDouble(periodStr); rateOfRet = Double.parseDouble(rateStr) / 100; numPerYear = Integer.parseInt(numWDStr); initialText.setText(nf.format(result)); } showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); initialText.setText(""); } } // Compute the required initial investment. double compute() { double b, e; double t1, t2; t1 = (regWDAmount * numPerYear) / rateOfRet; b = (1 + rateOfRet/numPerYear); e = numPerYear * numYears; t2 = 1 - (1 / Math.pow(b, e)); return t1 * t2; } } Part IV result = compute(); 1040 PART IV Applying Java Figure 33-4 The Annuity applet Finding the Maximum Annuity for a Given Investment Another annuity calculation computes the maximum annuity (in terms of a regular withdrawal) available from a given investment over a specified period of time. For example, if you have $500,000 in a retirement account, how much can you take out each month for 20 years, assuming a 6 percent rate of return? The formula that computes the maximum withdrawal is shown here: Maximum Withdrawal = principal * (((rateOfRet / wdPerYear) / (–1 + ((rateOfRet / wdPerYear ) + 1)wdPerYear * numYears)) + (rateOfRet / wdPerYear)) where rateOfRet specifies the rate of return, principal contains the value of the initial investment, wdPerYear specifies the number of withdrawals per year, and numYears specifies the length of the annuity in years. The MaxWD applet shown next computes the maximum periodic withdrawals that can be made over a specified length of time for an assumed rate of return. The applet produced by this program is shown in Figure 33-5. /* Compute the maximum annuity that can be withdrawn from an investment over a period of time. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; /* */ public class MaxWD extends JApplet implements ActionListener { JTextField maxWDText, orgPText, periodText, rateText, numWDText; JButton doIt; Chapter 33 double principal; double rateOfRet; double numYears; int numPerYear; // // // // Financial Applets and Servlets 1041 initial principal annual rate of return length of time in years number of withdrawals per year NumberFormat nf; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { makeGUI(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can't create because of "+ exc); } } // Set up and initialize the GUI. private void makeGUI() { // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); JLabel JLabel JLabel JLabel orgPLab = new JLabel("Original Principal "); periodLab = new JLabel("Years "); rateLab = new JLabel("Rate of Return "); numWDLab = new JLabel("Number of Withdrawals per Year "); JLabel maxWDLab = new JLabel("Maximum Withdrawal "); maxWDText = new JTextField(10); periodText = new JTextField(10); orgPText = new JTextField(10); rateText = new JTextField(10); numWDText = new JTextField(10); // Max withdrawal field for display only. maxWDText.setEditable(false); doIt = new JButton("Compute"); // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); Part IV JLabel heading = new JLabel("Maximum Regular Withdrawals"); 1042 PART IV Applying Java // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(orgPLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(orgPText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(periodLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(periodText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(numWDLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(numWDText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(maxWDLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(maxWDText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); // Add all the components. add(heading); add(orgPLab); add(orgPText); add(periodLab); add(periodText); add(rateLab); add(rateText); add(numWDLab); add(numWDText); add(maxWDLab); add(maxWDText); add(doIt); // Register to receive action events. orgPText.addActionListener(this); periodText.addActionListener(this); rateText.addActionListener(this); numWDText.addActionListener(this); doIt.addActionListener(this); // Create a number format. nf = NumberFormat.getInstance(); Chapter 33 Financial Applets and Servlets 1043 nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } /* User pressed Enter on a text field or pressed Compute. Display the result if all fields are completed. */ public void actionPerformed(ActionEvent ae) { double result = 0.0; String String String String orgPStr = orgPText.getText(); periodStr = periodText.getText(); rateStr = rateText.getText(); numWDStr = numWDText.getText(); try { if(orgPStr.length() != 0 && periodStr.length() != 0 && rateStr.length() != 0 && numWDStr.length() != 0) { principal = Double.parseDouble(orgPStr); numYears = Double.parseDouble(periodStr); rateOfRet = Double.parseDouble(rateStr) / 100; numPerYear = Integer.parseInt(numWDStr); result = compute(); showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); maxWDText.setText(""); } } // Compute the maximum regular withdrawals. double compute() { double b, e; double t1, t2; t1 = rateOfRet / numPerYear; b = (1 + t1); e = numPerYear * numYears; t2 = Math.pow(b, e) - 1; return principal * (t1/t2 + t1); } } Part IV maxWDText.setText(nf.format(result)); } 1044 PART IV Applying Java Figure 33-5 The MaxWD applet Finding the Remaining Balance on a Loan Often, you will want to know the remaining balance on a loan. This is easily calculated if you know the original principal, the interest rate, and the number of payments made. To find the remaining balance, you must sum the payments, subtracting from each payment the amount allocated to interest, and then subtract that result from the principal. The RemBal applet, shown next, finds the remaining balance of a loan. The applet produced by this program is shown in Figure 33-6. // Find the remaining balance on a loan. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; /* */ public class RemBal extends JApplet implements ActionListener { JTextField orgPText, paymentText, remBalText, rateText, numPayText; JButton doIt; double double double double orgPrincipal; intRate; payment; numPayments; // // // // original principal interest rate amount of each payment number of payments made /* Number of payments per year. You could allow this value to be set by the user. */ final int payPerYear = 12; Chapter 33 Financial Applets and Servlets 1045 NumberFormat nf; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { makeGUI(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can't create because of "+ exc); } } // Set up and initialize the GUI private void makeGUI() { // Use a grid bag layout. GridBagLayout gbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbag); JLabel JLabel JLabel JLabel JLabel orgPLab = new JLabel("Original Principal "); paymentLab = new JLabel("Amount of Payment "); numPayLab = new JLabel("Number of Payments Made "); rateLab = new JLabel("Interest Rate "); remBalLab = new JLabel("Remaining Balance "); orgPText = new JTextField(10); paymentText = new JTextField(10); remBalText = new JTextField(10); rateText = new JTextField(10); numPayText = new JTextField(10); // Payment field for display only. remBalText.setEditable(false); doIt = new JButton("Compute"); // Define the grid bag. gbc.weighty = 1.0; // use a row weight of 1 gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTH; gbag.setConstraints(heading, gbc); // Anchor most components to the right. gbc.anchor = GridBagConstraints.EAST; Part IV JLabel heading = new JLabel("Find Loan Balance "); 1046 PART IV Applying Java gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(orgPLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(orgPText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(paymentLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(paymentText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(rateLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(rateText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(numPayLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(numPayText, gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; gbag.setConstraints(remBalLab, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbag.setConstraints(remBalText, gbc); gbc.anchor = GridBagConstraints.CENTER; gbag.setConstraints(doIt, gbc); // Add all the components. add(heading); add(orgPLab); add(orgPText); add(paymentLab); add(paymentText); add(numPayLab); add(numPayText); add(rateLab); add(rateText); add(remBalLab); add(remBalText); add(doIt); // Register to receive action events. orgPText.addActionListener(this); numPayText.addActionListener(this); rateText.addActionListener(this); paymentText.addActionListener(this); doIt.addActionListener(this); Chapter 33 Financial Applets and Servlets 1047 // Create a number format. nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } /* User pressed Enter on a text field or pressed Compute. Display the result if all fields are completed. */ public void actionPerformed(ActionEvent ae) { double result = 0.0; String String String String orgPStr = orgPText.getText(); numPayStr = numPayText.getText(); rateStr = rateText.getText(); payStr = paymentText.getText(); try { if(orgPStr.length() != 0 numPayStr.length() != rateStr.length() != 0 payStr.length() != 0) && 0 && && { orgPrincipal = Double.parseDouble(orgPStr); numPayments = Double.parseDouble(numPayStr); intRate = Double.parseDouble(rateStr) / 100; payment = Double.parseDouble(payStr); remBalText.setText(nf.format(result)); } showStatus(""); // erase any previous error message } catch (NumberFormatException exc) { showStatus("Invalid Data"); remBalText.setText(""); } } // Compute the loan balance. double compute() { double bal = orgPrincipal; double rate = intRate / payPerYear; for(int i = 0; i < numPayments; i++) bal -= payment - (bal * rate); return bal; } } Part IV result = compute(); 1048 PART IV Applying Java Figure 33-6 The RemBal applet Creating Financial Servlets Although applets are easy to create and use, they are only one half of the Java Internet equation. The other half is servlets. Servlets execute on the server side of the connection, and they are more appropriate for some applications. Because many readers may want to use servlets rather than applets in their commercial applications, the remainder of this chapter shows how to convert the financial applets into servlets. Because all the financial applets use the same basic skeleton, we will walk through the conversion of only one applet: RegPay. You can then apply the same basic process to convert any of the other applets into servlets on your own. As you will see, it’s not hard to do. NOTE For information on creating, testing, and running servlets, see Chapter 32. Converting the RegPay Applet into a Servlet It is fairly easy to convert the RegPay loan calculating applet into a servlet. First, the servlet must import the javax.servlet and javax.servlet.http packages. It must also extend HttpServlet, not JApplet. Next, you must remove all the GUI code. Then, you must add the code that obtains the parameters passed to the servlet by the HTML that calls the servlet. Finally, the servlet must send the HTML that displays the results. The basic financial calculations remain the same. It is only the way data is obtained and displayed that changes. The RegPayS Servlet The following RegPayS class is the servlet version of the RegPay applet. As the code is written, it assumes that RegPayS.class will be stored in Tomcat’s example servlets directory, as described in Chapter 32. Remember to enter its name into the web.xml file, also as described in Chapter 32. // A simple loan calculator servlet. import javax.servlet.*; import javax.servlet.http.*; Chapter 33 Financial Applets and Servlets 1049 import java.io.*; import java.text.*; public class RegPayS extends HttpServlet { double principal; // original principal double intRate; // interest rate double numYears; // length of loan in years /* Number of payments per year. You could allow this value to be set by the user. */ final int payPerYear = 12; NumberFormat nf; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String payStr = ""; // Create a number format. nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); the parameters. amountStr = request.getParameter("amount"); periodStr = request.getParameter("period"); rateStr = request.getParameter("rate"); try { if(amountStr != null && periodStr != null && rateStr != null) { principal = Double.parseDouble(amountStr); numYears = Double.parseDouble(periodStr); intRate = Double.parseDouble(rateStr) / 100; payStr = nf.format(compute()); } else { // one or more parameters missing amountStr = ""; periodStr = ""; rateStr = ""; } } catch (NumberFormatException exc) { // Take appropriate action here. } // Set the content type. response.setContentType("text/html"); // Get the output stream. PrintWriter pw = response.getWriter(); Part IV // Get String String String 1050 PART IV Applying Java // Display the necessary HTML. pw.print(" " + "
" + "Enter amount to finance:" + " "); pw.print("
Enter term in years:" + " "); pw.print("
Enter interest rate:" + " "); pw.print("
Monthly Payment:" + " "); pw.print("

"); pw.println("

"); } // Compute the loan payment. double compute() { double numer; double denom; double b, e; numer = intRate * principal / payPerYear; e = -(payPerYear * numYears); b = (intRate / payPerYear) + 1.0; denom = 1.0 - Math.pow(b, e); return numer / denom; } } The first thing to notice about RegPayS is that it has only two methods: doGet( ) and compute( ). The compute( ) method is the same as that used by the applet. The doGet( ) method is defined by the HttpServlet class, which RegPayS extends. This method is called by the server when the servlet must respond to a GET request. Notice that it is passed a reference to the HttpServletRequest and HttpServletResponse objects associated with the request. Chapter 33 Financial Applets and Servlets 1051 From the request parameter, the servlet obtains the arguments associated with the request. It does this by calling getParameter( ). The parameter is returned in its string form. Thus, a numeric value must be manually converted into its binary format. If no parameter is available, a null is returned. From the response object, the servlet obtains a stream to which response information can be written. The response is then returned to the browser by outputting to that stream. Prior to obtaining a PrintWriter to the response stream, the output type should be set to text/html by calling setContentType( ). RegPayS can be called with or without parameters. If called without parameters, the servlet responds with the necessary HTML to display an empty loan calculator form. Otherwise, if called with all needed parameters, RegPayS calculates the loan payment and redisplays the form, with the payment field filled in. The simplest way to invoke RegPayS is to link to its URL without passing any parameters. For example, assuming that you are using Tomcat, you can use this line to execute it:
Loan Calculator
Enter amount to finance:
Enter term in years:
Enter interest rate:
Monthly Payment:

Part IV This displays a link called Loan Calculator that links to the RegPayS servlet in the Tomcat example servlets directory. Notice that no parameters are passed. This causes RegPayS to return the complete HTML that displays an empty loan calculator page. You can also invoke RegPayS by first displaying an empty form manually, if you like. This approach is shown here, again using Tomcat’s example servlets directory: 1052 PART IV Applying Java Some Things to Try The first thing you might want to try is converting the other financial applets into servlets. Because all the financial applets are built on the same skeleton, simply follow the same approach as used by RegPayS. There are many other financial calculations that you might find useful to implement as applets or servlets, such as the rate of return of an investment or the amount of a regular deposit needed over time to reach a future value. You could also print a loan amortization chart. You might want to try creating a larger application that offers all the calculations presented in this chapter, allowing the user to select the desired calculation from a menu. CHAPTER 34 Creating a Download Manager in Java Have you ever had an Internet download interrupted, putting you back at square one? If you have ever connected to the Internet with a dialup connection, it’s very likely that you’ve run into this all too common nuisance. Everything from call-waiting disconnects to computer crashes can leave a download dead in its tracks. However, even with a high-speed connection, transmission disruptions can still occur. To say the least, restarting a download from scratch over and over can be a very time-consuming and frustrating experience. A sometimes overlooked fact is that many interrupted downloads can be resumed. This allows you to recommence downloading from the point at which a download terminates instead of having to begin anew. In this chapter, a tool called Download Manager is developed that manages Internet downloads for you and makes simple work of resuming interrupted downloads. It also lets you pause and then resume a download, and manage multiple downloads, simultaneously. At the core of the Download Manager’s usefulness is its ability to take advantage of downloading only specific portions of a file. In a classic download scenario, a whole file is downloaded from beginning to end. If the transmission of the file is interrupted for any reason, the progress made toward completing the downloading of the file is lost. The Download Manager, however, can pick up from where an interruption occurs and then download only the file’s remaining fragment. Not all downloads are created equal, though, and there are some that simply cannot be restarted. Details on which files are and aren’t resumable are explained in the following section. Not only is the Download Manager a useful utility, it is an excellent illustration of the power and succinctness of Java’s built-in APIs—especially as they apply to interfacing to the Internet. Because the Internet was a driving force behind the creation of Java, it should come as no surprise that Java’s networking capabilities are unsurpassed. For example, attempting to create the Download Manager in another language, such as C++, would entail significantly more trouble and effort. Understanding Internet Downloads To understand and appreciate the Download Manager, it’s necessary to shed some light on how Internet downloads really work. 1053 1054 PART IV Applying Java Internet downloads in their simplest form are merely client/server transactions. The client, your browser, requests to download a file from a server on the Internet. The server then responds by sending the requested file to your browser. In order for clients to communicate with servers, they must have an established protocol for doing so. The most common protocols for downloading files are File Transfer Protocol (FTP) and Hypertext Transfer Protocol (HTTP). FTP is usually associated generically with exchanging files between computers, whereas HTTP is usually associated specifically with transferring web pages and their related files (that is, graphics, sounds, and so on). Over time, as the World Wide Web has grown in popularity, HTTP has become the dominant protocol for downloading files from the Internet. FTP is definitely not extinct, though. For brevity’s sake, the Download Manager developed in this chapter will only support HTTP downloads. Nonetheless, adding support for FTP would be an excellent exercise for extending the code. HTTP downloads come in two forms: resumable (HTTP 1.1) and nonresumable (HTTP 1.0). The difference between these two forms lies in the way files can be requested from servers. With the antiquated HTTP 1.0, a client can only request that a server send it a file, whereas with HTTP 1.1, a client can request that a server send it a complete file or only a specific portion of a file. This is the feature the Download Manager is built on. An Overview of the Download Manager The Download Manager uses a simple yet effective GUI interface built with Java’s Swing libraries. The Download Manager window is shown in Figure 34-1. The use of Swing gives the interface a crisp, modern look and feel. The GUI maintains a list of downloads that are currently being managed. Each download in the list reports its URL, size of the file in bytes, progress as a percentage toward completion, and current status. The downloads can each be in one of the following different states: Downloading, Paused, Complete, Error, or Cancelled. The GUI also has controls for adding downloads to the list and for changing the state of each download in the list. When a download in the list is selected, depending on its current state, it can be paused, resumed, cancelled, or removed from the list altogether. The Download Manager is broken into a few classes for natural separation of functional components. These are the Download, DownloadsTableModel, ProgressRenderer, and DownloadManager classes, respectively. The DownloadManager class is responsible for the GUI interface and makes use of the DownloadsTableModel and ProgressRenderer classes Figure 34-1 The Download Manager GUI interface Chapter 34 Creating a Download Manager in Java 1055 for displaying the current list of downloads. The Download class represents a “managed” download and is responsible for performing the actual downloading of a file. In the following sections, we’ll walk through each of these classes in detail, highlighting their inner workings and explaining how they relate to each other. The Download Class The Download class is the workhorse of the Download Manager. Its primary purpose is to download a file and save that file’s contents to disk. Each time a new download is added to the Download Manager, a new Download object is instantiated to handle the download. The Download Manager has the ability to download multiple files at once. To achieve this, it’s necessary for each of the simultaneous downloads to run independently. It’s also necessary for each individual download to manage its own state so that it can be reflected in the GUI. This is accomplished with the Download class. The entire code for Download is shown here. Notice that it extends Observable and implements Runnable. Each part is examined in detail in the sections that follow. import java.io.*; import java.net.*; import java.util.*; // These are the status names. public static final String STATUSES[] = {"Downloading", "Paused", "Complete", "Cancelled", "Error"}; // These are the status codes. public static final int DOWNLOADING = 0; public static final int PAUSED = 1; public static final int COMPLETE = 2; public static final int CANCELLED = 3; public static final int ERROR = 4; private URL url; // download URL private int size; // size of download in bytes private int downloaded; // number of bytes downloaded private int status; // current status of download // Constructor for Download. public Download(URL url) { this.url = url; size = -1; downloaded = 0; status = DOWNLOADING; // Begin the download. download(); } Part IV // This class downloads a file from a URL. class Download extends Observable implements Runnable { // Max size of download buffer. private static final int MAX_BUFFER_SIZE = 1024; 1056 PART IV Applying Java // Get this download's URL. public String getUrl() { return url.toString(); } // Get this download's size. public int getSize() { return size; } // Get this download's progress. public float getProgress() { return ((float) downloaded / size) * 100; } // Get this download's status. public int getStatus() { return status; } // Pause this download. public void pause() { status = PAUSED; stateChanged(); } // Resume this download. public void resume() { status = DOWNLOADING; stateChanged(); download(); } // Cancel this download. public void cancel() { status = CANCELLED; stateChanged(); } // Mark this download as having an error. private void error() { status = ERROR; stateChanged(); } // Start or resume downloading. private void download() { Thread thread = new Thread(this); thread.start(); } // Get file name portion of URL. private String getFileName(URL url) { Chapter 34 Creating a Download Manager in Java 1057 String fileName = url.getFile(); return fileName.substring(fileName.lastIndexOf('/') + 1); } // Download file. public void run() { RandomAccessFile file = null; InputStream stream = null; try { // Open connection to URL. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Specify what portion of file to download. connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); // Connect to server. connection.connect(); // Check for valid content length. int contentLength = connection.getContentLength(); if (contentLength < 1) { error(); } /* Set the size for this download if it hasn't been already set. */ if (size == -1) { size = contentLength; stateChanged(); } // Open file and seek to the end of it. file = new RandomAccessFile(getFileName(url), "rw"); file.seek(downloaded); stream = connection.getInputStream(); while (status == DOWNLOADING) { /* Size buffer according to how much of the file is left to download. */ byte buffer[]; if (size - downloaded > MAX_BUFFER_SIZE) { buffer = new byte[MAX_BUFFER_SIZE]; } else { buffer = new byte[size - downloaded]; } Part IV // Make sure response code is in the 200 range. if (connection.getResponseCode() / 100 != 2) { error(); } 1058 PART IV Applying Java // Read from server into buffer. int read = stream.read(buffer); if (read == -1) break; // Write buffer to file. file.write(buffer, 0, read); downloaded += read; stateChanged(); } /* Change status to complete if this point was reached because downloading has finished. */ if (status == DOWNLOADING) { status = COMPLETE; stateChanged(); } } catch (Exception e) { error(); } finally { // Close file. if (file != null) { try { file.close(); } catch (Exception e) {} } // Close connection to server. if (stream != null) { try { stream.close(); } catch (Exception e) {} } } } // Notify observers that this download's status has changed. private void stateChanged() { setChanged(); notifyObservers(); } } The Download Variables Download begins by declaring several static final variables that specify the various constants used by the class. Next, four instance variables are declared. The url variable holds the Internet URL for the file being downloaded; the size variable holds the size of the download file in bytes; the downloaded variable holds the number of bytes that have been downloaded thus far; and the status variable indicates the download’s current status. Chapter 34 Creating a Download Manager in Java 1059 The Download Constructor Download’s constructor is passed a reference to the URL to download in the form of a URL object, which is assigned to the url instance variable. It then sets the remaining instance variables to their initial states and calls the download( ) method. Notice that size is set to –1 to indicate there is no size yet. The download( ) Method The download( ) method creates a new Thread object, passing it a reference to the invoking Download instance. As mentioned before, it’s necessary for each download to run independently. In order for the Download class to act alone, it must execute in its own thread. Java has excellent built-in support for threads and makes using them a snap. To use threads, the Download class simply implements the Runnable interface by overriding the run( ) method. After the download( ) method has instantiated a new Thread instance, passing its constructor the Runnable Download class, it calls the thread’s start( ) method. Invoking the start( ) method causes the Runnable instance’s (the Download class’) run( ) method to be executed. The run( ) Method RandomAccessFile file = null; InputStream stream = null; try { // Open connection to URL. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); First, run( ) sets up variables for the network stream that the download’s contents will be read from and sets up the file that the download’s contents will be written to. Next, a connection to the download’s URL is opened by calling url.openConnection( ). Since we know that the Download Manager supports only HTTP downloads, the connection is cast to the HttpURLConnection type. Casting the connection as an HttpURLConnection allows us to take advantage of HTTP-specific connection features such as the getResponseCode( ) method. Note that calling url.openConnection( ) does not actually create a connection to the URL’s server. It simply creates a new URLConnection instance associated with the URL that later will be used to connect to the server. After the HttpURLConnection has been created, the connection request property is set by calling connection.setRequestProperty( ), as shown here: // Specify what portion of file to download. connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); Part IV When the run( ) method executes, the actual downloading gets under way. Because of its size and importance, we will examine it closely, line by line. The run( ) method begins with these lines: 1060 PART IV Applying Java Setting request properties allows extra request information to be sent to the server the download will be coming from. In this case, the "Range" property is set. This is critically important, as the "Range" property specifies the range of bytes that is being requested for download from the server. Normally, all of a file’s bytes are downloaded at once. However, if a download has been interrupted or paused, only the download’s remaining bytes should be retrieved. Setting the "Range" property is the foundation for the Download Manager’s operation. The "Range" property is specified in this form: start-byte – end-byte For example, "0 – 12345". However, the end byte of the range is optional. If the end byte is absent, the range ends at the end of the file. The run( ) method never specifies the end byte because downloads must run until the entire range is downloaded, unless paused or interrupted. The next few lines are shown here: // Connect to server. connection.connect(); // Make sure response code is in the 200 range. if (connection.getResponseCode() / 100 != 2) { error(); } // Check for valid content length. int contentLength = connection.getContentLength(); if (contentLength < 1) { error(); } The connection.connect( ) method is called to make the actual connection to the download’s server. Next, the response code returned by the server is checked. The HTTP protocol has a list of response codes that indicate a server’s response to a request. HTTP response codes are organized into numeric ranges of 100, and the 200 range indicates success. The server’s response code is validated for being in the 200 range by calling connection.getResponseCode( ) and dividing by 100. If the value of this division is 2, then the connection was successful. Next, run( ) gets the content length by calling connection.getContentLength( ). The content length represents the number of bytes in the requested file. If the content length is less than 1, the error( ) method is called. The error( ) method updates the download’s status to ERROR, and then calls stateChanged( ). The stateChanged( ) method will be described in detail later. After getting the content length, the following code checks to see if it has already been assigned to the size variable: /* Set the size for this download if it hasn't been already set. */ if (size == -1) { size = contentLength; stateChanged(); } Chapter 34 Creating a Download Manager in Java 1061 As you can see, instead of assigning the content length to the size variable unconditionally, it only gets assigned if it hasn’t already been given a value. The reason for this is because the content length reflects how many bytes the server will be sending. If anything other than a 0-based start range is specified, the content length will represent only a portion of the file’s size. The size variable has to be set to the complete size of the download’s file. The next few lines of code shown here create a new RandomAccessFile using the filename portion of the download’s URL that is retrieved with a call to the getFileName( ) method: // Open file and seek to the end of it. file = new RandomAccessFile(getFileName(url), "rw"); file.seek(downloaded); The RandomAccessFile is opened in "rw" mode, which specifies that the file can be written to and read from. Once the file is open, run( ) seeks to the end of the file by calling the file.seek( ) method, passing in the downloaded variable. This tells the file to position itself at the number of bytes that have been downloaded—in other words, at the end. It’s necessary to position the file at the end in case a download has been resumed. If a download is resumed, the newly downloaded bytes are appended to the file and they don’t overwrite any previously downloaded bytes. After preparing the output file, a network stream handle to the open server connection is obtained by calling connection.getInputStream( ), as shown here: stream = connection.getInputStream(); while (status == DOWNLOADING) { /* Size buffer according to how much of the file is left to download. */ byte buffer[]; if (size - downloaded > MAX_BUFFER_SIZE) { buffer = new byte[MAX_BUFFER_SIZE]; } else { buffer = new byte[size - downloaded]; } // Read from server into buffer. int read = stream.read(buffer); if (read == -1) break; // Write buffer to file. file.write(buffer, 0, read); downloaded += read; stateChanged(); } This loop is set up to run until the download’s status variable changes from DOWNLOADING. Inside the loop, a byte buffer array is created to hold the bytes that will be downloaded. The buffer is sized according to how much of the download is left to complete. If there is more left to download than the MAX_BUFFER_SIZE, the Part IV The heart of all the action begins next with a while loop: 1062 PART IV Applying Java MAX_BUFFER_SIZE is used to size the buffer. Otherwise, the buffer is sized exactly at the number of bytes left to download. Once the buffer is sized appropriately, the downloading takes place with a stream.read( ) call. This call reads bytes from the server and places them into the buffer, returning the count of how many bytes were actually read. If the number of bytes read equals –1, then downloading has completed and the loop is exited. Otherwise, downloading is not finished and the bytes that have been read are written to disk with a call to file.write( ). Then the downloaded variable is updated, reflecting the number of bytes downloaded thus far. Finally, inside the loop, the stateChanged( ) method is invoked. More on this later. After the loop has exited, the following code checks to see why the loop was exited: /* Change status to complete if this point was reached because downloading has finished. */ if (status == DOWNLOADING) { status = COMPLETE; stateChanged(); } If the download’s status is still DOWNLOADING, this means that the loop exited because downloading has been completed. Otherwise, the loop was exited because the download’s status changed to something other than DOWNLOADING. The run( ) method wraps up with the catch and finally blocks shown here: } catch (Exception e) { error(); } finally { // Close file. if (file != null) { try { file.close(); } catch (Exception e) {} } // Close connection to server. if (stream != null) { try { stream.close(); } catch (Exception e) {} } } If an exception is thrown during the download process, the catch block captures the exception and calls the error( ) method. The finally block ensures that if the file and stream connections have been opened, they get closed whether an exception has been thrown or not. As an exercise, you might try changing this code to use the new try-withresources statement to manage these resources. The stateChanged( ) Method In order for the Download Manager to display up-to-date information on each of the downloads it’s managing, it has to know each time a download’s information changes. To handle this, the Observer software design pattern is used. The Observer pattern is analogous Chapter 34 Creating a Download Manager in Java 1063 to an announcement’s mailing list where several people register to receive announcements. Each time there’s a new announcement, each person on the list receives a message with the announcement. In the Observer pattern’s case, there’s an observed class with which observer classes can register themselves to receive change notifications. The Download class employs the Observer pattern by extending Java’s built-in Observable utility class. Extending the Observable class allows classes that implement Java’s Observer interface to register themselves with the Download class to receive change notifications. Each time the Download class needs to notify its registered Observers of a change, the stateChanged( ) method is invoked. The stateChanged( ) method first calls the Observable class’ setChanged( ) method to flag the class as having been changed. Next, the stateChanged( ) method calls Observable’s notifyObservers( ) method, which broadcasts the change notification to the registered Observers. Action and Accessor Methods The Download class has numerous action and accessor methods for controlling a download and getting data from it. Each of the pause( ), resume( ), and cancel( ) action methods simply does as its name implies: pauses, resumes, or cancels the download, respectively. Similarly, the error( ) method marks the download as having an error. The getUrl( ), getSize( ), getProgress( ), and getStatus( ) accessor methods each return their current respective values. The ProgressRenderer class is a small utility class that is used to render the current progress of a download listed in the GUI’s "Downloads" JTable instance. Normally, a JTable instance renders each cell’s data as text. However, often it’s particularly useful to render a cell’s data as something other than text. In the Download Manager’s case, we want to render each of the table’s Progress column cells as progress bars. The ProgressRenderer class shown here makes that possible. Notice that it extends JProgressBar and implements TableCellRenderer: import java.awt.*; import javax.swing.*; import javax.swing.table.*; // This class renders a JProgressBar in a table cell. class ProgressRenderer extends JProgressBar implements TableCellRenderer { // Constructor for ProgressRenderer. public ProgressRenderer(int min, int max) { super(min, max); } /* Returns this JProgressBar as the renderer for the given table cell. */ public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { // Set JProgressBar's percent complete value. Part IV The ProgressRenderer Class 1064 PART IV Applying Java setValue((int) ((Float) value).floatValue()); return this; } } The ProgressRenderer class takes advantage of the fact that Swing’s JTable class has a rendering system that can accept “plug-ins” for rendering table cells. To plug into this rendering system, first, the ProgressRenderer class has to implement Swing’s TableCellRenderer interface. Second, a ProgressRenderer instance has to be registered with a JTable instance; doing so instructs the JTable instance as to which cells should be rendered with the “plug-in.” Implementing the TableCellRenderer interface requires the class to override the getTableCellRendererComponent( ) method. The getTableCellRendererComponent( ) method is invoked each time a JTable instance goes to render a cell for which this class has been registered. This method is passed several variables, but in this case, only the value variable is used. The value variable holds the data for the cell being rendered and is passed to JProgressBar’s setValue( ) method. The getTableCellRendererComponent( ) method wraps up by returning a reference to its class. This works because the ProgressRenderer class is a subclass of JProgressBar, which is a descendent of the AWT Component class. The DownloadsTableModel Class The DownloadsTableModel class houses the Download Manager’s list of downloads and is the backing data source for the GUI’s "Downloads" JTable instance. The DownloadsTableModel class is shown here. Notice that it extends AbstractTableModel and implements the Observer interface: import java.util.*; import javax.swing.*; import javax.swing.table.*; // This class manages the download table's data. class DownloadsTableModel extends AbstractTableModel implements Observer { // These are the names for the table's columns. private static final String[] columnNames = {"URL", "Size", "Progress", "Status"}; // These are the classes for each column's values. private static final Class[] columnClasses = {String.class, String.class, JProgressBar.class, String.class}; // The table's list of downloads. private ArrayList downloadList = new ArrayList(); // Add a new download to the table. public void addDownload(Download download) { // Register to be notified when the download changes. download.addObserver(this); Chapter 34 Creating a Download Manager in Java 1065 downloadList.add(download); // Fire table row insertion notification to table. fireTableRowsInserted(getRowCount() - 1, getRowCount()- 1); } // Get a download for the specified row. public Download getDownload(int row) { return downloadList.get(row); } // Remove a download from the list. public void clearDownload(int row) { downloadList.remove(row); // Fire table row deletion notification to table. fireTableRowsDeleted(row, row); } // Get table's column count. public int getColumnCount() { return columnNames.length; } // Get a column's class. public Class getColumnClass(int col) { return columnClasses[col]; } // Get table's row count. public int getRowCount() { return downloadList.size(); } // Get value for a specific row and column combination. public Object getValueAt(int row, int col) { Download download = downloadList.get(row); switch (col) { case 0: // URL return download.getUrl(); case 1: // Size int size = download.getSize(); return (size == -1) ? "" : Integer.toString(size); case 2: // Progress return new Float(download.getProgress()); case 3: // Status return Download.STATUSES[download.getStatus()]; } return ""; } Part IV // Get a column's name. public String getColumnName(int col) { return columnNames[col]; } 1066 PART IV Applying Java /* Update is called when a Download notifies its observers of any changes */ public void update(Observable o, Object arg) { int index = downloadList.indexOf(o); // Fire table row update notification to table. fireTableRowsUpdated(index, index); } } The DownloadsTableModel class essentially is a utility class utilized by the "Downloads" JTable instance for managing data in the table. When the JTable instance is initialized, it is passed a DownloadsTableModel instance. The JTable then proceeds to call several methods on the DownloadsTableModel instance to populate itself. The getColumnCount( ) method is called to retrieve the number of columns in the table. Similarly, getRowCount( ) is used to retrieve the number of rows in the table. The getColumnName( ) method returns a column’s name given its ID. The getDownload( ) method takes a row ID and returns the associated Download object from the list. The rest of the DownloadsTableModel class’ methods, which are more involved, are detailed in the following sections. The addDownload( ) Method The addDownload( ) method, shown here, adds a new Download object to the list of managed downloads and consequently a row to the table: // Add a new download to the table. public void addDownload(Download download) { // Register to be notified when the download changes. download.addObserver(this); downloadList.add(download); // Fire table row insertion notification to table. fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1); } This method first registers itself with the new Download as an Observer interested in receiving change notifications. Next, the Download is added to the internal list of downloads being managed. Finally, a table row insertion event notification is fired to alert the table that a new row has been added. The clearDownload( ) Method The clearDownload( ) method, shown next, removes a Download from the list of managed downloads: // Remove a download from the list. public void clearDownload(int row) { downloadList.remove(row); // Fire table row deletion notification to table. fireTableRowsDeleted(row, row); } Chapter 34 Creating a Download Manager in Java 1067 After removing the Download from the internal list, a table row deleted event notification is fired to alert the table that a row has been deleted. The getColumnClass( ) Method The getColumnClass( ) method, shown here, returns the class type for the data displayed in the specified column: // Get a column's class. public Class getColumnClass(int col) { return columnClasses[col]; } All columns are displayed as text (that is, String objects) except for the Progress column, which is displayed as a progress bar (which is an object of type JProgressBar). The getValueAt( ) Method // Get value for a specific row and column combination. public Object getValueAt(int row, int col) { Download download = downloadList.get(row); switch (col) { case 0: // URL return download.getUrl(); case 1: // Size int size = download.getSize(); return (size == -1) ? "" : Integer.toString(size); case 2: // Progress return new Float(download.getProgress()); case 3: // Status return Download.STATUSES[download.getStatus()]; } return ""; } This method first looks up the Download corresponding to the row specified. Next, the column specified is used to determine which one of the Download’s property values to return. The update( ) Method The update( ) method is shown here. It fulfills the Observer interface contract allowing the DownloadsTableModel class to receive notifications from Download objects when they change. /* Update is called when a Download notifies its observers of any changes. */ public void update(Observable o, Object arg) { int index = downloadList.indexOf(o); Part IV The getValueAt( ) method, shown next, is called to get the current value that should be displayed for each of the table’s cells: 1068 PART IV Applying Java // Fire table row update notification to table. fireTableRowsUpdated(index, index); } This method is passed a reference to the Download that has changed, in the form of an Observable object. Next, an index to that download is looked up in the list of downloads, and that index is then used to fire a table row update event notification, which alerts the table that the given row has been updated. The table will then rerender the row with the given index, reflecting its new values. The DownloadManager Class Now that the foundation has been laid by explaining each of the Download Manager’s helper classes, we can look closely at the DownloadManager class. The DownloadManager class is responsible for creating and running the Download Manager’s GUI. This class has a main( ) method declared, so on execution it will be invoked first. The main( ) method instantiates a new DownloadManager class instance and then calls its show( ) method, which causes it to be displayed. The DownloadManager class is shown here. Notice that it extends JFrame and implements Observer. The following sections examine it in detail. import import import import import import java.awt.*; java.awt.event.*; java.net.*; java.util.*; javax.swing.*; javax.swing.event.*; // The Download Manager. public class DownloadManager extends JFrame implements Observer { // Add download text field. private JTextField addTextField; // Download table's data model. private DownloadsTableModel tableModel; // Table listing downloads. private JTable table; // These are the buttons for managing the selected download. private JButton pauseButton, resumeButton; private JButton cancelButton, clearButton; // Currently selected download. private Download selectedDownload; // Flag for whether or not table selection is being cleared. private boolean clearing; Chapter 34 Creating a Download Manager in Java 1069 // Constructor for Download Manager. public DownloadManager() { // Set application title. setTitle("Download Manager"); // Set window size. setSize(640, 480); // Set up file menu. JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); JMenuItem fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X); fileExitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionExit(); } }); fileMenu.add(fileExitMenuItem); menuBar.add(fileMenu); setJMenuBar(menuBar); // Set up add panel. JPanel addPanel = new JPanel(); addTextField = new JTextField(30); addPanel.add(addTextField); JButton addButton = new JButton("Add Download"); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionAdd(); } }); addPanel.add(addButton); // Set up Downloads table. tableModel = new DownloadsTableModel(); table = new JTable(tableModel); table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { tableSelectionChanged(); } }); // Allow only one row at a time to be selected. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); Part IV // Handle window closing events. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { actionExit(); } }); 1070 PART IV Applying Java // Set up ProgressBar as renderer for progress column. ProgressRenderer renderer = new ProgressRenderer(0, 100); renderer.setStringPainted(true); // show progress text table.setDefaultRenderer(JProgressBar.class, renderer); // Set table's row height large enough to fit JProgressBar. table.setRowHeight( (int) renderer.getPreferredSize().getHeight()); // Set up downloads panel. JPanel downloadsPanel = new JPanel(); downloadsPanel.setBorder( BorderFactory.createTitledBorder("Downloads")); downloadsPanel.setLayout(new BorderLayout()); downloadsPanel.add(new JScrollPane(table), BorderLayout.CENTER); // Set up buttons panel. JPanel buttonsPanel = new JPanel(); pauseButton = new JButton("Pause"); pauseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionPause(); } }); pauseButton.setEnabled(false); buttonsPanel.add(pauseButton); resumeButton = new JButton("Resume"); resumeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionResume(); } }); resumeButton.setEnabled(false); buttonsPanel.add(resumeButton); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionCancel(); } }); cancelButton.setEnabled(false); buttonsPanel.add(cancelButton); clearButton = new JButton("Clear"); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { actionClear(); } }); clearButton.setEnabled(false); buttonsPanel.add(clearButton); Chapter 34 Creating a Download Manager in Java 1071 // Add panels to display. getContentPane().setLayout(new BorderLayout()); getContentPane().add(addPanel, BorderLayout.NORTH); getContentPane().add(downloadsPanel, BorderLayout.CENTER); getContentPane().add(buttonsPanel, BorderLayout.SOUTH); } // Exit this program. private void actionExit() { System.exit(0); } // Verify download URL. private URL verifyUrl(String url) { // Only allow HTTP URLs. if (!url.toLowerCase().startsWith("http://")) return null; // Verify format of URL. URL verifiedUrl = null; try { verifiedUrl = new URL(url); } catch (Exception e) { return null; } // Make sure URL specifies a file. if (verifiedUrl.getFile().length() < 2) return null; return verifiedUrl; } // Called when table row selection changes. private void tableSelectionChanged() { /* Unregister from receiving notifications from the last selected download. */ if (selectedDownload != null) selectedDownload.deleteObserver(DownloadManager.this); Part IV // Add a new download. private void actionAdd() { URL verifiedUrl = verifyUrl(addTextField.getText()); if (verifiedUrl != null) { tableModel.addDownload(new Download(verifiedUrl)); addTextField.setText(""); // reset add text field } else { JOptionPane.showMessageDialog(this, "Invalid Download URL", "Error", JOptionPane.ERROR_MESSAGE); } } 1072 PART IV Applying Java /* If not in the middle of clearing a download, set the selected download and register to receive notifications from it. */ if (!clearing && table.getSelectedRow() > -1) { selectedDownload = tableModel.getDownload(table.getSelectedRow()); selectedDownload.addObserver(DownloadManager.this); updateButtons(); } } // Pause the selected download. private void actionPause() { selectedDownload.pause(); updateButtons(); } // Resume the selected download. private void actionResume() { selectedDownload.resume(); updateButtons(); } // Cancel the selected download. private void actionCancel() { selectedDownload.cancel(); updateButtons(); } // Clear the selected download. private void actionClear() { clearing = true; tableModel.clearDownload(table.getSelectedRow()); clearing = false; selectedDownload = null; updateButtons(); } /* Update each button's state based off of the currently selected download's status. */ private void updateButtons() { if (selectedDownload != null) { int status = selectedDownload.getStatus(); switch (status) { case Download.DOWNLOADING: pauseButton.setEnabled(true); resumeButton.setEnabled(false); cancelButton.setEnabled(true); clearButton.setEnabled(false); break; case Download.PAUSED: pauseButton.setEnabled(false); resumeButton.setEnabled(true); cancelButton.setEnabled(true); Chapter 34 Creating a Download Manager in Java 1073 clearButton.setEnabled(false); break; case Download.ERROR: pauseButton.setEnabled(false); resumeButton.setEnabled(true); cancelButton.setEnabled(false); clearButton.setEnabled(true); break; default: // COMPLETE or CANCELLED pauseButton.setEnabled(false); resumeButton.setEnabled(false); cancelButton.setEnabled(false); clearButton.setEnabled(true); } } else { // No download is selected in table. pauseButton.setEnabled(false); resumeButton.setEnabled(false); cancelButton.setEnabled(false); clearButton.setEnabled(false); } /* Update is called when a Download notifies its observers of any changes. */ public void update(Observable o, Object arg) { // Update buttons if the selected download has changed. if (selectedDownload != null && selectedDownload.equals(o)) SwingUtilities.invokeLater(new Runnable() { public void run() { updateButtons(); } }); } // Run the Download Manager. public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { DownloadManager manager = new DownloadManager(); manager.setVisible(true); } }); } } The DownloadManager Variables DownloadManager starts off by declaring several instance variables, most of which hold references to the GUI controls. The selectedDownload variable holds a reference to the Download object represented by the selected row in the table. Finally, the clearing instance variable is a boolean flag that tracks whether or not a download is currently being cleared from the Downloads table. Part IV } 1074 PART IV Applying Java The DownloadManager Constructor When the DownloadManager is instantiated, all of the GUI’s controls are initialized inside its constructor. The constructor contains a lot of code, but most of it is straightforward. The following discussion gives an overview. First, the window’s title is set with a call to setTitle( ). Next, the setSize( ) call establishes the window’s width and height in pixels. After that, a window listener is added by calling addWindowListener( ), passing a WindowAdapter object that overrides the windowClosing( ) event handler. This handler calls the actionExit( ) method when the application’s window is closed. Next, a menu bar with a "File" menu is added to the application’s window. Then the "add" panel, which has the text field and button, is set up. An ActionListener is added to the "Add Download" button so that the actionAdd( ) method is called each time the button is clicked. The downloads table is constructed next. A ListSelectionListener is added to the table so that each time a row is selected in the table, the tableSelectionChanged( ) method is invoked. The table’s selection mode is also updated to ListSelectionModel.SINGLE_SELECTION so that only one row at a time can be selected in the table. Limiting row selection to only one row at a time simplifies the logic for determining which buttons should be enabled in the GUI when a row in the download table is selected. Next, a ProgressRenderer class is instantiated and registered with the table to handle the "Progress" column. The table’s row height is updated to the ProgressRenderer’s height by calling table.setRowHeight( ). After the table has been assembled and tweaked, it is wrapped in a JScrollPane to make it scrollable and then added to a panel. Finally, the buttons panel is created. The buttons panel has Pause, Resume, Cancel, and Clear buttons. Each of the buttons adds an ActionListener that invokes its respective action method when it is clicked. After creating the buttons panel, all of the panels that have been created are added to the window. The verifyUrl( ) Method The verifyUrl( ) method is called by the actionAdd( ) method each time a download is added to the Download Manager. The verifyUrl( ) method is shown here: // Verify download URL. private URL verifyUrl(String url) { // Only allow HTTP URLs. if (!url.toLowerCase().startsWith("http://")) return null; // Verify format of URL. URL verifiedUrl = null; try { verifiedUrl = new URL(url); } catch (Exception e) { return null; } // Make sure URL specifies a file. if (verifiedUrl.getFile().length() < 2) return null; Chapter 34 Creating a Download Manager in Java 1075 return verifiedUrl; } This method first verifies that the URL entered is an HTTP URL since only HTTP is supported. Next, the URL being verified is used to construct a new URL class instance. If the URL is malformed, the URL class constructor will throw an exception. Finally, this method verifies that a file is actually specified in the URL. The tableSelectionChanged( ) Method The tableSelectionChanged( ) method, shown here, is called each time a row is selected in the downloads table: /* If not in the middle of clearing a download, set the selected download and register to receive notifications from it. */ if (!clearing && table.getSelectedRow() > -1) { selectedDownload = tableModel.getDownload(table.getSelectedRow()); selectedDownload.addObserver(DownloadManager.this); updateButtons(); } } This method starts by seeing if there is already a row currently selected by checking if the selectedDownload variable is null. If the selectedDownload variable is not null, DownloadManager removes itself as an observer of the download so that it no longer receives change notifications. Next the clearing flag is checked. If the table is not empty and the clearing flag is false, then first the selectedDownload variable is updated with the Download corresponding to the row selected. Second, the DownloadManager is registered as an Observer with the newly selected Download. Finally, updateButtons( ) is called to update the button states based on the selected Download’s state. The updateButtons( ) Method The updateButtons( ) method updates the state of all the buttons on the button panel based on the state of the selected download. The updateButtons( ) method is shown here: /* Update each button's state based on the currently selected download's status. */ private void updateButtons() { if (selectedDownload != null) { int status = selectedDownload.getStatus(); switch (status) { case Download.DOWNLOADING: pauseButton.setEnabled(true); Part IV // Called when table row selection changes. private void tableSelectionChanged() { /* Unregister from receiving notifications from the last selected download. */ if (selectedDownload != null) selectedDownload.deleteObserver(DownloadManager.this); 1076 PART IV Applying Java resumeButton.setEnabled(false); cancelButton.setEnabled(true); clearButton.setEnabled(false); break; case Download.PAUSED: pauseButton.setEnabled(false); resumeButton.setEnabled(true); cancelButton.setEnabled(true); clearButton.setEnabled(false); break; case Download.ERROR: pauseButton.setEnabled(false); resumeButton.setEnabled(true); cancelButton.setEnabled(false); clearButton.setEnabled(true); break; default: // COMPLETE or CANCELLED pauseButton.setEnabled(false); resumeButton.setEnabled(false); cancelButton.setEnabled(false); clearButton.setEnabled(true); } } else { // No download is selected in table. pauseButton.setEnabled(false); resumeButton.setEnabled(false); cancelButton.setEnabled(false); clearButton.setEnabled(false); } } If no download is selected in the downloads table, all of the buttons are disabled, giving them a grayed-out appearance. However, if there is a selected download, each button’s state will be set based on whether the Download object has a status of DOWNLOADING, PAUSED, ERROR, COMPLETE, or CANCELLED. Handling Action Events Each of DownloadManager’s GUI controls registers an ActionListener that invokes its respective action method. ActionListeners are triggered each time an action event takes place on a GUI control. For example, when a button is clicked, an ActionEvent is generated and each of the button’s registered ActionListeners is notified. You may have noticed a similarity between the way ActionListeners work and the Observer pattern discussed earlier. That is because they are the same pattern with two different naming schemes. Compiling and Running the Download Manager Compile DownloadManager like this: javac DownloadManager.java DownloadsTableModel.java ProgressRenderer.java Download.java Run DownloadManager like this: javaw DownloadManager Chapter 34 Creating a Download Manager in Java 1077 The Download Manager is easy to use. First, enter the URL of a file that you want to download in the text field at the top of the screen. For example, to download a file called 0072229713_code.zip from the McGraw-Hill web site enter http://www.mhprofessional.com/downloads/products/0072229713/0072229713_code.zip This is the file that contains the code for my book The Art of Java, which I co-authored with James Holmes. After adding a download to the Download Manager, you can manage it by selecting it in the table. Once selected, you can pause, cancel, resume, and clear a download. Figure 34-2 shows the Download Manager in action. The Download Manager as it stands is fully functional, with the ability to pause and resume downloads as well as download multiple files at once; however, there are several enhancements that you may want to try on your own. Here are some ideas: proxy server support, FTP and HTTPS support, and drag-and-drop support. A particularly appealing enhancement is a scheduling feature that lets you schedule a download at a specific time, perhaps in the middle of the night when system resources are plentiful. Note that the techniques illustrated in this chapter are not limited to downloading files in the typical sense. There are many other practical uses for the code. For example, many software programs distributed over the Internet come in two pieces. The first piece is a small, compact application that can be downloaded quickly. This small application contains a mini download manager for downloading the second piece, which is generally much larger. This concept is quite useful, especially as the size of applications increases, which typically leads to an increase in the potential for download interruptions. You might want to try adapting the Download Manager for this purpose. Figure 34-2 The Download Manager in action Part IV Enhancing the Download Manager This page intentionally left blank APPENDIX Using Java’s Documentation Comments As explained in Part I, Java supports three types of comments. The first two are the // and the /* */. The third type is called a documentation comment. It begins with the character sequence /**. It ends with */. Documentation comments allow you to embed information about your program into the program itself. You can then use the javadoc utility program (supplied with the JDK) to extract the information and put it into an HTML file. Documentation comments make it convenient to document your programs. You have almost certainly seen documentation generated with javadoc, because that is the way the Java API library was documented. The javadoc Tags The javadoc utility recognizes the following tags: Tag Meaning @author Identifies the author. {@code} Displays information as-is, without processing HTML styles, in code font. @deprecated Specifies that a program element is deprecated. {@docRoot} Specifies the path to the root directory of the current documentation. @exception Identifies an exception thrown by a method or constructor. {@inheritDoc} Inherits a comment from the immediate superclass. {@link} Inserts an in-line link to another topic. {@linkplain} Inserts an in-line link to another topic, but the link is displayed in a plaintext font. {@literal} Displays information as is, without processing HTML styles. @param Documents a parameter. @return Documents a method’s return value. @see Specifies a link to another topic. 1079 1080 PART IV Applying Java Tag Meaning @serial Documents a default serializable field. @serialData Documents the data written by the writeObject( ) or writeExternal( ) methods. @serialField Documents an ObjectStreamField component. @since States the release when a specific change was introduced. @throws Same as @exception. {@value} Displays the value of a constant, which must be a static field. @version Specifies the version of a class. Document tags that begin with an “at” sign (@) are called stand-alone tags (also called block tags), and they must be used on their own line. Tags that begin with a brace, such as {@code}, are called in-line tags, and they can be used within a larger description. You may also use other, standard HTML tags in a documentation comment. However, some tags, such as headings, should not be used because they disrupt the look of the HTML file produced by javadoc. As it relates to documenting source code, you can use documentation comments to document classes, interfaces, fields, constructors, and methods. In all cases, the documentation comment must immediately precede the item being documented. Some tags, such as @see, @since, and @deprecated, can be used to document any element. Other tags apply only to the relevant elements. Each tag is examined next. NOTE Documentation comments can also be used for documenting a package and preparing an overview, but the procedures differ from those used to document source code. See the javadoc documentation for details on these uses. @author The @author tag documents the author of a class or interface. It has the following syntax: @author description Here, description will usually be the name of the author. You will need to specify the -author option when executing javadoc in order for the @author field to be included in the HTML documentation. {@code} The {@code} tag enables you to embed text, such as a snippet of code, into a comment. That text is then displayed as is in code font, without any further processing, such as HTML rendering. It has the following syntax: {@code code-snippet} @deprecated The @deprecated tag specifies that a program element is deprecated. It is recommended that you include @see or {@link} tags to inform the programmer about available alternatives. The syntax is the following: @deprecated description Appendix Using Java’s Documentation Comments 1081 Here, description is the message that describes the deprecation. The @deprecated tag can be used in documentation for fields, methods, constructors, classes, and interfaces. {@docRoot} {@docRoot} specifies the path to the root directory of the current documentation. @exception The @exception tag describes an exception to a method. It has the following syntax: @exception exception-name explanation Here, the fully qualified name of the exception is specified by exception-name, and explanation is a string that describes how the exception can occur. The @exception tag can only be used in documentation for a method or constructor. {@inheritDoc} This tag inherits a comment from the immediate superclass. {@link} The {@link} tag provides an in-line link to additional information. It has the following syntax: Here, pkg.class#member specifies the name of a class or method to which a link is added, and text is the string that is displayed. {@linkplain} Inserts an in-line link to another topic. The link is displayed in plain-text font. Otherwise, it is similar to {@link}. {@literal} The {@literal} tag enables you to embed text into a comment. That text is then displayed as is, without any further processing, such as HTML rendering. It has the following syntax: {@literal description} Here, description is the text that is embedded. @param The @param tag documents a parameter. It has the following syntax: @param parameter-name explanation Here, parameter-name specifies the name of a parameter. The meaning of that parameter is described by explanation. The @param tag can be used only in documentation for a method or constructor, or a generic class or interface. Part IV {@link pkg.class#member text} 1082 PART IV Applying Java @return The @return tag describes the return value of a method. It has the following syntax: @return explanation Here, explanation describes the type and meaning of the value returned by a method. The @return tag can be used only in documentation for a method. @see The @see tag provides a reference to additional information. Its most commonly used forms are shown here: @see anchor @see pkg.class#member text In the first form, anchor is a link to an absolute or relative URL. In the second form, pkg.class#member specifies the name of the item, and text is the text displayed for that item. The text parameter is optional, and if not used, then the item specified by pkg.class#member is displayed. The member name, too, is optional. Thus, you can specify a reference to a package, class, or interface in addition to a reference to a specific method or field. The name can be fully qualified or partially qualified. However, the dot that precedes the member name (if it exists) must be replaced by a hash character. @serial The @serial tag defines the comment for a default serializable field. It has the following syntax: @serial description Here, description is the comment for that field. @serialData The @serialData tag documents the data written by the writeObject( ) and writeExternal( ) methods. It has the following syntax: @serialData description Here, description is the comment for that data. @serialField For a class that implements Serializable, the @serialField tag provides comments for an ObjectStreamField component. It has the following syntax: @serialField name type description Here, name is the name of the field, type is its type, and description is the comment for that field. @since The @since tag states that an element was introduced in a specific release. It has the following syntax: @since release Appendix Using Java’s Documentation Comments 1083 Here, release is a string that designates the release or version in which this feature became available. @throws The @throws tag has the same meaning as the @exception tag. {@value} {@value} has two forms. The first displays the value of the constant that it precedes, which must be a static field. It has this form: {@value} The second form displays the value of a specified static field. It has this form: {@value pkg.class#field} Here, pkg.class#field specifies the name of the static field. @version The @version tag specifies the version of a class or interface. It has the following syntax: @version info The General Form of a Documentation Comment After the beginning /**, the first line or lines become the main description of your class, interface, field, constructor, or method. After that, you can include one or more of the various @ tags. Each @ tag must start at the beginning of a new line or follow one or more asterisks (*) that are at the start of a line. Multiple tags of the same type should be grouped together. For example, if you have three @see tags, put them one after the other. In-line tags (those that begin with a brace) can be used within any description. Here is an example of a documentation comment for a class: /** * This class draws a bar chart. * @author Herbert Schildt * @version 3.2 */ What javadoc Outputs The javadoc program takes as input your Java program’s source file and outputs several HTML files that contain the program’s documentation. Information about each class will be in its own HTML file. javadoc will also output an index and a hierarchy tree. Other HTML files can be generated. Part IV Here, info is a string that contains version information, typically a version number, such as 2.2. You will need to specify the -version option when executing javadoc in order for the @version field to be included in the HTML documentation. 1084 PART IV Applying Java An Example that Uses Documentation Comments Following is a sample program that uses documentation comments. Notice the way each comment immediately precedes the item that it describes. After being processed by javadoc, the documentation about the SquareNum class will be found in SquareNum.html. import java.io.*; /** * This class demonstrates documentation comments. * @author Herbert Schildt * @version 1.2 */ public class SquareNum { /** * This method returns the square of num. * This is a multiline description. You can use * as many lines as you like. * @param num The value to be squared. * @return num squared. */ public double square(double num) { return num * num; } /** * This method inputs a number from the user. * @return The value input as a double. * @exception IOException On input error. * @see IOException */ public double getNumber() throws IOException { // create a BufferedReader using System.in InputStreamReader isr = new InputStreamReader(System.in); BufferedReader inData = new BufferedReader(isr); String str; str = inData.readLine(); return (new Double(str)).doubleValue(); } /** * This method demonstrates square(). * @param args Unused. * @exception IOException On input error. * @see IOException */ public static void main(String args[]) throws IOException { SquareNum ob = new SquareNum(); double val; Appendix Using Java’s Documentation Comments 1085 System.out.println("Enter value to be squared: "); val = ob.getNumber(); val = ob.square(val); System.out.println("Squared value is " + val); } Part IV } This page intentionally left blank Introduction & bitwise AND, 66, 67, 68–69 Boolean logical AND, 75–76 and bounded type declarations, 336 && (short-circuit AND), 75, 76–77 * multiplication operator, 28, 61–62 regular expression quantifier, 913 used in import statement, 194, 321 @ annotation syntax, 276 used with tags (javadoc), 1080, 1083 | bitwise OR, 66, 67, 68–69 Boolean logical OR, 75–76 || (short-circuit OR), 75, 76–77 [ ], 33, 51, 52, 54, 56, 58 character class specification, 913, 917 ^ bitwise exclusive OR, 66, 67, 68–69 Boolean logical exclusive OR, 75–76 : (used with a label), 105 , (comma), 33, 95 format flag, 557, 559 { }, 24, 25, 26, 30, 33, 45, 53, 56, 81, 82, 89, 211 used with javadoc tags, 1080 =, 27, 74, 77 = = (Boolean logical operator), 75 = = (relational operator), 28, 74, 260, 266 versus equals( ), 380–381 !, 75–76 !=, 74, 75 /, 61–62 /* */, 24 /** */, 32, 1079 //, 25 <, 28, 74 <> diamond operator (type inference), 359–360 and generic type parameter, 328, 334, 346 , 277, 279 <<, 66, 69–70 <=, 74 –, 61, 62 format flag, 557, 558 – –, 30, 61, 64–65 % used in format conversion specifier syntax, 550 modulus operator, 61, 63 ( format flag, 557, 559 ( ), 25, 33, 79, 114, 123 used to raise the precedence of operations, 33, 79, 376 . dot operator, 111, 117–118, 146, 170 in multileveled package statement, 188 in import statement, 194 regular expression wildcard character, 913, 916 separator, 33 ... (variable-length argument syntax), 156, 159 + addition operator, 61–62 concatenation operator, 27, 152–153, 374–376 regular expression quantifier, 913, 915–916 unary plus, 61, 62 ++, 30, 61, 64–66 # format flag, 557, 559 ? regular expression quantifier, 913, 916 wildcard argument specifier, 338, 341, 344, 367 ?: (ternary if-then-else operator), 75, 77–78 >, 28, 74 >>, 66, 70–72 >>>, 66, 72–73 >=, 74 ; (semicolon), 26, 33, 90 used in try-with-resources statement, 305, 589 ~ (bitwise unary NOT operator), 66, 67, 68–69 _ (underscore), 42, 43 1087 1088 Index A abs( ), 131–132, 434 abstract type modifier, 182, 185, 199 Abstract Window Toolkit. See AWT (Abstract Window Toolkit) AbstractButton class, 969, 971, 972 AbstractCollection class, 465, 467, 474 AbstractList class, 465, 466, 509 AbstractMap class, 487, 488, 489, 491 AbstractQueue class, 465, 473 AbstractSequentialList class, 465, 469 AbstractSet class, 465, 470, 472, 475 accept( ), 586, 587, 656, 682 Access control, 141–144 example program, 191–194 and inheritance, 142, 144, 163–164 and packages, 187, 190–194 Access modifiers, 25, 142, 190–191 acos( ), 433 acquire( ), 864–867 ActionEvent class, 710, 711, 776, 777, 787, 812, 956, 967, 969, 975 ActionListener interface, 720, 721, 776, 787, 812, 956, 969, 975 actionPerformed( ), 721, 776, 777, 779, 956, 957, 969, 975, 976 adapt( ), 907 Adapter classes, 729–731 add( ), 457, 458, 459, 460, 470, 471, 477, 739, 774, 779, 784, 786, 799, 803, 811, 952–953, 975, 987, 988 addActionListener( ), 956 addAll( ), 457, 458, 459, 460, 495 addCookie( ), 1006, 1007, 1013 addElement( ), 510, 511 addFirst( ), 464, 465, 469 addImage( ), 836, 837 addItem( ), 985 addKeyListener( ), 709 addLast( ), 464, 465, 469, 470 addMouseListener( ), 726 addMouseMotionListener( ), 709, 726 Address, Internet, 668 addSuppressed( ), 222 addTab( ), 977 addTListener( ) 936 addTypeListener( ), 708, 709 AdjustmentEvent class, 710, 711–712, 790 AdjustmentListener interface, 720, 721, 790 adjustmentValueChanged( ), 721 Algorithms, collection, 454–455, 495–500, 508 ALIGN, 700 allocate( ), 631, 641, 660, 663 ALT, 700 anchor constraint field, 806, 807–808 AND operator bitwise (&), 66, 67, 68–69 Boolean logical (&), 75–76 and bounded type declarations, 336 short-circuit (&&), 75, 76–77 Android programming, 16 Animation, cell, 857–860 AnnotatedElement interface, 282, 284, 451 Annotation interface, 276, 282, 450 Annotation(s), 13, 14, 275–288 built-in, 286–288 declaration example, 276 marker, 284–285 member, default value for, 282–284 obtaining all, 281–282 reflection to obtain, using, 277–282 restrictions on, 288 retention policy for, specifying, 276–277 single-member, 285–286 annotationType( ), 276 Annuity for a given investment, maximum applet for finding, 1040–1044 formula to compute, 1040 Annuity, initial investment needed for desired applet for finding, 1036–1040 formula to compute, 1036 Apache Software Foundation, 994 API packages, table of core Java, 909–911 APPEND, 421 append( ), 391–392, 449, 611, 795 Appendable interface, 449, 551, 605, 610, 619 appendCodePoint( ), 394 appendTo( ), 421 Applet, 8, 16, 307–309, 1019 architecture, 690–691, 696 basics, 687–690 colors, setting and obtaining, 694–695 examples for financial calculations, 1020–1047 executing, 308–309, 687–688, 699–701 and the Internet, 8–9 and main( ), 26, 110, 308, 309, 688 outputting to console, 706 passing parameters to, 701–703 request for repaint, 695–698 skeleton, 691–693 and socket connections, 671 as source and listener for events, 726 string output to, 308, 688, 694, 696 Swing, 687, 688, 949, 957–959 viewer, 308–309, 687, 699, 739 Applet class, 307, 687–706, 719, 726, 739, 830, 832, 957, 959 methods, table of, 689–690 applet package, 289, 307, 687 Index APPLET tag, HTML, 308, 309, 688 full syntax for, 699–701 AppletContext interface, 687, 700, 704–706 methods, table of, 705 AppletStub interface, 687, 706 appletviewer, 308, 687 status window, using, 699 Application launcher (java), 24, 188, 189 and main( ), 25 ARCHIVE, 701 AreaAveragingScaleFilter class, 844 areFieldsSet, 531 Argument(s), 116, 120 command-line, 25, 154–155 index, 560–563 passing, 136–138 type. See Type argument(s) variable-length. See Varargs wildcard. See Wildcard arguments Arithmetic operators, 61–66 ArithmeticException, 209, 210, 220 Array class, 451 Array(s), 25, 51–58, 147, 185 boundary checks, 53 converting collections into, 459, 468–469 copying with arraycopy( ), 423, 425–426 declaration syntax, alternative, 58 dynamic, 451, 466–469, 474, 509 and the for-each loop, 97–101 and generics, 366–367 implemented as objects, 147 indexes, 52 initializing, 53, 56–57 length instance variable of, 147–149 multidimensional, 54–57 one-dimensional, 51–53 of strings, 154 and valueOf( ), 386–387 and varargs, 156 ArrayBlockingQueue class, 889 arraycopy( ), 423, 425–426 ArrayDeque class, 465, 474–475, 515 ArrayIndexOutOfBoundsException, 213, 220, 502, 503, 504 ArrayList class, 465, 466–469, 482, 509, 511 pre-generics versus generics version of the, 505–507 Arrays class, 501–505 ArrayStoreException, 220, 501, 502, 503 arrive( ), 876 arriveAndAwaitAdvance( ), 876, 879, 882 arriveAndDeregister( ), 876, 879 ASCII character set, 39, 40, 43 and strings on the Internet, 373, 378 asin( ), 433 1089 asList( ), 501 Assembly language, 4, 5 assert statement, 13, 316–319 Assertion, 316–319 AssertionError, 316 Assignment operator(s) =, 27, 74, 77 arithmetic compound (op=), 61, 63–64 bitwise compound, 66, 73–74 Boolean logical, 75 atan( ), 433 atan2( ), 433 Atomic operations, 892–893 AtomicInteger class, 863, 892–893 AtomicLong class, 863, 892 AttributeView interface, 639 AudioClip interface, 687, 706 Autoboxing/unboxing, 14, 268, 270–275, 329, 330 Boolean and Character values, 274 and the Collections Framework, 456, 469 definition of, 270 and error prevention, 274–275 and expressions, 272–273 and methods, 271–272 when to use, 275 Autocloseable interface, 298, 304, 449–450, 562, 569, 588, 589, 591, 594, 605, 607, 608, 609, 610, 619, 623, 625, 631, 641, 654, 672, 683 Automatic resource management (ARM), 208, 303–306, 449, 562, 674 available( ), 591, 592–594, 625, 626 availableProcessors( ), 903 await( ), 869–871, 872, 890 awaitAdvance( ), 882 awaitInterruptibly( ), 882 AWT (Abstract Window Toolkit), 289, 307, 309, 687, 688, 773 and applet architectural constraints, 696 classes, table of some, 736–737 color system, 755 controls. See Controls, AWT creating stand-alone windows with, 747–748 layout managers. See Layout manager(s) support for imaging, 829 support for text and graphics, 749 and Swing, 735, 945, 946 AWTEvent class, 710, 736 bit mask constants, 822–823 B B, 4 BASIC, 4 BasicFileAttributes class, 638–639, 652 methods, table of, 638 1090 Index BasicFileAttributeView interface, 639 Basic multilingual plane (BMP), 414 BCP 47, 539 BCPL, 4 BeanInfo interface, 934, 936–937 Beans, Java. See Java Beans Bell curve, 540 Bell Laboratories, 6 Berkeley UNIX, 667 Berners-Lee, Tim, 675 Beyond Photography, The Digital Darkroom (Holzmann), 840 Binary literals, 42 numbers and integers, 66–67 binarySearch( ), 495, 496, 501 BitSet class, 527–530 methods, table of, 527–528 Bitwise operators, 66–74 BLOCKED, 256 Blocks of code. See Code blocks Boolean, 35 literals, 43 logical operators, 75–77 Boolean class, 269, 274, 414–416 methods, table of, 416 boolean data type, 35, 40–41, 43, 48 and relational operators, 74–75 booleanValue( ), 269, 416 Border interface, 964 BorderFactory class, 964 BorderLayout class, 736, 798–800, 956 Boxing, 270 break statement, 84–87, 102–106 and the for-each loop, 98–99 as form of goto, 104–106 Buffer class, 630–631 methods, table of, 630–631 Buffer, NIO, 630–631 BufferedInputStream class, 291, 599–601, 651 BufferedOutputStream class, 291, 599, 601–602, 651 BufferedReader class, 292, 293, 294–295, 616–618 BufferedWriter class, 292, 619 Buffering, double, 833–836 bulkRegister( ), 882 Button class, 736, 776 extending, 823–824 ButtonGroup class, 965, 975 ButtonModel interface, 948, 969 Buttons, 720 push. See Push buttons radio. See Radio buttons Swing, 956–957, 969–977 ButtonUI, 948 Byte class, 269, 403, 410 methods defined by, table of, 404 byte data type, 35, 36–37, 41 ByteArrayInputStream class, 291, 596–597 ByteArrayOutputStream class, 291, 598–599 ByteBuffer class, 631, 640, 641, 644, 660 get( ) and put( ) methods, table of, 632 Bytecode, 9–10, 12, 13, 16, 23–24, 312–313, 323, 436 byteValue( ), 269, 398, 399, 400, 404, 405, 406, 408 C C history of, 4–5 and Java, 3, 5, 7, 11 C Programming Language, The (Kernighan and Ritchie), 4 C++ history of, 5–6 and Java, 3, 7, 11 C# and Java, 8 Calendar class, 530, 531–534, 535, 539 constants, 533 methods defined by, table of commonly used, 532–533 Call-by-reference, 136, 137–138 Call-by-value, 136–137, 138 call( ), 885, 907 Callable interface, 863, 885, 886–887, 907 CallSite class, 451 cancel( ), 545, 546, 906 Canvas class, 736, 739, 830 capacity( ), 389–390, 510, 630 capacityIncrement Vector data member, 509 Card layouts, 802–805 CardLayout class, 736, 802–803 CaretEvent class, 967 Case sensitivity and Java, 23, 25, 32 case statement, 84–87, 88–89 Casts, 48–49, 50, 326, 328, 329, 330, 331, 332 and casting one instance of a generic class into another, 357–358 and erasure, 328–329, 361–362 using instanceof with, 310–312 catch block(s), 207, 208, 210–213, 218 displaying exception description within, 212 and the more precise (final) rethrow feature, 225, 226 multi-catch feature of, 225–226 using multiple, 212–213 and nested try statements, 211, 214 cbrt( ), 434 ceil( ), 434 CGI (Common Gateway Interface), 10, 993–994 Index Channel interface, 631–632 Channel(s), NIO, 630, 631–633 char data type, 35, 39–40, 48, 61 Character class, 269, 274, 411–414 methods, table of various, 413, 415 support for 32–bit Unicode, 414 Character(s), 35, 39–40 basic multilingual plane (BMP), 414 changing case of, 387 classes (regular expressions), 913, 917 code point, 414 escape sequences, 43, 44 extraction from String objects, 377–378 formatting an individual, 551 literals, 43 supplemental, 414 Character.Subset class, 397, 414 Character.UnicodeBlock class, 397, 414 CharArrayReader class, 292, 614–615 CharArrayWriter class, 292, 615–616 charAt( ), 153, 377, 390–391, 448 CharBuffer class, 631 CharSequence interface, 371, 392, 395, 448, 912 Charsets, 633 charValue( ), 269, 411 Check boxes, 691, 720, 779–783 Swing, 973–975 checkAll( ), 836 Checkbox class, 736, 779–780 extending, 824–825 CheckboxGroup class, 736, 782–783 extending, 825–826 CheckboxMenuItem class, 736, 810, 811, 812 checked... methods, 496, 499 checkedCollection( ), 496, 499 checkedList( ), 496, 499 checkedMap( ), 496, 499 checkedSet( ), 496, 499 checkID( ), 836 Choice class, 736, 783–784 extending, 826 Choice controls, 720, 783–786 Class class, 277–278, 281, 282, 429–433, 639, 919 methods, table of some, 430–431 .class filename extension, 24, 112 class keyword, 24, 109 CLASS retention policy, 276–277 Class(es), 109–128 abstract, 181–184, 185, 199 access levels of, 190–191 adapter, 729–731 character, regular expression, 913, 917 and code, 23, 109, 190 in collections, storing user-defined, 480–481 1091 constructor. See Constructor controlling access to. See Access control as a data type, 109, 111, 113, 114, 126 definition of, 19 encapsulation achieved through, 126–127 final, 185 general form of, 109–110 generic. See Generic class inner. See Inner classes and interfaces, 196, 197–200 libraries, 23, 34 literal, 279 member. See Member, class name and source file name, 23, 24 nested, 149, 151 packages as containers for, 187, 190 public, 191 scope defined by a, 45–46 type for bounded types, using a, 335–336 ClassCastException, 220, 457, 459, 461, 463, 483, 484, 485, 492, 499, 501, 503, 504 ClassDefinition class, 450 ClassFileTransformer interface, 450 ClassLoader class, 433 classModifiers( ), 922 ClassNotFoundException, 221, 625 CLASSPATH, 188, 189, 926 –classpath option, 188, 189 ClassValue class, 448 clear( ), 457, 458, 483, 517, 527, 532, 630 Client/server model, 8, 10, 667 and sockets, 671–675 clone( ), 185, 427–429, 447, 510, 517, 527, 530, 532, 536, 1008 Cloneable interface, 427–429 CloneNotSupportedException, 221, 427, 447 Cloning, potential dangers of, 427–429 close( ), 298, 303–306, 450, 549, 562, 564, 569, 573, 588, 589, 591, 592, 596, 598, 608, 609, 611, 614, 623, 624, 625, 626, 672, 674, 683 within a finally block, calling, 300 Closeable interface, 298, 304, 569, 588, 591, 594, 605, 607, 608, 610, 619, 631 COBOL, 4 CODE, 700, 701 Code base, 704 Code blocks, 28, 30–31, 45, 82–83 and the break statement, 104–106 and scopes, 45, 46 static, 145, 313 Code point, definition of, 414 Code, unreachable, 213 CODEBASE, 700 codePointAt( ), 388, 394, 414, 415 1092 Index codePointBefore( ), 388, 394, 415 codePointCount( ), 388, 394 Collection interface, 456, 457–459 methods defined by, table of, 458 Collection-view, 455, 482, 483–484 Collection(s), 325, 453–524 algorithms, 454–455, 495–500, 508 into arrays, converting, 459, 468–469 and autoboxing, 456, 469 classes, 465–476 concurrent, 862, 889 dynamically typesafe view of a, 499 and the for-each version of the for loop, 97, 101, 456, 479–480 Framework. See Collections Framework generic nature of, 455 interfaces, 454, 456–465 and iterators, 455, 456, 476–480 and legacy classes and interfaces, 508–524 modifiable versus unmodifiable, 457 pre-generics, problems with, 505–506 and primitive types, 456, 469 storing user-defined classes in, 480–481 and synchronization, 465, 499, 508 and type safety, 455, 499, 505 when to use, 524 Collections class, 454, 495, 499, 508 algorithms defined by, table of, 495–499 Collections Framework, 13, 97, 101, 270, 453–524 advantages of generics as applied to the, 455, 505–508 JDK 5 changes to, 325, 455–456 overview, 454–455 Color class, 736, 755–757 constants 694 Combo boxes, Swing, 984–986 ComboBoxModel interface, 985 Comment, 24–25 documentation, 32, 1079–1085 Common Gateway interface (CGI), 10, 993–994 Comparable interface, 348, 381, 448, 530, 585 Comparable interface, 634 Comparator interface, 457, 489, 492 comparator( ), 461, 474, 484 Comparators, 472, 474, 489, 490, 492–495 compare( ), 399, 400, 404, 405, 406, 408, 416, 492–493 compareAndSet( ), 863, 892 compareTo( ), 265, 266, 381–382, 399, 400, 404, 405, 406, 408, 412, 416, 447, 448, 493, 530, 585 compareToIgnoreCase( ), 382 Compilation unit, 23 compile( ), 911 Compiler class, 436 Compiler, Java, 23–24 and main( ), 25 Component class, 688, 691, 694, 696, 709, 719, 726, 736, 738, 739, 742, 749, 774, 796, 828, 830, 948, 949, 960 ComponentAdapter class, 730 componentAdded( ), 721 ComponentEvent class, 710, 712, 713, 719 componentHidden( ), 721 ComponentListener interface, 720, 721, 730 componentMoved( ), 721 componentRemoved( ), 721 componentResized( ), 721 Components, AWT, 945–946, 948 and overriding paint( ), 828 Components, Swing, 948–949 architecture, 947–948 class names for, table of, 948–949 heavyweight, 949 lightweight, 946, 965 painting, 959–964 componentShown( ), 721 ComponentUI, 948 compute( ), 895, 896, 900, 903, 905, 908 concat( ), 385 Concurrency utilities, 14, 861–908 versus traditional multithreading and synchronization, 908 Concurrent API, 861–862 packages, 862–863 Concurrent collection classes, 862, 889 Concurrent program, definition of, 861 ConcurrentHashMap class, 863, 889 ConcurrentLinkedDeque, 889 ConcurrentLinkedQueue class, 863, 889 ConcurrentSkipListMap class, 889 ConcurrentSkipListSet class, 889 Condition class, 890 connect( ), 672 Console class, 620–622 methods, table of, 621 console( ), 423, 620 const keyword, 33 Constants, 32 Constructor class, 278, 281, 282, 451, 920 Constructor(s), 114, 121–124 in class hierarchy, order of calling, 174–175 default, 114, 123 enumeration, 263–265 factory methods versus overloaded, 669 generic, 346–347 object parameters for, 135–136 overloading, 132–134 parameterized, 123–124 and super( ), 167–170, 174, 323 this( ) and overloaded, 321–323 constructorModifiers( ), 922 Index Container class, 688, 736, 739, 774, 796, 799, 800, 948, 949, 961 ContainerAdapter class, 730 ContainerEvent class, 710, 712–713 ContainerListener interface, 720, 721, 730 Container(s), Swing, 948, 949 lightweight versus heavyweight, 949 panes, 949 Containment hierarchy, 948, 949 contains( ), 388, 457, 458, 471, 510, 517 containsAll( ), 457, 458 Content pane, 949, 952–953, 957, 963, 977, 980, 987, 991 default layout manager of JFrame, 953, 956 contentEquals( ), 388 Context switching, 227, 257 rules for, 229 continue statement, 106–107 Control statements. See Statements, control Controls, AWT, 773–795 fundamentals, 773–774 convert( ), 888 Convolution filters, 846, 852 Cookie class, 1004, 1007–1008 methods, table of, 1008 CookieHandler class, 681 CookieManager class, 681 CookiePolicy interface, 681 Cookies, 1007–1008 example servlet using, 1013–1015 CookieStore interface, 681 copy( ), 636, 648–649 copyOf( ), 475, 476, 501 copyOfRange( ), 502 CopyOnWriteArrayList class, 863, 889 CopyOnWriteArraySet class, 889 copySign( ), 435 cos( ), 38, 433 cosh( ), 433 countDown( ), 869–871 CountDownLatch class, 862, 869–871 countStackFrames( ), 437 createImage( ), 830, 839, 844 createLineBorder( ), 964 CropImageFilter class, 844–845 Currency class, 547–548 methods, table of, 547–548 currentThread( ), 231, 438 currentTimeMillis( ), 423, 425 CyclicBarrier class, 862, 871–873 D Data types, 27. See also Type(s); Types, primitive DatagramPacket class, 682, 683–684 1093 Datagrams, 668, 682–685 server/client example, 684–685 DatagramSocket class, 632, 682–683 DataInput interface, 607, 608, 609, 625 DataInputStream class, 291, 607, 608–609 DataOutput interface, 607, 608, 609, 623 DataOutputStream class, 291, 607–608 Date class, 530–531, 927, 928 methods, table of, 530 DateFormat class, 530, 539, 927–929 Deadlock, 249–251, 253, 437 Decrement operator (– –), 30, 61, 64–65 decrementAndGet( ), 863, 892 deepEquals( ), 502 deepHashCode( ), 504 deepToString( ), 504 default clause for annotation member, 282–283 statement, 84–85 DefaultMutableTreeNode class, 987, 988 DelayQueue class, 889 Delegation event model, 708–709, 710 and Beans, 936 and event listeners, 708, 709, 720–723 and event sources, 708–709, 719–720 and Swing, 954 using, 723–729 delete operator, 125 delete( ), 393, 584, 636 deleteCharAt( ), 393 deleteOnExit( ), 585 delimiter( ), 572 Delimiters, 525 Scanner class, 564, 570–572 @Deprecated built-in annotation, 286, 287 Deque interface, 456, 463–465, 469, 474 methods, table of, 464 descendingIterator( ), 462, 464, 465 destroy( ), 417, 420, 437, 440, 689, 691, 692, 693, 695, 957, 994, 997, 999 Destructors versus finalize( ), 126 Dialog boxes, 816–822 file, 820–822 Dialog class, 736, 816 Diamond operator (<>), 359–360 Dictionary class, 454, 508, 515–516 abstract methods, table of, 515 digit( ), 412 Dimension class, 736, 740, 754 Directories as File objects, 583, 585–586 creating, 588 Directory, listing the contens of a using list( ), 585–587 using listFiles( ), 587 using NIO, 654–657 1094 Index Directory tree, obtaining a list of files in a, 657–659 DirectoryStream class, 654 DirectoryStream.Filter interface, 656 dispose( ), 816 DLL (dynamic link library), 314, 315, 316 do-while loop, 90–93 Document base, 704 Document interface, 967 Document/view methodology, 544 @Documented built-in annotation, 286 doDelete( ), 1009, 1010 doGet( ), 1009, 1010, 1011 doHead( ), 1009, 1010 Domain name, 668, 669 Domain Naming Service (DNS), 668 doOptions( ), 1009, 1010 doPost( ), 1009, 1010, 1012 doPut( ), 1009, 1010 DosFileAttributes class, 639, 654 DosFileAttributeView interface, 639 Dot operator (.), 111, 117–118, 146, 170, 188, 194 doTrace( ), 1009, 1010 Double buffering, 833–836 Double class, 269, 398–402 methods, table of, 400–402 double data type, 35, 38–39, 42 DoubleBuffer class, 631 doubleValue( ), 269, 398, 399, 401, 404, 405, 406, 408 Download Manager, 1053–1077 compiling and running, 1076–1077 enhancing, suggestions for, 1077 overview of, 1054 Downloads, Internet operation of, 1054 resuming interrupted, 1053 drawArc( ), 752–753 drawImage( ), 831, 834, 835 drawLine( ), 749–750, 960 drawOval( ), 751–752 drawPolygon( ), 753–754 drawRect( ), 750–751, 960 drawRoundRect( ), 750–751 drawString( ), 308, 688, 694, 696, 765 Dynamic link library (DLL), 313–314, 315, 316 Dynamic method dispatch, 178–181 lookup, 198 resolution, 196 E E (Math constant), 433 Early binding, 184 echoCharIsSet( ), 793 Eclipse IDE, 994, 995 Edit control, 792 element( ), 463 elementAt( ), 510, 511 elementCount Vector data member, 509 elementData[ ] Vector data member, 509 elements( ), 510, 515, 517 ElementType enumeration, 286–287, 450 else, 81–84 empty( ), 513 EMPTY_LIST static variable, 499 EMPTY_MAP static variable, 499 EMPTY_SET static variable, 499 EmptyStackException, 512, 513 enableEvents( ), 822–823, 824, 826, 827 Encapsulation, 5, 18–19, 20, 22–23, 126–127, 167 and access control, 141 and scope rules, 46 end( ), 912 endsWith( ), 380, 634 ensureCapacity( ), 390, 467, 510 entrySet( ), 483–484, 486, 489, 518 enum, 259, 447, 476, 491 Enum class, 265, 447 methods, table of, 447 EnumConstantNotPresentException, 220 enumerate( ), 438, 440, 443 Enumeration interface, 508–509, 511–512, 513, 525, 526, 603 Enumeration(s), 14, 259–268, 447, 513 = = relational operator and, 260, 266 as a class type in Java, 259, 263–265 constants, 259, 260, 263, 264, 265–266 constructor, 263–265 restrictions, 265 values in switch statements, using, 260–261 variable, declaring an, 260 EnumMap class, 487, 491–492 EnumSet class, 465, 475–476 factory methods, table of, 476 Environment properties, list of, 427 equals( ), 153, 185, 186, 265–266, 276, 378–379, 399, 401, 404, 405, 406, 408, 414, 416, 427, 446, 447, 458, 459, 483, 487, 492, 493, 502, 516, 527, 530, 532, 670, 760 versus = =, 380–381 equalsIgnoreCase( ), 379 Erasure, 329, 361–365, 366 and ambiguity errors, 364–365 bridge methods and, 362–364 err, 292, 293, 423 Error class, 208–209, 217, 224, 620 Errors ambiguity, 364–365 Index assertions to check for, using, 316–318 autoboxing/unboxing and prevention of, 274–275 automatic type promotions and compile-time, 50 compile-time versus run-time, 331–332 generics and prevention of, 330–332, 506 raw types and run-time, 351 run-time, 12, 207, 310. See also Exception handling unreachable code, 108 Event and applets, 690–691 definition of an, 708 design patterns for an, 936 dispatching thread and Swing, 953–954, 957, 959 driven programs, 707, 953 loop with polling, 228 model, delegation. See Delegation event model multicasting and unicasting, 936 Event handling, 690, 707–733 and adapter classes, 729–731 event classes, 709–719 by extending AWT components, 708, 822–828 and inner classes, 731–733 keyboard, 726–729 mouse, 723–726 and Swing, 946, 953–957 See also Delegation event model Event listener interfaces, 720–723 and adapter classes, 729–731 table of commonly used, 720 EventListener interface, 578 EventListenerProxy class, 577 EventObject class, 577, 709–710, 1009 EventSetDescriptor class, 936, 938, 940 Exception class, 208–209, 221, 223, 224 Exception classes and generics, 367 Exception handling, 12, 93, 102, 207–226, 299–300, 301–302, 303 block, general form of, 208 and chained exceptions, 13, 224–225 and creating custom exceptions, 221–223 and the default exception handler, 209–210, 216 and the more precise (final) rethrow feature, 225, 226 and suppressed exceptions, 222, 306 and uncaught exceptions, 450 Exceptions, built-in run-time, 208–209, 220 checked, table of, 221 constructors for, 217 unchecked RuntimeException, table of, 220 Exceptions, I/O, 588–589 exchange( ), 873–875 1095 Exchanger class, 862, 873–875 exec( ), 416, 417–418, 420, 421 execute( ), 882, 896, 905 Executor interface, 863, 882, 883 Executors, 863 using, 882–887 Executors class, 863, 883 ExecutorService interface, 863, 882–883, 885 exists( ), 583, 636, 652 exitValue( ), 417, 420 exp( ), 434 expm1( ), 434 Expressions and autoboxing/unboxing, 272–273 automatic type promotion in, 49–51 regular. See Regular expressions extends, 161, 163, 205, 335, 339 and bounded wildcard arguments, 341, 344 Externalizable interface, 623 F false, 34, 40, 41, 43, 75, 76 FALSE, 414 FAT file system, 639, 654 Field class, 278, 281, 282, 451, 920 Field, final, 146–147 fieldModifiers( ), 922 fields array, 531 File attribute(s) File to access, using, 582–585 interfaces, 648–639 NIO to access, using, 639, 652–654 view interfaces, 639 File class, 563, 582–588, 605, 619, 652 instance into a Path instance, converting a, 585, 635, 652 methods, 583–585, 592 file( ), 421 File(s) to a buffer, map a, 633, 644, 647, 661–663, 664 close( ) to close a, using, 300–302, 306 I/O, 297–306, 582–588. See also NIO pointer, 609, 610 source, 23–24, 110 system, accessing the, 640 try-with-resources to automatically close a, using, 303–306 FileChannel class, 632, 633, 641, 644, 645–646, 660 FileDialog class, 736, 820–822 FileFilter interface, 587 FileInputStream class, 291, 297–298, 592–594, 632, 660, 661, 662, 663 1096 Index FilenameFilter interface, 586–587 FileNotFoundException, 298, 301, 588, 592, 594, 612 FileOutputStream class, 291, 297–298, 302, 594–596, 632, 663 FileReader class, 292, 564, 612 Files class, 582, 633, 635–637, 639, 648, 649, 652, 654, 657 methods, table of a sampling of, 636–637 FileStore class, 630 FileSystem class, 640 FileSystems class, 640 FileVisitor interface, 657–658 FileVisitResult enumeration, 658 FileWriter class, 292, 613–614 fill( ), 497, 503 fillArc( ), 752–753 fillInStackTrace( ), 222 fillOval( ), 751–752 fillPolygon( ), 753–754 fillRect( ), 750–751 fillRoundRect( ), 750–751 FilteredImageSource class, 839, 844 FilterInputStream class, 291, 599, 608 FilterOutputStream class, 291, 599, 607 FilterReader class, 292 FilterWriter class, 292 final, 146–147 to prevent class inheritance, 185 to prevent method overriding, 184 finalize( ), 125–126, 185, 427 finally block, 207, 208, 218–220, 300–301, 589 Financial calculations, applets and servlets for, 1019–1052 find( ), 912, 914–915, 916 findInLine( ), 572 findWithinHorizon( ), 572–573 Finger protocol, 675 first( ), 461, 803 firstElement( ), 510, 511 firstKey( ), 484 flip( ), 630, 647 Float class, 269, 398–400, 402 methods, table of, 399–400 float data type, 35, 38, 42 Floating-point(s), 35, 38–39 literals, 42–43 FloatBuffer class, 631 floatValue( ), 269, 398, 399, 401, 404, 405, 406, 408 floor( ), 434, 462 FlowLayout class, 736, 797–798, 956 flush( ), 549, 588, 592, 601, 611, 621, 623, 624 Flushable interface, 588, 591, 594, 605, 607, 610, 619, 620 FocusAdapter class, 730 FocusEvent class, 710, 712, 713 focusGained( ), 721 FocusListener interface, 720, 721, 730 focusLost( ), 721 Font class, 736, 759–760, 761, 762, 764 methods, table of some, 760 Font(s), 759–772 creating and selecting, 762–763 determining available, 760–761 information, obtaining, 764 metrics to manage text output, using, 764–772 terminology used to describe, 765 FontMetrics class, 736, 764–766 methods, table of some, 765–766 for loop, 29–31, 93–102 enhanced. See For-each version of the for loop variations, 96–97 For-each version of the for loop, 14, 93, 97–101 and arrays, 97–101 and the break statement, 98–99 and collections, 97, 101, 456, 479–480 general form, 97 and the Iterable interface, 449, 456, 479 and maps, 482 forceTermination( ), 882 forDigit( ), 412 Fork/Join Framework, 15, 229, 257, 578, 861–862, 863, 883, 893–908 advantages to using the, 893–894 classes, main, 894–897 tips for using the 908 Fork/Join Framework divide-and-conquer strategy, 895, 896, 897, 898–900, 908 and the sequential processing threshold interaction with the level of parallelism, 900–903 Fork/Join Framework tasks, 894 asynchronous execution of, 905 cancelling, 906 completion status of, 906 and the parallelism level, 896 restarting, 906 starting, 896, 905 and subtasks, 897 that do not return a result, 894, 895, 903 that return a result, 894, 895–896, 903 fork( ), 894–895, 903, 905, 907 ForkJoinPool class, 863, 883, 894, 895, 896–897, 900, 903, 905, 907–908 and work stealing, 897, 907 ForkJoinTask class, 863, 894–895, 896, 897, 907 Format flags, 557–560 Format specifiers, 548, 549–562 argument index with, using an, 560–562 Index and format flags, 557–560 and specifying minimum field width, 555–556 and specifying precision, 556–557 suffixes for the time and date, table of, 553 table of, 551 uppercase versions of, 560 format( ), 388, 549–550, 607, 619, 621, 927 Formattable interface, 578 FormattableFlags class, 577 Formatter class, 548–562, 606 closing an instance of the, 562 constructors, 548–549 methods, table of, 549 See also Format specifiers forName( ), 430, 919 FORTRAN, 4, 5 Frame class, 736, 738, 739–740, 741, 742 Frame window (s), 739–748 creating stand-alone, 747–748 handling events in, 742–747 within applet, creating, 741–742 Frank, Ed, 6 freeMemory( ), 418–419 from( ), 421 FTP (File Transfer Protocol), 668, 675, 1054 Future interface, 863, 885–887 G Garbage collection, 12, 125, 126, 139, 418–419, 451, 837 gc( ), 418, 419, 423 Generic class example program with one type parameter, 326–329 example program with two type parameters, 332–334 general form, 334 hierarchies, 352–359 and instanceof, 355–357 overriding methods in a, 358–359 and raw types, 349–352 and type inference, 359–361 Generic constructors, 346–347 Generic interfaces, 326, 347–349 and classes, 348–349 Generic method, 326, 338, 344–346, 366 Generics, 13, 14, 270, 325–367 and ambiguity errors, 364–365 and arrays, 366–367 and casts, 326, 328, 329 and the Collections Framework, 325, 455, 505–508 and compatibility with pre-generics code, 349–352, 361 and exception classes, 367 1097 restrictions on using, 365–367 type checking and, 329, 330–332, 351 GenericServlet class, 997, 999, 1002, 1008 get( ), 459, 460, 470, 483, 488, 515, 517, 527, 532, 638, 640, 681, 886, 888, 892 and buffers, 631, 632, 643, 661 getActionCommand( ), 711, 777, 787, 969, 975, 976 getActiveThreadCount( ), 908 getAddListenerMethod( ), 940 getAddress( ), 670, 684 getAdjustable( ), 712 getAdjustmentType( ), 712, 790 getAlignment( ), 775 getAllByName( ), 669, 670 getAllFonts( ), 761 getAndSet( ), 863, 892, 893 getAnnotation( ), 278, 282, 430, 444 getAnnotations( ), 281–282, 430, 444 getApplet( ), 700, 705 getAppletContext( ), 689, 705 getApplets( ), 705 getArrivedParties( ), 882 getAscent( ), 765, 766 getAttribute, 1000, 1001, 1007, 1015 getAttributeNames( ), 1007, 1015 getAudioClip( ), 689, 705, 706 getAvailableFontFamilyNames( ), 760–761 getBackground( ), 694 getBeanInfo( ), 940 getBlue( ), 756 getButton(), 717 getByAddress( ), 670 getByName( ), 669 getBytes( ), 378, 594 getCause( ), 222, 224 getChannel( ), 632, 660, 662, 663 getChars( ), 377–378, 391, 613 getChild( ), 713 getClass( ), 185, 186, 277, 427, 429, 432, 921 getClickCount( ), 717 getCodeBase( ), 689, 704 getColor( ), 756 getComponent( ), 712 getConstructor( ), 278, 430 getConstructors( ), 430, 920 getContainer( ), 733 getContentLength( ), 677 getContentLengthLong( ), 677 getContentPane( ), 953, 956 getContents( ), 575 getContentType( ), 677, 1001 getCookies( ), 1005, 1014 getData( ), 684 getDate( ), 677 1098 Index getDateInstance( ), 927 getDateTimeInstance( ), 929 getDeclaredAnnotations( ), 282, 430, 444 getDeclaredMethods( ), 430, 921 getDefault( ), 536, 538 getDescent( ), 765, 766 getDirectionality( ), 414 getDirectory( ), 821 getDisplayCountry( ), 538 getDisplayLanguage( ), 538 getDisplayName( ), 538 getDocumentBase( ), 689, 704 getEchoChar( ), 793 getErrorStream( ), 417 getEventSetDescriptors( ), 936, 943 getExpiration( ), 677 getExponent( ), 435 GetField inner class, 625 getField( ), 278, 431 GetFieldID( ), 315 getFields( ), 431, 920 getFile( ), 821 getFileAttributeView( ), 639 getFiles( ), 822 getFirst( ), 464, 469 getFollowRedirects( ), 679 getFont( ), 760, 764, 765 getForeground( ), 694 getFreeSpace( ), 585 getGraphics( ), 696, 749, 834 getGreen( ), 756 getHeaderField( ), 677 getHeaderFieldKey( ), 677 getHeaderFields( ), 677, 681 getHeight( ), 765, 766, 961 getHostAddress( ), 670 getHostName( ), 671 getIcon( ), 966 getID( ), 438, 536, 710 getImage( ), 689, 705, 830–831 getInetAddress( ), 672, 683 getInitParameter( ), 1000 getInitParameterNames( ), 1000 getInputStream( ), 417, 420, 672, 677, 1001 getInsets( ), 800, 961 getInstance( ), 532, 534, 547 GetIntField( ), 315 getItem( ), 715, 784, 787, 812, 972, 973–974 getItemCount( ), 784, 787 getItemSelectable( ), 715, 787 getKey( ), 487, 489 getKeyChar( ), 716 getKeyCode( ), 716 getLabel( ), 776, 780, 811 getLast( ), 464, 469 getLastModified( ), 677 getLeading( ), 765, 766 getLength( ), 684 getListenerType( ), 940 getLocale( ), 574, 689 getLocalGraphicsEnvironment( ), 761 getLocalHost( ), 669 getLocalizedMessage( ), 222 getLocalPort( ), 672, 683 getLocationOnScreen( ), 717 getMaximum( ), 790 getMessage( ), 217, 222 getMethod( ), 278, 280, 431, 940, 1005 getMethodDescriptors( ), 936 getMethods( ), 431, 920 getMinimum( ), 790 getMinimumSize( ), 796–797 getModifiers( ), 711, 714, 921 getModifiersEx( ), 714 getName( ), 230, 232, 431, 438, 440, 445, 583, 634, 652, 760, 921, 940, 1008, 1010, 1014 getNameCount( ), 634 getNewState( ), 719 GetObjectClass( ), 315 getOffset( ), 536, 684 getOldState( ), 719 getOppositeComponent( ), 713 getOppositeWindow( ), 719 getOutputStream( ), 417, 420, 672, 1001 getParallelism( ), 903 getParameter( ), 689, 701, 702, 1001, 1003, 1011, 1012 getParameterNames( ), 1001, 1003 getParent( ), 440, 583, 634, 652, 882 getPath( ), 987, 1008 getPhase( ), 877 getPoint( ), 716–717 getPoolSize( ), 908 getPort( ), 672, 683, 684 getPreciseWheelRotation( ), 718 getPreferredSize( ), 796–797 getPriority( ), 230, 240, 438 getProperties( ), 424, 519 getProperty( ), 424, 426, 520–521, 522 getPropertyDescriptors( ), 936, 937, 943 getQueuedTaskCount( ), 907 getRed( ), 756 getRegisteredParties( ), 882 getRemoveListenerMethod( ), 940 getRequestMethod( ), 679 getResponseCode( ), 679 getResponseMessage( ), 679 getRGB( ), 756 getRuntime( ), 417, 418 getScript( ), 539 Index getScrollAmount( ), 718 getScrollType( ), 718 getSelectedCheckbox( ), 782 getSelectedIndex( ), 784, 786, 982 getSelectedIndexes( ), 787 getSelectedItem( ), 784, 786, 985 getSelectedItems( ), 787 getSelectedText( ), 792, 795 getSelectedValue( ), 982–983 getServletConfig( ), 999 getServletContext( ), 1000 getServletInfo( ), 999 getServletName( ), 1000 getSession( ), 1005, 1009, 1010, 1015 getSize( ), 740, 754, 760 getSource( ), 710, 778, 975 getStackTrace( ), 222, 438, 446 getState( ), 256–257, 438, 780, 811 getStateChange( ), 715, 787 getStream( ), 705 getSuperclass( ), 431, 432 getSuppressed( ), 222, 306 getSurplusQueuedTaskCount( ), 907 getText( ), 775, 792, 795, 966, 967, 969 getTimeInstance( ), 928 getUnarrivedParties( ), 882 getTotalSpace( ), 585 getUsableSpace( ), 585 getValue( ), 487, 489, 712, 789, 1008, 1010, 1014 getWheelRotation( ), 718 getWhen( ), 711 getWidth( ), 961 getWindow( ), 719 getWriter( ), 997, 1001 getX( ), 716 getXOnScreen( ), 717 getY( ), 716 getYOnScreen( ), 717 GIF image format, 829–830 Glass pane, 949 Glassfish, 994, 995 Glob, 655–656 Gosling, James, 6 goto keyword, 33 Goto statement using labeled break as form of, 104–106 using labeled continue as form of, 107 grabPixels( ), 841–842 Graphics context, 307, 693, 749 sizing, 754–755 Graphics class, 307, 308, 693, 694, 737, 749, 756, 764, 831, 834 drawing methods, 749–753 GraphicsEnvironment class, 737, 760, 761 1099 GregorianCalendar class, 531, 534–536, 539 Grid bag layouts, 805–810 GridBagConstraints class, 737, 806–808 constraint fields, table of, 806–807 GridBagLayout class, 737, 805–806, 808, 810 GridLayout class, 737, 801–802 gridwidth constraint field, 806, 808 group( ), 639, 874 GZIP file format, 579 H Hash code, 471 Hash table, 470–471 hashCode( ), 185, 276, 399, 401, 404, 405, 406, 408, 414, 416, 427, 445, 446, 447, 458, 483, 487, 504, 516, 527, 530, 760 Hashing, 471 HashMap class, 487, 488–489, 490, 491, 516 HashSet class, 465, 470–472 Hashtable class, 466, 508, 516–519 and iterators, 518 legacy methods, table of, 517 hasMoreElements( ), 508, 526 hasMoreTokens( ), 526 hasNext( ), 477, 478 hasNextX( ) Scanner methods, 564, 566 table of, 565 Headers, 677 HeadlessException, 740, 774 headMap( ), 484, 485 headSet( ), 461, 462 HEIGHT, 700 Hexadecimals, 41, 42 as character values, 43 Hierarchical abstraction and classification, 18 and inheritance, 19, 161 High surrogate char, 414 Histogram, 842 Hoare, C.A.R., 230 Holzmann, Gerard J., 840 HotSpot technology, 10 HSB (hue-saturation-brightness) color model, 756 HSBtoRGB( ), 756 HSPACE, 700, 701 HTML (Hypertext Markup Language), 993 file for an applet, 308, 688, 699–701 HTTP, 668, 675, 993 downloads, 1054 GET requests, handling, 1010–1011 and HttpURLConnection class, 679 port, 668 POST requests, handling, 1010, 1012–1013 requests, 993, 994, 1004–1005, 1010 response, 993, 994, 997, 1004, 1005–1006 1100 Index HTTP (continued) and URLConnection class, 677 HTTP session stateful, 681 tracking, 1015–1016 HttpCookie class, 681 HttpServlet class, 1004, 1008 methods, table of, 1009 HttpServletRequest interface, 1004, 1015 methods, table of several, 1005 HttpServletResponse interface, 1004, 1005 methods, table of, 1006 HttpSession interface, 1004, 1006, 1010, 1015 methods, table of several, 1007 HttpSessionBindingEvent class, 1004, 1010 HttpSessionBindingListener interface, 1004, 1006 HttpSessionEvent class, 1004, 1009, 1010 HttpURLConnection class, 679–681 hypot( ), 435 I Icon interface, 966 Icons, Swing button, 969 Identifiers, 24, 32, 33, 44 IdentityHashMap class, 487, 491 IEEEremainder( ), 435 if statement, 28–29, 30, 81–84 boolean variable used to control the, 82, 274 nested, 83 and recursive methods, 140 switch statement versus, 88–89 if-else-if ladder, 83–84 IllegalAccessException, 218, 221 IllegalArgumentException, 220, 457, 459, 461, 463, 475, 483, 484, 485, 502, 503, 504 IllegalFormatException, 550 IllegalMonitorStateException, 220 IllegalStateException, 220, 457, 463, 465, 912, 1006 IllegalThreadStateException, 220 Image class, 737, 829, 830–831 ImageConsumer interface, 841–843, 844 ImageFilter class, 844–857 ImageIcon class, 965, 966 ImageObserver interface, 831, 832–833, 835 ImageProducer interface, 830, 839–840, 841, 844 imageUpdate( ), 832–833 bit flags, table of, 833 Images, 829–860 animation of, 857–860 creating, loading, displaying, 830–832 double buffering and, 833–836 stream model for, 844 Imaging, 829 IMG tag, 701 implements clause, 197 and generic interfaces, 348–349 import statement, 194–195 and static import, 319–321 in, 292, 293, 420, 423 Increment operator (++), 30, 61, 64–66 indexOf( ), 382–383, 394, 395, 459, 460, 510, 511 IndexOutOfBoundsException, 220, 459 Inet4Address class, 671 Inet6Address class, 671 InetAddress class, 669–671, 683 InetSocketAddress class, 683 infinity (IEEE floating-point specification value), 402 inForkJoinPool( ), 907 INHERIT, 421 InheritableThreadLocal class, 444 Inheritance, 5, 19–21, 22–23, 142, 145, 161–186 and annotations, 288 and enumerations, 265 final and, 184–185 and interfaces, 187, 197, 206 multilevel, 171–174 and multiple superclasses, 163, 187 @Inherited built-in annotation, 286, 287 init( ), 689, 691, 692, 693, 695, 741, 994, 997, 999 and Swing, 957, 959 initCause( ), 222, 224 Inline method calls, 184 Inner classes, 149–152, 731–733 anonymous, 732–733 InputEvent class, 710, 713–714, 715, 716 InputMismatchException, 566 InputStream class, 290, 291, 293, 563, 564, 590, 591, 592, 596, 599, 600, 602, 608, 628, 650 methods, table of, 591 objects, concatenating, 603–605 InputStreamReader class, 292, 293 insert( ), 392, 795 Insets class, 737, 800–801, 961 Instance of a class, 19, 109 See also Object(s) Instance variables accessing, 111, 116, 117–118, 120 definition of, 19, 110 hiding, 125 static, 145–146 as unique to their object, 111, 112–113 using super to access hidden, 170–171 instanceof operator, 310–312, 482 and generic classes, 355–357 InstantiationException, 221 Index Instrumentation interface, 450 int, 27, 35, 36, 37 and integer literals, 41 IntBuffer class, 631 Integer class, 269, 403, 410–411 constructors, 269 methods, table of, 406–407 Integer(s), 35, 36–38, 66–67 literals, 41–42 interface keyword, 187, 196 and annotations, 276 interfaceModifiers( ), 922 Interface(s), 187, 196–206 general form of, 196–197 generic. See Generic interfaces implementing, 197–199 and the inheritance hierarchy, 196 inheritance of, 205–206 member, 200 methods, 196, 197–198 nested, 200 reference variables, 198–199, 204 types for bounded types, using, 336 variables, 197, 204–205 Internet, 3, 6, 7, 8, 12, 16, 667 addresses, obtaining, 671 addressing scheme, 668 downloads, 1053–1054 and portability, 7, 8, 9 and security, 8–9 Internet Engineering Task Force (IETF) BCP 47, 539 Internet Protocol (IP) addresses, 668 definition of, 667 InterNIC, 672, 674 InterruptedException, 221, 232, 842 Introspection, 934–937, 940, 943 Introspector class, 939, 940 intValue( ), 269, 398, 400, 401, 404, 405, 406, 408 InvalidPathException, 638 Investment, future value of an applet for finding, 1028–1031 formula to compute, 1028 Investment required to achieve a future value applet for finding, 1032–1036 formula to compute, 1032 invoke( ), 895, 896, 905 invokeAll( ), 895, 900, 907 invokeAndWait( ), 954, 959 invokeLater( ), 954, 959 I/O, 26, 289–307, 581–628 and applets, 307, 309 1101 channel-based, 13, 290, 629. See also NIO; NIO and channel-based I/O classes, list of, 581–582 console, 26, 93, 289, 293–297 error handling, 299–300 exceptions, 588–589 file, 297–306, 582–588 formatted. See I/O, formatted interfaces, list of, 582 new. See NIO redirection, 421 streams. See Streams I/O, formatted, 14, 548–573 format specifiers. See Format specifiers using Formatter, 548–562. See also Formatter class using printf( ), 155, 620 using Scanner, 563–573. See also Scanner class io package. See java.io package IOError, 620 IOException, 93, 293, 298, 301, 302, 588, 591, 592, 596, 602, 610, 613, 623, 624, 625, 635, 654, 657, 672, 682 ipadx constraint field, 806, 808 ipady constraint field, 806, 808 IPv4 (Internet Protocol, version 4), 668, 669, 670, 671 IPv6 (Internet Protocol, version 6), 668, 669, 670, 671 isAbsolute( ), 584, 635 isAlive( ), 230, 238–240, 438 isAltDown( ), 714 isAltGraphDown( ), 714 isAnnotationPresent( ), 282, 284, 445 isBound( ), 672, 684, 940 isCancelled( ), 906 isClosed( ), 672 isCompletedAbnormally( ), 906 isCompletedNormally( ), 906 isConnected( ), 672, 684 isConstrained( ), 940 isControlDown( ), 714 isDigit( ), 412, 413, 414 isDirectory( ), 585, 636, 638 isEditable( ), 793, 795 isEmpty( ), 388, 457, 458, 483, 510, 515, 517, 527 isEnabled( ), 811 isExecutable( ), 636, 652 isFile( ), 584 isHidden( ), 585, 636, 639 isInfinite( ), 400, 401, 402–403 isLeapYear( ), 535 isLetter( ), 412, 413, 414 isLowercase( ), 412, 413 isMetaDown( ), 714 isMulticastAddress( ), 671 isMultipleMode( ), 822 1102 Index isNaN( ), 400, 401, 402–403 ISO-Latin-1 character set, 39, 43 isPopupTrigger( ), 717 isPublic( ), 921 isQuiescent( ), 907 isReadable( ), 636, 652 isSelected( ), 972, 973, 974, 975 isSet array, 531 isSet( ), 532 isShiftDown( ), 714 isShutdown( ), 908 isTemporary( ), 713 isTerminated( ), 908 isTimeSet, 531 isUppercase( ), 412, 413 isWhitespace( ), 412 413 isWritable( ), 636, 639, 652 ItemEvent class, 710, 714–715, 780, 784, 787, 812, 972, 973 ItemListener interface, 720, 721, 780, 784, 812, 972, 973 ItemSelectable interface, 715 itemStateChanged( ), 721, 780, 784, 972, 973 Iterable interface, 449, 456, 457, 479, 482, 509 Iterable interface, 634, 654 Iteration statements, 81, 89–102 Iterator, 455, 456, 476–480 and maps, 482 and PriorityQueue, 474 and synchronized collections, 499 Iterator interface, 455, 457, 476–477, 478–479, 507, 508 methods, table of, 477 iterator( ), 449, 458, 459, 477–478, 654 J J2SE 5, new features of, 13–14 JApplet class, 687, 949, 957, 959 Java API packages, table of core, 909–911 and C, 3, 5, 7, 11 and C++, 3, 7, 11 and C#, 8 design features (buzzwords), 10–13 history of, 3, 6–8, 13–16 and the Internet, 3, 6, 7–9, 12, 16, 667, 1053 as interpreted language, 9, 10, 12 keywords, 33–34 as strongly typed language, 10, 11, 35, 41 versions of, 13–14 and the World Wide Web, 6, 7, 11 Java Archive (JAR) files, 578 Java Beans, 432, 451, 909, 919, 933–943 advantages of, 934 API, 938–940 customizers, 937 demonstration program, 940–943 introspection, 934–937, 940, 943 properties. See Property, Java Bean serialization, 937 .java filename extension, 23 Java Community Process (JCP), 16 Java EE SDK, 994, 998 Java Foundation Classes (JFC), 946 java (Java application launcher). See Application launcher (java) Java Native Interface (JNI), 313 Java Network Launch Protocol (JNLP), 688, 699 java package, 194 Java SE 7, 14–16 Java Virtual Machine (JVM), 9–10, 12, 13, 16, 24, 25, 417, 437, 451 java.applet package, 289, 307, 687 java.awt package, 707, 710, 736, 829, 830, 956 classes, tables of some, 736–737 java.awt.Dimension class, 919 java.awt.event package, 707, 709, 710, 720, 729, 954, 956 event classes, table of commonly used, 710 interfaces, table of commonly used, 720 java.awt.image package, 829, 839, 844, 860 java.beans package, 936, 938–940 classes, table of, 938–939 interfaces, tables of, 938 java.io package, 289, 290–292, 298, 304, 581–582, 588, 629, 652 classes, list of, 581–582 interfaces, list of, 582 java.io.Externalizable interface, 937 java.io.IOException, 93 java.io.Serializable interface, 937 java.lang package, 194, 220–221, 277, 286, 292, 298, 304, 348, 371, 397–451, 588 classes and interfaces, list of, 397 implicit importation of the, 194 java.lang.annotation package, 276, 286, 450 java.lang.annotation.RententionPolicy enumeration, 276–277, 450 java.lang.image package, 841 java.lang.instrument package, 450 java.lang.invoke package, 451 java.lang.management package, 451 java.lang.ref package, 451 java.lang.reflect package, 277, 282, 451, 909, 919 classes, table of, 920 java.net package, 667, 681 classes and interfaces, list of, 668–669 java.nio package, 290, 581, 585, 629, 630 Index java.nio.channels package, 629, 631, 633 java.nio.channels.spi package, 629 java.nio.charset package, 629, 633 java.nio.charset.spi package, 629 java.nio.file package, 629, 634 java.nio.file.attribute package, 629, 634, 638 java.nio.file.spi package, 629, 634 java.rmi package, 909, 924 java.text package, 909, 927 java.util package, 453–454, 508, 525, 707, 709 classes, list of top-level, 453–454 interfaces defined by, list of, 454 java.util.concurrent package, 578, 862–863, 888 java.util.concurrent.atomic package, 578, 862, 863, 892 java.util.concurrent.locks package, 578, 862, 863, 889, 890, 892 java.util.jar package, 578 java.util.logging package, 578 java.util.prefs package, 578 java.util.regex package, 579, 909, 911 java.util.spi package, 579 java.util.zip package, 579 javac (Java compiler), 23–24, 188 and generics, 325 javadoc tags, 1079–1083 utility program, 1079, 1083 javah.exe, 314, 315 javap, 364 javax.imageio package, 860 javax.servlet package, 997, 998–1002 interfaces and classes, list of core, 998–999 javax.servlet.http package, 998, 1004–1010 interfaces and classes, list of core, 1004 javax.swing package, 948, 950, 951, 965, 987 javax.swing.event package, 954, 967, 982, 987 javax.swing.table package, 990 javax.swing.tree package, 987 JButton class, 949, 956, 965, 969–971 JCheckBox class, 965. 969, 971, 973–975 JComboBox class, 965, 984–986 JComponent class, 948, 949, 957, 960, 961, 965 JDialog class, 949 JDK 7 (Java SE 7 Development Kit), 14, 23 JFrame class, 949, 950, 951–952, 953, 963 JIT (Just-In-Time) compiler, 10, 12 JLabel class, 950, 952, 954, 965–967 JLayeredPane class, 949 JList class, 965, 981–984 jni.h, 315 jni_md.h, 315 join( ), 230, 238–240, 439, 894, 895, 903, 905 Joy, Bill, 6 1103 JPanel class, 949, 963, 977 JPEG image format, 830 JRadioButton class, 965, 969, 971, 975–977 JRootPane class, 949 JScrollBar class, 949 JScrollPane class, 965, 979–981, 982, 987, 988, 990 JTabbedPane class, 965, 977–979 JTable class, 965, 990–992 JTextCompenent class, 967 JTextField class, 965, 967–968, 1025 JToggleButton class, 965, 969, 971–973, 975 JToggleButton.ToggleButtonModel class, 971 JTree class, 965, 986–989 Jump statements, 81, 102–108 Just In Time (JIT) compiler, 10, 12 JVM (Java Virtual Machine), 9–10, 12, 13, 16, 24, 25, 417, 437, 451 JWindow class, 949 K Kernighan, Brian, 4 Key codes, virtual, 715–716, 728 KeyAdapter class, 730 Keyboard events, handling, 726–729 KeyEvent class, 710, 712, 713, 715–716 KeyListener interface, 720, 722, 726–729, 730 keyPressed( ), 722, 726, 727 keyReleased( ), 722, 726 keys( ), 515, 517 keySet( ), 483, 484, 518, 575, 681 keyTyped( ), 722, 726, 727 Keywords, table of Java, 33 L Label AWT standard control, 775–776 Swing, 950, 952, 965–967 used with break statement, 104 used with continue statement, 107 Label class, 737, 775–776 last( ), 461, 803 lastElement( ), 510, 511 lastIndexOf( ), 382–383, 395, 459, 460, 510, 511 lastKey( ), 484 Late binding, 184 Layered pane, 949 Layout managers, 739, 773, 796–810 default, 773, 796, 797 LayoutManager interface, 796 length instance variable of arrays, 147–149 length( ), 153, 374, 389–390, 448, 527 1104 Index Lexer (lexical analyzer), 525 Libraries, class, 23, 34 Lindholm, Tim, 6 LineNumberInputStream deprecated class, 582 LineNumberReader class, 292 LinkedBlockingDeque class, 889 LinkedBlockingQueue class, 889 LinkedHashMap class, 487, 490–491 LinkedHashSet class, 465, 472 LinkedList class, 465, 469–470 example program using the, 480–481 LinkedTransferQueue, 889 List class, 737, 786 extending, 826–827 List controls, 720, 786–788 List interface, 456, 459, 466, 469, 478, 501, 509, 511 methods, table of, 460 List, Swing, 981–984 list( ) and directories, 583, 585–587 listFiles( ), 587 ListIterator interface, 457, 476–477, 478–479, 507 methods, table of, 477 listIterator( ), 460, 478 ListModel, 982 ListResourceBundle class, 575 ListSelectionEvent class, 982, 983, 990 ListSelectionListener interface, 982, 983 ListSelectionModel, 982, 990 Literals, 32, 41–44 class, 279 regular expression, 913 string, 374 load( ), 418, 424, 520, 522–524 loadLibrary( ), 314, 418, 424 Loan balance, applet to find, 1044–1047 Loan payments applet to compute, 1020–1028 formula for calculating, 1020 servlet to compute, 1048–1051 Locale class, 387, 538–539, 927, 928 Locale Data Markup Language (LDML), 539 Locale.Builder class, 539 Lock interface, 863, 890 methods, table of, 890 lock( ), 863, 890 lockInterruptibly( ), 890 Locks, 889–892 log( ) math method, 434 servlet method, 1000, 1002 log10( ), 434 log1p( ), 434 Logical operators bitwise, 67–69 Boolean, 75–77 long, 35, 36, 37–38 literal, 41 Long class, 269, 403, 410 methods, table of, 408–409 LongBuffer class, 631 longValue( ), 269, 398, 400, 401, 404, 405, 406, 408 Look and feels, 946–947 lookup( ), 925 loop( ), 706 Loop(s), 81 Boolean object to control, using a, 274 continue statement and, 106–107 do-while, 90–93 for. See for loop infinite, 96–97, 103 nested, 102, 104, 105–106 with polling, event, 228, 245 while, 89–90 Low surrogate char, 414 M main( ), 25–26, 110, 142, 145 and applets, 26, 110, 308, 309, 688 and the java application launcher, 25 and command-line arguments, 25, 154–155 and Swing programs, 953–954 and windowed applications, 747, 748 main (default name of main thread), 232 MalformedURLException, 675 Map interface, 482–484, 486, 488, 491, 515, 516, 518 methods, table of, 483 map( ), 633, 644, 647, 662, 664 Map(s), 455, 482–492 classes, 487–492 collection-view of a, obtaining a, 455, 482, 483–484 interfaces, 482–487 Map.Entry interface, 482, 486–487 methods, table of, 487 MapMode.PRIVATE, 644 MapMode.READ_ONLY, 644 MapMode.READ_WRITE, 644, 647 MappedByteBuffer class, 631, 644 mark( ), 591, 592, 597, 600, 603, 611, 616, 631 markSupported( ), 591, 600, 603, 610, 611, 616 Matcher class, 911, 912 matcher( ), 912 matches( ), 388, 912, 914, 919 Math class, 45, 131, 433–436 rounding methods, table of, 434–435 and static import example, 319–321 max( ), 434, 497, 500 MAX_EXPONENT, 399 MAX_PRIORITY, 240, 437 Index MAX_RADIX, 411 MAX_VALUE, 399, 403, 411 MediaTracker class, 737, 829, 836–839 Member, class, 19, 110 access and inheritance, 163–164 access, table of, 191 controlling access to, 141–144 static, 145–146 Member interface, 451, 919 Memory allocation using new, 51–52, 53, 113–114 leaks, 298, 304 management, in Java, 11–12, 125 and Runtime class, 418–419 MemoryImageSource class, 839–840, 844 Menu bars and menus, 773, 810–816 action command string of, 812 and events, 812 Menu class, 737, 810, 811 Menu item as an event source, 720 MenuBar class, 737, 810, 811 MenuItem class, 737, 810–811, 812 Metadata, 276 See also Annotation(s) Method class, 278, 281, 282, 451, 920, 921, 940 Method(s), 19, 110, 115–121 abstract, 181–184 and annotations, 276, 288 and autoboxing, 271–272 bridge, 362–364 calling, 117, 119 dispatch, dynamic, 178–181 and the dot (.) operator, 111, 117, 118 factory, 669 final, 147, 184 general form, 116 generic, 326, 338, 344–346, 366 getter, 934 hidden, using super to access, 170–171, 176 inlining, 184 interface, 196, 197–198 lookup, dynamic, 198 native, 312–316 overloading, 129–134, 158–160, 177 overriding. See Overriding, method and parameters, 116, 119–121 passing an object to, 137–138 recursive, 139–141 resolution, dynamic, 196 returning an object from, 138–139 returning a value from, 118–119 scope defined by, 45–47 setter, 934 static, 145–146 subclasser responsibility, 182 synchronized, 230, 241–243 type inference and, 360 varargs. See Varargs variable-arity, 155 MethodDescriptor class, 936, 939, 940 MethodHandle class, 451 methodModifiers( ), 922 MethodType class, 451 MIME (Multipurpose Internet Mail Extensions), 993, 997 min( ), 434, 497, 500 minimumLayoutSize( ), 796 MIN_EXPONENT, 399 MIN_NORMAL, 399 MIN_PRIORITY, 240, 437 MIN_RADIX, 411 MIN_VALUE, 399, 403, 411 mkdir( ), 588 mkdirs( ), 588 Model-Delegate component architecture, 947–948 Model-View-Controller (MVC) component architecture, 947 Modifier class, 921, 922 “is” methods, table of, 923 Modulus operator (%), 61, 63 Monitor, 230, 241, 243 Mouse events, handling, 723–726 MouseAdapter class, 730 mouseClicked( ), 722, 730 mouseDragged( ), 722, 729 mouseEntered( ), 722 MouseEvent class, 710, 712, 713, 716–717 mouseExited( ), 722 MouseListener interface, 720, 722, 723–726, 730 MouseMotionAdapter class, 729, 730 MouseMotionListener interface, 709, 720, 722, 723–726, 729, 730 mouseMoved( ), 722, 729 mousePressed( ), 722 mouseReleased( ), 722 MouseWheelEvent class, 710, 717–718 MouseWheelListener interface, 720, 722, 723 mouseWheelMoved( ), 722 Multicore systems, 228–229, 257, 862, 863, 893–894, 898 Multitasking, 227, 229 Multithreaded programming, 7, 11, 12, 227–257 context switching. See Context switching effectively using, 257 and multicore versus single-core systems, 228 Observable class, Observer interface and, 544 and spurious wakeup, 245 and StringBuilder class, 395 and synchronization. See Synchronization and threads. See Thread(s) 1105 1106 Index Multithreaded programming (continued) versus the concurrency utilities, traditional, 861, 908 and parallel programming, 893–894 versus single-threaded system, 228 MutableComboBoxModel, 985 MutableTreeNode interface, 987 MVC (Model-View-Controller) component architecture, 947 N NAME, 700 Name-space collisions between instance variables and local variables, 125 packages and, 187, 188, 321 Naming class, 924, 925 NaN, 399, 402 nanoTime( ), 424, 425, 900 native modifier, 313 Natural ordering, 448, 492 Naughton, Patrick, 6 NavigableMap interface, 482, 484–486, 489 methods, table of, 485–486 NavigableSet interface, 456, 461, 472, 473 methods, table of, 462 Negative numbers in Java, representation of, 66, 67 NEGATIVE_INFINITY, 399 NegativeArraySizeException, 220, 501 .NET Framework, 8 NetBeans, 994, 995 Networking, 667–685 basics, 667–668 classes and interfaces, list of, 668–669 new, 51–52, 53, 113–114, 121, 123, 125, 139, 182 autoboxing and, 271 and enumerations, 260, 263 and type inference, 359–360 NEW, 256 New I/O. See NIO newByteChannel( ), 633, 636, 641, 644, 645, 646 newCachedThreadPool( ), 883 newCondition( ), 890 newDirectoryStream( ), 636, 654, 655–657 newFixedThreadPool( ), 883 newInputStream( ), 637, 649, 650–651 newOutputStream( ), 637, 649, 651 newScheduledThreadPool( ), 883 next( ), 477, 478, 566, 803 nextAfter( ), 435 nextBoolean( ), 539, 566 nextBytes( ), 539 nextDouble( ), 205, 539, 540, 566, 568, 570 nextElement( ), 508–509, 526, 605 nextFloat( ), 539, 540, 566 nextGaussian( ), 539, 540 nextInt( ), 539, 566, 570 nextLong( ), 539, 566 nextToken( ), 526 nextUp( ), 435 nextX( ) Scanner methods, 564, 566, 568, 570 table of, 566 NIO, 15, 581, 629–665 packages, list of, 629 pre-JDK 7 NIO versus new, 660 reading a file using pre-JDK 7, 660–663 for path and file system operations, using, 652–659 for stream-based I/O, using, 649–651 writing to a file using pre-JDK 7, 663–665 NIO and channel-based I/O copying a file using, 648–649 reading a file using, 641–645 writing to a file using, 645–648 NIO.2, 629, 640, 652 NORM_PRIORITY, 240, 437 NoSuchElementException, 461, 463, 484, 509, 566, 573 NoSuchFieldException, 221 NoSuchMethodException, 221, 278 NOT operator bitwise unary (~), 66, 67, 68–69 Boolean logical unary (!), 75–76 NotDirectoryException, 654 notepad, 420, 423 notify( ), 185, 186, 245, 247–249, 254–255, 427, 890, 908 notifyAll( ), 185, 186, 245, 427 notifyObservers( ), 541, 542 NotSerializableException, 627 null, 34, 113 Null statement, 90 NullPointerException, 217, 220, 457, 459, 461, 463, 475, 483, 484, 485, 501, 516, 573, 605 Number class, 269, 398 NumberFormat class, 927, 1024 NumberFormatException, 221, 269, 702 Numbers, formatting, 551–552, 555–557, 1024 O Oak, 6 Object class, 185–186, 326, 427 as a data type, problems with using the, 330–332, 506 methods, table of, 185, 427 Object reference variables and abstract classes, 182, 184 Index assigning, 115 declaring, 113 and cloning, 427–428 and dynamic method dispatch, 178–181 interface, 198–199 to superclass reference variable, assigning subclass, 166, 170 OBJECT tag, 688, 701 Object-oriented programming (OOP), 5, 6, 17–23, 109 model in Java, 11 Object(s), 19, 109, 114 bitwise copy (clone) of, 427 creating, 111, 113–114 initialization with a constructor, 121, 123–124 to a method, passing, 137–138 monitor, implicit, 230, 243 as parameters, 134–136 returning, 138–139 serialization of. See Serialization type at run time, determining, 310–312 Object.notify( ), 890 Object.wait( ), 890 ObjectInput interface, 625 methods defined by, table of, 625 ObjectInputStream class, 291, 625 methods defined by, table of, 626 ObjectOutput interface, 623 methods defined by, table of, 623 ObjectOutputStream class, 291, 624 methods defined by, table of, 624 Objects class, 577 Observable class, 541–544 methods, table of, 541 Observer interface, 541–544 Octals, 41 as character values, 43 of( ), 475, 476 offer( ), 463, 474 offerFirst( ), 463, 464, 469 offerLast( ), 463, 464, 469 offsetByCodePoints( ), 388, 395 onAdvance( ), 879, 882 open( ), 633 openConnection( ), 676, 678, 679 OpenOption interface, 635 Operator(s) arithmetic, 61–66 assignment. See Assignment operator(s) bitwise, 66–74 Boolean logical, 75–77 conditional-and, 77 conditional-or, 77 1107 diamond (<>), 359–360 parentheses and, 79 precedence, table of, 78 relational, 28, 41, 74–75 ternary if-then-else (?:), 77–78 OR operator (|) bitwise, 66, 67, 68–69 Boolean logical, 75–76 OR operator, short-circuit (||) Boolean logical, 75, 76–77 Oracle, 14 Ordinal value enumeration constants, 265–266 ordinal( ), 265, 266, 447 out output stream, 26, 292, 293, 420, 423 out( ), 549, 551 OutputStream class, 290, 291, 296, 590, 591, 599, 601, 605, 607, 619, 624, 628, 651 methods, table of, 592 OutputStreamWriter class, 292 Overloading methods, 129–134, 158–160, 177 @Override, built-in annotation, 286, 287 Overriding, method, 175–181 and dynamic method dispatch, 178–181 final to prevent, using, 184 in a generic class, 358–359 and run-time polymorphism, 178, 179, 181 P Package(s), 142, 187–196, 206 access to classes contained in, 190–194, 195 core Java API, table of, 909–911 defining, 188 finding, 188–189 importing, 194–196 Swing, 950 version data, obtaining, 444 Package class, 282, 444–445 methods, table of, 444–445 package statement, 188, 194 Paint mode, setting, 757–759 paint( ), 297, 691, 692, 693, 694, 695, 696, 698, 742, 749, 832, 957, 960 lightweight components and overriding, 828 Paintable area, computing, 961 paintBorder( ), 960 paintChildren( ), 960 paintComponent( ), 960, 963, 964 Painting in Swing, 959–964 Panel class, 688, 737, 738, 739, 803 Panes, Swing container, 949. See also Content pane Parallel programming. See Programming, parallel PARAM NAME, 700, 701 1108 Index Parameter(s), 25, 116, 119–121 applets and, 701–703 and constructors, 123–124 objects as, 134–136 and overloaded constructors, 134 and overloaded methods, 129, 177 and the scope of a method, 46 servlet, reading, 298–300 type. See Type parameter(s) variable-length (varargs), 157 Parameterized types, 326, 328 parseBoolean( ), 416 parseByte( ), 404, 410 parseDouble( ), 401 parseFloat( ), 400 parseInt( ), 407, 410 parseLong( ), 409, 410 parseShort( ), 405, 410 Parsing, definition of, 525 Pascal, 4 Passwords, reading, 620 Path interface, 582, 585, 634–635, 640, 641, 652, 654, 660 instance for stream-based I/O, using a, 649–651 methods, table of a sampling of, 634–635 obtaining an instance of the, 638, 640 Paths class, 638, 640 Pattern class, 911–912 Pattern matching. See Regular expressions PatternSyntaxException, 913 Payne, Jonathan, 6 peek( ), 463, 513 peekFirst( ), 464, 469 peekLast( ), 464, 469 Peers, native, 945–946, 828 Persistence (Java Beans), 937 Phaser class, 862, 875–882 compatability with fork/join, 908 PI (Math constant), 433 PIPE, 421 PipedInputStream class, 291 PipedOutputStream class, 291 PipedReader class, 292 PipedWriter class, 292 PixelGrabber class, 841–843, 844 play( ), 690, 706 Pluggable look and feel (PLAF), 946–947, 948, 992 PNG file format, 830, 831 Point class, 717, 747 Pointers, 59, 113 poll( ), 463, 474 pollFirst( ), 462, 464, 469 Polling, 228, 245 pollLast( ), 462, 464, 469 Polygon class, 737, 753 Polymorphism, 5, 21–23 and dynamic method dispatch, 178–181, 182 and interfaces, 196, 199–200, 204 and overloaded methods, 129, 131, 132 pop( ), 464, 465, 513 PopupMenu class, 737, 816 Port, 667, 675 Portability problem, 6–7, 8, 9, 10, 12, 16 and data types, 36 and native methods, 316 and thread context switching, 229 POSITIVE_INFINITY, 399 PosixFileAttributes class, 639, 654 PosixFileAttributeView interface, 639 postVisitDirectory( ), 658 pow( ), 319–321, 434 preferredLayoutSize( ), 796 previous( ), 477, 803 preVisitDirectory( ), 658 print( ), 27, 34, 296, 297, 376, 606, 620, 1002 printf( ) function, C/C++, 548 method, Java, 155, 562, 606–607, 619, 620, 621 println( ), 26, 27, 34, 186, 296, 297, 376, 606, 619, 620, 1002 and applets, 706 and String objects, 58 printStackTrace( ), 222 PrintStream class, 291, 293, 296, 562, 605–607 PrintWriter class, 292, 296–297, 562, 619–620, 997 PriorityBlockingQueue class, 889 PriorityQueue class, 465, 473–474 private access modifier, 25, 142–144, 190–191 and inheritance, 163–164 Process class, 416–417, 420, 421 methods, table of, 417 Process, definition of, 416 Process-based versus thread-based multitasking, 227 processActionEvent( ), 823, 827 processAdjustmentEvent( ), 823, 827 ProcessBuilder class, 416, 421–423 methods, table of, 421–422 ProcessBuilder.Redirect class, 421 ProcessBuilder.Redirect.Type enumeration, 421 processComponentEvent( ), 823 processFocusEvent( ), 823 processItemEvent( ), 823, 824, 826 processKeyEvent( ), 823 processMouseEvent( ), 823 processMouseMotionEvent( ), 823 processMouseWheelEvent( ), 823 processTextEvent( ), 823 Index Programming multithreaded. See Multithreaded programming object-oriented. See Object-oriented programming process-oriented, 17, 18, 22 structured, 4, 5 Programming, parallel, 15, 229, 862, 863, 893–894 and specifying the level of parallelism, 896, 900–903 Project Coin, 15 Properties class, 454, 508, 519–522 methods, table of, 520 Properties, environment, 426 Property, Java Bean, 940 bound and constrained, 937 design patterns for, 934–935, 937, 940 PropertyChangeEvent, 937 PropertyChangeListener interface, 937, 938 PropertyDescriptor class, 936, 939, 940, 942 PropertyPermission class, 577 PropertyResourceBundle class, 575 PropertyVetoException, 937 protected access modifier, 126, 142, 190–191 public access modifier, 25, 142–143, 190–191 Push buttons, 691, 776–779 action command string of, 776, 778, 779, 969 Swing, 954–957, 969–971 push( ), 464, 465, 513 Pushback, 602 PushbackInputStream, 291, 599, 602–603 PushbackReader class, 292, 618–619 put( ), 483, 488, 489, 491, 515, 517 and buffers, 631, 632, 646–647, 664 putAll( ), 483, 491 PutField inner class, 624 Q Query string, 1011 Queue interface, 456, 462–463, 469, 473, 474 methods, table of, 463 quietlyInvoke( ), 907 quietlyJoin( ), 907 R Race condition, 243 Radio buttons, 782 Swing, 975–977 Radix, 403 radix( ), 573 Random class, 205, 539–540 methods, table of, 539 random( ), 435 RandomAccess interface, 457, 482 1109 RandomAccessFile class, 609–610, 632, 664 range( ), 475, 476 Raw types, 349–352, 508 and erasure, 362 READ, 421 read( ), 93, 292, 293–294, 298–299, 303, 449, 591, 600, 602, 611, 618, 625, 626, 633, 641, 650, 661 and end-of-file condition, 303 Readable interface, 449, 563, 569, 610 ReadableByteChannel interface, 563 readAttributes( ), 637, 639, 652 readBoolean( ), 608, 626 readDouble( ), 608, 626 Reader class, 291, 292, 293, 590, 610, 612, 614, 628 methods defined by, table of, 611 readExternal( ), 623 readInt( ), 608, 626 readLine( ), 294–295, 410, 620, 621, 1002 readObject( ), 625, 626 readPassword( ), 620, 621 ReadWriteLock interface, 892 Real numbers, 38 rebind( ), 924 receive( ), 683 Recursion, 139–141 and the Fork/Join Framework divide-and-conquer strategy, 897 RecursiveAction class, 863, 894, 895, 897, 898, 899, 903 RecursiveTask class, 863, 894, 895–896, 897 example program using, 903–905 Redirect class, 421 ReentrantLock, 890 ReentrantReadWriteLock, 892 Reflection, 277, 451, 909, 919–923 and annotations, 277–282 ReflectiveOperationException, 221 regionMatches( ), 379–380 register( ), 876 Regular expressions, 389, 564, 571, 909, 911–919 syntax, 913 wildcards and quantifiers, 911, 913, 915–916 reinitialize( ), 906 Relational operators, 28, 41, 74–75 release( ), 864–867 Remote interface, 924 Remote method invocation (RMI), 12, 622, 909, 923–927 RemoteException, 924 remove( ), 457, 458, 460, 463, 471, 477, 483, 515, 516, 517, 774, 953 removeActionListener( ), 956 removeAll( ), 457, 458, 774 removeAttribute( ), 1007, 1015 removeEldestEntry( ), 491 1110 Index removeElement( ), 510, 511 removeElementAt( ), 510, 511 removeFirst( ), 464, 469 removeKeyListener( ), 709 removeLast( ), 465, 469 removeTListener( ), 936 removeTypeListener( ), 709 renameTo( ), 584 repaint( ), 696–698, 742, 960 replace( ), 385, 393–394 replaceAll( ), 388, 498, 912, 917–918 replaceFirst( ), 388 replaceRange( ), 795 ReplicateScaleFilter class, 844 reset( ), 573, 591, 592, 597, 599, 600, 603, 611, 616, 631 resolve( ), 634, 635 Resource bundles, 573–577 ResourceBundle class, 573–575 methods, table of, 574–575 ResourceBundle.Control class, 573 resume( ), 13, 251–254, 437, 443 retainAll( ), 457, 458 @Retention built-in annotation, 277, 286 RetentionPolicy enumeration, 276–277, 450 return statement, 108, 116 reverse( ), 392–393, 407, 409, 498 reverseBytes( ), 405, 407, 409 reverseOrder( ), 498, 500 rewind( ), 631, 643, 646, 647, 661, 664 RGB (red-green-blue) color model, 756 default, 840 RGBImageFilter class, 844, 845–857 RGBtoHSB( ), 756 Richards, Martin, 4 rint( ), 435 Ritchie, Dennis, 4 RMI compiler (rmic), 925–926 rmi protocol, 925 rmiregistry (RMI registry), 926 round( ), 435 Run-time system, Java, 9. See also Java Virtual Machine (JVM) type information, 13, 310, 355, 357, 432 run( ), 230, 233, 234, 437, 439, 544, 545, 907, 954 overriding, 235, 236, 544 using a flag variable with, 255–256 Runnable interface, 230–231, 437, 544, 905, 907, 954 implementing the, 233–235, 236 Runtime class, 416, 417–420, 903 executing other programs and, 420 memory management and, 418–419 methods, table of some, 417–418 RUNTIME retention policy, 276–277, 278, 281 RuntimeException class, 208–209, 217, 220, 224 RuntimePermission class, 445 S @SafeVarargs built-in annotation, 286, 287 save( ), 519 scalb( ), 434 Scanner, 525 Scanner class, 563–573 closing an instance of the, 569 constructors, 563–564 delimiters, 564, 570–572 demonstration programs, 567–570 hasNextX( ) methods, table of, 565 how to use, 564, 566 methods, miscellaneous, 572–573 nextX( ) methods, table of, 566 schedule( ), 545–546 ScheduledExecutorService interface, 883 ScheduledThreadPoolExecutor class, 863, 883 Scientific notation, 42, 551–552 Scopes in Java, 45–47 Scroll bars, 720, 788–791, 979 Scroll pane, 979–981 Scrollbar class, 737, 789 extending, 827–828 search( ), 513–514 Security manager, 298, 994 Security problem, 8, 9–10, 16 and native methods, 316 and servlets, 994 SecurityException, 220, 298, 417, 423, 589, 606, 654, 657 SecurityManager class, 445 seek( ), 610 SeekableByteChannel interface, 633, 641, 644, 645 select( ), 784, 787, 792, 795 Selection statements, 81–89 Selectors, 633 Semaphore, 861, 862, 863–869 and setting initial synchronization state, 869 Semaphore class, 862, 863–864 send( ), 683 Separable Model architecture, 947 Separators, 33 SequenceInputStream class, 291, 603–605 Serializable interface, 622–623, 907 Serialization, 622–628 example program, 626–628 and Java Beans, 937 and static variables, 623 and transient variables, 623, 627 Server, 667 ServerSocket class, 632, 671, 681–682 service( ), 994, 997, 999 ServiceLoader class, 577 Servlet interface, 998, 999 methods, table of, 999 Index Servlet(s), 10, 16, 993–1016, 1019 advantages of, 994 API, 998 development options, 994–1000 example program for a simple, 996–998 financial calculation example, 1048–1052 life cycle of, 994 parameters, reading, 1002–1004 and portability, 10 and security, 994 and session tracking, 1015–1016 using Tomcat to develop, 994, 995–996 ServletConfig interface, 998, 1000 ServletContext interface, 998, 1000 methods, table of various, 1000 ServletException class, 999, 1002 ServletInputStream class, 999, 1002 ServletOutputStream class, 999, 1002 ServletRequest interface, 997, 998, 1000, 1002 methods, table of various, 1001 ServletResponse interface, 997, 998, 1000 methods, table of various, 1001 Session tracking, HTTP, 1015–1016 Set interface, 456, 459–460, 470, 475, 484, 486 Set-view, obtaining, 483, 488–489, 518 set( ), 459, 460, 470, 477, 528, 532–533, 892 setActionCommand( ), 779, 812, 969, 975 setAddress( ), 684 setAlignment( ), 775 setAttribute( ), 1000, 1007, 1015 setBackground( ), 694, 755 setBlockIncrement( ), 790 setBorder( ), 963–964 setBounds( ), 739, 796 setChanged( ), 541 setCharAt( ), 390–391 setColor( ), 756 setConstraints( ), 806 setContentType( ), 997, 1001 setData( ), 684 setDefault( ), 536, 538 setDefaultCloseOperation( ), 952 setDisabledIcon( ), 969 setEchoChar( ), 793 setEditable( ), 793, 795, 1025 setEnabled( ), 811 setFollowRedirects( ), 679 setFont( ), 762 setForeground( ), 694, 755 setIcon( ), 966 SetIntField( ), 315 setLabel( ), 776, 780, 811 setLastModified( ), 585 setLayout( ), 796, 953 setLength( ), 390, 610, 684 setLocation( ), 739 setMaxAge( ), 1008, 1015 setMultipleMode( ), 822 setName( ), 232, 439 setPaintMode( ), 758 setPort( ), 684 setPreferredSize( ), 739, 790 setPressedIcon( ), 969 setPriority( ), 240, 439 setReadOnly( ), 585 setRequestMethod( ), 680 setRolloverIcon( ), 969 setSelectedCheckbox( ), 782 setSelectedIcon( ), 969 setSelectionMode( ), 982 setSize( ), 510, 739, 740, 741, 952 setSoTimeout( ), 683 setStackTrace( ), 222 setState( ), 780, 811 setStream( ), 705 setText( ), 775, 792, 795, 966, 969, 1025 setTitle( ), 740 setUnitIncrement( ), 790 setValue( ), 487, 789, 1008 setValues( ), 789 setVisible( ), 740, 741, 953 setXORMode( ), 757, 758–759 Sheridan, Mike, 6 Shift operators, bitwise, 66, 69–73 Short class, 269, 403, 410 methods defined by, table of, 405 short data type, 35, 36, 37, 41 ShortBuffer class, 631 shortValue( ), 269, 398, 400, 402, 404, 405, 407, 409 show( ), 803 showDocument( ), 704–706 showStatus( ), 690, 699, 706 shuffle( ), 498, 500 shutdown( ), 882, 885, 897, 908 shutdownNow( ), 908 Sign extension, 71 signal( ), 890 signum( ), 407, 409, 435 SimpleBeanInfo class, 937, 943 SimpleDateFormat class, 539, 929–930 formatting string symbols, table of, 929 SimpleFileVisitor class, 658, 659 SimpleTimeZone class, 537–538 sin( ), 38, 433 SingleSelectionModel, 977 sinh( ), 433 SIZE, 399, 403 size( ), 457, 458, 471, 483, 511, 515–516, 517, 528, 637, 638 skip( ), 573, 591, 592–594, 600, 611, 625 1111 1112 Index sleep( ), 230, 232, 237, 238, 439, 888 slice( ), 631 Slider box, 789 Socket class, 632, 671–675, 680, 682 Socket(s) overview, 667 TCP/IP client, 671–675 TCP/IP server, 671, 681–682 SocketAddress class, 683 SocketChannel class, 632, 633 SocketException, 683 sort( ), 498, 503–504 SortedMap interface, 482, 484 methods, table of, 484 SortedSet interface, 456, 460–461 methods, table of, 461 Source code file, naming a, 23–24 SOURCE retention policy, 276–277 split( ), 388, 918 sqrt( ), 38, 45, 319–321, 434 Stack definition of, 21, 126–127 ways to implement a, 201 Stack class, 454, 466, 508, 513–515 methods, table of, 513 Stack frame, 446 Stack trace, 209–210, 216, 446 StackTraceElement class, 222, 446 methods, table of, 446 StandardOpenOption class, 635, 650, 651 enumeration, table of values for the, 637 StandardOpenOption.CREATE, 637, 645, 651 StandardOpenOption.READ, 637, 648, 650 StandardOpenOption.TRUNCATE_EXISTING, 637, 651 StandardOpenOption.WRITE, 637, 645, 651 Standard Template Library (STL), 455 start( ), 230, 233, 234, 235, 416, 422, 439, 690, 691, 692, 693, 695, 698, 741, 912, 915, 957, 959 startsWith( ), 380, 635 State enumeration, 256 Statements, 26 null, 90 Statements, control, 28 iteration, 81, 89–102 jump, 81, 102–108 selection, 81–89 static, 25, 145–146, 149, 313, 319, 320–321 member restrictions, 366 Static import, 14, 319–321 stop( ), 13, 253–254, 437, 690, 691, 692, 693, 695, 698, 706, 741, 957 store( ), 519, 520, 522–524 Stream(s) benefits, 628 buffered, 599–603 classes, byte, 290–291, 590–610 classes, character, 290, 291–292, 590, 610–620 closing, 589–590 definition of, 290, 581 filtered, 599, 628 and NIO, 649–651 predefined, 292–293 strictfp, 312 StrictMath class, 436 String class, 25, 58, 152–154, 371, 448, 563 constructors, 372–374 String(s) arrays of, 154 changing case of characters in, 387, 412, 413 comparison, 153, 378–382 concatenating, 152–153, 374–376, 385, 391–392 constants, 152 converting data into a, 376, 386–387 creating, 152, 372–374 extracting characters from, 377–378 formatted, creating a, 549–551 formatting a, 551, 556 immutable, 371, 389 length, obtaining, 153, 374 literals, 43–44, 374 modifying, 384–386 numbers to and from, converting, 410–411 as objects, 44, 58, 152, 371 parsing a formatted input, 525 reading, 294–296 searching, 382–383 StringBuffer class, 152, 371, 373, 384, 389–395, 448 StringBufferInputStream deprecated class, 582 StringBuilder class, 371, 373, 384, 395, 448, 549 and synchronization, 395 StringIndexOutOfBounds exception, 220 StringReader class, 292 StringTokenizer class, 525–526 methods, table of, 526 stringWidth( ), 766 StringWriter class, 292 Stroustrup, Bjarne, 6 Stubs (RMI), 925–926 Subclass, 20, 161, 162, 163, 179 subList( ), 459, 460 subMap( ), 484, 486 submit( ), 885 subSequence( ), 389, 395, 448 subSet( ), 461, 462, 473 substring( ), 384, 394 Sun Microsystems, 6, 14 super, 145 and bounded wildcard arguments, 344 and methods or instance variables, 170–171, 176 Index super( ), 323 and superclass constructors, 167–170, 174 Superclass, 20, 161, 162, 163, 179, 187 abstract, 181–184 Supplemental character, definition of, 414 @SuppressWarnings built-in annotation, 286, 288 suspend( ), 13, 251–254, 437, 443 Swing, 13, 289, 307, 309, 687, 735, 945–964, 965–992 applet, example of a simple, 957–959 application, example of a simple, 950–954 and the AWT, 735, 945, 946 component classes, list of, 948–949 components. See Components, Swing download manger using, 1053–1077 event handling, 953–957 history of, 945–946 and MVC architecture, 947 packages, list of, 950 and painting, 957, 959–964 threading issues, 953–954, 957 Swing: A Beginner’s Guide (Schildt), 945 SwingConstants interface, 966 SwingUtilities class, 954 switch statement, 84–89 and auto-unboxing, 273 using enumeration constants to control a, 85, 260–261 using a String to control a, 15, 84–85, 87–88 versus the if statement, 88–89 Synchronization, 12, 229–230, 241–245 and atomic operations, 892–893 and collections, 465, 499, 508 and deadlock, 249–251, 253 and interprocess communication, 245–251 objects, using, 863–882 race condition and, 243 and StringBuilder class, 395 via synchronized block, 243–245, 499 via synchronized method, 230, 241–243 versus concurrency utilities, traditional, 861, 908 synchronized modifier, 241, 908 used with method, 241–243 used with object, 243–245 synchronizedList( ), 498, 499 synchronizedSet( ), 498, 499 Synchronizers, 862 SynchronousQueue class, 889 System class, 26, 34, 292, 423–426 methods, table of, 423–424 System.console( ), 423, 620 System.err standard error stream, 292, 293 System.getProperties( ), 424, 519 System.getProperty( ), 424, 426 System.in standard input stream, 292, 293, 564, 620 System.in.read( ), 93 1113 System.nanoTime( ), 900 System.out standard output stream, 26, 292, 293, 296, 297, 562, 605, 606, 620 and static import, 321 T Tabbed panes, 977–979 TableColumnModel, 990 TableModel, 990 TableModelEvent class, 990 tailMap( ), 484, 486 tailSet( ), 461, 462 tan( ), 433 tanh( ), 433 @Target built-in annotation, 286–287 TCP/IP, 12, 668 client sockets, 671–675 disadvantages of, 682 server sockets, 671, 681–682 See also Transmission Control Protocol (TCP) TERMINATED, 256 Ternary if-then-else operator (?:), 75, 77–78 Text area, 794–796 Text components as an event source, 720 Text fields, 792–794, 1025 Swing, 967–968 Text formatting using java.text classes, 909, 927–930 Text output using font metrics, managing, 764–772 TextArea class, 737, 794–795 textChanged( ), 722 TextComponent class, 737, 792, 795 TextEvent class, 710, 718, 795 TextField class, 737, 792 TextListener interface, 720, 722 this, 124–125, 145 this( ), 321–323 Thompson, Ken, 4 Thread class, 13, 230–231, 232, 437–439, 544 constructors, 233, 236, 437 extending, 235–236 methods, table of, 438–439 Thread(s) creating, 232–237 daemon, 897, 905 and deadlock, 249–251, 253, 437 definition of, 227 executors to manage, using, 863, 701–887 group, 232, 439–443 main, 231–232, 234–235, 236, 237, 238 messaging, 230, 245–250 pool, 883–885, 894 priorities, 229, 240–241, 437 resuming, 251–256, 441–443 states of, possible, 229, 256–257 1114 Index Thread(s) (continued) stopping, 253–256 suspending, 230, 232, 251–256, 441–443 and Swing, event dispatching, 953–954, 957, 959 synchronization. See Synchronization Thread.UncaughtExceptionHandler interface, 450 ThreadGroup class, 439–443, 450 methods, table of, 440–441 ThreadLocal class, 444 ThreadPoolExecutor class, 863, 883 throw, 207, 216–217 Throwable class, 208–209, 212, 216, 217, 221, 224, 306, 367, 445 methods defined by, table of, 222 obtaining object of the, 216–217 throws, 207, 217–218, 220 Thumb, 789 time, 531 Time and date formatting, 552–554, 927–930 java.util classes that deal with, 530–539 timedJoin( ), 889 timedWait( ), 889 TIMED_WAITING, 256 Timer class, 544–546 methods, table of, 545–546 TimerTask class, 544–546 methods, table of, 545 Timestamp, event, 711 TimeUnit enumeration, 863, 869, 886, 888–889 TimeZone class, 536–537 methods defined by, table of some, 536–537 to( ), 421 toAbsolutePath( ), 635, 652 toArray( ), 458, 459, 468–469 toBinaryString( ), 407, 409, 411 toCharArray( ), 378 toDays( ), 888 toDegrees( ), 436 toFile( ), 635 toHexString( ), 400, 402, 407, 409, 411 toHours( ), 888 Tokens, 525, 564 toLanguageTag( ), 539 toLowerCase( ), 387, 413, 414 Tomcat, 994, 995–996 toMicros( ), 888 toMillis( ), 888 toMinutes( ), 888 toNanos( ), 888 toOctalString( ), 407, 409, 411 TooManyListenersException, 936 toPath( ), 585, 635, 652 toRadians( ), 436 toSeconds( ), 888 toString( ), 185, 186, 212, 221, 222, 270, 276, 282, 297, 376–377, 386, 400, 402, 404, 405, 16:11, 409, 410, 416, 427, 431, 439, 441, 445, 446, 447, 448, 467, 504, 511, 517, 528, 529, 530, 531, 548, 549, 550, 606, 620, 634, 635, 671, 710, 760, 766, 907, 919, 987 totalMemory( ), 418–419 toUpperCase( ), 387, 413 transient modifier, 309, 937 translatePoint( ), 717 Transmission Control Protocol (TCP) definition of, 667 and stream-based I/O, 668 See also TCP/IP TreeExpansionEvent class, 987 TreeExpansionListener interface, 987 TreeMap class, 487, 492, 524 TreeModel, 987 TreeModelEvent class, 987 TreeModelListener interface, 987 TreeNode interface, 987 TreePath class, 987 Trees, Swing, 986–989 TreeSelectionEvent class, 987 TreeSelectionListener interface, 987, 988 TreeSelectionModel, 987 TreeSet class, 465, 471, 472–473, 492, 524 trim( ), 385–386 trimToSize( ), 395, 467, 511 true, 34, 40, 41, 43, 75, 76 TRUE, 414 True and false in Java, 43, 75 Truncation, 49 try block(s), 207, 208, 210–216, 218–219 nested, 214–216 try-with-resources statement, 15, 208, 225, 298, 303–306, 449–450, 562, 569, 588, 589–590, 596, 632, 634, 641, 672, 674, 683 advantages to using, 590 tryLock( ), 863, 890 tryUnfork( ), 907 Two’s complement, 66–67 TYPE, 399, 403, 411, 414, 416 Type argument(s), 328, 330, 334 and bounded types, 335–337 and generic class hierarchies, 352 and type inference, 359–361 Type conversion automatic, 35, 48, 130–131 narrowing, 48 widening, 48 Index Type enumeration, 421 Type interface, 451 Type parameter(s) and bounded types, 334–336, 348–349 cannot create an instance of a, 365–366 and class hierarchies, 353–355 and erasure, 361–362, 366 and primitive types, 330 and static members, 366 and type safety, 330 used with a class, 327, 332, 334 used with a method, 328, 345, 346 Type safety and arrays, 367 and collections, 455, 499, 505 and generic methods, 346 and generics, 325, 326, 329, 330–332, 455, 505, 506 and raw types, 349–352 and wildcard arguments, 337–339, 341 Typesafe view of a collection, obtaining a dynamically, 499 Type(s), 27 bounded, 335, 336 casting, 48–49, 50 checking, 10, 11, 35, 329, 330–332, 351, 367 class as a data, 109, 111, 113, 114, 126 inference, 359–361 parameterized, 326, 328 promotion, 37, 49–51, 69–70 raw. See Raw types simple, 35 Types, primitive, 35–36, 114, 136, 268–269, 270, 275, 330 and collections, 456, 469 to a string representation, converting, 375, 376, 386 to or from a sequence of bytes, converting, 607, 608 wrappers for, 268–270, 275, 330, 398–416 TypeNotPresentException, 220 U UDP protocol, 667, 668, 682 UI delegate, 947, 948 ulp( ), 434, 435 UnavailableException class, 999, 1002 Unboxing, 270 uncaughtException( ), 450 UncaughtExceptionHandler interface, 450 Unchecked warnings and raw types, 351 UnicastRemoteObject, 924 1115 Unicode, 39, 40, 43, 290, 291, 373, 378, 388, 394, 395, 414, 610 code points, table of some Character methods providing support for, 415 support for 32–bit, 414 Unicode Technical Standard (UTS) 35, 539 Uniform Resource Identifier (URI), 681 Uniform Resource Locator (URL). See URL (Uniform Resource Locator) UNIX, 4, 667 UnknownHostException, 669, 670 unlock( ), 863, 890 unmodifiable... collections methods, 499 Unreachable code, 213 unread( ), 602, 618 UnsupportedOperationException, 220, 457, 459, 477, 483, 499, 639 update( ), 541, 542, 693, 694, 695, 696, 749 overriding, 693 URI (Uniform Resource Identifier), 681 URI class, 681 URL (Uniform Resource Locator), 675, 681, 993 specification format, 675 URL class, 675–676, 678, 679, 704 URLConnection class, 676–679 useDelimiter( ), 570–572 User Datagram Protocol (UDP), 667, 668, 682 useRadix( ), 573 UTS 35, 539 UUID class, 577 V value (annotation member name), 285, 286 VALUE (PARAM NAME), 701 valueBound( ), 1006 valueChanged( ), 982, 983, 987, 988 valueOf( ), 262–263, 376, 386–387, 400, 402, 404, 405, 407, 409, 416, 447, 528 values( ), 262–263, 483, 484 valueUnbound( ), 1006 van Hoff, Arthur, 6 Varargs, 14, 155–160 and ambiguity, 159–160 methods, overloading, 158–159 and Java’s printf( ), 155 parameter, 157, 475 Variable(s), 44–47 declaration, 27, 29, 44–45, 46 definition of, 26, 44 dynamic initialization of, 45 enumeration, 260 final, 147 1116 Index Variable(s) (continued) instance. See Instance variables interface, 197, 204–205 object reference. See Object reference variables scope and lifetime of, 45–47 Vector class, 454, 466, 482, 508, 509–513 legacy methods, table of, 510–511 VetoableChangeListener interface, 937, 938 Viewport, scroll pane, 980 visitFile( ), 658, 659 void, 25, 116 Void class, 416 volatile modifier, 310 VSPACE, 700, 701 W wait( ), 185, 186, 245, 247–249, 254–255, 427, 890, 908 waitFor( ), 417, 420 WAITING, 256 WALL_TIME, 538 walkFileTree( ), 657–659 Warth, Chris, 6 Watchable interface, 634 WeakHashMap class, 487 Web browser executing applet in, 308, 309, 687, 691, 699, 739 using status window of, 699 Web server and servlets, 993, 994 weightx constraint field, 807, 808 while loop, 89–90 Whitespace, 32, 82 from a string, removing, 385–386 whois, 668, 672, 674, 675 WIDTH, 700 Wildcard arguments, 337–344 bounded, 339–344 used in creating an array, 367 Window displaying information within a, 749 as an event source, 720 frame. See Frame window fundamentals, 738–739 and graphics, 749 status, using, 699 Window class, 719, 737, 739, 816 windowActivated( ), 723 WindowAdapter class, 730 windowClosed( ), 723 windowClosing( ), 723, 740, 741 WindowConstants interface, 952 windowDeactivated( ), 723 windowDeiconified( ), 723 WindowEvent class, 710, 712, 718–719 WindowFocusListener interface, 720, 723 windowGainedFocus( ), 723 windowIconified( ), 723 WindowListener interface, 720, 723, 730, 740, 741 windowLostFocus( ), 723 windowOpened( ), 723 Work stealing, 897, 907 World Wide Web (WWW), 6, 7, 11, 675 wrap( ), 631 Wrappers, primitive type, 268–270, 275, 330, 398–416 WRITE, 421 write( ), 292, 296, 302, 592, 611, 623, 624, 633, 646, 647, 651, 663, 664 writeBoolean( ), 608, 624 writeDouble( ), 608, 624 Writer class, 291, 292, 590, 610, 613, 628 methods defined by, table of, 611–612 writeExternal( ), 623 writeInt( ), 608, 624 writeObject( ), 623 writeTo( ), 599 X XOR (exclusive OR) operator (^) bitwise, 66, 67, 68–69 Boolean logical, 75–76 Y Yellin, Frank, 6 Z Zero crossing, 67 ZIP file format, 579

Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.6
Linearized                      : No
Universal                       : PDF
Author                          : Herbert Schildt
Code Mantra                     : LLC
Www Codemantra Com              : )
Modify Date                     : 2011:06:23 15:21:44+05:30
Create Date                     : 2011:06:19 10:16:09+05:30
EBX PUBLISHER                   : McGraw-Hill Education
Page Layout                     : SinglePage
Page Mode                       : UseOutlines
Has XFA                         : No
Page Count                      : 1153
XMP Toolkit                     : XMP toolkit 2.9.1-13, framework 1.6
About                           : uuid:bdff6b9d-7b56-44b9-b7fa-6a44bed37015
Producer                        : Acrobat Distiller 7.0.5 for Macintosh
Trapped                         : False
Code Mantra 002 C0020 LLC       : http://www.codemantra.com
Universal 0020 PDF              : The process that creates this PDF constitutes a trade secret of codeMantra, LLC and is protected by the copyright laws of the United States
Metadata Date                   : 2011:06:23 15:21:44+05:30
Creator Tool                    : Adobe InDesign CS5 (7.0)
Document ID                     : uuid:65c51333-c3f8-496d-b6bb-d17594ac5db2
Instance ID                     : uuid:81168ca3-4d74-4940-9efe-1dfbbc26f375
Format                          : application/pdf
Title                           : Java
Creator                         : Herbert Schildt
EXIF Metadata provided by
EXIF.tools

Navigation menu