100% found this document useful (1 vote)
9 views

Java: The Complete Reference 12th Edition Herbert Schildt - eBook PDF instant download

The document provides links to download various programming and reference eBooks, including 'Java: The Complete Reference 12th Edition' by Herbert Schildt. It highlights the author's expertise in programming and Java, along with other related titles available for download. Additionally, it includes information about the technical editor and the structure of the book's contents.

Uploaded by

hoglematuziy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
9 views

Java: The Complete Reference 12th Edition Herbert Schildt - eBook PDF instant download

The document provides links to download various programming and reference eBooks, including 'Java: The Complete Reference 12th Edition' by Herbert Schildt. It highlights the author's expertise in programming and Java, along with other related titles available for download. Additionally, it includes information about the technical editor and the structure of the book's contents.

Uploaded by

hoglematuziy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Java: The Complete Reference 12th Edition

Herbert Schildt - eBook PDF install download

https://ebookluna.com/download/java-the-complete-reference-ebook-
pdf/

Download more ebook from https://ebookluna.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Java: A Beginner’s Guide 7th Edition Herbert Schildt -


eBook PDF

https://ebookluna.com/download/java-a-beginners-guide-ebook-pdf/

ebookluna.com

Java: A Beginner’s Guide, Eighth Edition Herbert Schildt -


eBook PDF

https://ebookluna.com/download/java-a-beginners-guide-eighth-edition-
ebook-pdf/

ebookluna.com

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


Programs Today 9th Edition Herbert Schildt - eBook PDF

https://ebookluna.com/download/java-a-beginners-guide-create-compile-
and-run-java-programs-today-ebook-pdf/

ebookluna.com

Tableau 10 Complete Reference (eBook PDF)

https://ebookluna.com/product/tableau-10-complete-reference-ebook-pdf/

ebookluna.com
Mosby’s dental drug reference 12th Edition - eBook PDF

https://ebookluna.com/download/mosbys-dental-drug-reference-ebook-pdf/

ebookluna.com

(eBook PDF) Kelly L. Murdock's Autodesk 3ds Max 2018


Complete Reference Guide

https://ebookluna.com/product/ebook-pdf-kelly-l-murdocks-autodesk-3ds-
max-2018-complete-reference-guide/

ebookluna.com

(Original PDF) Mosby's Dental Drug Reference - E-Book 12th


Edition

https://ebookluna.com/product/original-pdf-mosbys-dental-drug-
reference-e-book-12th-edition/

ebookluna.com

Gomella and Haist's Clinician's Pocket Reference, 12th


Edition Leonard G. Gomella - eBook PDF

https://ebookluna.com/download/gomella-and-haists-clinicians-pocket-
reference-12th-edition-ebook-pdf/

ebookluna.com

(eBook PDF) Technology In Action Complete 12th Edition by


Alan Evans

https://ebookluna.com/product/ebook-pdf-technology-in-action-
complete-12th-edition-by-alan-evans/

ebookluna.com
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


Other documents randomly have
different content
"Well, I can't help that, can I? If you'd been a different sort of woman, I'd
have----"

"You said that before. Had we not better get to business?"

"What business?"

"The insurance business. I don't care for you, and you show very plainly
that you don't care for me. It is useless for us to struggle together like a
couple of ill-matched dogs in leash. Give me fifteen thousand of this
money, and then you can marry your Lola woman."

Jim turned white again. "You seem jolly anxious to get rid of me."

"Can you wonder if I do? How many women would take this scandalous
matter as quietly as I do?"

"It's not scandalous," said Kaimes, fiercely. "She thinks that I am a


bachelor, and I'm not even engaged to her. I have tried to be true to you,
Leah," declared Jim, pathetically.

His wife shrugged her shoulders. It was rather late in the day for Jim to talk
sentiment, besides being a waste of time. "Well?" she asked, facing him
squarely.

Jim read her purpose in a very flinty face. "I'll do what you want," he said
weakly.

"Then there's no more to be said," remarked Leah, coldly, moving towards


the door of her bedroom. "Demetrius will explain, if you will afford him
half an hour's private conversation."

"Leah, do you really mean it?"

"I have meant it from the first moment you put the idea into my head," she
said in a harsh voice. "This underhand love-making of yours only makes me
the more determined."

"But there was no----"


"Don't lie, Jim. A man can no more love two women than he can serve two
masters. Is it to be Lola Fajardo, or myself?"

"I leave it to you, Leah."

"Then I choose the fifteen thousand pounds," she said, and vanished into the
bedroom. Jim took an impulsive step towards the door, but the sharp click
of a turning key showed him that he was locked out for ever.

That evening Leah talked so gaily, and looked so beautiful, that her father-
in-law was absolutely fascinated. "Is it all right between you and James?"
he asked graciously.

"Yes," Leah assured him; "we understand one another thoroughly."

CHAPTER XII

Leah welcomed the New Year at Firmingham, with the fervent hope that its
bounty would bestow the insurance money, and rid her of an official
husband. It really seemed as though Providence, or the fetish, was in a
benign mood, for Jim caught the worst of colds while skating. Being
confined to an undesired bed, and fed with food tasteless to a cultivated
palate, he lost both flesh and temper. Demetrius talked gravely of weak
lungs, and hinted at inherited consumption. The Duke was anxious, but
scarcely surprised, and recalled similar cases of a grandmother, two
ancestors, and a rackety uncle. Lady Jim encouraged these pulmonary
recollections for obvious reasons. She and Demetrius winked privately at
one another like the celebrated augurs, when they heard the old man's
lamentations. Nature was acting strictly on the lines of the Russian's
proposed medicine, and there was no need to dose Jim into a sickly likeness
of Garth. Day by day he grew as white-faced, as haggard, and as lean, until
he became alarmed at the anxiety of Providence to forward the schemes of
himself and Leah.

But there was no end to the kindness of an overruling fate. Jim's illness
afforded his wife the opportunity of posing as a sister of mercy, and she
fussed round the patient so ostentatiously, that the Duke was quite touched.
He began to think that Leah was a true ministering angel, and not the
money-wasting doll he had considered her to be. Jim grinned as Leah
measured medicine, and fed him with gruel, and read him interesting bits
from the sporting journals.

"I believe I'm goin' to get well," he chuckled.

"Why so, dear?" asked his wife, who was profuse of adjectives in private,
so that they might slip out the more easily in public.

"You look so uncommon dismal."

"It is necessary to keep up appearances," Leah assured him. "Besides, this


will be the last chance of my doing anything for you. In future, Lola will
soothe your weary pillow;" after which and similar passages of arms, Jim
would curse himself to sleep, and wake up to accuse his wife of wishing to
poison him.

