Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

Java: The Complete Reference Herbert

Schildt
Visit to download the full and correct content document:
https://ebookmeta.com/product/java-the-complete-reference-herbert-schildt/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Java: The Complete Reference (Complete Reference


Series) 13th Edition Schildt

https://ebookmeta.com/product/java-the-complete-reference-
complete-reference-series-13th-edition-schildt/

Java: The Complete Reference, Twelfth Edition Schildt

https://ebookmeta.com/product/java-the-complete-reference-
twelfth-edition-schildt/

Java: A Beginner's Guide: Create, Compile, and Run Java


Programs Today Herbert Schildt

https://ebookmeta.com/product/java-a-beginners-guide-create-
compile-and-run-java-programs-today-herbert-schildt/

Primary Mathematics 3A Hoerst

https://ebookmeta.com/product/primary-mathematics-3a-hoerst/
Java Complete book James Gary

https://ebookmeta.com/product/java-complete-book-james-gary/

Java 17 Quick Syntax Reference 3rd Edition Mikael


Olsson

https://ebookmeta.com/product/java-17-quick-syntax-reference-3rd-
edition-mikael-olsson/

Java 17 Quick Syntax Reference: A Pocket Guide to the


Java SE Language, APIs, and Library Mikael Olsson

https://ebookmeta.com/product/java-17-quick-syntax-reference-a-
pocket-guide-to-the-java-se-language-apis-and-library-mikael-
olsson/

Information Security The Complete Reference Second


Edition Rhodes Ousley Mark

https://ebookmeta.com/product/information-security-the-complete-
reference-second-edition-rhodes-ousley-mark/

Java in a Nutshell: A Desktop Quick Reference Benjamin


Evans

https://ebookmeta.com/product/java-in-a-nutshell-a-desktop-quick-
reference-benjamin-evans/
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: i

The
Complete Java™
Reference Twelfth Edition

00-FM.indd 1 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: ii

About the Author


Best-selling author Herbert Schildt has written extensively about programming
for over three decades and is a leading authority on the Java language. Called “one
of the world’s foremost authors of books about programming” by International
Developer magazine, his 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, Introducing JavaFX 8 Programming, and Swing:
A Beginner’s Guide. He has also written extensively about C, C++, and C#.
Featured as one of the rock star programmers in Ed Burns’ book Secrets of
the Rock Star Programmers: Riding the IT Crest, Schildt is interested in all
facets of computing, but his primary focus is computer languages. Schildt
holds both BA and MCS degrees from the University of Illinois. His website is
www.HerbSchildt.com.

About the Technical Editor


Dr. Danny Coward has worked on all editions of the Java platform. He led the
definition of Java Servlets into the first version of the Java EE platform and
beyond, web services into the Java ME platform, and the strategy and planning
for Java SE 7. He founded JavaFX technology and, most recently, designed the
largest addition to the Java EE 7 standard, the Java WebSocket API. From
coding in Java, to designing APIs with industry experts, to serving for several
years as an executive to the Java Community Process, he has a uniquely broad
perspective into multiple aspects of Java technology. In addition, he is the
author of two books on Java programming: Java WebSocket Programming and
Java EE 7: The Big Picture. Most recently, he has been applying his knowledge
of Java to helping scale massive Java-based services for one of the world’s most
successful software companies. Dr. Coward holds a bachelor’s, master’s, and
doctorate in mathematics from the University of Oxford.

00-FM.indd 2 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: iii

The
Complete Java™
Reference Twelfth Edition

Herbert Schildt

New York Chicago San Francisco


Athens London Madrid Mexico City
Milan New Delhi Singapore Sydney Toronto

00-FM.indd 3 22/09/21 6:45 PM


Copyright © 2022 by McGraw Hill. 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-1-26-046342-2
MHID: 1-26-046342-7

The material in this eBook also appears in the print version of this title: ISBN: 978-1-26-046341-5,
MHID: 1-26-046341-9.

eBook conversion by codeMantra


Version 1.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 Education 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 visit the Contact Us page at www.mhprofessional.com.

Information has been obtained by McGraw Hill from sources believed to be reliable. However, because of the possibility of
human or mechanical error by our sources, McGraw Hill, or others, McGraw Hill does not guarantee the accuracy, adequacy,
or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of
such information.

Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any in-
formation contained in this Work, and is not responsible for any errors or omissions.

TERMS OF USE

This is a copyrighted work and McGraw-Hill Education 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 Education’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 EDUCATION 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, IN-
CLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICU-
LAR PURPOSE. McGraw-Hill Education 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 Education 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 Education has no responsibility for the content of any information accessed through
the work. Under no circumstances shall McGraw-Hill Education 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.
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents at a Glance
PART I The Java Language
1 The History and Evolution of Java 3
2 An Overview of Java 21
3 Data Types, Variables, and Arrays 39
4 Operators 67
5 Control Statements 87
6 Introducing Classes 117
7 A Closer Look at Methods and Classes 137
8 Inheritance 171
9 Packages and Interfaces 199
10 Exception Handling 227
11 Multithreaded Programming 247
12 Enumerations, Autoboxing, and Annotations 277
13 I/O, Try-with-Resources, and Other Topics 315
14 Generics 347
15 Lambda Expressions 391
16 Modules 421
17 Switch Expressions, Records,
  and Other Recently Added Features 449
PART II The Java Library
18 String Handling 483
19 Exploring java.lang 511
20 java.util Part 1: The Collections Framework 571
21 java.util Part 2: More Utility Classes 653
22 Input/Output: Exploring java.io 713
23 Exploring NIO 763
24 Networking 795
25 Event Handling 819
26 Introducing the AWT: Working with
  Windows, Graphics, and Text 851
27 Using AWT Controls, Layout Managers, and Menus 879
28 Images 929
29 The Concurrency Utilities 955
30 The Stream API 1005
31 Regular Expressions and Other Packages 1031

00-FM.indd 5 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

vi  Java: The Complete Reference

PART III Introducing GUI Programming with Swing


32 Introducing Swing 1061
33 Exploring Swing 1079
34 Introducing Swing Menus 1109
PART IV Applying Java
35 Java Beans 1145
36 Introducing Servlets 1157
PART V Appendixes
A Using Java’s Documentation Comments 1183
B Introducing JShell 1191
C Compile and Run Simple Single-File Programs in
  One Step 1201

Index
1203

00-FM.indd 6 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi

Part I The Java Language


Chapter 1 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 Impacted the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Moving Beyond Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
A Faster Release Schedule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Servlets: Java on the Server Side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Multithreaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Architecture-Neutral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Interpreted and High Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
A Culture of Innovation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Chapter 2 An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Two Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The Three OOP Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

vii

00-FM.indd 7 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

viii  Java: The Complete Reference

A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27


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

00-FM.indd 8 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   ix

Automatic Type Promotion in Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54


The Type Promotion Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Introducing Type Inference with Local Variables . . . . . . . . . . . . . . . . . . . . . . 62
Some var Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
A Few Words About Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Chapter 4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
The Basic Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
The Modulus Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Arithmetic Compound Assignment Operators . . . . . . . . . . . . . . . . . . . 69
Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
The Bitwise Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
The Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The Unsigned Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Bitwise Operator Compound Assignments . . . . . . . . . . . . . . . . . . . . . . . 79
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Boolean Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Chapter 5 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Java’s Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Traditional switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The For-Each Version of the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Local Variable Type Inference in a for Loop . . . . . . . . . . . . . . . . . . . . . 108
Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Using break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Using continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

00-FM.indd 9 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

x  Java: The Complete Reference

Chapter 6 Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117


Class Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The General Form of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
A Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Declaring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
A Closer Look at new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Assigning Object Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Introducing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Adding a Method to the Box Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Adding a Method That Takes Parameters . . . . . . . . . . . . . . . . . . . . . . . 127
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Instance Variable Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
A Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Chapter 7 A Closer Look at Methods and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Overloading Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Using Objects as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
A Closer Look at Argument Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Introducing Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Understanding static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Introducing final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Arrays Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Introducing Nested and Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Exploring the String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Using Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Varargs: Variable-Length Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Overloading Vararg Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Varargs and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Local Variable Type Inference with Reference Types . . . . . . . . . . . . . . . . . . 168
Chapter 8 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Inheritance Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Member Access and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
A More Practical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
A Superclass Variable Can Reference a Subclass Object . . . . . . . . . . . 176
Using super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Using super to Call Superclass Constructors . . . . . . . . . . . . . . . . . . . . . 177
A Second Use for super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

00-FM.indd 10 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xi

Creating a Multilevel Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181


When Constructors Are Executed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Dynamic Method Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Why Overridden Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Applying Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Using final with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Using final to Prevent Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Using final to Prevent Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Local Variable Type Inference and Inheritance . . . . . . . . . . . . . . . . . . . . . . . 195
The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Chapter 9 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Defining a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Finding Packages and CLASSPATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
A Short Package Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Packages and Member Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
An Access Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Importing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Defining an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Nested Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Applying Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Variables in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Interfaces Can Be Extended . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Default Interface Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Default Method Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
A More Practical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Multiple Inheritance Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Use static Methods in an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Private Interface Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Final Thoughts on Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Chapter 10 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Exception-Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Exception Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Uncaught Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Using try and catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Displaying a Description of an Exception . . . . . . . . . . . . . . . . . . . . . . . 232
Multiple catch Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Nested try Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

