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

Java: The Complete Reference Herbert Schildt instant download

Ebook

Uploaded by

rantsicargua
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
39 views

Java: The Complete Reference Herbert Schildt instant download

Ebook

Uploaded by

rantsicargua
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Java: The Complete Reference Herbert Schildt pdf

download

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

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Java: The Complete Reference (Complete Reference


Series) 13th Edition Schildt

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

Java: The Complete Reference, Twelfth Edition Schildt

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

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


Programs Today Herbert Schildt

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

DK Eyewitness Prague 2020 Travel Guide 2nd Edition Dk


Eyewitness

https://ebookmeta.com/product/dk-eyewitness-prague-2020-travel-
guide-2nd-edition-dk-eyewitness/
Unexpected Addition an over the top forbidden professor
and student age gap instalove romance 1st Edition Karla
Doyle Flirt Club

https://ebookmeta.com/product/unexpected-addition-an-over-the-
top-forbidden-professor-and-student-age-gap-instalove-
romance-1st-edition-karla-doyle-flirt-club/

Get Rich with Dividends A Proven System for Earning


Double Digit Returns 3rd Edition Marc Lichtenfeld

https://ebookmeta.com/product/get-rich-with-dividends-a-proven-
system-for-earning-double-digit-returns-3rd-edition-marc-
lichtenfeld/

Northwind 1st Edition Paulsen Gary

https://ebookmeta.com/product/northwind-1st-edition-paulsen-gary/

What Makes Humans Unique Evolution and the Two


Structures of Mind 1st Edition Michael Robbins

https://ebookmeta.com/product/what-makes-humans-unique-evolution-
and-the-two-structures-of-mind-1st-edition-michael-robbins/

Cambridge IGCSE Chemistry Coursebook with Digital


Access 2 Years Cambridge International IGCSE 5th
Edition Harwood

https://ebookmeta.com/product/cambridge-igcse-chemistry-
coursebook-with-digital-access-2-years-cambridge-international-
igcse-5th-edition-harwood/
British Coastal Weapons vs German Coastal Weapons The
Dover Strait 1940 44 1st Edition Neil Short

https://ebookmeta.com/product/british-coastal-weapons-vs-german-
coastal-weapons-the-dover-strait-1940-44-1st-edition-neil-short/
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


Other documents randomly have
different content
“No, you haven’t;” and here he opened the door in the side of the
chimney, and let her look in.

“Why, what in the world is this for?”

“This is a smoke-house; you see it’s on one side of the chimney,


so that there won’t be heat enough go in there to melt the hams or
fish. All you have to do, when you want to smoke anything, is to
hang it up on these lug-poles, and the common fire you have every
day will smoke it. It’ll be a nice place for Ben, when he has an ox-
yoke, wooden bowl, or shovel to season or toughen. Now I want you
to see the cellar.”

He pulled from his pocket a horn filled with tinder, and striking a
spark into it with a flint and steel, kindled a piece of pitch-wood, and
they went down.

“O, my! if here isn’t an arch; what a nice place that will be to keep
my milk, when I get it.”

“Now we’ve got a light, let’s look into the oven.”

“I know that oven will bake well,” said Sally; “it looks as though it
would. Now, I think this is a real nice place, and that Ben has made
a good trade; and, if we have our health, we can pay for it well
enough. Only think how much we’ve saved by living in this house,
which is good enough for young folks just beginning, and better
than many have. Why, it ain’t a month since the trees were growing,
and now it’s all done. Didn’t he make a good trade, Uncle Isaac?”

“He made a better one when he got you, you little humming-bird,”
said Uncle Isaac, who was brim full, and could no longer restrain
himself; patting her on the head, “you would suck honey out of a
rock.”

“I’m much obliged to you, you good old man. I’ll tell you what
we’ll do (that is, when we are able); you shall come over here with
Aunt Hannah, and bring all your tools, and we’ll part off the front
rooms, and have a front entry, ceil up the kitchen, have Uncle Sam
to build fireplaces in the front rooms, and Joe Griffin to make fun for
us. I’ll make you some of those three-cornered biscuit and custard
puddings you like so well. In the evenings we’ll have a roaring fire;
you can tell stories, and we will sit and listen, and knit. Ben says this
is the greatest place for gunning that ever was; and you can bring
on your float and gun, and you and Uncle Sam can gun to your
heart’s content. Ain’t I building castles in the air?” cried Sally, with
another laugh, that made the house ring; “but we must go off, or we
shall be caught.”