This fortunate illness kept Lady Jim at Firmingham when the house-party
disintegrated. But as the Duke was a twaddling old ass, and Jim the most
trying of patients, Leah looked upon her ten days' boredom as a kind of
Lenten penance. Besides, she had frequent confabulations with Demetrius,
to settle details of the plot. Already the doctor had explained to the Duke
that Garth would die easier in the tropics, and Funchal had been selected as
the most agreeable place for his demise.

"And then?" asked Lady Jim.

"Your husband must go to Jamaica, to wait events."


"What events?"

"Those which I propose to bring about," retorted Demetrius, who had his
reasons for not explaining himself too fully.

Leah did not question him closely. With a selfish regard for her own safety,
in case anything might leak out, she preferred that the doctor should arrange
matters in his own way. But she obeyed instructions to the extent of hinting
to the Duke that Kingston was the very best place for dear Jim's weak lungs.

"Will you go with him?" asked Pentland, anxiously.

"Oh no," said Lady Jim, sweetly; "we mustn't make too much fuss over
him, else he'll think he's going to die."

"He might," sighed the Duke. "I had an uncle----" and he described the
sufferings of old Lord George for the tenth time.

Leah comforted him after the manner of one Bildad, a Shuhite. "Oh,
Kingston will do Jim no end of good, my dear Duke. It won't cure one lung,
but it may patch up the other. And then, you know, if he gets worse, I can
always reach him in fourteen days."

"Does Demetrius think he will die?" asked the Duke, piteously.

"He doesn't think poor Jim will ever be so strong as he was," said Leah,
gravely; "but he'll hang on, with care."

"Just like my grandmother," muttered the Duke, and then detailed the
sufferings of a dowager duchess, who couldn't be kept alive beyond the age
of sixty.

"If Jim lives till that age, I shall be content," said Leah.

"Are you thinking of the insurance money?" demanded Pentland, with


sudden anger.

"What insurance money? Oh yes, I think Jim did mention something about
an insurance."
"He gets it if he lives till sixty."

"Really! I don't quite understand, Duke, but I'm sure it's all right."

"I hope so, my dear. Has he made his will?"

"No. Why should he?"

"Because, in the event of his dying, the insurance money should be left to
you. No will means trouble."

Leah had never thought of a will, as it seemed natural that the money
should come to her without the necessity of paying lawyers' bills. But her
quick brain seized the chance of smoothing the way to acquiring the fortune
with as little trouble as possible, and she promptly cornered the Duke. "You
speak to him," she suggested.

And this the Duke did, with the result that a will leaving the money to Leah
was drawn up and signed, after some opposition, by Jim. He did not at all
relish the carrying out of this necessary step. It was too like preparing a
death certificate to please Jim.

However, as a reward for his obedience, Demetrius set him on his legs, and
Jim went to Torquay with the devoted Leah. But when he was settled in a
comfortable hotel as an interesting invalid, and with a superfluity of pretty
girls to soothe him with sympathy, Lady Jim left him for a round of visits to
various country-houses. Now that the Duke was out of sight, Jim's
connubial comforts were out of mind; but Leah left strict injunctions that he
was not to put on flesh. Within the month, she was to see him start for
Jamaica, and impressed upon him the necessity of looking quite ready to
depart for a place where Jim had no desire to go.

"I don't see why you want to make a holy show of me," grumbled Jim.

"We must make your death appear as plausible as possible."

"But I don't want to look like a livin' skeleton."


"Oh, I don't think Lola will mind," said Leah, cruelly, and started out to
enjoy herself in the best of spirits.

While at Lord Sargon's seat in Shropshire, she met Askew in the company
of the fixture. The young man's betrothed was extremely like a dairy-maid,
and her frocks set Lady Jim's teeth on edge. If she could combine colours
that did not match, she always did so, and her character was as colourless as
her wardrobe was gaudy. Marjory was the creature's name, and her
conversation was the "Pa-pa!" "Mam-ma!" of a squeaking doll.

"How much are you paying for her?" asked Leah, after satisfying herself
that the young lady was really a woman.

"Five thousand a year," replied the lieutenant, sulkily.

"What a bargain!"

"Don't laugh at me," he implored; "you know there is but one woman in the
world for me."

"So you told me. Lola--what's her name?"

"Some one nearer and dearer than her!" he murmured, with what the
Americans call "goo-goo" eyes, whereat Lady Jim laughed, and allowed
him to fetch and carry, and sit on his hind legs and bark prettily, like a well-
trained lap-dog. It amused her, and kept him on tenterhooks. The only
annoying thing was, that Marjory seemed to care little for this annexation of
her lover. She much preferred a fox-hunting squire, who talked "stables,"
and glowered on Askew for not appreciating the dairy-maid.

In this capture of another woman's man, Leah combined pleasure with


business. She did not wish to spoil Jim's little game with the Spanish lady,
and it would never do for Askew to detail Mr. Berring's past in a quarter
where such betrayal would lead to trouble. By this time the amorous sailor
was the slave of beauty, so Lady Jim was sufficiently mistress of his will to
limit his correspondence. This she did one evening after dinner, while
admiring Marjory's new frock.
"Yellow and green," murmured Leah, when she and Askew filled up a
corner, and watched frantic people playing bridge; "poached egg on
spinach. If you design her gowns, Mr. Askew, I should advise a less lavish
use of primary colours."

"She means well," he muttered, apologetically.

"People who need excuses for existing always do," retorted Lady Jim; "but
she is really a sweetly simple girl, with two ideas, neither of which includes
you, my dear boy. I am sure you will be very happy together, doing cake-
walks."

"Doing cake-walks?"

"That sort of dress always makes me think of South Carolina and the 'old
Kentucky home,' you know. They invented cake-walks there, I believe. But
I forgot--you prefer places below the equator."

"I never think of South America," he protested.

"Of course not. The jewel is more attractive than the casket. When did you
last hear from Señorita Fajardo?"

"I never had a letter from her in my life."

"She is cautious, it seems. Are you?"

"I don't write to her, if that is what you mean. I did love her----"

"What a polite thing to say to me!"

"But I don't any longer. You see, I thought that Berring--your----"

"There's nothing in that," said Lady Jim, quickly. "There never really was,
and if you really love this estancia lady, why not marry her?"

"I am engaged already."

"To me, or to that pretty, vivacious girl over there?"


As Marjory was looking particularly like a wooden Dutch doll at the
moment, Askew reddened.

"I wish you wouldn't say these things, Lady Jim----"

"Lady James!"

"Lady James, then. Marjory can't help herself."

"It seems to me she has--to that intelligent young man with the face like a
sheep and the manners of a costermonger."

"They were boy and girl together."

"And are still, from the infantile look of them. I quite expect to see their
nurse arrive. You know, it won't do," said Leah, gravely; "here I am making
fun of Marjory, and you aren't man enough to stand up for her."