00-FM.indd 11 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xii  Java: The Complete Reference

finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Java’s Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Creating Your Own Exception Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Chained Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Three Additional Exception Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Chapter 11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
The Java Thread Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
The Thread Class and the Runnable Interface . . . . . . . . . . . . . . . . . . . . 250
The Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Implementing Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Extending Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Choosing an Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Creating Multiple Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Using isAlive( ) and join( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Using Synchronized Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
The synchronized Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Interthread Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Suspending, Resuming, and Stopping Threads . . . . . . . . . . . . . . . . . . . . . . . . 272
Obtaining a Thread’s State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Using a Factory Method to Create and Start a Thread . . . . . . . . . . . . . . . . . 275
Using Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Chapter 12 Enumerations, Autoboxing, and Annotations . . . . . . . . . . . . . . . . . . . . . 277
Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Enumeration Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
The values( ) and valueOf( ) Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Java Enumerations Are Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Enumerations Inherit Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Another Enumeration Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
The Numeric Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Autoboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Autoboxing and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Autoboxing/Unboxing Occurs in Expressions . . . . . . . . . . . . . . . . . . . 291
Autoboxing/Unboxing Boolean and Character Values . . . . . . . . . . . . 292

00-FM.indd 12 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xiii

Autoboxing/Unboxing Helps Prevent Errors . . . . . . . . . . . . . . . . . . . . 293


A Word of Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Annotation Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Specifying a Retention Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Obtaining Annotations at Run Time by Use of Reflection . . . . . . . . . 296
The AnnotatedElement Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Using Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Marker Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Single-Member Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
The Built-In Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Type Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Repeating Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Some Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Chapter 13 I/O, Try-with-Resources, and Other Topics . . . . . . . . . . . . . . . . . . . . . . . 315
I/O Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Byte Streams and Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
The Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Reading Console Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Reading Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Writing Console Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
The PrintWriter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Automatically Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
The transient and volatile Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Introducing instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
strictfp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Native Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Using assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Assertion Enabling and Disabling Options . . . . . . . . . . . . . . . . . . . . . . 339
Static Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Invoking Overloaded Constructors Through this( ) . . . . . . . . . . . . . . . . . . . 342
A Word About Value-Based Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Chapter 14 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
What Are Generics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
A Simple Generics Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Generics Work Only with Reference Types . . . . . . . . . . . . . . . . . . . . . . 352
Generic Types Differ Based on Their Type Arguments . . . . . . . . . . . . 352
How Generics Improve Type Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
A Generic Class with Two Type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 355
The General Form of a Generic Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Bounded Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

00-FM.indd 13 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xiv  Java: The Complete Reference

Using Wildcard Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359


Bounded Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Creating a Generic Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Generic Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Generic Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Raw Types and Legacy Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Generic Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Using a Generic Superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
A Generic Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Run-Time Type Comparisons Within a Generic Hierarchy . . . . . . . . 378
Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Overriding Methods in a Generic Class . . . . . . . . . . . . . . . . . . . . . . . . . 380
Type Inference with Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Local Variable Type Inference and Generics . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Erasure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Bridge Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Ambiguity Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Some Generic Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Type Parameters Can’t Be Instantiated . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Restrictions on Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Generic Array Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Generic Exception Restriction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Chapter 15 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Introducing Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Lambda Expression Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Functional Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Some Lambda Expression Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Block Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Generic Functional Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Passing Lambda Expressions as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Lambda Expressions and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Lambda Expressions and Variable Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Method References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Method References to static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Method References to Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . 407
Method References with Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Constructor References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Predefined Functional Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Chapter 16 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Module Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
A Simple Module Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Compile and Run the First Module Example . . . . . . . . . . . . . . . . . . . . . 426
A Closer Look at requires and exports . . . . . . . . . . . . . . . . . . . . . . . . . . 428

00-FM.indd 14 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xv

java.base and the Platform Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429


Legacy Code and the Unnamed Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Exporting to a Specific Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Using requires transitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Use Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Service and Service Provider Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
The Service-Based Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
A Module-Based Service Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Module Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Three Specialized Module Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Open Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
The opens Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
requires static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Introducing jlink and Module JAR Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Linking Files in an Exploded Directory . . . . . . . . . . . . . . . . . . . . . . . . . 446
Linking Modular JAR Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
JMOD Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
A Brief Word About Layers and Automatic Modules . . . . . . . . . . . . . . . . . . 448
Final Thoughts on Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Chapter 17 Switch Expressions, Records, and Other Recently Added Features . . . . 449
Enhancements to switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Use a List of case Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Introducing the switch Expression and the yield Statement . . . . . . . . 452
Introducing the Arrow in a case Statement . . . . . . . . . . . . . . . . . . . . . . 454
A Closer Look at the Arrow case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Another switch Expression Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Text Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Text Block Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Understanding Leading Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Use Double Quotes in a Text Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Escape Sequences in Text Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Record Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Create Record Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Another Record Constructor Example . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Create Record Getter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Pattern Matching with instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Pattern Variables in a Logical AND Expression . . . . . . . . . . . . . . . . . . 474
Pattern Matching in Other Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Sealed Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Sealed Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Sealed Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Future Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480

00-FM.indd 15 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xvi  Java: The Complete Reference

Part II The Java Library


Chapter 18 String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
The String Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Special String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
String Concatenation with Other Data Types . . . . . . . . . . . . . . . . . . . . 487
String Conversion and toString( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Character Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
charAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
getChars( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
getBytes( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
toCharArray( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
String Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
equals( ) and equalsIgnoreCase( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
regionMatches( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
startsWith( ) and endsWith( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
equals( ) Versus == . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
compareTo( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Searching Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Modifying a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
substring( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
concat( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
trim( ) and strip( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Data Conversion Using valueOf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Changing the Case of Characters Within a String . . . . . . . . . . . . . . . . . . . . . 499
Joining Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Additional String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
StringBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
StringBuffer Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
length( ) and capacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
ensureCapacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
setLength( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
charAt( ) and setCharAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
getChars( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
append( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
insert( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
reverse( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
delete( ) and deleteCharAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
substring( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Additional StringBuffer Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509

00-FM.indd 16 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xvii

Chapter 19 Exploring java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511


Primitive Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Double and Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Understanding isInfinite( ) and isNaN( ) . . . . . . . . . . . . . . . . . . . . . . . . 516
Byte, Short, Integer, and Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Additions to Character for Unicode Code Point Support . . . . . . . . . . 528
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Executing Other Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Runtime.Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
ProcessBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Using currentTimeMillis( ) to Time Program Execution . . . . . . . . . . 540
Using arraycopy( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Environment Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
System.Logger and System.LoggerFinder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Using clone( ) and the Cloneable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
ClassLoader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Exponential Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Rounding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Miscellaneous Math Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
StrictMath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Thread, ThreadGroup, and Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
The Runnable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
ThreadGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
ThreadLocal and InheritableThreadLocal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
ModuleLayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
RuntimePermission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Throwable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
SecurityManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
StackTraceElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
StackWalker and StackWalker.StackFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565

00-FM.indd 17 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xviii  Java: The Complete Reference

ClassValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
The CharSequence Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
The Comparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
The Appendable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
The Iterable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
The Readable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
The AutoCloseable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
The Thread.UncaughtExceptionHandler Interface . . . . . . . . . . . . . . . . . . . . 567
The java.lang Subpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
java.lang.annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
java.lang.constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
java.lang.instrument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
java.lang.invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
java.lang.management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
java.lang.module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
java.lang.ref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
java.lang.reflect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Chapter 20 java.util Part 1: The Collections Framework . . . . . . . . . . . . . . . . . . . . . . . 571
Collections Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
The Collection Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
The Collection Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
The List Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
The Set Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
The SortedSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
The NavigableSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
The Queue Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
The Deque Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
The Collection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
The LinkedList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
The HashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
The LinkedHashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
The TreeSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
The PriorityQueue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
The ArrayDeque Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
The EnumSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Accessing a Collection via an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Using an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
The For-Each Alternative to Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Spliterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Storing User-Defined Classes in Collections . . . . . . . . . . . . . . . . . . . . . . . . . . 602
The RandomAccess Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Working with Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
The Map Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
The Map Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612

00-FM.indd 18 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xix

Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Using a Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
The Collection Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
The Legacy Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
The Enumeration Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Hashtable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Using store( ) and load( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Parting Thoughts on Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Chapter 21 java.util Part 2: More Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
StringTokenizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
BitSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Optional, OptionalDouble, OptionalInt, and OptionalLong . . . . . . . . . . . . 658
Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
GregorianCalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
TimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
SimpleTimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Timer and TimerTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Currency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
The Formatter Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
The Formatter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Formatting Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Formatting Strings and Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
Formatting Time and Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
The %n and %% Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Specifying a Minimum Field Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Specifying Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
Using the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
The Space, +, 0, and ( Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
The Comma Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
The # Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
The Uppercase Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
Using an Argument Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Closing a Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
The Java printf( ) Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691

00-FM.indd 19 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xx  Java: The Complete Reference

Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
The Scanner Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Scanning Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Some Scanner Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Setting Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
Other Scanner Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
The ResourceBundle, ListResourceBundle,
and PropertyResourceBundle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Miscellaneous Utility Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 706
The java.util Subpackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
java.util.concurrent, java.util.concurrent.atomic,
and java.util.concurrent.locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
java.util.function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
java.util.jar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
java.util.logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
java.util.prefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
java.util.random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
java.util.regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
java.util.spi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
java.util.stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
java.util.zip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Chapter 22 Input/Output: Exploring java.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
The I/O Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Using FilenameFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
The listFiles( ) Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Creating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
The AutoCloseable, Closeable, and Flushable Interfaces . . . . . . . . . . . . . . . 720
I/O Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Two Ways to Close a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
The Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
The Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
InputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
OutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
FileInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
FileOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
ByteArrayInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
ByteArrayOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Filtered Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
Buffered Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
SequenceInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
PrintStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
DataOutputStream and DataInputStream . . . . . . . . . . . . . . . . . . . . . . . 740
RandomAccessFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742

00-FM.indd 20 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xxi

The Character Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742


Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Writer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
FileReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
FileWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
CharArrayReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
CharArrayWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
BufferedReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
BufferedWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
PushbackReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
PrintWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
The Console Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
Serializable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
Externalizable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
ObjectOutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
ObjectOutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
ObjectInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
ObjectInputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
A Serialization Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Stream Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Chapter 23 Exploring NIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
The NIO Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
NIO Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
Charsets and Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
Enhancements Added by NIO.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
The Path Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
The Files Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
The Paths Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
The File Attribute Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
The FileSystem, FileSystems, and FileStore Classes . . . . . . . . . . . . . . . 775
Using the NIO System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Use NIO for Channel-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Use NIO for Stream-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
Use NIO for Path and File System Operations . . . . . . . . . . . . . . . . . . . 787
Chapter 24 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
Networking Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
The java.net Networking Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . 796
InetAddress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Factory Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
Inet4Address and Inet6Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
TCP/IP Client Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799

00-FM.indd 21 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xxii  Java: The Complete Reference

URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
URLConnection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
HttpURLConnection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
The URI Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
TCP/IP Server Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
Datagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
DatagramSocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
DatagramPacket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
A Datagram Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Introducing java.net.http . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
Three Key Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
A Simple HTTP Client Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Things to Explore in java.net.http . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
Chapter 25 Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
Two Event Handling Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
The Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Event Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
Event Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
The ActionEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
The AdjustmentEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
The ComponentEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
The ContainerEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
The FocusEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
The InputEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
The ItemEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
The KeyEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
The MouseEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
The MouseWheelEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
The TextEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
The WindowEvent Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
Sources of Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
Event Listener Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
The ActionListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
The AdjustmentListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
The ComponentListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
The ContainerListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
The FocusListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
The ItemListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
The KeyListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
The MouseListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

00-FM.indd 22 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xxiii

The MouseMotionListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 835


The MouseWheelListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
The TextListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
The WindowFocusListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
The WindowListener Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
Using the Delegation Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
Some Key AWT GUI Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837
Handling Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840
Adapter Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
Anonymous Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
Chapter 26 Introducing the AWT: Working with Windows, Graphics, and Text . . . 851
AWT Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
Window Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Working with Frame Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
Setting the Window’s Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
Hiding and Showing a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
Setting a Window’s Title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
Closing a Frame Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
The paint( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
Displaying a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
Setting the Foreground and Background Colors . . . . . . . . . . . . . . . . . . 857
Requesting Repainting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Creating a Frame-Based Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
Introducing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860
Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860
Drawing Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860
Drawing Ellipses and Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861
Drawing Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861
Drawing Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861
Demonstrating the Drawing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 862
Sizing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Working with Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865
Color Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865
Setting the Current Graphics Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866
A Color Demonstration Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866

00-FM.indd 23 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xxiv  Java: The Complete Reference

Setting the Paint Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868


Working with Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
Determining the Available Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
Creating and Selecting a Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
Obtaining Font Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874
Managing Text Output Using FontMetrics . . . . . . . . . . . . . . . . . . . . . . . . . . . 875
Chapter 27 Using AWT Controls, Layout Managers, and Menus . . . . . . . . . . . . . . . . 879
AWT Control Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Adding and Removing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Responding to Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
The HeadlessException . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
Using Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Handling Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Applying Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
Handling Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
CheckboxGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889
Choice Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891
Handling Choice Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892
Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894
Handling Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
Managing Scroll Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897
Handling Scroll Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
Using a TextField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900
Handling a TextField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901
Using a TextArea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903
Understanding Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904
FlowLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 905
BorderLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Using Insets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908
GridLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910
CardLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
GridBagLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 914
Menu Bars and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
A Word About Overriding paint( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
Chapter 28 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
Image Fundamentals: Creating, Loading, and Displaying . . . . . . . . . . . . . . 930
Creating an Image Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Loading an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Displaying an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
Double Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
ImageProducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
MemoryImageSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935

00-FM.indd 24 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xxv

ImageConsumer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
PixelGrabber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
ImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
CropImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
RGBImageFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
Additional Imaging Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
Chapter 29 The Concurrency Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
The Concurrent API Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
java.util.concurrent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
java.util.concurrent.atomic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
java.util.concurrent.locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
Using Synchronization Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
CountDownLatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
CyclicBarrier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
Exchanger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
Phaser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
Using an Executor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
A Simple Executor Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Using Callable and Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
The TimeUnit Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
The Concurrent Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984
Atomic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
Parallel Programming via the Fork/Join Framework . . . . . . . . . . . . . . . . . . . 988
The Main Fork/Join Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988
The Divide-and-Conquer Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 992
A Simple First Fork/Join Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
Understanding the Impact of the Level of Parallelism . . . . . . . . . . . . . 995
An Example that Uses RecursiveTask<V> . . . . . . . . . . . . . . . . . . . . . . . 998
Executing a Task Asynchronously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001
Cancelling a Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001
Determining a Task’s Completion Status . . . . . . . . . . . . . . . . . . . . . . . 1002
Restarting a Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
Things to Explore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
Some Fork/Join Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004
The Concurrency Utilities Versus Java’s Traditional Approach . . . . . . . . . 1004
Chapter 30 The Stream API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
Stream Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
Stream Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006
How to Obtain a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
A Simple Stream Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
Reduction Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013
Using Parallel Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015
Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018

00-FM.indd 25 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xxvi  Java: The Complete Reference

Collecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022
Iterators and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026
Use an Iterator with a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026
Use Spliterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
More to Explore in the Stream API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1030
Chapter 31 Regular Expressions and Other Packages . . . . . . . . . . . . . . . . . . . . . . . . 1031
Regular Expression Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Matcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Regular Expression Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Demonstrating Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Two Pattern-Matching Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039
Exploring Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039
Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1040
Remote Method Invocation (RMI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1044
A Simple Client/Server Application Using RMI . . . . . . . . . . . . . . . . . 1044
Formatting Date and Time with java.text . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
DateFormat Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
SimpleDateFormat Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
The java.time Time and Date API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051
Time and Date Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
Formatting Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
Parsing Date and Time Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
Other Things to Explore in java.time . . . . . . . . . . . . . . . . . . . . . . . . . . 1057

Part III Introducing GUI Programming with Swing


Chapter 32 Introducing Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061
The Origins of Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061
Swing Is Built on the AWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
Two Key Swing Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
Swing Components Are Lightweight . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
Swing Supports a Pluggable Look and Feel . . . . . . . . . . . . . . . . . . . . . 1062
The MVC Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
Components and Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
The Top-Level Container Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
The Swing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
A Simple Swing Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071
Painting in Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
Painting Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
Compute the Paintable Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075
A Paint Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076

00-FM.indd 26 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xxvii

Chapter 33 Exploring Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1079


JLabel and ImageIcon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1079
JTextField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
The Swing Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083
JButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083
JToggleButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1085
Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1088
Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1089
JTabbedPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1092
JScrollPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
JList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
JComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1099
Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
JTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105
Chapter 34 Introducing Swing Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1109
Menu Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1109
An Overview of JMenuBar, JMenu, and JMenuItem . . . . . . . . . . . . . . . . . . 1111
JMenuBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1111
JMenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1112
JMenuItem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1113
Create a Main Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1114
Add Mnemonics and Accelerators to Menu Items . . . . . . . . . . . . . . . . . . . 1118
Add Images and Tooltips to Menu Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120
Use JRadioButtonMenuItem and JCheckBoxMenuItem . . . . . . . . . . . . . . 1121
Create a Popup Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
Create a Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1127
Use Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1129
Put the Entire MenuDemo Program Together . . . . . . . . . . . . . . . . . . . . . . . 1135
Continuing Your Exploration of Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1141

Part IV Applying Java


Chapter 35 Java Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1145
What Is a Java Bean? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1145
Advantages of Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146
Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146
Design Patterns for Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146
Design Patterns for Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1148
Methods and Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1148
Using the BeanInfo Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1148
Bound and Constrained Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149
Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149
Customizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149

00-FM.indd 27 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xxviii  Java: The Complete Reference

The JavaBeans API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1150


Introspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
PropertyDescriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
EventSetDescriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
MethodDescriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
A Bean Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
Chapter 36 Introducing Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1157
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1157
The Life Cycle of a Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1158
Servlet Development Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1158
Using Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159
A Simple Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1160
Create and Compile the Servlet Source Code . . . . . . . . . . . . . . . . . . . 1161
Start Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161
Start a Web Browser and Request the Servlet . . . . . . . . . . . . . . . . . . . 1161
The Servlet API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1162
The jakarta.servlet Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1162
The Servlet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163
The ServletConfig Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164
The ServletContext Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164
The ServletRequest Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164
The ServletResponse Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164
The GenericServlet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166
The ServletInputStream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166
The ServletOutputStream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166
The Servlet Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166
Reading Servlet Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166
The jakarta.servlet.http Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1168
The HttpServletRequest Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1168
The HttpServletResponse Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1168
The HttpSession Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1169
The Cookie Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1170
The HttpServlet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1171
Handling HTTP Requests and Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173
Handling HTTP GET Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173
Handling HTTP POST Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175
Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176
Session Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178

Part V Appendixes
Appendix A Using Java’s Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . 1183
The javadoc Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1183
@author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1184
{@code} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
@deprecated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185

00-FM.indd 28 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Contents   xxix

{@docRoot} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
@exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
@hidden . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
{@index} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
{@inheritDoc} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186
{@link} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186
{@linkplain} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186
{@literal} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186
@param . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186
@provides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186
@return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187
@see . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187
@serial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187
@serialData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187
@serialField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188
@since . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188
{@summary} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188
{@systemProperty} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188
@throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188
@uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188
{@value} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188
@version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1189
The General Form of a Documentation Comment . . . . . . . . . . . . . . . . . . . 1189
What javadoc Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1189
An Example that Uses Documentation Comments . . . . . . . . . . . . . . . . . . . 1189
Appendix B Introducing JShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1191
JShell Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1191
List, Edit, and Rerun Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194
Add a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1195
Create a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1196
Use an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1196
Evaluate Expressions and Use Built-in Variables . . . . . . . . . . . . . . . . . . . . . 1198
Importing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1198
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1199
Some More JShell Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1199
Exploring JShell Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1200
Appendix C Compile and Run Simple Single-File Programs in One Step . . . . . . . 1201

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1203

00-FM.indd 29 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: xxx

This page intentionally left blank

00-FM.indd 30
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

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. It is also a powerful, general-purpose programming language
suitable for a wide variety of purposes. 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 twelfth edition, and it has been updated
for Java SE 17 (JDK 17). As a result, this edition of the book contains a substantial amount of
new material, updates, and changes. Of special interest are the discussions of the following
key features that have been added to the Java language since the previous edition of this book:

• Enhancements to switch
• Records
• Pattern matching with instanceof
• Sealed classes and interfaces
• Text blocks

Collectively, these constitute a substantial set of new features that significantly expand the
range, scope, and expressiveness of the language. The switch enhancements add power and
flexibility to this foundational control statement. The inclusion of records offers an efficient
way to aggregate data. The addition of pattern matching to instanceof enables a more
streamlined and resilient approach to a common programming task. Sealed classes and
interfaces give you fine-grained control over inheritance. Text blocks let you enter multiline
string literals, greatly simplifying the process of inserting such strings into your source code.
Collectively, these new features fundamentally expand the ways in which you can design and
implement solutions.

xxxi

00-FM.indd 31 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xxxii  Java: The Complete Reference

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.

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.
Next, it describes annotations, enumerations, autoboxing, generics, modules, and lambda
expressions. I/O is also introduced. The final chapter in Part I covers several recently added
features: records, sealed classes and interfaces, the enhanced switch, pattern matching with
instanceof, and text blocks.
Part II examines key aspects of Java’s standard API library. Topics include strings, I/O,
networking, the standard utilities, the Collections Framework, the AWT, event handling,
imaging, concurrency (including the Fork/Join Framework), regular expressions, and the
stream library.
Part III offers three chapters that introduce Swing.
Part IV contains two chapters that show examples of Java in action. The first discusses
Java Beans. The second presents an introduction to servlets.

Special Thanks
I want to give special thanks to Patrick Naughton, Joe O’Neil, 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 22, 24, and 28 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 31, 33, 35, and 36 of this edition. Joe
helped on several of my books, and his input has always been top-notch.
Danny Coward is the technical editor for this edition of the book. Danny has worked on
several of my books, and his advice, insights, and suggestions have always been of great value
and much appreciated.

00-FM.indd 32 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Preface   xxxiii

I also want to thank my wife Sherry for all of her contributions to this book, and to my
other books as well. Her thoughtful advice, proofreading, and indexing have always
contributed greatly to a successful outcome of each project.

HERBERT SCHILDT

00-FM.indd 33 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

xxxiv  Java: The Complete Reference

For Further Study


Java: The Complete Reference is your gateway to the Herb Schildt series of Java programming
books. Here are others that you will find of interest:

Herb Schildt’s Java Programming Cookbook

Java: A Beginner’s Guide

Introducing JavaFX 8 Programming

Swing: A Beginner’s Guide

The Art of Java

00-FM.indd 34 22/09/21 6:45 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: 1

PART

CHAPTER 1
I The Java Language

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

01-ch01.indd 1 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: 2

CHAPTER 12
Enumerations, Autoboxing,
and Annotations

CHAPTER 13
I/O, Try-with-Resources,
and Other Topics

CHAPTER 14
Generics

CHAPTER 15
Lambda Expressions

CHAPTER 16
Modules

CHAPTER 17
Switch Expressions,
Records, and Other Recently
Added Features

01-ch01.indd 2 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

CHAPTER

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 occur 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 object-
oriented 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.

01-ch01.indd 3 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

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 may 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.

01-ch01.indd 4 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Chapter 1 The History and Evolution of Java   5

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 (Prentice-Hall, 1978). C was formally standardized in December 1989, when the

Part I
American National Standards Institute (ANSI) standard for C was adopted.
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, at the time it was hardly a language that encouraged 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.

01-ch01.indd 5 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

6  PART I The Java Language

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. 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 at the time) 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

01-ch01.indd 6 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Chapter 1 The History and Evolution of Java   7

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

Part I
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 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 low-priority 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

01-ch01.indd 7 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

8  PART I The Java Language

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,
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 throughout 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 Impacted 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
At the time of Java’s creation, one of its most exciting features was the applet. An applet is
a special kind of Java program that is designed to be transmitted over the Internet and
automatically executed inside a Java-compatible web browser. If the user clicks a link that
contains an applet, the applet will download and run in the browser. Applets were intended
to be small programs. They were 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 allowed some functionality to be moved from the
server to the client.

01-ch01.indd 8 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

Chapter 1 The History and Evolution of Java   9

The creation of the applet was important because, at the time, it expanded the universe
of objects that could 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

Part I
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.
In the early days of Java, applets were a crucial part of Java programming. They illustrated
the power and benefits of Java, added an exciting dimension to web pages, and enabled
programmers to explore the full extent of what was possible with Java. Although it is likely
that there are still applets in use today, over time they became less important. For reasons that
will be explained, beginning with JDK 9, the phase-out of applets began, with applet support
being removed by JDK 11.

Security
As desirable as dynamic, networked programs are, they can also present serious problems in
the areas of security and portability. Obviously, a program that downloads and executes 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,
beginning with security.
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 programs to be safely downloaded and executed on the client computer, it was
necessary to prevent them from launching such an attack.
Java achieved this protection by enabling you to confine an application to the Java
execution environment and prevent it from accessing other parts of the computer.
(You will see how this is accomplished shortly.) The ability to download programs with a
degree of confidence that no harm will be done may have been 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. In other words, a mechanism that allows the same application
to be downloaded and executed by a wide variety of CPUs, operating systems, and browsers is
required. It is not practical to have different versions of the application for different computers.

01-ch01.indd 9 21/09/21 5:35 PM


CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9

10  PART I The Java Language

The same application 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 allowed 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 what is called the Java
Virtual Machine (JVM), which is part of the Java Runtime Environment (JRE). 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 a JRE 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 manages program execution. Thus, it is possible for the JVM to
create a restricted execution environment, called the sandbox, that contains the program,
preventing unrestricted access to the machine. 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 an entire Java program is
not compiled into executable code all at once. 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.

01-ch01.indd 10 21/09/21 5:35 PM


Another random document with
no related content on Scribd:
kinsman, since he happened to be mounted, in pursuit of the missing
officer. (And at this point the officer in question had remarked rather
stiffly that he trusted Mr. Cameron knew that his absence from the
scene of conflict was due only to his having gone for reinforcements,
and Mr. Cameron had replied politely that no other explanation had
even occurred to him.)
They were at the top of the pass at last, and had a fine view
before them; but the captive did not find it so, the mountains being
too high for his taste and the downward path too steep. Stones rolled
away from beneath the grey’s hoofs; now and then he slipped a trifle,
for which his owner, leading him carefully by the bridle, apologised.
He would not have come this way, he said, but that it was the
shortest from the spot where he and Captain Windham had ‘chanced
to meet’, as he put it. And then all at once the descent was less
steep and they were looking down on a glen among the mountains,
with a little lake, some signs of cultivation, grazing sheep and cattle,
and, in the midst of trees, the roof and chimneys of a house, whence
a welcome smoke ascended.
“There is Loch na h-Iolaire,” said the young Highlander at Captain
Windham’s bridle, pointing to the sheet of water; and he paused after
he had said it, because, though Captain Windham could not guess it,
he never came upon the loch from any point of the compass without
a little fountain of joy bubbling up and singing to itself in his heart.
“And there is the house of Ardroy, our destination. I am sure that you
will be glad of a meal and a bed, sir.”
Keith admitted it, and the descent continued, in the face of the
sunset afterglow. His captor did not live in a cave, then—but the
Englishman had abandoned that idea some time ago. Indeed Mr.
Cameron was apparently a landed proprietor with tenants, for
besides sheep, goats and cows, there were a good many roughly
constructed cottages scattered about. By and by, skirting the end of
the little lake and its birch-trees, they struck into another track, and
Keith saw the house in front of him, a simple but not undignified two-
storeyed building of which one end was slightly lower than the other,
as if it had been added to. Over the porch was a coat-of-arms, which
successive layers of whitewash had made difficult to decipher.
“I expect that my aunt, who keeps house for me, and my guests
are already supping,” observed the young owner of this domain,
assisting his prisoner to dismount. “We will join them with as little
delay as possible. Excuse me if I precede you.” He walked in and
opened a door on his right. “Aunt Marget, I have brought a visitor
with me.”
From behind him the ‘visitor’ could see the large raftered room,
with a long table spread for a meal, and a generous hearth, by which
were standing an elderly man and a girl. But in the foreground was a
middle-aged lady, well-dressed and comely, exclaiming, “My dear
Ewen, what possesses you to be so late? And what’s this we hear
about a brush with the Elector’s troops near Loch Lochy? . . . Mercy
on us, who’s this?”
“A guest whom I have brought home with me from the Glen,”
replied the late-comer. “Yes, there has been a skirmish.—Captain
Windham, let me present you to my aunt, Miss Cameron, to Miss
Grant, and to Mr. Grant, sometime of Inverwick.”
Keith bowed, and the two ladies curtseyed.
“You are just going to sit down to supper?” queried the master of
the house. “We shall be glad of it; and afterwards, Aunt Margaret,
pray find some bandages and medicaments for Captain Windham,
who has met with a bad fall.”
“I had perhaps better tell you, madam,” interpolated Keith at this
point, holding himself rather erect, “that, though Mr. Cameron is kind
enough to call me a guest, I am in reality his prisoner.—But not one
who will put you to any inconvenience of wardership,” he added
quickly, seeing the look which passed over the lady’s expressive
countenance. “I have given Mr. Cameron my parole of honour, and I
assure you that even ‘the Elector’s’ officers observe that!” (For he
believed so then.)
Miss Cameron surveyed him with humour at the corners of her
mouth. “Every country has its own customs, Captain Windham; now I
warrant you never speak but of ‘the Pretender’ in London. You are
English, sir?”
“I have that disability, madam.”
“Well, well,” said Miss Cameron, breaking into a smile, “even at
that, no doubt you can eat a Highland supper without choking. But
take the Captain, Ewen, and give him some water, for I’m sure he’ll
be wanting to wash off the traces of battle.”
“I should be grateful indeed,” began Keith uncomfortably,
wondering how much blood and dirt still decorated his face; but his
captor broke in: “You must not think that I am responsible for Captain
Windham’s condition, Aunt Margaret. His horse came down as he
was riding to fetch reinforcements from Kilcumein, and he was
disabled before ever I overtook him.”
“An accident, sir—or was the poor beast shot?” queried Miss
Cameron.
“An accident, madam,” responded Keith. “A heron, I presume it
was, rose suddenly from the lake and startled him; I was riding very
fast, and he came down, breaking his leg. I twisted my ankle,
besides being stunned for a while, so that I must apologise if my
appearance——” And this time he put up his hand to his forehead.
“A heron, did you say?” exclaimed Ewen Cameron’s voice beside
him, surprised and almost incredulous. “A heron brought your horse
down?”
“Yes,” replied Keith, surprised in his turn. “Why not, Mr.
Cameron? An unusual mischance, I dare say, and none of my
seeking, I assure you; but it is true.”
“I don’t doubt your word, sir,” replied the young man; yet there
was something puzzled in the gaze which he turned on his prisoner.
“It is . . . yes, unusual, as you say. Herons, as a rule——” He broke
off. “If you will come with me, Captain Windham, you shall refresh
yourself before we eat.”

Captain Windham sat down to a better supper than he had met


since he left London, and even in London he would not have tasted
such trout and venison, and might well have drunk worse claret. Out
of regard for him, perhaps, or out of discretion, the conversation
never touched on political matters, though he thought that he could
feel a certain excitement simmering below the surface of the talk.
(And well it might, he reflected; had not the master of the house this
day committed himself to overt hostilities against His Majesty’s
Government?) The elderly gentleman in the grey wig, who appeared
to have been living recently in Paris, discoursed most innocuously of
French châteaux and their gardens, with frequent references to
Versailles and Marly, and appeals to his daughter—“You remember
the day of our little expedition to the château of Anet, my dear?”
Keith would have thought the deserted shrine of St. Germain a more
likely goal of pilgrimage, for he took Mr. Grant, from his mere
presence here, to be a Jacobite.
But surely his daughter would have preferred to this mild talk of
parterres and façades a recital by Mr. Ewen Cameron of his
afternoon’s prowess! As far as their personal conflict went, Captain
Windham was perfectly willing that this encounter should be related
by a victor who was evidently disposed to allow the fullest weight to
the physical disabilities of the vanquished; yet he was grateful for the
tact with which Mr. Cameron (in his presence at least) had glossed
over the flight of the Royal Scots from the bridge. Only questions,
indeed, drew from him the partial information which he furnished. He
would tell them more afterwards, no doubt. . . . Who was this pretty
Miss Grant with the blue fillet in her dark hair—a kinswoman? If she
was the future mistress of the house, young Cameron had good
taste. So, to be just, had the lady.
But, despite the courtesy shown him, the unwilling guest was not
sorry when, very soon after supper, it was suggested that he should
retire, for his ankle was painful and one shoulder ached, though he
protested that he could look after his own hurts. His conqueror
showed him to his bedchamber exactly as a host might have done.
The room was of a fair size, and had good old-fashioned furniture;
and, presumably because it had been for some time unused, there
was even a fire burning. An elderly woman brought up a crock of hot
water, a salve and linen for bandages, and the Englishman was then
left to her ministrations. And it was not long before his discreet
questions had drawn from this dame, who was not very
communicative, and spoke English as though it were a foreign
tongue to her, the information that Miss Grant was to marry the laird
in the autumn. Keith privately hoped that the prospective bridegroom
might not find himself in prison before that time, as a consequence of
having laid hands on himself—if of nothing worse—though, after that
venison, he resolved that he would not lift a finger to send him there.
When his ankle had been bathed and bound up, and the elderly
servant had withdrawn, the soldier removed his sash, coat and wig,
and extended himself in a comfortable chair in front of the fire, with
his bandaged foot on another. There were books to his hand, as he
discovered by reaching up to a shelf on the wall; but, having pulled
some down, he did not, at first, find that the effort had repaid him. He
had captured a Terence, a Horace, Télémaque, and Montesquieu’s
Lettres Persanes. They all had Ewen Cameron’s name written in
them.
Keith whistled. He was turning over the leaves of the Lettres
when there was a knock and his host—or gaoler—re-entered.
“I hope that Marsali has made you comfortable, sir?—Those
books are not very entertaining, I am afraid. If you intend reading into
the night—which I fear must mean that your foot is paining you—I
will see whether I cannot find you something else. I believe that my
aunt has Mr. Fielding’s novel of Joseph Andrews somewhere.”
“Pray do not trouble, sir,” replied Keith. “I intend to go to bed and
sleep; it was only idleness which directed my hand to that shelf
there. I see that you read French and Latin, Mr. Cameron?” And
even as he uttered the words he thought how ill-bred was the
remark, and the surprise which he had not been able entirely to keep
out of his tone.
But the young Highlander answered quite simply, in his gentle,
rather slow voice: “I was partly educated in France—for that, you
know, is easier for us Jacobites. As to Latin, yes, I can read it still,
though I am afraid that my iambics would only procure me the ferule
nowadays.”
Captain Windham’s ideas about the Northern barbarians were
undergoing startling changes. He had already noticed that none of
the inmates of this house used the vernacular which he was
accustomed to hear in the Lowlands; they spoke as good English as
himself, if with an unfamiliar and not displeasing lilt. A little to cover
his annoyance at his own lack of breeding he remarked, “France,
yes; I suppose that your connection is close. And now that the . . .
that a certain young gentleman has come thence——”
“Yes?” asked the other in a slightly guarded manner.
“No, perhaps we had best not engage upon that topic,” said
Keith, with a slight smile. “I will imitate your own courteous discretion
at supper, Mr. Cameron, in saying so little about the episode at the
bridge, of which indeed, as a soldier, I am not proud.—By the way,
having myself introduced that subject, I will ask you if you can make
clear a point in connection with it which has puzzled me ever since.
How was it that no attempt at pursuit—or at least no immediate
attempt—was made by the body posted there?”
“That is easily explained,” replied Ewen Cameron promptly. “The
Keppoch MacDonalds there dared not let you see how few they
were, lest your men should have rallied and crossed the bridge after
all.”
“How few?” repeated Captain Windham, thinking he had not
heard aright. “But, Mr. Cameron, there were a quantity of
Highlanders there, though, owing to the trees it was impossible to
form an accurate estimate of their numbers.”
“No, that would be so,” said his captor, looking at him rather
oddly. “You may well have thought the bridge strongly held.”
“You mean that it was not?” And, as his informant merely shook
his head, Keith said impatiently, but with a sudden very unpleasant
misgiving, “Do you know how many men were there, Mr. Cameron?”
Mr. Cameron had taken up a fresh log, and now placed it
carefully in position on the fire before answering. “I believe,” he said,
with what certainly sounded like reluctance, “that there were not
above a dozen there—to be precise, eleven men and a piper.”
Keith’s fingers closed on the arms of his chair. “Are you jesting,
sir?”
“Not in the least,” replied the young man, without any trace either
of amusement or of elation. “I know it to be a fact, because I spoke
afterwards with their leader, MacDonald of Tiendrish. They used an
old trick, I understand, to pass themselves off as more than they
really were.”
He continued to look at the fire. Captain Windham, with a
suppressed exclamation, had lowered his injured foot to the ground,
and then remained silent, most horribly mortified. Two companies of
His Majesty’s Foot turning tail before a dozen beggarly Highlanders
with whom they had not even stayed to exchange shots! The solace,
such as it had been, of reflecting that the recruits had in the end
been surrounded and outnumbered, was swept clean away, for he
knew now that they would never have come to this pass but for their
initial poltroonery. Keith had lost all desire for further converse, and
every instinct of patronage was dead within him. Why the devil had
he ever asked that question?
“I think, sir,” observed his captor, turning round at last, “that it
would be better, would it not, if you went to bed? I hope that you
have been given everything that is necessary?”
“Everything, thank you,” replied Keith shortly. “And also, just now,
something that I could well have done without.” He tried to speak
lightly, yet nothing but vexation, he knew, sounded in his tone.
“I am sorry,” said the Highlander gravely. “I would not have told
you the number had you not pressed me for it. Forget it, sir.” He went
to the door. “I hope that your injured ankle will not keep you awake.”
That ill office was much more likely to be performed by the piece
of news which he had presented to the sufferer. “Eleven men and a
piper!” repeated Captain Keith Windham of the Royal Scots when
the door was shut; and with his sound leg he drove his heel viciously
into the logs of Highland pine.
CHAPTER III

Captain Keith Windham, unwillingly revisiting the neighbourhood of


High Bridge, which was populated with leaping Highlanders about
nine feet high, and permeated, even in his dream, with the dronings
and wailings of the bagpipe, woke, hot and angry, to find that the
unpleasant strains at least were real, and were coming through the
window of the room in which he lay. He remained a moment blinking,
wondering if they portended some attack by a hostile clan; and finally
got out of bed and hobbled to the window.
In front of the house a bearded piper was marching solemnly up
and down, the ribbons on the chanters of his instrument fluttering in
the morning breeze. There was no sign of any armed gathering.
“Good Gad, it must be the usual reveillé for the household!” thought
the Englishman. “Enough to put a sensitive person out of temper for
the rest of the day.” And he returned to bed and pulled the blankets
over his ears.
At breakfast, an excellent meal, and a pleasant one also, where
very civil enquiries were made concerning the night he had spent
and the state of his injuries, Miss Cameron expressed a hope that he
had not been unduly disturbed by Neil MacMartin’s piobaireachd,
adding that he was not as fine a piper as his father Angus had been.
Keith was then thankful that he had not heard Angus.
When the meal was over he strayed to the window and looked
out, wondering how he should occupy himself all day, but determined
upon one thing, that he would not let these Camerons guess how
bitterly he was mortified over the matter of the bridge. Outside the
porch his host (save the mark!) was already talking earnestly to a
couple of Highlanders, in one of whom Captain Windham had no
difficulty in recognising the ‘cut-throat’ of the previous day; the other,
he fancied, was the musician of the early morning. “I wish I could
persuade myself that Mr. Cameron were putting a ban upon that
performance,” he thought; but he hardly hoped it.
Presently the young laird came in. He was wearing the kilt to-day,
and for the first time Keith Windham thought that there was
something to be said for that article of attire—at least on a man of his
proportions.
“Is not that your attendant of yesterday out there?” remarked the
soldier idly.
“Lachlan MacMartin? Yes. The other, the piper who, I am afraid,
woke you this morning, is his brother Neil.—Captain Windham,” went
on the piper’s master in a different tone, “what I am going to tell you
may be news to you, or it may not, but in either case the world will
soon know it. To-day is Saturday, and on Monday the Prince will set
up his standard at Glenfinnan.”
There was a second’s silence. “And you, I suppose, Mr.
Cameron, intend to be present . . . and to cross the Rubicon in his
company?”
“All Clan Cameron will be there,” was the reply, given with a
probably unconscious lift of the head. “And as in consequence of this
I shall be pretty much occupied to-day, and little at home, I would
advise you, if I may, not to go out of sight of the house and policies.
You might——” Ewen Cameron hesitated for a moment.
“I might find myself tempted to abscond, you were going to say?”
struck in his captive . . . and saw at once, from the bleak look which
came into those blue eyes, that his pleasantry did not find favour.
“I should not dream of so insulting you,” replied Ardroy coldly. “I
was merely going to say that it might not be oversafe for you, in that
uniform, if you did.” And as he was evidently quite offended at the
idea that he could be supposed to harbour such a suspicion of his
prisoner, there was nothing for the latter to do but to beg his pardon,
and to declare that he had spoken—as indeed he had—in the
merest jest.
“But perhaps this young mountaineer cannot take a jest,” he
thought to himself when they had parted. “I’ll make no more—at least
outwardly.” But he was not to keep this resolution.
And indeed he had little but occasional glimpses of young Ardroy
or of any of the family that morning. The whole place was in a bustle
of preparation and excitement. Tenants were (Keith surmised from
various indications) being collected and armed; though only single
Highlanders, wild and unintelligible persons, appeared from time to
time in the neighbourhood of the house. Miss Cameron and Miss
Grant seemed to be equally caught up in the swirl, and Mr. Grant
was invisible. The only idle person in this turmoil, the captive
Englishman, sat calmly on the grass plot at a little distance from the
house, with The History of the Adventures of Mr. Joseph Andrews in
his hand, half amused to see the inhabitants of this ant-heap—thus
he thought of them—so busy over what would certainly come to
nothing, like all the other Jacobite attempts.
And yet he reflected that, for all the futility of such preparations,
those who made them were like to pay very dearly for them. Ewen
Cameron would get himself outlawed at the least, and somehow he,
whom Ewen Cameron had defeated yesterday, would be sorry. The
young Highlander had certainly displayed towards his captive foe the
most perfect chivalry and courtesy, and to this latter quality Keith
Windham, who could himself at will display the most perfect
rudeness, was never blind. And yet—a sardonically comforting
reflection—a rebel must find the presence of an English soldier not a
little embarrassing at this juncture.
It was partly a desire to show that he too possessed tact, and
partly pure boredom, which caused Captain Windham, in the latter
half of the afternoon, to disregard the warning given him earlier, and
to leave the neighbourhood of the house. He helped himself to a
stout stick on which to lean in case of necessity, though his ankle
was remarkably better and hardly pained him at all, and started to
stroll along the bank of the loch. Nobody had witnessed his
departure. And in the mild, sometimes obscured sunshine, he
followed the path round to the far side, thinking that could the little
lake only be transported from these repellent mountains and this
ugly purple heather into more civilised and less elevated
surroundings, it would not be an ill piece of water.
Arrived on the farther side, he began idly to follow a track which
led away from the lake and presently started to wind upwards among
the heather. He continued to follow it without much thinking of what
he was doing, until suddenly it brought him round a fold of the
mountain side to a space of almost level ground where, beside a
group of pine trees, stood three low thatched cottages. And there
Captain Windham remained staring, not exactly at the cottages, nor
at the score or so of Highlanders—men, women, and children—in
front of these dwellings, with their backs turned to him, but at the
rather puzzling operations which were going forward on top of the
largest croft.
At first Captain Windham thought that the man astride the roof
and the other on the short ladder must be repairing the thatch, until
he saw that, on the contrary, portions of this were being relentlessly
torn off. Then the man on the roof plunged in his arm to the shoulder
and drew forth something round and flat, which he handed to the
man on the ladder, who passed it down. Next came something long
that glittered, then another round object, then an unmistakable
musket; and with that Keith realised what he was witnessing—the
bringing forth of arms which should have been given up at the
Disarming Act of 1725, but which had been concealed and saved for
just such an emergency as the present.
Now there came bundling out several broadswords tied together
and another musket. But a man in a bright scarlet coat with blue
facings and long white spatterdashes is altogether too conspicuous a
figure in a mountain landscape, and Keith had not in fact been there
more than a minute before a boy who had turned to pick up a targe
saw him, gave a yell, and pointing, screamed out something in
Gaelic. Every face was instantly turned in the intruder’s direction,
and moved by the same impulse each man snatched up a weapon
and came running towards him, even he on the roof sliding down
with haste.
Captain Windham was too proud to turn and flee, nor would it
much have advantaged him; but there he was, unarmed save for a
staff, not even knowing for certain whether these hornets upon
whose nest he had stumbled were Mr. Cameron’s tenants or no, but
pretty sure that they would not understand English, and that he could
not therefore convince them of his perfect innocence. Deeply did he
curse his folly in that moment.
He had at any rate the courage not to attempt to defend himself;
on the contrary, he deliberately threw his stick upon the ground, and
held out his hands to show that they were empty. The foremost
Highlander, who was brandishing one of those unpleasant basket-
hilted swords, hesitated, as Keith had hoped, and shouted
something; on which the rest rushed round, and as many hands as
possible laid hold of Captain Windham’s person. He staggered under
the impact, but made no resistance, for, to his great relief, he had
already recognised in the foremost assailant with the broadsword the
scowling visage of Lachlan MacMartin, and beside him the milder
one of his brother Neil, Mr. Cameron’s piper. Even if they did not
understand English, these two would at least know who he was.
“I am your master’s prisoner,” he called out, wishing the others
would not press so upon him as they clutched his arms. “You had
better do me no harm!”
In Lachlan’s face there was a sort of sullen and unwilling
recognition. He spoke rapidly to his brother, who nodded and gave
what was presumably an order. Reluctantly the clutching hands
released their grip of Keith, their owners merely glowering at him; but
they did not go away, though the circle now opened out a little. A
couple of women had joined the group, and a small child or two; all
talked excitedly. Keith had never thought to feel gratitude towards
the wolf-like Lachlan, but at this moment he could almost have
embraced him, since but for him and Neil his own might well have
been the first blood on those resuscitated claymores.
His preserver now advanced, his hand on his dirk, and addressed
the soldier, rather to his surprise, in English. “You may pe the laird’s
prisoner,” he said between his teeth, “but why did you come up
here?—You came to spy, to spy!” He almost spat the words in the
intruder’s face. “And with spiess, who haf seen what they should not
haf seen, there iss a ferry short way . . . either thiss,” he unsheathed
an inch or two of his dirk, “or the lochan down yonder with a stone
round the neck!”
“I am not a spy,” retorted Captain Windham haughtily. “I knew
nothing of there being cottages here; I was taking a walk, and came
upon you entirely by accident.”
“A walk, when yesterday your foot wass so hurt that you must
ride the laird’s horse!” hissed Lachlan, bringing out all the sibilants in
this not ineffective retort. “All thiss way for pleasure with a foot that
iss hurt! And then you will pe going back to the saighdearan dearg—
to the red soldiers—at Kilcumein and pe telling them. . . . Ach, it will
certainly pe petter . . .” And his fingers closed round the black hilt at
his groin; Keith had never seen fingers which more clearly itched to
draw and use a weapon.
But at this point Neil the piper intervened, laying his hand on his
brother’s arm, shaking his head, and speaking earnestly in their
native tongue, and Keith, concluding that a professional musician (if
that term could possibly be extended to one who produced sounds
like this morning’s) would be a man of peace, felt more secure, not
knowing that in a fray the piper habitually gave his pipes to his boy
and fought with the best. But he heartily wished himself back at the
house again; it would have been far better had he taken his host’s
warning to heart instead of making a foolish jest about it.
During the colloquy, however, there approached the group a
handsome, venerable old man whom Keith had not previously
noticed. He came towards them tapping the ground with a long staff,
as if of uncertain sight, and said something first to Lachlan and then
to Neil. The piper appeared to listen with attention, and on that
turned to the captive.
“My father iss asking you,” he said, in a manner which suggested
that he was seeking for his words in an unfamiliar tongue, “to permit
him to touch you, and to pe speaking with you. He iss almost blind.
He hass not the English, but I will pe speaking for him.”
“Certainly, if he wishes it,” replied Captain Windham with
resignation, thinking that ‘permission’ to touch him might well have
been asked earlier, and not taken so violently for granted.
Neil took his father’s hand, and led him up to the interview. The
old man, who was obviously not completely blind, peered into the
Englishman’s face, while his hands strayed for a moment or two over
his shoulders and breast. He then addressed a question to his elder
son, who translated it.
“He asks if you wass meeting a curra yesterday?”
“If I had any notion what a curra was,” returned Keith, “I might be
able to satisfy your father’s curiosity. As it is——”
“A curra,” explained Neil, struggling, “iss . . . a large bird, having a
long . . . a long . . .”
“It iss called ‘heron’ in the English,” interposed Lachlan. And he
added violently, “Mallachd ort! wass you meeting a heron
yessterday?”
The Erse sounded like an objurgation (which it was) and the
speaker’s eyes as they glared at Keith had turned to dark coals. It
was evidently a crime in these parts to encounter that bird, though to
the heron’s victim himself it wore rather the aspect of a calamity.
Ignoring this almost frenzied query he replied shortly to the official
interpreter: “Yes, unfortunately I did meet a heron yesterday, which
by frightening my horse led to—my being here to-day.”
Lachlan MacMartin smote his hands together with an exclamation
which seemed to contain as much dismay as anger, but Neil
contented himself with passing on this information to his parent, and
after a short colloquy turned once more to the Englishman. “My
father iss taibhsear,” he explained. “That iss, he hass the two sights.
He knew that the heron would pe making Mac—the laird to meet with
you.”
“Gad, I could wish it had not!” thought Keith; but judged it more
politic not to give this aspiration utterance.
“And he asks you whether you wass first meeting Mac ’ic Ailein
near watter?”
“If that name denotes Mr. Ewen Cameron,” replied Keith, “I did.
Near a good deal of ‘watter’.”
This was passed on to the seer, involving the repetition of a word
which sounded to Captain Windham like “whisky,” and roused in his
mind a conjecture that the old man was demanding, or about to
demand, that beverage. None, however, was produced, and after
thanking the Englishman, in a very courtly way, through the medium
of his son, the soothsayer departed again, shaking his head and
muttering to himself; and Keith saw him, when he reached the
cottages, sit down upon a bench outside the largest and appear to
fall into a reverie.
Directly he was safely there, Lachlan MacMartin reverted with
startling suddenness to his former character and subject of
conversation.
“You haf seen what you should not haf seen, redcoat!” he
repeated fiercely. “Pefore you go away from thiss place you shall be
swearing to keep silence!”
“That I certainly shall not swear to do,” replied Captain Windham
promptly. “I am not accustomed to take an oath at any man’s bidding,
least of all at a rebel’s.”
Again the dark flame shone in the Highlander’s eyes.
“And you think that we will pe letting you go, Sassenach?”
“I think that you will be extremely sorry for the consequences if
you do not,” returned the soldier. “You know quite well that if you lay
a finger upon me you will have to answer for it to your master or
chief, or whatever he is!”
“We are the foster-brothers of Mac ’ic Ailein,” responded Lachlan
slowly. (“What, all of you?” interjected Keith. “I wish him joy of you!”)
“He knows that all we do iss done for him. If we should pe making a
misstake, not knowing hiss will . . . or if you should fall by chance
into the loch, we should pe sorry, but we could not help it that your
foot should pe slipping, for it wass hurt yessterday . . . and you would
nefer go back to Kilcumein to tell the saighdearan dearg what you
haf peen seeing.”
He did not now seem to be threatening, but rather, with a kind of
gloomy satisfaction, thinking out a plausible course of action with
regard to the intruder, and it was a good deal more disquieting to the
latter than his first attitude. So was the expression on the faces of
the other men when Lachlan harangued them volubly in his own
language. His brother Neil alone appeared to be making some
remonstrance, but in the end was evidently convinced, and almost
before the unlucky officer realised what was toward, the whole group
had launched themselves upon him.
Keith Windham fought desperately, but he had no chance at all,
having been surrounded and almost held from the outset, and in a
moment he was borne down by sheer weight of numbers. Buttons
came off his uniform, his wig was torn bodily from his head by some
assailant who probably imagined that he had hold of the
Sassenach’s own hair, he was buffeted and nearly strangled, and lay
at last with his face pressed into the heather, one man kneeling upon
his shoulders, while another tied his hands behind his back, and a
third, situated upon his legs, secured his ankles. Outraged and
breathless, the soldier had time for only two sensations: surprise that
no dirk had yet been planted in him, and wonder whether they really
meant to take him down and throw him into the lake.
The struggle had been conducted almost in silence; but
conversation broke out again now that he was overpowered. Only for
a moment, however; then, as suddenly, it ceased, and the heavy,
bony knees on Captain Windham’s shoulder-blades unexpectedly
removed themselves. A sort of awestruck silence succeeded. With
faint thoughts of Druids and their sacrifices in his mind Keith
wondered whether the patriarchal soothsayer were now approaching
to drive a knife with due solemnity into his back . . . or, just possibly,
to denounce his descendants’ violence. But he could not twist
himself to look, for the man on his legs, though apparently smitten
motionless, was still squatting there.
And then a voice that Keith knew, vibrating with passion,
suddenly shouted words in Erse whose purport he could guess. The
man on his legs arose precipitately. And next moment Ewen
Cameron was kneeling beside him in the heather, bending over him,
a hand on his shoulder. “Captain Windham, are you hurt? God
forgive me, what have they been doing? Tied!” And in a moment he
had snatched a little knife out of his stocking and was cutting Keith’s
bonds. “Oh, why did I let you out of my sight! For God’s sake tell me
that you are not injured!”
He sounded in the extreme of anxiety—and well he might be,
thought the indignant Englishman, who made no haste to reply that,
if exhausted, he was as yet unwounded. He made in fact no reply at
all, while the young chieftain, white with agitation and anger, helped
him to his feet. When at last he stood upright, hot and dishevelled,
and very conscious of the fact, Captain Windham said, in no friendly
tone:
“You were just in time, I think, Mr. Cameron—that is, if, now that
you are here, your savages will obey you.”
From pale the young man turned red. “I warned you, if you
remember,” he said rather low, and then, leaving Captain Windham
to pick up his hat and wig and to restore some order to his attire,
strode towards the silent and huddled group of his retainers, who
had retreated in a body nearer to the crofts. Angry and humiliated as
Keith felt, it was some consolation to him, as he brushed the pieces
of heather off his uniform, and pulled his wig once more over his own
short dark hair, to observe that, whatever their master was saying to
them in the Erse, it seemed to have a most salutary and withering
effect. Even the redoubtable Lachlan, who hoarsely uttered some
remark, presumably an excuse, was reduced to complete silence,
either by the very terse and vigorous reply which he drew upon
himself, or by the threatening attitude of the speaker.
All this time the prophetic elder had sat at his cottage door
listening, with his head tilted back in the manner of the blind, but
taking no part in the reckoning which was falling upon the offenders,
just as (presumably) he had sat throughout the assault. And having
made short work of the culprits, the rescuer now seemed in haste to
remove the rescued, and came towards him, his eyes still very blue
and fierce.
“If you will allow me, Captain Windham, I will take you back to the
house, away from these savages, as you rightly call them.”
“Thanks, I can return safely enough, no doubt,” replied Keith
indifferently, pulling down his waistcoat. “There are no more
encampments of them, I believe, on the way back.”
“I should prefer to escort you,” returned Ardroy, most acutely
vexed, as was evident. And, since his vexation did not at all
displease the Englishman, he picked up his staff and preceded him
in silence off the plateau.
They had gone some way down the mountain path before Ewen
Cameron spoke again.
“I had no right to accept your sword,” he said, in a voice still bitter
with mortification, “if I could not protect you against my own
followers. I would not have had this happen for a thousand pounds. I
can offer you no apologies that are deep enough for such an
outrage.”
“Except for the loss of some buttons, I am not much the worse,”
replied Keith dryly without turning his head.
“But I am,” he heard the Highlander say behind him in a low
voice.
Nothing more passed between them until they had arrived at the
level of the loch, but by that time a rather remarkable change had
come upon Keith. Much better and more dignified to make light of
the outrage which he had just suffered than to exaggerate it by
sulking over it. Besides, he was beginning to be sorry for the
palpable distress of that punctilious young man, Mr. Cameron of
Ardroy, who could not in very justice be blamed for what had
happened.
So he stopped and turned round. “Mr. Cameron,” he said frankly,
“I have no one but myself to thank for the rough handling I received
up yonder. You warned me not to go far afield; and moreover I acted
like a fool in staying there to watch. Will you forgive my ill-temper,
and let me assure you that I shall think no more of the episode
except to obey your warnings more exactly in future.”
Ardroy’s face cleared wonderfully. “You really mean that, sir?”
“Assuredly. I ran my head into the lion’s mouth myself. I shall be
obliged if you will not mention my folly to the ladies; a soldier’s self-
esteem, you know, is easily hurt.” His smile went up a little at the
corner.
A sparkle came into Ewen Cameron’s eyes. “You are generous,
Captain Windham, and I am not deceived by your plea for silence. I
am so ashamed, however, that I welcome it for the sake of my own
self-esteem.”
“But I mean what I say,” returned Keith. (He was quick enough in
the uptake, this young chief of barbarians!) “It was the act of an utter
fool for me, in this uniform, to stand gaping at . . . at what was going
on up there. You know what it was, I presume?” he added, with a lift
of the eyebrows.
“Naturally,” said Ewen without embarrassment. “It was that which
brought me up there—most fortunately. But now,” he went on with a
frown, “now I am not sure that I shall allow those arms to be carried
by men in my tail who have so disgraced themselves and me.—Let
us go on, if you will, for when I have escorted you to the house I shall
return to deal with that question.”
“You seemed,” observed Keith, as they went on once more, “to
be dealing with it pretty satisfactorily just now!” (So he proposed, as
a punishment, to debar the offenders from the pleasures of armed
rebellion!)
“At least, before I consent to their following me on Monday,” said
the dispenser of justice, striding on, “they shall all beg your pardon!”
“Oh, pray excuse them that!” exclaimed Keith, not at all
welcoming the prospect. “I should be horribly embarrassed, I assure
you. Moreover, I can almost sympathise with their zeal—now that
there is no prospect of my being thrown into the lake here with a
stone round my neck.”
His captor stopped. “Was that what they were going to do?” he
asked in a horrified tone.
“They spoke of it, since I would not promise to keep silence on
what I had seen. They were quite logical, you know, Mr. Cameron,
for what I saw was certainly not meant for the eyes of an English
officer!”
“You were my prisoner—my guest—they had no excuse
whatever!” declared the young man, wrath beginning to seize on him
again. “Neil and Lachlan knew that, if the others did not. And Angus
—what was Angus about not to stop them?”
“Is that the blind veteran who takes such an interest in the natural
history of these parts?”
“What do you mean?” asked his companion.
“Why,” answered Keith, who was after all enjoying a kind of
secret revenge by quizzing him, “that he was particular to enquire,
through his estimable son, your piper, whether I had encountered a
heron before I made your acquaintance yesterday.”
The mention of that fowl appeared for the second time to startle
his host (though until that moment Keith had forgotten its effect upon
him last night). “Ah, my foster-father asked you that?” he murmured,
and looked upon the Englishman with a rather troubled and
speculative gaze. But Keith had found a new subject of interest. “Is
the old gentleman really your foster-father?” he enquired. “But of
course he must be, if his sons are your foster-brothers.”
“I think,” said the foster-son somewhat hastily, “that you can
return safely from here, so, if you will excuse me, Captain Windham,
I will now go back to Slochd nan Eun.”
“To execute judgment,” finished Keith with a smile. “Indeed, I am
not so devoid of rancour as to wish to hinder you. But if you do
condemn your foster-relations to stay at home,” he added rather
meaningly, “you will be doing them a good turn rather than an ill
one.”
It seemed doubtful, however, if Ewen Cameron had heard this
remark, for he was already striding lightly and quickly back in the
direction of the mountain path, his kilt swinging about his knees as
he went.

It was an odd coincidence that at supper that evening, after


Angus MacMartin’s name had come up in some talk between Miss
Cameron and Mr. Grant, the former should turn to Captain Windham
and ask if he had seen their taibhsear or seer? Seeing instantly from
Ardroy’s face that he was regretting the introduction of his foster-
father’s name into the conversation, Keith made malicious haste to
reply that he had contrived to get as far as the soothsayer’s dwelling,
and that his reception there had been a memorable experience.
Immediately the ladies asked if Angus had ‘seen’ anything while the
visitor was there, to which Keith, with a glance at his host, replied
with great suavity that such might well have been the case, since he
appeared, towards the end of the visit, to be entirely withdrawn from
outward events.
“He left the honours to his interesting sons,” he explained with a
smile, “who entertained me so . . . so wholeheartedly that if Mr.
Cameron had not appeared upon the scene I might be there still.”
But at this point Ewen, with a heightened colour, forcibly changed the
conversation.

You might also like