A little breeze had sprung up, and Uncle Isaac putting up a bush
for a sail, they landed on the other side without detection.

He said he never wanted to tell anything so much in his life, as he


did to tell Ben how much Sally was delighted with the island; but he
resolutely kept it to himself.

As it would be difficult getting off in the winter, Ben carried on


provisions, hay for a cow, and for oxen that he might get
occasionally. He put the hay in a stack out of doors. He bought the
hay of Joe Griffin’s father, and Joe was to deliver it on the island.
Being disappointed in respect to the man who was engaged to help
him, he took old Uncle Sam Yelf, as better than nobody. There was a
long easterly swell; the scow rolled a good deal, and, the hay
hanging over the side and getting wet, she began to fill. At some
distance from them Sydney Chase and Sam Hadlock were fishing.
“Shall I holler, Mr. Griffin?” said Yelf, who was terribly frightened, and
had a tremendous voice.

“Yes.”

“What shall I holler?”

“Holler fire.”
“Fire! fire! fire!” screamed Yelf.

As their neighbors rowed up, they could not help laughing to see
two men up to their waists in water, and one of them crying fire.

“I thought,” said the old man, “I’d holler what I could holler the
loudest.”
CHAPTER XVII.

THEY MARRY, AND GO ON TO THE ISLAND.

The wedding was at the widow Hadlock’s; but Captain Rhines


made the infare, as ’twas called,—which was an entertainment given
the day after the wedding at the house of the bridegroom. To this
were invited all who had aided in building the house, including the
girls who prepared the victuals; and a merry time they had of it.

It was very hard for Sally and her mother to part. Since the death
of her father, and while the other children were small, Sally had been
her mother’s great dependence; and, as they came to the edge of
the water, the widow lifted up her voice and wept.

Sally, with her eyes full, strove to comfort her mother.

“Well, I ought not to feel so, I know; but it sort o’ brings up


everything, and tears open all the old wounds. May God bless you!
you’ve been a good child to me in all my trials, and, I doubt not,
you’ll make a good wife. There’s a blessing promised in the
Scriptures to those who are dutiful to their parents. Keep the Lord’s
day, Sally, as you’ve been taught to do, and seek the one thing
needful.”

Ben had chosen a sunny, calm morning, that the impressions


made upon Sally’s mind might be as pleasant as possible, not
dreaming that she had already visited the island, and been all over
the house. Nevertheless, as he sat down to the oars, his old fears
began somewhat to revive; but Providence ordered matters in a
much better manner than he could have done, to render Sally’s first
impressions of the island both pleasant and permanent.
When he left it the last time, knowing that Sally would return with
him, he had crammed the great fireplace with dry wood, and pushed
under the forestick the top of a dry fir, with the leaves all on, and
covered with cones full of balsam. They were well on their way when
a black cloud rose suddenly from the north-west, denoting that the
wind, which had been south for some days, was about to shift, with
a squall.

“We are two thirds over now,” said Ben; “we shall be head to the
sea, and soon get under the lee of the island; ’tis better to go ahead
than to go back.”

“I wish we were there now,” said Sally to herself, as she thought


of that sheltered spot behind the thick woods, that no wind could
get through.

“Sit down in the bottom of the canoe, Sally; if the water flies over
you, don’t move.”

When the squall struck, the wind seemed to shriek right out, and
in an instant raised a furious sea, drenching them with water from
head to foot. Sally uttered not a word, but sat perfectly still, though
the cold spray flew over and ran under her, wetting her through and
through.

The little boat, managed with consummate skill and strength, rode
the sea like an egg-shell. It began to grow smoother as they
approached the high woods on the island, when Ben, exerting his
strength, drove her through the water, and they were soon at the
mouth of the brook, where it was as smooth as a mill-pond. Jumping
out, he dragged the canoe from the water, and, taking Sally out,
stood her, all dripping, on the beach.

“What a calm place,” she exclaimed, “after that dreadful sea! O,