The young man coloured still deeper, and mumbled something about a
woman's privilege. Shortly he made a lame excuse, and left Leah to devote
himself to Marjory, who was not grateful for the attention. Leah did not
mind. She had learned that Askew did not correspond with Lola Fajardo,
and had no intention of doing so; therefore there was little likelihood that
Jim's fettered past would ever become known at the Estancia, San Jago.
Being really a good-natured woman with her affections thoroughly under
control, Leah half decided to loosen her apron-strings and let Askew lead
his bargain to the altar. But this she did not do, for two obtrusive reasons,
firstly, the fox-hunting squire and Marjory were made for one another; and
secondly, it would be just as well to keep the sailor under her eye for the
next year. She did not wish him to hark back to Lima, for melodramatic
purposes.

After a very pleasant visit, thanks to Askew's infatuation, Lady Jim returned
to Curzon Street. There she found a letter from Demetrius announcing that
he and Garth had sailed for Madeira early in the previous week, and that it
would be as well if Lord James Kaimes journeyed forthwith to Jamaica.
Leah promptly sent an answer to her accomplice at Funchal, a telegram to
Jim, a paragraph to a society paper, and a lengthy letter of sorrowful
forebodings to the Duke. Then she sat down to wait events, and, meanwhile,
considered the situation.

Pentland was all right, thanks to her cajoling. Before she left Firmingham
he had arranged to free the income, to pay the debts, and to allow her to
occupy the Curzon Street house until such time as Jamaica should kill or
cure Jim. That interesting invalid had gone halves over the cheque, and
Leah's purse still contained over fifty pounds, which would do for the
present. But she intended to get a few hundreds from the Duke, by playing
off Jim's sickly looks and her own lonely condition of grass-widowhood. It
was really very satisfactory, and she found it hard to look miserable, as in
duty bound, when Pentland arrived to see the last of Jim. Leah arranged that
the parting between father and son should be in town. She did not want to
have a bereaved father bothering at Southampton. The journey back to town
after Jim's dispatch would be boring at the best, and her consolatory powers
were not great.

"You look disgustingly fit," said Leah, when Jim was established on the
drawing-room sofa, with a rug and a few unnecessary medicine bottles, and
other sick-room paraphernalia.

"Sorry I can't be more of a corpse," growled the invalid; "but it's not easy to
pretend you're a goner, when y' feel fit to jump over the moon."

"Try and cough louder," suggested his wife.

"Shan't! It hurts m' throat. Hang it, I've lost three stone. I believe you want
me dead in real earnest."

Lady Jim thought for a moment. "No, I don't," she said, truly enough. "You
haven't treated me over well, and I should have been a different woman, had
you been a different man----"

"Divorce court lingo," said Jim, remembering what she had said at
Firmingham, and with a derisive laugh.

"All the same, I hope you'll have a good time in South America."
"Why not in Jamaica?"

"Because you've got to be thoroughly sick there. Demetrius will come along
later with Garth's corpse, and----"

"Ugh! Drop it! What about the money--my share?"

"I'll get the cash, as soon as you are sent home."

"Me? What for? Ain't I goin' to disappear?"

"Of course," said Leah, impatiently; "but Demetrius has to embalm your
body and bring you home to the family vault."

"I say, don't," cried Jim uneasily; "that's the other Johnny you're talkin'
about. Leah," he looked round cautiously, "I hope Demetrius won't polish
off that poor fellow. He's a sort of relative of mine, y' know."

"Don't worry your head," said Lady Jim, calmly. "Garth's dying as fast as he
can; he may be dead by this time, for all we know. And don't think that I
would allow Demetrius to be so wicked," she cried, with virtuous
indignation. "I'm not a criminal."

"Oh, Lord!" was all Jim could find to say, as he thought of what they were
doing, and conversation ended for the time being. Leah went to the theatre
and supper at the Savoy that evening, leaving Jim to practise coughing
amongst the useless medicine bottles.

Next day, both Pentland and his eldest son arrived at eleven, and were
informed by a sad-faced wife that her dear husband would travel to
Southampton by the afternoon train. At the sight of Leah's dismal looks and
attentive care, Frith expressed his opinion that women were protean.

"Never thought you cared so much for Jim," he said bluntly.

"Oh, I don't for a moment say that I think Jim is a good man," was Leah's
artistic reply; "and we've had our tiffs, like other married people. But Jim's
my husband, after all. And he has his good points."
"What are they?"

Lady Jim was not prepared with a catalogue of her husband's perfections.
"Oh, I don't know," she murmured vaguely; "he drinks in moderation, you
know. That's something."

"There's no virtue in resisting a non-existent temptation," said the Marquis,


grimly. "Jim doesn't come of a drinking family."

"Of a consumptive one, I believe," retorted Leah, softly.

Frith was nettled at the implied slight. "Not at all," he said, with unusual
gruffness. "Look at me."

"But that poor Garth----"

"Oh, he--I don't understand--and if you----" Frith coloured as he met her


derisive eyes, and devoted himself to his brother.

Lady Jim left the affectionate trio together, lengthening out their farewells,
and retired, laughing, to her room. It was really amusing to think that Jim,
who was as healthy as a trout in a pond, should be wept over, and coddled,
and pitied, and generally elevated to a sainthood. The business was serious
enough, no doubt; but Leah could not help seeing the humorous side. She
felt unequal to keeping a grave face while the comedy in the drawing-room
was being played, and therefore did not rejoin her husband till the principal
comedians had departed.

"We are a couple of rotters," said Jim, gloomily, when she appeared.

"Speak for yourself, my dear," she retorted coolly. "Well, and what did they
say?"

"Never you mind. You'd only snigger over a father takin' leave of his dyin'
son."

"Oh! I did not know that the Duke had seen Harold Garth."
"Leah," cried her husband, fiercely, "you're a--never mind. Whatever you
are, I'm another."

"Did the Duke leave a cheque for me?" asked Leah, more business-like than
sympathetic.

Jim banged about among the medicine bottles. "Five hundred."

"Dear man," cried his wife, snatching the cheque from his very reluctant
hand. "I must go and dress for the journey."

"Won't you kiss me, Leah?" quavered Jim, really moved, and quite
forgetting the rascally plot in which he was taking so prominent a part.

At the door she turned with an expression of withering scorn. "Keep your
kisses for your wife, Mr. Berring!" cried this too-previous widow, and left
him to digest the insult at his leisure.

CHAPTER XIII

The paragraph sent by Leah to her pet editor intimated concisely to the tuft-
hunting world of Tom, Dick, and Harriet, that the suddenly developed
pulmonary complaint of Lord James Kaimes necessitated his wintering in
Jamaica. This intelligence surprised the clubs, as Jim's hectoring voice and
devotion to damp field sport had always suggested aggressively sound
lungs.
"Never knew him to be chippy in his life," growled one man, who admired
Leah as much as he hated Jim for possessing her. "What's his game this
time, I wonder?"

"Perhaps he wants to get away from his wife," hinted a pigeon of Jim's
plucking. "Bit of a tongue, hasn't she?"

"Tongue be hanged! She has both wit and beauty."

The pigeon sniggered, knowing the speaker's devotion to Delilah. "Oh,


Kaimes appreciates those qualities--in another man's wife."

"Scandal! Scandal!" murmured a meek member, blessed with a spouse


whose looks prevented temptation. "Kaimes has dined with us many times,
but I never saw----"

"No; you wouldn't," struck in a sporting baronet, whom Leah snubbed on


every possible occasion. "Jim likes red-haired women."

"Then why doesn't he stick to the one he's legally entitled to?"

"Because she sticks to him. If she'd only syndicate her admirers in the D.
C., Jim 'ud be after her like an Indian mosquito in search of a new arrival.
I'll bet there's some petticoat in this Jamaica business;" and the sportsman
looked round for some one to pander to his besetting sin--but no one gave
him a chance of committing it.

Contradiction and argument arrived with the oldest inhabitant of Clubland,


whose memory was as exasperating as his verbosity. "Wrong! All wrong,"
he purred, like the tame cat he had been for half a century. "Kaimes is really
consumptive. I remember his grandmother dying of tuberculosis. It's in the
family, along with gout and water on the brain."

"Oh, bosh! If Jim was sick, he'd sin more judiciously."

"I never knew that damnation depended upon health," was the retort. "Take
a case in point. During the Great Exhibition----"
Leah's admirer cut short a much-dreaded anecdote. "She'll make a lovely
widow."

"I don't believe in second-hand brides myself," said the horsey man,
venturing an epigram. "'Sides, her tongue--cuts like a knife. Even the mares
shy when she kicks."

"Wit! wit!" explained the admirer, who misread French memoirs. "She is
Madame de Rambouillet--without a history."

"Hum! She hasn't published one yet, but I dare say----"

"Tut! tut!" interrupted the ancient. "Madame de Rambouillet was, and Lady
James is, entirely respectable."

"And the horse is the noblest of all animals," snapped the baronet.

"Maybe, though the beast doesn't improve your morals," and the laugh was
with the oldest inhabitant.

"Wonder if Kaimes will die," pondered the man who saw Leah as a
probable widow and a possible wife.

"Lay you ten to five he won't."

"You will lose; you will most assuredly lose," said the octogenarian. "Very
consumptive family, the Kaimes. And our friend is just the sort of healthy
man to depart suddenly."

"Where to?" asked the pigeon.

"Hu-s-s-sh!" droned the meek member; "that's a serious question."

"To Jim!" finished the racing man, smartly; "but I don't care. Jim, dead or
alive, is equally useless to me."

"Oh! He isn't in your debt, then?"


"Catch me trusting him--not much. But what's the use of talking obituary
notices? Let's bridge."

"If your play is as bad as your grammar, I prefer to stand out," said
Methuselah, and the symposium broke up, in time to prevent bickering
between crabbed age and irreverent youth.

There were many such talks during the nine minutes' wonder of Jim's
unexpected sickness, and it was generally considered that he would return
in spirits of wine to the family vault. Leah did not hear these encouraging
prognostications, so conducive to the entire success of the plot. She was
tolerating life at San Remo, under the hired roof of a truly great dame, who
wished to disentangle her from the golden nets of ultra-fast society. A grass-
widow has to be more careful to keep up appearances than the genuine
crape article, even at the risk of being bored by highly placed humanity, as
dull as stainless. Lady Hengist and her friends belonged to that seventh
heaven where newly rich Peris and the Mammons who cocker them seek
admittance in vain. Social laws differ from those of nature, inasmuch as the
gilded scum does not invariably rise to the top. Hence the creation of the
over-discussed smart set, which is taken by the suburban reader of back
stairs journalism as representative of the British aristocracy.

Lord Hengist came of an autochthonous family which had been at home


when William the Conqueror raided the ancestral cabin. His wife was
descended from a knight who emigrated from Normandy in 1066, with
apparently several million others, judging by the claims put forward by
those who enter the peerage. This alliance--they were too great to talk of
mere marriage--resulted in two children, not made of ordinary clay, but
compounded of the superlative porcelain sort. Their parents possessed a
genuine mediæval castle, as uncomfortable as the builders knew how to
make it, and which had the rare distinction of possessing a state-bedroom in
which Elizabeth had never slept. The family archives read like the Book of
Numbers, and their ancestors had made history at opulent wages for the
benefit of the Hengist coffers. The men had sided with the Stewarts and
ratted to the Guelphs; the women bloomed in Lely and Kneller portraits in
loosely slipping clothes, with pastoral accessories; and finally, the present
head of the house, with four seats, two children, a charming wife and a
large income, lived comfortably on the loot of ages. Of all these things Lord
and Lady Hengist were so proud that they had no need to exhibit pride.

Well-born as Leah Kaimes was, the pleasant, if somewhat stately and stiff,
life of these genuine rulers wearied her intensely. Bread and milk is insipid
after a repast of ortolans in aspic, and a motor-flight is more exhilarating
than a donkey-ride. Moreover, it annoyed her to see how sensibly the
Hengists spent their many pounds a day. They could have had much more
fun for the money, had they known the right shops; but they patronised out-
of-date establishments, where the goods were of an excellent quality, but
just five minutes behind the newest things. Of course, this was Leah's
figurative way of saying that the Hengists came out of the Ark. They always
bought the wrong things at the wrong shops, and had a middle-class eye to
the lasting quality of the goods they purchased. They were clothed rather
than dressed, and being colour-blind, invariably chose garments which
matched abominably with their complexions. In a word, the Hengists were
so commonplace as to be original. Lady Jim could not understand why they
should have been thrust into positions which they could not fill. It was like
bringing cows into the drawing-room.

"It's so hard for me to taste the pleasures of self-denial," complained


Hengist, one day, as they sauntered on the promenade.

"I don't think it is wise to attempt the extraction of sunbeams from


cucumbers," said Leah, dryly.

"Dean Swift said that, but he was an egotist," replied Hengist, in his serious
way, that reminded Lady Jim of Lionel at his worst. "It is more blessed to
give than to receive, you know."

"Is it, indeed? Who said so?"

"The wisest and most loving of mankind. And it is a true saying. I assure
you, that if I deny myself something I greatly desire, and send the money
which would have purchased the gratification to a charity, I feel absolutely
happy."

"I don't think I ever tried that experiment."


"You will not know true happiness till you do, Lady James."

"Then I must make a bid for Paradise," she answered, privately thinking
that the man talked sad nonsense.

"It's a dreadful thing to be able to have the moon for the asking," went on
Hengist, reflectively.

"That's your epigrammatic way of putting it, I suppose; but the moon won't
drop from her sphere for me, howl as I may. You are very lucky to
command the planet, Lord Hengist."