you wicked Ben, how could you tell me ’twas such an awful place?”
“You’re shaking with the cold; let’s go where there’s a fire;” and
catching her up, he ran into the house with her; then striking fire, he
lighted the fir top under the forestick; in an instant the bright flame
flashed through the pile of wood, and roared up the chimney,
diffusing a cheerful warmth through the room. Ben pulled up the
great settle; Sally stretched herself upon it, her wet garments
smoking in the heat.

“Isn’t this nice?” she said, as, safe from danger, she basked in the
warm blaze. “I shall always love this great fireplace after this, as
long as I live.”

Ben was delighted. He knew by experience the power of strong


contrasts,—for the whole life of a seaman is made up of them,—and
that nothing could have made the island seem so much like home to
Sally, as there finding safety when in danger, and warmth when
shivering with cold.

They now went over the house together; and Sally made Ben
completely happy by telling him she would have been thankful for a
house not half so good. We see in this well-matched and hardy pair
the representatives of those who laid broad and deep the
foundations of our free institutions, and whose strength was in their
homes.

They flung themselves with alacrity upon these hardships, which


were to procure for them a heritage of their own,—the product of
their own energies,—confident in their own resources, and the
protection of that Being whom they had been educated to believe
helps those who help themselves.

They were now on an island, in the stormy Atlantic, six miles from
the nearest land, which, with the exception of a little strip of grass
along the beach, was an unbroken forest.

Here they had commenced married life, in the face of a long, hard
winter.
It may seem to many of our readers idle to talk about happiness
in relation to people in such circumstances. They, perhaps judging
from their own feelings, wonder how they could pass their time.

In the first place, they had health and strength, were not troubled
with dyspepsia, and hence did not look at life through green
spectacles. They took pride in overcoming obstacles, and feeling that
they were equal to the emergency. They had plenty to do from the
time they rose in the morning till they went to bed at night; not a
moment to brood over and dread difficulties; and a June day was
too short for all they found to do in it. Finally, they loved each other,
had an object to look forward to, had never known any of those
things which are considered by many as necessary to happiness, and
thus neither pined after nor missed them.

Sally had plenty of bed-clothes, which she had made herself; also
beautiful table-cloths and towels of linen, figured, that she had spun,
woven, and bleached; and tow towels, coarse sheets, and table-
cloths for every day. One little looking-glass, about six inches by
eight in size, graced the wall, with a comb-case, made of
pasteboard, hanging below it. They had one really beautiful piece of
furniture, which her father had brought from England—a mahogany
secretary, with book-cases and drawers, and inlaid with different
kinds of wood, contrasting strangely with the rough logs against
which it rested. They had chairs with round posts, and bottoms
made of ash-splints; mugs, bowls, a tea-pot, and pitchers of earthen
ware; and pewter plates, from the largest platter to the smallest
dishes and porringers; also an iron skillet. Ben had a shoe-maker’s
bench, awls and lasts, and quite a good set of carpenter’s tools.

Sally now put all the earthen and new pewter ware upon the
dressers, which made quite a show.

“I declare, Ben, I’ve forgotten my candle-moulds, and we’ve got


no light. Here’s a lamp, but not a drop of oil or wick in it.”
“I’ll shoot a seal,—I saw three or four on the White Bull when we
came over,—then to-morrow you can try out the blubber.”

Ben was better than his word, for before night he shot two.

There was one piece of property that Sally valued more than
anything else, because ’twas alive, and there was such a look of
home about it.

The widow Hadlock had a line-backed cow, that gave a great mess
of milk. Sally had milked her ever since she was large enough to
milk; indeed, she milked her that memorable night when Ben and
Sam Johnson went blueberrying in the widow’s parlor.

They raised a calf from her, which was marked just like the old
cow, and Mrs. Hadlock had given it to Sally. The creature, having
been brought up with a large stock of cattle, missing her mates, had
been very lonesome on the island, and roared and moaned a great
deal. As Sally opened the door to throw out some water, the heifer
came on the gallop, and, putting her feet on the door-stone, rubbed
her nose against Sally’s shoulder, and licked her face. The tears
came into Sally’s eyes in a moment. “You good old soul,” said she,
putting her arms round her neck,—half a mind to kiss her,—“do you
know me, and were you glad to see me? I wish I had an ear of corn
to give you.”