"So the world thinks, but it forgets that there is the curse of satiety."

"Is there? I never knew it existed. I only wish I could cram the twelve hours
of the day with twenty-four of pleasure."

"Have you ever had everything you wished for, Lady James?"

"No!" said Leah, promptly. "I'd have the sun as well as the moon, and the
stars thrown in, if I had my way."

"Only to be bored by the acquisition of the lot."

"Me bored--oh dear no! I am too stupid. It is only clever people like
yourself who suffer from ennui. I only wish I were a Roman empress, with
provinces for a dowry. Those dear women knew how to live."

"But in the majestic pages of Gibbon----"

"Who? Oh, that man who came to think he was the Roman Empire. Now
his work would bore me--I'm not stupid enough to appreciate him."

"Julia"--this was Lady Hengist--"Julia and I read Gibbon during the


honeymoon, and received much instruction."

"Oh, Lord!" said Lady Jim; "as though honeymoons were not disagreeable
enough without that!" The idea made her laugh consumedly. In her mind's
eye she saw this new Paolo and Francesca reading heavy prose in ten
volumes. But Hengist did not even smile--he had absolutely no sense of
humour. Besides, he considered his companion's chatter painfully frivolous,
and sighed to think that she had such a light nature. Leah, still laughing,
glanced sideways. "I shall begin to think you are discontented, Lord
Hengist."

"I am, that I cannot do the good I should like to do. Both Julia and I wish to
benefit mankind."

"The twelve labours of Hercules, with no thanks for their accomplishment."

"We don't want thanks, but results," said Hengist, austerely; "and we can
commence in a small way. Next summer we intend to invite five hundred
Whitechapel children to the Castle. Will you come and help us to entertain
them, Lady James?"

"Delighted," yawned Leah, for the man spoke like a copy-book; "but I hope
you'll wash them first. It will prevent disease, and give some new soap a
philanthropic advertisement."

Hengist eyed her suspiciously. He was a very, very dull young lord, large-
hearted and unintelligent, who took life so seriously that he had almost
forgotten how to laugh. England clean, England contented, England happy.
He constantly started crusades to bring on a premature millennium, and
earned his reward, after the manner of reformers, by being abused in
halfpenny newspapers as one who attempted to avert certain revolution, by
stuffing the starving with sweets. Lady Jim thought him a bore and a prig,
and too virtuous to be amusing. But that he and his wife were of use to her,
she would not have endured this presentation of his year-before-last's Tree-
of-Knowledge apples. He never plucked fresh fruit, and his Eve was quite
as blind as he in discerning up-to-date harvests. Still, Hengist was a sort of
bell-wether, leading a flock of prize sheep towards a closely guarded fold.
Leah liked the fun and money and adulation of the smart set, but she had no
notion of being a shut-out Peri from that dull paradise that the newly rich
longed for. Besides, its very dullness gave a fillip to her enjoyment of the
larky amusements of those who could not enter the sacred ark.
"I am really very fond of children," she said, to do away with the effect of
her last remark. "I wish I had some myself," and she sighed very prettily.
"Hilda Frith is more fortunate than I, with her two dear babies."

"Both girls. I fancy Frith would like a son and heir."

"I'm sure he would, and both Jim and I would be the very first to
congratulate him."

"Your husband is next in succession?"

"Yes, poor dear! But Frith is strong and healthy, while darling Jim--oh, I
can't bear to talk about it."

This was perfectly true. To invent sentimental domestic histories and bewail
a husband she detested was difficult, even to a woman of Leah's
imagination and tact. But Hengist thought it was very good of her to talk so
generously, and paid her serious compliments till she began to think that
some unpardonable sin had thrown her into the society of this prosing
creature. It was like reading the dictionary, or drinking Homburg waters, or
paying bills. The sight of a friend made her gasp with relief, after the
manner of a pearl-diver rising to take the air.

"Here's Lady Richardson and Sir Billy," she said with a frown, for her
companion's benefit. "So horrid, to interrupt our nice conversation!"

"We can pass them," replied Hengist, decidedly pleased.

"Oh, I don't think so," was Leah's quick reply. "It would look rude; and
then, Fanny Richardson never passes any one who will listen to her prattle
of chiffons. Besides, Billy is a nice boy--quite a little man. Don't you think
so?"

"Too much a man for his years," said her companion, austerely. "I do not
like Chesterfields in their teens. The lad's manners are too good--much too
good."

"Can any child be much too good?"


"In the wrong way of over-artificiality, yes. Sir William----"

"He likes to be called Sir Billy!"

"So flippant. His mother should insist----"

"She! She never insists on anything, except having the newest dye and the
best-cut frock, and a few dozen male ears to pour her babble into. Billy can
do no wrong in her eyes, nor in mine. He is such an admirer of women."

"And at the age of thirteen," groaned Hengist.

"Come now, even you must have made love to some pretty pastry-cook's
daughter when you were at Eton. There must be some of the old Adam in
you, Lord Hengist."

"I was never an entirely modern child," replied the serious man, evasively,
and with a sad eye on the trim figure of the rapidly approaching Billy. "To
think that he should take dinner pills, and know the difference between
sweet and dry champagne! What will the next generation be?"

"Boys and girls," said Leah, flippantly. "Good day, Fanny."

The vivacious little fairy who warmly greeted Lady Jim and her solemn
escort was as pretty and fragile and dainty as a Dresden china shepherdess,
and quite a credit to the maid who re-created her every morning. There was
nothing natural about her, save her genuine adoration of Billy, and that
arose from a knowledge that royalty had made it fashionable to exploit the
nursery. Blonde and plump, jimp and graceful, dressed in perfect taste, and
coloured in the latest fashion, she was popular even with her own
discriminating sex. Hengist thought her a respectable doll, with no
particular vices, and did not object to having her at the Castle. But he
disapproved of Billy the precocious, which was decidedly unfair, as Billy
could scarcely help shaping himself to the mould into which he had been
slipped by a mother who required his assistance to play the pretty comedy
of the widow's only son.
"How are you, Leah darling? So sweet you look, and Lord Hengist too. A
most unexpected meeting, and so delightful," babbled Lady Richardson,
who talked more and said less than any society gramophone. "Billy and I
are just going to Monte Carlo, to plunge on the red. Reggy Lake is to meet
us at the station; such a nice boy--Lancers, you know--a great chum of
Billy's. Won't you come too, Leah, to brighten Billy up? He's got the hump,
poor boy, as his new nerve-tonic doesn't suit him, and such a lovely, lovely
day as it is too. Don't you think so, Lord Hengist?"