After this the cow made no more ado, but went to feeding,
perfectly contented with the knowledge that her old mistress was
present. As night came on, Sally made the discovery that they had
no milk-pail; but Ben was equal to the emergency: he cut down a
maple, cut a trough in it, drove the cow astride of it, while Sally
milked her into this novel pail. That evening Ben dug out a pine log,
put a bottom in it, and a bail, then drove two hoops on it, and made
a milk-pail.

The next day Sally tried out the seals, while Ben went into the
swamp and got some cooper’s flags, which he cut into short pieces,
for lamp-wicks.

Fowling, for a person in Ben’s situation, was not merely a source


of pleasure, but of profit, as the feathers sold readily for cash, the
bodies were good for food, and could be exchanged at the store for
groceries, or with the farmers for wool and flax, which Sally made
into cloth.

Ben had a little yellow dog, with white on the end of his tail, that
would play. Sea-fowl possess a great share of curiosity, which leads
them to swim up to anything strange, in order to see what it is. They
would often swim in to a squirrel, playing in the bushes at the
water’s edge, to see what he’s about. The gunners take advantage
of this trait in their character; they teach a little dog to play with a
stone on the beach: he’ll roll it along the ground, stand up on his
hind legs with it in his fore paws, and when he gets tired of it, his
master’ll throw him another from his ambush. The birds swim in to
see what he is doing, and are killed, and the little dog swims off and
brings them ashore. All dogs cannot be taught this, only those who
have a genius for it.

Tige Rhines would pick up birds right in the surf, or in the dead of
winter, but could never be taught to play; he was too dignified.

It is impossible for one destitute of a taste for fowling to conceive


of the intensity which the passion will acquire by indulgence. Ben
was so eager for birds, that he would lie on a ledge till Sailor froze
his ears and tail. There were a great many minks on the island,
whose furs were valuable: these Sailor would track to their holes,
when Ben would smoke them out.

The widow Hadlock had brought up her family to cherish a great


reverence for the Lord’s day. Ben had been trained by his mother in
the same way; but, after leaving home, he, like most seafaring men,
carried a traveller’s conscience, and did many things on that day
which would not have met her approval.
One Sabbath morning a whole flock of coots swam into the mouth
of the brook to drink; ’twas a superb chance for a shot. Ben, without
a moment’s hesitation, took down his gun from the hook, and was
just going out the door when Sally laid her hand on his arm.

“Ben, where are you going?”

“To shoot those coots; I never saw such a chance for a shot in my
life. I shouldn’t wonder if I could knock over twenty with this big
gun.”

“Why, Ben, you must be out of your head; do you know what day
’tis? would you go gunning on the Lord’s day?”

“No, I wouldn’t go a-gunning; but when they come right in under


my nose, asking to be shot, I’d shoot them.”

“Well, I never would begin by breaking the Lord’s day; ’tis not
right, and we shall not prosper; if we’ve not much else, let us, at
least, have a clear conscience. What do you think your father and
mother would say, if they heard you had fired a gun on the Lord’s
day?”

“It wouldn’t trouble father much; he would do the same himself;


but ’twould mother, and I see it does you.”

He took his ramrod, and thumped on the side of the house; the
coots took to flight in an instant.

“There goes the temptation,” said he. “I didn’t know before that
you was a professor of religion.”

“No more I ain’t, nor a possessor either; wish I was; but I mean
to keep the Lord’s day; I’ll do that much, any way.”

“I know you’re right, Sally; but you must make some allowance for
a feller who has been so long at sea, and couldn’t keep it, if he
would, as people can ashore. Suppose a hawk was carrying off a
chicken on the Sabbath—wouldn’t you let me shoot it?”

“No, I’m sure I wouldn’t; but if an eagle was carrying off a baby, I
would.”

This was the first and only time Ben ever took the gun down on
the Sabbath. They made it a day of rest.

They had some good books, and one Sally’s mother had given her,
which she was very fond of reading, called “Hooks and Eyes for
Christian’s Breeches.” It was a queer title, but a very good book. In
those days people did not wear suspenders, but kept their breeches
up by buttoning the waistband, or by a belt. Where people were
well-formed, and had good hips, they would keep up very well; but
when they were all the way of a bigness, or were careless and didn’t
button their waistbands tight, they would slip down; so some had
hooks and eyes to keep them up, and prevent this by hooking them
to the waistcoat. Thus this book was designed for those slouching,
careless Christians who needed hooks and eyes to their breeches,
and were slack in their religious duties.
CHAPTER XVIII.