The respectable Hengist's hair bristled at this incoherent speech, and did not
lie down again at the look in Billy's eyes. Dressed in a particularly smart
Eton suit, gloved and silk-hatted and patent-leather-booted with fashionable
accuracy, the boy appraised Lady Jim's beauty in a calm way, which would
have made a captain of dragoons blush. Behind his graceful, nonchalant,
handsome mask of youth was hidden an old, old man, and in many ways
Hengist was his junior. He certainly blushed when Leah gave him an
amused glance, but this was Billy's way of mashing the sex. He knew the
value of youthful diffidence, backed by mature knowledge.

"Should not your little boy be at school?" asked Hengist, scandalised into
an implied snub.

Sir William looked at the troubled face of his elder with the serenity of a
cherub. "Goin' back nex' week," said he, carefully dropping his "g's." "Th'
little mother wanted me to look after her for a bit."

"Billy can't trust me out of his sight," giggled Lady Richardson. "He's so
afraid I'll give him a second father."

"Not Reggy Lake, anyhow. He's a rotter!"

"What's a rotter, Sir Billy?" asked Lady Jim, enjoying the disgusted looks of
Hengist.

"A fellow who rots."

"What an admirable definition?"


Billy rapidly dropped his left eyelid, and showed a set of white teeth. "I
don't carry coals to your Newcastle," he said parabolically. "Say, Lady Jim,
chuck this chappy, and come to Charlie's Mount."

The wink and the speech were lost on Hengist, for he was being worried by
Lady Richardson. She danced before him, a pretty figure gowned in burnt-
almond red, and would have distracted his heart with daintiness but that
Julia kept that article in the nursery.

"Do join us, Lord Hengist," she pleaded seductively. "Such fun, when you
know the ropes. Billy can show them to you."

"Out of the mouth of babes and sucklings," quoted Hengist, ironically.


"Quite a new reading, Lady Richardson."

"Now you are horrid," said the widow, who did not know in the least what
he meant. "I'll tell your wife. By the way, how is she, and the darling,
darling twins? Twins are too sweet. I wish Billy was a twin."

"One of Sir William is quite sufficient."

"I'm sure I don't know what you are talking about, and it's very horrid of
you to say so. Billy is adored."

"Is he ever whipped?"

Lady Richardson gave a scream. "How barbarous! The man who tried to
whip Billy would have to order his coffin beforehand. Billy can handle his
bunches of fives, I can tell you, Lord Hengist."

"His what?"

"It's Billy's way of putting boxing. You should see him give the postman's
knock! Oh, he is clever! He can drive a motor, too, and pick out the winner
five times out of ten."

"Does he know the kings of England?"


"No; he hasn't been to Court yet, and of course, there's only one. How funny
you are! Well," Lady Richardson put her head on one side like a coaxing
cock-robin, "are you coming with Billy and me? Do, oh do! We have
afternoon tea with Monsieur Aksakoff and his daughter."

"What's that?" asked Leah, overhearing the names; "the Russian man?"

"Stiff sort of fella'!" said young Eton. "Nothin' birdish about him.
Daughter's a clipper, though. Say, little mother, we'd best get. Th' train won't
wait, y' know."

Before he had finished speaking Lady Jim had made up her mind. She had
not heard from Demetrius, and it was not impossible that he had written to
Katinka. In spite of his discouraging love-making he kept in with her, on
the chance that she might be able to procure his pardon, and in any case she
was useful in keeping him posted in the doings of the Third Section. The
girl was so infatuated that she never saw he was making use of her in this
way, and constantly wrote to him about any official gossip she heard. There
was something pathetic in her devotion and heart-whole love for the man
who deceived her. But Leah did not look at the matter in this way. She knew
that Katinka, if any one, would have news of the doctor, and being anxious
to learn how Garth was progressing towards the grave, she turned to
Hengist.

"I think I'll go over," she said in a low voice. "Jim asked me to see M.
Aksakoff on some business. Would Julia mind?"

"Not at all," said Hengist, heartily, and quite deceived. "I would escort you,
only I have some letters to write about the distress in London."

"Oh, Billy will look after us," said that young gentleman's mother.

"I have driven a team before now," observed Billy, with dignity.

I Hengist gave him a reproving look (which Billy bore very stoically), and
whispered to Leah as they parted, "Don't encourage that lad."
"I don't think he needs much encouragement," said Lady Jim, laughing, and
the two women walked away with Billy between them. Hengist stood where
he was and frowned.

"Charming woman, Lady James," he murmured, gazing after Leah's


amethystine gown; "but that lad--ugh!" He shook his head over young
England up-to-date; then returned to the villa to hear the twins say the
alphabet. Life had its compensations, even for a millionaire peer.

CHAPTER XIV

After the happy-go-lucky fashion of Italian officialism, the train was


detained for some time at Ventimiglia. Lady Richardson, unsettled as a fly,
changed her seat five times, and complained garrulously.

"Captain Lake is so very particular," she explained, producing a pocket-


mirror and a powder-puff to repair possible damages. "He can't bear to be
kept waiting five minutes."

"Then I should make him wait five hours," replied Leah, calmly. "It doesn't
do to spoil men."

"You spoil me," said Sir Billy, audaciously.

"Pooh! You are merely a rascal in the making. I wouldn't hint how we
govern your sex, if you were anything but a grub."

The boy laughed complacently. "I'm a very nice grub."


"Very precocious, at all events. You know much more than is good for you.
Fanny, you should whip him."

"I haven't the heart or the muscle, my dear. The only safe thing will be to
marry a strong man with a bad temper."

"I should jolly well like to see the stepfather who would pitch into me."

"You will, if you don't behave. Isn't that eyebrow a little crooked, Billy?"
and she fingered it delicately.

"Don't think so; but you have a smudge of powder on your chin."

"So I have. How horrid! There!" dusting it off. "What a comfort you are to
your darling mammy, my own! Kiss me."

Billy brushed her rouge with careful lips, and after a glance to see that he
had not blurred the picture, Lady Richardson put away the mirror.

"Thank goodness, we're moving again," she prattled. "I do hope Reggy
won't be in a bad temper."

"I'll square that, little mother. Been to the theatres lately, Lady Jim?"

"No," answered Leah, amused by his man-about-town air. "Is there anything
good on?"

"Awful stuff," announced Billy, with the conviction, of mature judgment.


"Couldn't sit out more than two plays. The Woman with Three Husbands
isn't bad, though. Very French, of course. Saw it four times before I told the
little mother she couldn't face it."

"How alluring! Will you take me?"

Billy was obviously shocked. "No woman should see that piece. I can stand
heaps, but----" an after-me-the-deluge shrug hinted at the degradation of the
drama.
"Yes, poor darling," chimed in his mother; "he was blushing three inches
deep all over when he came home."

"I am glad to hear that Billy can blush at all," murmured Lady Jim. "How's
the betting, William?"

"Tolerable! I pulled off a fiver on Fly-by-Night; but a man in my form lost a


tenner, silly juggins."

"Oh! How old is that man?"