THE BRIDAL CALL.

Parents and friends of the new-married pair had watched with no


small anxiety their progress through the squall. During the height of
it, they could see the canoe when it rose upon the top of a wave; as
it disappeared in a trough of the sea, the widow clasped her hands
convulsively, and gave them up for lost.

“They are safe,” cried Captain Rhines, drawing a long breath;


“they’ve got under the lee of the island. John, run to the house and
get my spy-glass.”

With the aid of the glass he saw them land, and Ben carry Sally to
the house in his arms.

“She’s fainted with fright, poor thing; it’s a rough beginning for
her,” said the widow.

“He only wants to get her to the fire; there’s nothing the matter
with her but a good soaking.”

’Twas now the Indian summer, with calm moonlight nights.

“Wife,” said Captain Rhines, “I expect Sally’s mother is dying to


know how she got on the island that morning. If we don’t go now,
we shan’t be able to go this winter; it’ll be too rough by and by.
John, run over there, and ask her if she would like to go and see
Sally.”

“Can I go, too, father?”


“Yes, I want you to help row; so do your chores, tie up the cattle,
and bear a hand about it.”

Sally had washed her supper dishes, and Ben was pulling off his
boots, when the door was opened, and in walked the party. It was a
most joyful surprise to the new-married couple.

“Why, mother!” exclaimed Sally, kissing her again and again; “I


was thinking the other day whether you would ever venture to come
on to this island; and now you’re here so soon, and in the fall of the
year, too!”

“Indeed, Sally, you know I never lacked for courage, only for
strength. You must needs think I had a strong motive.”

But, of all the group, none seemed more delighted than John. He
stared at the log walls, looked up the chimney, capered round the
room with Sailor, and finally getting up in Ben’s lap, put both arms
round his neck, and fairly cried for joy.

“How should you like to live on here, Johnnie?” said Ben.

“O, shouldn’t I like it! you’d better believe.”

“I shot two seals the other day, on the White Bull; and within a
week I’ve killed fifty birds, of all kinds.”

“Won’t you ask father to let me come on and stay a little while,
and go gunning? O, I do miss you so!”

“I shouldn’t wonder if there were ducks now feeding on the flats;


take my gun; she’s all loaded.”

The moment Sailor saw the gun taken down, he was all ready: so
perfectly was he trained, that when it was not desirable he should
play, he would lie still till the gun was fired, and then bring in the
game.
“How I should like to be on here in the daytime!” said John. “Do
you know, Ben, I was never here in all my life before?”

“Why, Sally,” said her mother, “how did you get over in that
dreadful squall? We were all watching you, and felt so worried!
Wasn’t you frightened almost to death?”

“No, mother, I wasn’t much frightened; but I was terrible cold, and
wet all through. I never saw anything look so good, in all my life, as
this great fireplace did, for Ben made a roaring fire in it; and I’m just
as happy and contented as I can be.”

In the midst of this conversation the door opened, and in walked


Uncle Isaac.

“It was such a pleasant night,” said he, addressing the captain, “I
told Hannah we’d take a run down to your house; and when I found
you’d come over here, I thought I’d take your gunning float and
follow suit.”

“Why didn’t you bring Hannah with you?” inquired Sally.

“Well, I wanted to; but she ain’t much of a water-fowl, and was
afraid to come in a tittlish gunning float, and said she’d stay and visit
Captain Rhines’s girls; but she sends her love to you, and says if
she’d known I was coming, she’d sent you over a bag of apples.”

“How this does carry a body back!” said the widow; “it don’t seem
but t’other day since I was living in a log house; and how much I’ve
been through since then!”

They then went all over the house, and down cellar.

“Well, Isaac,” said Captain Rhines, “you’ve done yourself credit in


building this house; I knew you would. ’Tisn’t much like the house I
was born in; that wasn’t tighter than a wharf, except while it was
stuffed with moss and clay; and some of that was always falling out.
I’ve gone to bed many a night, and waked up in a snow drift,
because the wind had blown the clay out, and the snow in; but I
thought, when I was coming up from the shore, and saw it standing
here in the moonlight, that it was as much like the one father built,
after his boys got big enough to be of some help to him, as two
peas in a pod: just as many windows, just as high, and with a bark
roof; but it ain’t much like it other-ways; for the timber wan’t hewed
—only the bark and knots taken off where it came together; but this
is as tight as a churn. And then that fireplace; I wouldn’t believed it
possible.”