"Sixteen, and thinks he's twenty. Awfully saucy chap though. Went nap on a
girl, and another fella' scooped th' pool."
"Don't they teach English at Eton, Billy?"

The youth was quite undisturbed. "Try to," he assured her; "but there's no
snap about the classical rot they give us. Oh, here we are."

"And there is Reggy," cried Lady Richardson, craning her dyed head out of
the window like another Jezebel. "How d'y do, Captain Lake? Lovely day!
So sorry we're late. You know Lady James Kaimes?"

"I have that pleasure," said the tall young soldier, saluting. "Very sorry to
hear your husband is ill, Lady James."

"Thanks! But I daresay Jamaica will pull him round, Captain Lake."

"Hope it won't," breathed Billy, at her elbow, as the lift soared.

"Why, you horrid little boy?"

"There'll be a chance for me."

"No, no! You're too much of a general lover, Billy."

"Girls do run a man so hard, nowadays," observed Billy, pathetically.

"It was different in your youth, no doubt. But I am not a girl, and quite old
enough to box the ears of conceited urchins."

"Do!--if you'll let me give you a kiss for a blow."

"What precocious Christianity! You had better apply to that pretty American
girl near the Casino door."

"Miss Mamie Mulrady? Oh, I can get her kisses without fightin'. Not bad-
lookin', is she? Lots of tin, an' as spry as they make 'em. There's th' little
mother an' that rotter chippin' into th' Casino. Shall we follow, Lady Jim?"

They were stopped on the steps by Miss Mulrady, "who knew both, and
claimed acquaintance through a wholly unnecessary lorgnette. She was a
vivacious Wild West product, who exaggerated the vernacular, because
Europeans expected to find the Californian girl of fiction in real life. Her
exaggerated slang was assumed out of sheer amusement, and she greatly
enjoyed the amazed looks of those who heard her talk good Anglo-Saxon,
which she did, when she escaped from fools to forgather with wise men.

"How are you, Miss Mulrady?" asked Billy solemnly.

"Keepin' afloat, I guess, but that's about all. The dollars I've lost buckin' the
tiger would have bought me a dozen husbands."

"Foreign ones are cheap, I believe," said Leah, admiring the prairie-flower's
Paris frock more than her republican manner.

"You make me smile. I'm goin' to run tandem with Sir Billy here--me first
and he the wheeler."

"No go," said the boy, quite able to hold his own. "I'm not goin' to marry a
Bret Harte girl."

"Oh, do," replied Miss Mulrady, in the purest of English, and placing two
small gloved hands together. "I'll be a wife and a mother in one."

"What economy!" smiled Lady Jim. "Are you coming into the 'devil's
parlour'?"

"Later. I'm waiting for Mr. Askew."

Leah started. She thought that Askew was safe in Shropshire, making
attempts to civilise the fixture. "Harry Askew?"

"That's so," assented Miss Mulrady, relapsing into her Wild West
vocabulary, and with a keen look. "He called on Mommo an' me, when he
was cruisin' out 'Frisco way. We're negotiatin' a system to break this old
bank."

"You evidently wish to be popularised in a song," said Lady Jim, languidly.


"How long has Mr. Askew been devoting his energies to such things?" This
with an angry reflection that he had not called on her.
"You might reckon it twenty-four hours," said the American, admiring her
pointed brown shoe. "He's here for his health."

"I've heard that excuse before, with regard to Monte Carlo."

"Shouldn't wonder. We ticket our sins best sugar. Sir Billy, come along an'
buy me candy at the stores."

"But your man, Miss Mulrady--the Askew chap?"

"Lady Jim an' I 'ull swap humans. What say?" and she looked at Leah,
mischievously overdoing the slang.

"I never swap what isn't my own property," answered Lady Jim, considering
this offer too Western, and resenting the familiarity to the extent of walking
into the Casino with her head very much in the air. America could hold her
own with the mother-country, and Leah did not approve.

"She wants to be the whole show an' the box-office," murmured Mamie,
mischievously. "Stay here, Bub."

"I am sorry to refuse a lady," replied Billy, resenting the word; "but I've put
my money on Lady Jim, this trip."

"On the red--hair, you mean. Go bye-bye with your nurse, then. Here's Mr.
Askew, he's older than you."

"And easier to please," snapped the youth, much offended. "You'll excuse
me, Miss Mulrady, but a man can't keep a woman waitin'."

He retired into what Lady Jim called the "devil's parlour" with a Floreat
Etona air, and Miss Mulrady, after a glance at the ears which she longed to
box soundly, turned to receive a breathless apology from the belated Askew.

"There's a friend of yours gone in to sin for an hour," said she, when a treaty
was concluded.

"I have so many friends--so-called."


"Of the high-toned gilt-edged sort, with red scalps?"

Askew comprehended in a second. "Lady Jim," he stammered; "yes, I heard


that she was at San Remo. What's she doing here?"

"Visitin' the sick an' the poor," said Mamie, shrewdly. "It's what folks come
to Monte for. Guess, she best drop in on you--a sicker man I never saw, an'
you'll be poor enough by th' time we're through with this old system of
yours. I know a bank where th' wild time goes. You may look all through
Bacon without findin' that remark--it's my own. Let's get."

Thus, with barbaric japes, did the child of nature lead her companion into
the gilded halls of iniquity, and the two jostled the well-dressed crowd
which circulated round the tables. The silence was that of an arctic night,
save for the droning voices of the croupiers, and at times a hurried whisper
of joy or dismay.

"Goin' in for rouge et noir with Lady Jim?" asked Miss Mulrady, alluding to
the hair of Askew and his friend; "or perhaps she's sportin' on trente et
quarante, to suit her years."

"She's under thirty," growled Askew, crossly.

"An' you're under the weather, considerable," retorted the American,


sharply. "Get up steam an' fizzle a bit, can't you?"

"Shall I war-whoop, or dance a horn-pipe?"

"Neither I prefer originality."

"Try the system, then;" and Askew pushed his way through the Mammon-
worshippers to where the roulette ball wheeled its fatal round.

Lady Jim did not play. She had stupidly forgotten her peacock's feather and
could not risk loss with her small capital. But Billy, having the audacity and
luck of innocence, was at hand, so she gave him five hundred francs to
experiment.

"We'll halve the winnings."


"Never take money from a woman," said Billy, gravely; "but I don't mind a
fly. Got any sportin' number?"

"Thirteen, because that's your age. There is Mademoiselle Aksakoff, I wish


to speak to her;" and she moved gracefully towards the tall, pale girl, while
Young Iniquity, with the air of a Vanderbilt, planked her money on the odd
number.

Katinka Aksakoff grew crimson when Lady Jim saluted her, and would
have evaded the meeting if possible. She might have been a nun from the
looks of her, and was garbed in unrelieved black, which Leah concluded
was mourning for unrequited affection. After that fleeting wave of colour,
her thin, oval face grew marble white, and a pair of dark questioning eyes
appeared twice as large and three times as brilliant as they had been before
resting on Lady Jim's gracious smile.

"So glad to meet you," murmured Leah, as they shook hands in the air.
"Lady Richardson and I have come to tea. Where is your father?"

"He is talking with the German ambassador," replied Katinka, without a


smile, and with Siberian coldness.

"So fortunate. We can chat without interruption."

"I scarcely think we have much to chat about."

"Oh yes," rejoined Lady Jim, with perfect good-humour. "When you learn
how you misjudge me, we shall get on capitally."

"Pardon. I do not understand."

"Probably not, since I have yet to make my explanation. Let us walk on the
terrace, and you can throw me over, to where they shoot the pigeons, if my
conversation displeases you."

"Ah, but it is so strange!"

"And so necessary--to your peace of mind."


"No!" Mademoiselle Aksakoff's face grew scarlet once more, and she
pressed her hand to her heart, as though she felt there a cruel pain. Perhaps
she did, poor soul! But the stoicism of the Slav enabled her to summon up a
wry smile, and to bow her head, as she followed her brilliant rival. With the
excess of an ill-governed, passionate heart did she hate this woman; but as a
Niobe, frozen and cold, did she appear when they were pacing the terrace.
And not one single word of her companion's sugared speech was she
prepared to believe.

Leah's eyes rested appreciatively on the varied beauty of God's work and
man's improvements. The huddled white houses of Monaco crowned its
giant rock, which bulked hugely against the blended azure of sea and sky.
The placid waters ringed its base with foam, and stretched with sparks and
dashes of fire towards an immeasurable horizon. Landward bunched the red
roofs of the town, below arid and precipitous heights, soaring massively
into the radiant and ever-deepening blue. A balmy wind, like some invisible
alchemist, changed the sombre green of the olive-groves to patches of
glittering silver. Near at hand spread the lustrous foliage of lemon- and
orange-trees, nor was wanting the almond-blossom of the far east. They
walked under palms suggestive of Bedouin life, and, to the well-read, of
Heine's sad little song, immortal and heart-rendingly true. Roses and
violets, and flowers of many shapes and hues, bordered the terrace; the
wide sea laughed at their feet, and behind them rose the palatial structure of
the Casino, gorgeous as the Golden House of Nero. It was Fairyland, and
Lady Jim said so to her sad companion, who was too blinded by love to see
beauty anywhere when the beloved was absent.

"We can talk in French, if you like," said Leah, after she had paid her tribute
to nature.

"In English, I think," replied the Russian girl; "my father wishes me to
speak only your tongue, while we remain in London, so that I may
improve."

"You can't," answered Leah, genuinely complimentary. "Your accent is


much better than a born English person; also your grammar, and your
choice of words."
"We take the trouble to learn your language, whereas you English do not."

"We're too busy annexing the world to bother about philological lessons,"
said Lady Jim, remembering Heine's remark anent the Romans.

"Possibly," assented Katinka, with a chilling smile; "but, interesting as this


conversation is, I do not see its necessity."

"Monsieur Demetrius," began Leah, abruptly, when Mademoiselle raised a


protesting hand.

"We need not speak of him, madame."

"Why not? He is a mutual friend. I know you fancy----"

"I fancy nothing," interrupted the other, haughtily. "Words are not needed
where he is concerned."

"But explanations are. You think that I love Demetrius!"

Katinka flushed painfully, and she put her hand suddenly to her throat.

"I forbid you to speak," she said, in a stifled voice.

"Nonsense. We are not in Russia, where people kneel down and say please.
Besides, it is necessary for your peace of mind that you should hear what I
have to say."

"You made that remark before, Lady James."

"True, and I make it again, to emphasise my meaning, though I hate


repetition. Demetrius loves you."

"No, no! It is you who----"

"Pish! His heart is yours; his science mine."

"His science!" Mademoiselle Aksakoff looked surprised.


"What else do you think attracted me? I am an English cat, and I have no
lovers. Do you remember La Fontaine's fable?"

"Lady James, be plain with me."

"I am trying to be. You think that I love Demetrius, and that he is devoted to
me. It is not so."

Katinka winced. She did not like such plain speaking, and, moreover,
doubted its truth. "If I could think so, I would----"

"Of course you can think so," said Lady Jim, amiably. "Demetrius is
particularly clever in curing consumptive diseases. For that reason I
conversed with him a great deal. My husband is very ill, and I wanted the
doctor to cure him. If Demetrius thought that my liking for his society
meant anything else, he is an egotist. My advice is, that you should procure
his pardon and marry him."

"There are obstacles in the way."

"I am not one, I assure you."

"Are you speaking honestly?"

"I am!" and the eyes of the two women met. Katinka searched the hard blue
orbs of the great lady with painful intensity, and Leah bore the scrutiny with
the knowledge that her conduct had been, and always would be, perfectly
correct. Had she been the least in love with the doctor, she would not have
dared to submit to that probing, painful gaze. Women may deceive mere
men; they cannot deceive one another, especially in affairs of the heart.
When Katinka withdrew her eyes she was satisfied that Lady Jim cared
nothing for Demetrius. Without explanation, she burst into rapid and
wrathful speech, and Leah's feminine perspicacity enabled her to guess the
unuttered preamble, which a man would have required to be put into words.

"Why then do you lure him to your feet?" cried the Russian girl, in a sharp,
pained voice. "If you love him not, why torture him, and me? I know he
loves you--I know--I know--oh yes, I know."
"You do not. His love for me--if it can be called so--is the mere passing
fancy of a man for a woman who has been kind to him."

"Too kind," muttered Katinka, vengefully.

"Not at all. But men are so conceited that they think a woman's smile means
a woman's love. You have a golden heart, yet you throw it into the greedy
hands of this selfish egotist----"

"He is not that," gasped the girl.

"Yes, he is, and much worse. Demetrius possesses the selfishness of a


woman and the vanity of a man."

"You reverse the proper order."

"No, I don't. Men are far vainer than women, and women more selfish than
men. I'm selfish myself, therefore I am happy. You are one of those self-
tormenting, self-denying angels, who make men what they are--vain,
greedy, conceited, lord-of-creation beasts. And I insult the beasts by such a
comparison."

"I thought you liked men."

"I use them, and I detest them," retorted Lady Jim, speaking more plainly
than was her custom. "There are good men--I don't deny that, for I know
one at least"--she was thinking of Lionel; "but the majority--ugh! God help
the women like yourself, who give their hearts into the keeping of such
animals!"

"You love your husband, surely."

"We all love our husbands--it's part of the Church Service to love them.
Pah!--I am not here to talk of my marriage, but of yours. You know now
that I don't care for Demetrius, and that I desired his help merely for my
husband's sake."

"Yes. I have wronged you;" and Katinka put out her hand.

You might also like