“Well,” said Uncle Isaac, “I did the best I could; but I think Sam
beat the whole of us. I should be glad to swap my fireplace and
chimney for that, and give a yoke of oxen to boot.”

“Do you know, Isaac, there’s nothing carries me back to my boy


days like that old chamber? It’s the very image of ours; it seems to
me as if I was setting there now, on a rainy day, astraddle of a tub,
shelling corn on the handle of mother’s frying-pan, with my thoughts
running all over the world, longing to go to sea, and contriving how I
should get father’s consent.”

A loud mewing was now heard in the corner of the room.

“I declare to man,” said the widow, “I’ve been so taken up with


old times, I forgot. See here, Sally,”—opening her basket and taking
out a kitten,—“I thought she’d be company for you. You know them
speckled chickens, Sally, that the old top-knot hen hatched out.”

“Yes, mother.”

“Well, the hawks carried off three of ’em; and I meant to brought
the rest over to you, but Sam said they wouldn’t lay much this
winter; you’d have to buy corn, and you’d better have ’em in the
spring. But I’ve brought you over a pillow-case full of flax.”

“I,” said Mrs. Rhines, “brought you over some wool.”


“And I,” said Captain Rhines, “a barrel of cider and some
vegetables, to go with your coots and salt beef.”

“While I,” said Uncle Isaac, “am all the one that’s come empty-
handed; but I know what I’ll do; I’ll give you a pig, and Ben can get
him next time he comes off.”

John now came in, bringing five ducks, that he had shot.

“He’s just like the rest of us, Ben,” said his father: “I believe it runs
in the breed of us to shoot.”

“Let him come over here, and stay a day or two, and gun with
me.”

“He’s too good a boy,”—patting him fondly on the head;—“I


couldn’t get along without him.”

“That is just the reason,” said his mother, “that he ought to be


gratified once in a while. It’s a great deal better he should be here
with Ben, than with some of the boys he goes with; I should feel
much easier about him than I do when he’s with them in boats, and
gunning. I’m always afraid they’ll shoot one another, or be drowned.”

“Well, it’s just as his mother says; I’m at home so little, I don’t
interfere with her concerns; she’s cap’n; I’m only passenger.”

“But you’re going to be at home all the time now; and I should
like to give up my authority.”

“By the way, Ben, I’ve had a letter from Mr. Welch; he says large,
handsome masts, bowsprits, and spars are in great demand; that he
can find a market in Boston and Salem, in the spring, for all you can
send him.”

“I’m going to cut small spars directly, father; but I want snow to
fall the large ones on, else I shall have to bed them with brush, for
fear of breaking them.”

“He says that the war in Europe is throwing all the carrying trade
into the hands of neutrals; that now we’ve got our government
going, it’ll be snapping times; and that while they’re all fighting like
dogs over a bone, we can run off with the bone; and if I want to try
a voyage, he has a vessel for me.”

“Well, you’re not going,” said his wife; “you’ve been enough, and
you’ve done enough. If Ben could afford to give up going to sea, in
the prime of life, for the sake of Sally, I’m sure you can, in your old
age, for the sake of Betsey; and you belong to me for the rest of
your life.”

“Old!” said the captain, dancing over the room; “I don’t feel a bit
old. I should like a little cash, just to fix up the buildings a little, buy
that timber lot that joins the rye field; and then”—with a comical
look at his wife—“I should like to do a little more for the minister. I
should be so thankful, sometimes, if somebody would come in that
could talk about anything else than some old horse, or cow, or
sheep that’s got the mulligrubs!”

“Father,” said John, as they were preparing to go, “why can’t I stay
now?”

“Because, child, I want you to help me row.”

“Let him stay,” said Uncle Isaac, who, from instinct, always took
the part of the boys; “I’ll go over with you.”

“But there’s my float over here, and I want to go gunning to-


morrow.”

“We’ll take her in tow,” said Uncle Isaac.

With mutual good wishes they now separated, leaving John in


high glee at the result, with Ben, for a visit.

You might also like