100% found this document useful (2 votes)
7 views

Beginning Java 9 Fundamentals: Arrays, Objects, Modules, JShell, and Regular Expressions Sharan pdf download

The document is about 'Beginning Java 9 Fundamentals' by Kishori Sharan, covering essential concepts such as arrays, objects, modules, JShell, and regular expressions. It includes a detailed table of contents outlining various programming concepts, environment setup, and Java programming techniques. The book is designed for beginners and provides a comprehensive introduction to Java 9 features and functionalities.

Uploaded by

uslanhores0t
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
7 views

Beginning Java 9 Fundamentals: Arrays, Objects, Modules, JShell, and Regular Expressions Sharan pdf download

The document is about 'Beginning Java 9 Fundamentals' by Kishori Sharan, covering essential concepts such as arrays, objects, modules, JShell, and regular expressions. It includes a detailed table of contents outlining various programming concepts, environment setup, and Java programming techniques. The book is designed for beginners and provides a comprehensive introduction to Java 9 features and functionalities.

Uploaded by

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

Beginning Java 9 Fundamentals: Arrays, Objects,

Modules, JShell, and Regular Expressions Sharan


download

https://textbookfull.com/product/beginning-java-9-fundamentals-
arrays-objects-modules-jshell-and-regular-expressions-sharan/

Download more ebook from https://textbookfull.com


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

Biota Grow 2C gather 2C cook Loucas

https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-
loucas/

Java Language Features: With Modules, Streams, Threads,


I/O, and Lambda Expressions 2nd Edition Kishori Sharan

https://textbookfull.com/product/java-language-features-with-
modules-streams-threads-i-o-and-lambda-expressions-2nd-edition-
kishori-sharan/

Java 9 with JShell Introducing the full range of Java 9


s new features via JShell 1st Edition Gastón C. Hillar

https://textbookfull.com/product/java-9-with-jshell-introducing-
the-full-range-of-java-9-s-new-features-via-jshell-1st-edition-
gaston-c-hillar/

Java 9 Revealed: For Early Adoption and Migration 1st


Edition Kishori Sharan (Auth.)

https://textbookfull.com/product/java-9-revealed-for-early-
adoption-and-migration-1st-edition-kishori-sharan-auth/
JavaScript Regular Expressions 1st Edition Groner
Loiane Manricks Gabriel

https://textbookfull.com/product/javascript-regular-
expressions-1st-edition-groner-loiane-manricks-gabriel/

Java APIs, Extensions and Libraries Kishori Sharan

https://textbookfull.com/product/java-apis-extensions-and-
libraries-kishori-sharan/

Regular and Irregular Holonomic D Modules London


Mathematical Society Lecture Note Series 1st Edition
Masaki Kashiwara

https://textbookfull.com/product/regular-and-irregular-holonomic-
d-modules-london-mathematical-society-lecture-note-series-1st-
edition-masaki-kashiwara/

Big Java: Early Objects 7th Edition Cay S. Horstmann

https://textbookfull.com/product/big-java-early-objects-7th-
edition-cay-s-horstmann/

Java 9 Modularity Revealed: Project Jigsaw and Scalable


Java Applications Alexandru Jecan

https://textbookfull.com/product/java-9-modularity-revealed-
project-jigsaw-and-scalable-java-applications-alexandru-jecan/
Beginning Java 9
Fundamentals
Arrays, Objects, Modules, JShell,
and Regular Expressions

Second Edition

Kishori Sharan
Beginning Java 9
Fundamentals
Arrays, Objects, Modules, JShell,
and Regular Expressions

Second Edition

Kishori Sharan
Beginning Java 9 Fundamentals: Arrays, Objects, Modules, JShell, and Regular Expressions
Kishori Sharan
Montgomery, Alabama, USA
ISBN-13 (pbk): 978-1-4842-2843-2 ISBN-13 (electronic): 978-1-4842-2902-6
https://doi.org/10.1007/978-1-4842-2902-6
Library of Congress Control Number: 2017958824
Copyright © 2017 by Kishori Sharan
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Cover image by Freepik (www.freepik.com)
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Technical Reviewer: Wallace Jackson
Coordinating Editor: Mark Powers
Copy Editor: Kezia Endsley
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).
SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web
page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers
on GitHub via the book’s product page, located at www.apress.com/9781484228432. For more detailed
information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
Contents at a Glance

About the Author�������������������������������������������������������������������������������������������������xxvii


About the Technical Reviewer������������������������������������������������������������������������������xxix
Acknowledgments������������������������������������������������������������������������������������������������xxxi
Introduction��������������������������������������������������������������������������������������������������������xxxiii


■Chapter 1: Programming Concepts����������������������������������������������������������������������� 1

■Chapter 2: Setting Up the Environment��������������������������������������������������������������� 33

■Chapter 3: Writing Java Programs���������������������������������������������������������������������� 45

■Chapter 4: Data Types������������������������������������������������������������������������������������������ 99

■Chapter 5: Operators����������������������������������������������������������������������������������������� 145

■Chapter 6: Statements��������������������������������������������������������������������������������������� 191

■Chapter 7: Classes��������������������������������������������������������������������������������������������� 223

■Chapter 8: Methods������������������������������������������������������������������������������������������� 255

■Chapter 9: Constructors������������������������������������������������������������������������������������ 323

■Chapter 10: Modules����������������������������������������������������������������������������������������� 355

■Chapter 11: Object and Objects Classes������������������������������������������������������������ 395

■Chapter 12: Wrapper Classes���������������������������������������������������������������������������� 439

■Chapter 13: Exception Handling������������������������������������������������������������������������ 461

■Chapter 14: Assertions�������������������������������������������������������������������������������������� 511

■Chapter 15: Strings������������������������������������������������������������������������������������������� 521

iii
■ Contents at a Glance


■Chapter 16: Dates and Times����������������������������������������������������������������������������� 549

■Chapter 17: Formatting Data����������������������������������������������������������������������������� 631

■Chapter 18: Regular Expressions���������������������������������������������������������������������� 669

■Chapter 19: Arrays�������������������������������������������������������������������������������������������� 701

■Chapter 20: Inheritance������������������������������������������������������������������������������������� 755

■Chapter 21: Interfaces��������������������������������������������������������������������������������������� 823

■Chapter 22: Enum Types������������������������������������������������������������������������������������ 895

■Chapter 23: Java Shell��������������������������������������������������������������������������������������� 921

■Appendix A: Character Encodings��������������������������������������������������������������������� 985

■Appendix B: Documentation Comments������������������������������������������������������������ 997

Index������������������������������������������������������������������������������������������������������������������� 1023

iv
Contents

About the Author�������������������������������������������������������������������������������������������������xxvii


About the Technical Reviewer������������������������������������������������������������������������������xxix
Acknowledgments������������������������������������������������������������������������������������������������xxxi
Introduction��������������������������������������������������������������������������������������������������������xxxiii


■Chapter 1: Programming Concepts����������������������������������������������������������������������� 1
What Is Programming?����������������������������������������������������������������������������������������������������� 1
Components of a Programming Language����������������������������������������������������������������������� 4
Programming Paradigms�������������������������������������������������������������������������������������������������� 4
Imperative Paradigm������������������������������������������������������������������������������������������������������������������������������ 6
Procedural Paradigm������������������������������������������������������������������������������������������������������������������������������ 6
Declarative Paradigm����������������������������������������������������������������������������������������������������������������������������� 7
Functional Paradigm������������������������������������������������������������������������������������������������������������������������������� 8
Logic Paradigm��������������������������������������������������������������������������������������������������������������������������������������� 8
Object-Oriented Paradigm���������������������������������������������������������������������������������������������������������������������� 9
What Is Java?����������������������������������������������������������������������������������������������������������������� 12
The Object-Oriented Paradigm and Java������������������������������������������������������������������������ 13
Abstraction������������������������������������������������������������������������������������������������������������������������������������������� 14
Encapsulation and Information Hiding�������������������������������������������������������������������������������������������������� 23
Inheritance�������������������������������������������������������������������������������������������������������������������������������������������� 25
Polymorphism��������������������������������������������������������������������������������������������������������������������������������������� 26
Summary������������������������������������������������������������������������������������������������������������������������ 31

■Chapter 2: Setting Up the Environment��������������������������������������������������������������� 33
System Requirements���������������������������������������������������������������������������������������������������� 33
Installing JDK 9�������������������������������������������������������������������������������������������������������������� 33

v
■ Contents

The JDK Directory Structure������������������������������������������������������������������������������������������� 34


Verifying the JDK Installation����������������������������������������������������������������������������������������� 37
Starting the JShell Tool�������������������������������������������������������������������������������������������������� 38
Installing NetBeans 9����������������������������������������������������������������������������������������������������� 38
Configuring NetBeans���������������������������������������������������������������������������������������������������� 39
Summary������������������������������������������������������������������������������������������������������������������������ 43

■Chapter 3: Writing Java Programs���������������������������������������������������������������������� 45
The Goal Statement�������������������������������������������������������������������������������������������������������� 45
Using the JShell Tool������������������������������������������������������������������������������������������������������ 46
What Is a Java Program?����������������������������������������������������������������������������������������������� 46
Writing the Source Code������������������������������������������������������������������������������������������������� 47
Writing Comments���������������������������������������������������������������������������������������������������������� 48
Declaring a Module�������������������������������������������������������������������������������������������������������� 49
Declaring Types�������������������������������������������������������������������������������������������������������������� 51
Package Declaration����������������������������������������������������������������������������������������������������������������������������� 52
Import Declarations������������������������������������������������������������������������������������������������������������������������������ 53
Class Declaration���������������������������������������������������������������������������������������������������������������������������������� 54
Types Have Two Names������������������������������������������������������������������������������������������������������������������������ 59

Compiling the Source Code�������������������������������������������������������������������������������������������� 60


Packaging the Compiled Code��������������������������������������������������������������������������������������� 62
Running a Java Program������������������������������������������������������������������������������������������������ 64
Playing with Module Options������������������������������������������������������������������������������������������ 69
Listing Observable Modules����������������������������������������������������������������������������������������������������������������� 69
Limiting the Observable Modules��������������������������������������������������������������������������������������������������������� 70
Describing a Module����������������������������������������������������������������������������������������������������������������������������� 71
Printing Module Resolution Details������������������������������������������������������������������������������������������������������� 72
Dry Running Your Program������������������������������������������������������������������������������������������������������������������� 73

Enhancing a Module Descriptor������������������������������������������������������������������������������������� 73


Running Java Programs in Legacy Mode����������������������������������������������������������������������� 75

vi
■ Contents

Duplicate Modules on Module Path�������������������������������������������������������������������������������� 78


Syntax for Command-Line Options��������������������������������������������������������������������������������� 80
Writing Java Programs Using the NetBeans IDE������������������������������������������������������������ 81
Creating a Java Project������������������������������������������������������������������������������������������������������������������������� 81
Creating Modular JARs in NetBeans����������������������������������������������������������������������������������������������������� 88
NetBeans Project Directory Structure��������������������������������������������������������������������������������������������������� 89
Adding Classes to a Module����������������������������������������������������������������������������������������������������������������� 89
Customizing NetBeans Project Properties�������������������������������������������������������������������������������������������� 89
Opening an Existing NetBeans Project������������������������������������������������������������������������������������������������� 91

Behind the Scenes��������������������������������������������������������������������������������������������������������� 91


Summary������������������������������������������������������������������������������������������������������������������������ 95

■Chapter 4: Data Types������������������������������������������������������������������������������������������ 99
What Is a Data Type?������������������������������������������������������������������������������������������������������ 99
What Is an Identifier?��������������������������������������������������������������������������������������������������� 100
Keywords���������������������������������������������������������������������������������������������������������������������� 102
Data Types in Java�������������������������������������������������������������������������������������������������������� 102
Primitive Data Types in Java���������������������������������������������������������������������������������������� 107
Integral Data Types����������������������������������������������������������������������������������������������������������������������������� 108
Floating-Point Data Types������������������������������������������������������������������������������������������������������������������� 118

Underscores in Numeric Literals���������������������������������������������������������������������������������� 123


Java Compiler and Unicode Escape Sequence������������������������������������������������������������ 123
A Short Break��������������������������������������������������������������������������������������������������������������� 125
Binary Representation of Integers�������������������������������������������������������������������������������� 126
Diminished Radix Complement����������������������������������������������������������������������������������������������������������� 127
Radix Complement����������������������������������������������������������������������������������������������������������������������������� 128

Binary Representation of Floating-Point Numbers������������������������������������������������������� 129


32-bit Single-Precision Floating-Point Format����������������������������������������������������������������������������������� 131

Special Floating-Point Numbers����������������������������������������������������������������������������������� 134


Signed Zeros��������������������������������������������������������������������������������������������������������������������������������������� 134
Signed Infinities���������������������������������������������������������������������������������������������������������������������������������� 134

vii
■ Contents

NaN����������������������������������������������������������������������������������������������������������������������������������������������������� 135
Denormals������������������������������������������������������������������������������������������������������������������������������������������ 136

Rounding Modes���������������������������������������������������������������������������������������������������������� 136


Rounding Toward Zero������������������������������������������������������������������������������������������������������������������������ 137
Rounding Toward Positive Infinity������������������������������������������������������������������������������������������������������� 137
Rounding Toward Negative Infinity����������������������������������������������������������������������������������������������������� 137
Rounding Toward Nearest������������������������������������������������������������������������������������������������������������������� 138

IEEE Floating-Point Exceptions������������������������������������������������������������������������������������� 138


Division by Zero Exception������������������������������������������������������������������������������������������������������������������ 138
Invalid Operation Exception���������������������������������������������������������������������������������������������������������������� 138
Overflow Exception����������������������������������������������������������������������������������������������������������������������������� 139
Underflow Exception��������������������������������������������������������������������������������������������������������������������������� 139
Inexact Exception������������������������������������������������������������������������������������������������������������������������������� 139

Java and IEEE Floating-Point Standards���������������������������������������������������������������������� 140


Little-Endian and Big-Endian��������������������������������������������������������������������������������������� 140
Summary���������������������������������������������������������������������������������������������������������������������� 141

■Chapter 5: Operators����������������������������������������������������������������������������������������� 145
What Is an Operator?���������������������������������������������������������������������������������������������������� 145
Assignment Operator���������������������������������������������������������������������������������������������������� 147
Declaration, Initialization, and Assignment������������������������������������������������������������������ 149
Arithmetic Operators���������������������������������������������������������������������������������������������������� 150
Addition Operator (+)�������������������������������������������������������������������������������������������������������������������������� 151
Subtraction Operator (-)���������������������������������������������������������������������������������������������������������������������� 153
Multiplication Operator (*)������������������������������������������������������������������������������������������������������������������ 154
Division Operator (/)���������������������������������������������������������������������������������������������������������������������������� 155
Modulus Operator (%)������������������������������������������������������������������������������������������������������������������������� 157
Unary Plus Operator (+)���������������������������������������������������������������������������������������������������������������������� 159
Unary Minus Operator (-)�������������������������������������������������������������������������������������������������������������������� 159
Compound Arithmetic Assignment Operators������������������������������������������������������������������������������������� 160
Increment (++) and Decrement (--) Operators����������������������������������������������������������������������������������� 161

viii
■ Contents

String Concatenation Operator (+)������������������������������������������������������������������������������� 164


Relational Operators����������������������������������������������������������������������������������������������������� 169
Equality Operator (==)������������������������������������������������������������������������������������������������������������������������ 169
Inequality Operator (!=)���������������������������������������������������������������������������������������������������������������������� 172
Greater Than Operator (>)������������������������������������������������������������������������������������������������������������������ 172
Greater Than or Equal to Operator (>=)���������������������������������������������������������������������������������������������� 173
Less Than Operator (<)����������������������������������������������������������������������������������������������������������������������� 173
Less Than or Equal to Operator (<=)�������������������������������������������������������������������������������������������������� 174

Boolean Logical Operators������������������������������������������������������������������������������������������� 174


Logical NOT Operator (!)��������������������������������������������������������������������������������������������������������������������� 175
Logical Short-Circuit AND Operator (&&)�������������������������������������������������������������������������������������������� 175
Logical AND Operator (&)�������������������������������������������������������������������������������������������������������������������� 177
Logical Short-Circuit OR Operator (||)������������������������������������������������������������������������������������������������� 178
Logical OR Operator (|)������������������������������������������������������������������������������������������������������������������������ 178
Logical XOR Operator (^)�������������������������������������������������������������������������������������������������������������������� 178
Compound Boolean Logical Assignment Operators���������������������������������������������������������������������������� 179

Ternary Operator (? :)��������������������������������������������������������������������������������������������������� 180


Bitwise Operators��������������������������������������������������������������������������������������������������������� 180
Operators Precedence�������������������������������������������������������������������������������������������������� 184
Summary���������������������������������������������������������������������������������������������������������������������� 186

■Chapter 6: Statements��������������������������������������������������������������������������������������� 191
What Is a Statement?��������������������������������������������������������������������������������������������������� 191
Types of Statements����������������������������������������������������������������������������������������������������� 192
Declaration Statement������������������������������������������������������������������������������������������������������������������������ 192
Expression Statement������������������������������������������������������������������������������������������������������������������������� 192
Control Flow Statement���������������������������������������������������������������������������������������������������������������������� 193

A Block Statement�������������������������������������������������������������������������������������������������������� 194


The if-else Statement��������������������������������������������������������������������������������������������������� 195
The switch Statement�������������������������������������������������������������������������������������������������� 200

ix
■ Contents

The for Statement�������������������������������������������������������������������������������������������������������� 204


Initialization���������������������������������������������������������������������������������������������������������������������������������������� 205
Condition-Expression�������������������������������������������������������������������������������������������������������������������������� 206
Expression-List����������������������������������������������������������������������������������������������������������������������������������� 207

The for-each Statement������������������������������������������������������������������������������������������������ 209


The while Statement���������������������������������������������������������������������������������������������������� 210
The do-while Statement����������������������������������������������������������������������������������������������� 212
The break Statement���������������������������������������������������������������������������������������������������� 214
The continue Statement����������������������������������������������������������������������������������������������� 217
An Empty Statement���������������������������������������������������������������������������������������������������� 218
Summary���������������������������������������������������������������������������������������������������������������������� 219

■Chapter 7: Classes��������������������������������������������������������������������������������������������� 223
What Is a Class?����������������������������������������������������������������������������������������������������������� 223
Declaring a Class���������������������������������������������������������������������������������������������������������� 224
Declaring Fields in a Class������������������������������������������������������������������������������������������� 225
Creating Instances of a Class��������������������������������������������������������������������������������������� 226
The null Reference Type����������������������������������������������������������������������������������������������� 228
Using Dot Notation to Access Fields of a Class������������������������������������������������������������ 229
Default Initialization of Fields��������������������������������������������������������������������������������������� 232
Access Level Modifiers for a Class������������������������������������������������������������������������������� 233
Import Declarations������������������������������������������������������������������������������������������������������ 236
Single-Type Import Declaration���������������������������������������������������������������������������������������������������������� 237
Import-on-Demand Declaration���������������������������������������������������������������������������������������������������������� 239
Import Declarations and Type Search Order��������������������������������������������������������������������������������������� 241
Automatic Import Declarations����������������������������������������������������������������������������������������������������������� 247
Static Import Declarations������������������������������������������������������������������������������������������������������������������ 248

Summary���������������������������������������������������������������������������������������������������������������������� 251

x
■ Contents


■Chapter 8: Methods������������������������������������������������������������������������������������������� 255
What Is a Method?������������������������������������������������������������������������������������������������������� 255
Declaring Methods of a Class��������������������������������������������������������������������������������������� 255
Local Variables������������������������������������������������������������������������������������������������������������� 260
Rule #1����������������������������������������������������������������������������������������������������������������������������������������������� 260
Rule #2����������������������������������������������������������������������������������������������������������������������������������������������� 261
Rule #3����������������������������������������������������������������������������������������������������������������������������������������������� 261
Rule #4����������������������������������������������������������������������������������������������������������������������������������������������� 261

Instance Methods and Class Methods�������������������������������������������������������������������������� 264


Invoking a Method�������������������������������������������������������������������������������������������������������� 265
The Special main( ) Method������������������������������������������������������������������������������������������ 267
What Is this?����������������������������������������������������������������������������������������������������������������� 269
Access Levels for Class Members�������������������������������������������������������������������������������� 275
Access Level: A Case Study������������������������������������������������������������������������������������������ 282
What Is a Var-Args Method?����������������������������������������������������������������������������������������� 288
Overloading a Var-Args Method���������������������������������������������������������������������������������������������������������� 293
Var-Args Methods and the main() Method������������������������������������������������������������������������������������������ 294

Parameter-Passing Mechanisms���������������������������������������������������������������������������������� 295


Pass By Value������������������������������������������������������������������������������������������������������������������������������������� 296
Pass By Constant Value���������������������������������������������������������������������������������������������������������������������� 299
Pass By Reference������������������������������������������������������������������������������������������������������������������������������ 299
Pass By Reference Value�������������������������������������������������������������������������������������������������������������������� 303
Pass by Constant Reference Value����������������������������������������������������������������������������������������������������� 304
Pass by Result������������������������������������������������������������������������������������������������������������������������������������ 304
Pass by Value Result��������������������������������������������������������������������������������������������������������������������������� 304
Pass By Name������������������������������������������������������������������������������������������������������������������������������������� 305
Pass by Need�������������������������������������������������������������������������������������������������������������������������������������� 306
Parameter-Passing Mechanisms in Java��������������������������������������������������������������������� 306
Summary���������������������������������������������������������������������������������������������������������������������� 318

xi
■ Contents


■Chapter 9: Constructors������������������������������������������������������������������������������������ 323
What Is a Constructor?������������������������������������������������������������������������������������������������� 323
Declaring a Constructor����������������������������������������������������������������������������������������������� 323
Overloading a Constructor�������������������������������������������������������������������������������������������� 326
Writing Code for a Constructor������������������������������������������������������������������������������������� 327
Calling a Constructor from Another Constructor���������������������������������������������������������� 330
Using a return Statement Inside a Constructor������������������������������������������������������������ 332
Access Level Modifier for a Constructor���������������������������������������������������������������������� 333
Default Constructor������������������������������������������������������������������������������������������������������ 337
A static Constructor������������������������������������������������������������������������������������������������������ 338
Instance Initialization Block����������������������������������������������������������������������������������������� 338
static Initialization Block���������������������������������������������������������������������������������������������� 339
The final Keyword�������������������������������������������������������������������������������������������������������� 341
final Local Variables���������������������������������������������������������������������������������������������������������������������������� 342
final Parameters��������������������������������������������������������������������������������������������������������������������������������� 343
final Instance Variables���������������������������������������������������������������������������������������������������������������������� 343
final Class Variables��������������������������������������������������������������������������������������������������������������������������� 346
final Reference Variables�������������������������������������������������������������������������������������������������������������������� 346
Compile-Time vs. Runtime final Variables������������������������������������������������������������������������������������������ 347

Generic Classes������������������������������������������������������������������������������������������������������������ 347


Summary���������������������������������������������������������������������������������������������������������������������� 350

■Chapter 10: Modules����������������������������������������������������������������������������������������� 355
What Is a Module?�������������������������������������������������������������������������������������������������������� 355
Declaring Modules������������������������������������������������������������������������������������������������������� 356
Declaring Module Dependence������������������������������������������������������������������������������������ 358
An Example of Module Dependence����������������������������������������������������������������������������� 360
Troubleshooting���������������������������������������������������������������������������������������������������������������������������������� 366

Implicit Dependence����������������������������������������������������������������������������������������������������� 368


Optional Dependence��������������������������������������������������������������������������������������������������� 373

xii
■ Contents

Opening Modules and Packages���������������������������������������������������������������������������������� 373


Open Modules������������������������������������������������������������������������������������������������������������������������������������� 375
Opening Packages������������������������������������������������������������������������������������������������������������������������������ 375

Splitting Packages Across Modules����������������������������������������������������������������������������� 376


Restrictions in Module Declarations����������������������������������������������������������������������������� 377
Types of Modules��������������������������������������������������������������������������������������������������������� 377
Normal Modules��������������������������������������������������������������������������������������������������������������������������������� 379
Open Modules������������������������������������������������������������������������������������������������������������������������������������� 379
Automatic Modules����������������������������������������������������������������������������������������������������������������������������� 379
Unnamed Modules������������������������������������������������������������������������������������������������������������������������������ 383

Aggregator Modules����������������������������������������������������������������������������������������������������� 384


Knowing about Modules at Runtime���������������������������������������������������������������������������� 384
Migration Path to JDK 9����������������������������������������������������������������������������������������������� 386
Disassembling Module Definitions������������������������������������������������������������������������������� 388
Summary���������������������������������������������������������������������������������������������������������������������� 391

■Chapter 11: Object and Objects Classes������������������������������������������������������������ 395
The Object Class����������������������������������������������������������������������������������������������������������� 395
Rule #1����������������������������������������������������������������������������������������������������������������������������������������������� 396
Rule #2����������������������������������������������������������������������������������������������������������������������������������������������� 397

What Is the Class of an Object?������������������������������������������������������������������������������������ 399


Computing the Hash Code of an Object������������������������������������������������������������������������ 400
Comparing Objects for Equality������������������������������������������������������������������������������������ 404
String Representation of an Object������������������������������������������������������������������������������ 410
Cloning Objects������������������������������������������������������������������������������������������������������������ 414
Finalizing an Object������������������������������������������������������������������������������������������������������ 422
Immutable Objects������������������������������������������������������������������������������������������������������� 424
The Objects Class��������������������������������������������������������������������������������������������������������� 429
Bounds Checks����������������������������������������������������������������������������������������������������������������������������������� 430
Comparing Objects����������������������������������������������������������������������������������������������������������������������������� 430

xiii
■ Contents

Computing Hash Code������������������������������������������������������������������������������������������������������������������������ 431


Checking for Null�������������������������������������������������������������������������������������������������������������������������������� 432
Validating Arguments�������������������������������������������������������������������������������������������������������������������������� 432
Obtaining String Representation of Objects��������������������������������������������������������������������������������������� 433
Using the Objects Class���������������������������������������������������������������������������������������������������������������������� 433

Summary���������������������������������������������������������������������������������������������������������������������� 435

■Chapter 12: Wrapper Classes���������������������������������������������������������������������������� 439
Wrapper Classes���������������������������������������������������������������������������������������������������������� 439
Numeric Wrapper Classes������������������������������������������������������������������������������������������������������������������ 442
The Character Wrapper Class������������������������������������������������������������������������������������������������������������� 445
The Boolean Wrapper Class���������������������������������������������������������������������������������������������������������������� 447

Unsigned Numeric Operations�������������������������������������������������������������������������������������� 447


Autoboxing and Unboxing�������������������������������������������������������������������������������������������� 449
Beware of Null Values�������������������������������������������������������������������������������������������������� 452
Overloaded Methods and Autoboxing/Unboxing����������������������������������������������������������� 452
Comparison Operators and Autoboxing/Unboxing�������������������������������������������������������� 455
Collections and Autoboxing/Unboxing�������������������������������������������������������������������������� 457
Summary���������������������������������������������������������������������������������������������������������������������� 458

■Chapter 13: Exception Handling������������������������������������������������������������������������ 461
What Is an Exception?�������������������������������������������������������������������������������������������������� 461
An Exception Is an Object��������������������������������������������������������������������������������������������� 464
Using a try-catch Block������������������������������������������������������������������������������������������������ 464
Transfer of Control�������������������������������������������������������������������������������������������������������� 467
Exception Class Hierarchy�������������������������������������������������������������������������������������������� 469
Arranging Multiple catch Blocks���������������������������������������������������������������������������������� 470
A Multi-catch Block������������������������������������������������������������������������������������������������������ 473
Checked and Unchecked Exceptions��������������������������������������������������������������������������� 474
Checked Exception: Catch or Declare��������������������������������������������������������������������������� 477
Checked Exceptions and Initializers����������������������������������������������������������������������������� 484

xiv
■ Contents

Throwing an Exception������������������������������������������������������������������������������������������������� 485


Creating an Exception Class����������������������������������������������������������������������������������������� 486
The finally Block����������������������������������������������������������������������������������������������������������� 490
Rethrowing an Exception��������������������������������������������������������������������������������������������� 494
Analysis of Rethrown Exceptions��������������������������������������������������������������������������������� 497
Throwing Too Many Exceptions������������������������������������������������������������������������������������ 497
Accessing the Stack of a Thread���������������������������������������������������������������������������������� 499
The try-with-resources Block��������������������������������������������������������������������������������������� 502
Summary���������������������������������������������������������������������������������������������������������������������� 509

■Chapter 14: Assertions�������������������������������������������������������������������������������������� 511
What Is an Assertion?��������������������������������������������������������������������������������������������������� 511
Testing Assertions�������������������������������������������������������������������������������������������������������� 513
Enabling/Disabling Assertions�������������������������������������������������������������������������������������� 515
Using Assertions����������������������������������������������������������������������������������������������������������� 517
Checking for Assertion Status�������������������������������������������������������������������������������������� 518
Summary���������������������������������������������������������������������������������������������������������������������� 519

■Chapter 15: Strings������������������������������������������������������������������������������������������� 521
What Is a String?���������������������������������������������������������������������������������������������������������� 521
String Literals��������������������������������������������������������������������������������������������������������������� 522
Escape Sequence Characters in String Literals����������������������������������������������������������� 522
Unicode Escapes in String Literals������������������������������������������������������������������������������� 523
What Is a CharSequence?�������������������������������������������������������������������������������������������� 523
Creating String Objects������������������������������������������������������������������������������������������������ 523
Length of a String��������������������������������������������������������������������������������������������������������� 524
String Literals Are String Objects��������������������������������������������������������������������������������� 524
String Objects Are Immutable�������������������������������������������������������������������������������������� 525
Comparing Strings�������������������������������������������������������������������������������������������������������� 526
String Pool�������������������������������������������������������������������������������������������������������������������� 528

xv
■ Contents

String Operations��������������������������������������������������������������������������������������������������������� 530


Getting the Character at an Index������������������������������������������������������������������������������������������������������� 530
Testing Strings for Equality����������������������������������������������������������������������������������������������������������������� 531
Testing a String to be Empty��������������������������������������������������������������������������������������������������������������� 531
Changing the Case������������������������������������������������������������������������������������������������������������������������������ 532
Searching for a String������������������������������������������������������������������������������������������������������������������������� 532
Representing Values as Strings���������������������������������������������������������������������������������������������������������� 532
Getting a Substring����������������������������������������������������������������������������������������������������������������������������� 533
Trimming a String������������������������������������������������������������������������������������������������������������������������������� 533
Replacing Part of a String������������������������������������������������������������������������������������������������������������������� 533
Matching the Start and End of a String���������������������������������������������������������������������������������������������� 534

Splitting and Joining Strings���������������������������������������������������������������������������������������� 535


Strings in a switch Statement�������������������������������������������������������������������������������������� 536
Testing a String for Palindrome������������������������������������������������������������������������������������ 538
StringBuilder and StringBuffer������������������������������������������������������������������������������������� 539
String Concatenation Operator (+)������������������������������������������������������������������������������� 543
Language-Sensitive String Comparison����������������������������������������������������������������������� 543
Summary���������������������������������������������������������������������������������������������������������������������� 544

■Chapter 16: Dates and Times����������������������������������������������������������������������������� 549
The Date-Time API�������������������������������������������������������������������������������������������������������� 549
Design Principles���������������������������������������������������������������������������������������������������������� 550
A Quick Example���������������������������������������������������������������������������������������������������������� 551
Evolution of Timekeeping��������������������������������������������������������������������������������������������� 552
Time Zones and Daylight Savings Time������������������������������������������������������������������������ 555
Calendar Systems�������������������������������������������������������������������������������������������������������� 556
The Julian Calendar���������������������������������������������������������������������������������������������������������������������������� 556
The Gregorian Calendar���������������������������������������������������������������������������������������������������������������������� 557

ISO-8601 Standards for Datetime�������������������������������������������������������������������������������� 558


Exploring the New Date-Time API�������������������������������������������������������������������������������� 560
The ofXxx() Methods��������������������������������������������������������������������������������������������������������������������������� 560

xvi
■ Contents

The from() Methods���������������������������������������������������������������������������������������������������������������������������� 560


The withXxx() Methods����������������������������������������������������������������������������������������������������������������������� 561
The getXxx() Methods������������������������������������������������������������������������������������������������������������������������� 561
The toXxx() Methods��������������������������������������������������������������������������������������������������������������������������� 561
The atXxx() Methods��������������������������������������������������������������������������������������������������������������������������� 562
The plusXxx() and minusXxx() Methods���������������������������������������������������������������������������������������������� 562
The multipliedBy(), dividedBy(), and negated() Methods�������������������������������������������������������������������� 562

Instants and Durations������������������������������������������������������������������������������������������������� 563


Dividing a Duration by Another Duration��������������������������������������������������������������������������������������������� 566
Converting and Retrieving Duration Parts������������������������������������������������������������������������������������������ 566
Truncating Duration���������������������������������������������������������������������������������������������������������������������������� 567

Human-Scale Time������������������������������������������������������������������������������������������������������� 568


The ZoneOffset Class�������������������������������������������������������������������������������������������������������������������������� 568
The ZoneId Class�������������������������������������������������������������������������������������������������������������������������������� 570
Useful Datetime-Related Enums��������������������������������������������������������������������������������������������������������� 572
Local Date, Time, and Datetime���������������������������������������������������������������������������������������������������������� 577
Offset Time and Datetime������������������������������������������������������������������������������������������������������������������� 582
Zoned Datetime���������������������������������������������������������������������������������������������������������������������������������� 583

Same Instant, Different Times�������������������������������������������������������������������������������������� 587


Clocks��������������������������������������������������������������������������������������������������������������������������� 587
Periods������������������������������������������������������������������������������������������������������������������������� 589
Period Between Two Dates and Times������������������������������������������������������������������������� 591
Partials������������������������������������������������������������������������������������������������������������������������� 593
Adjusting Dates������������������������������������������������������������������������������������������������������������ 595
Querying Datetime Objects������������������������������������������������������������������������������������������ 600
Non-ISO Calendar Systems������������������������������������������������������������������������������������������ 605
Formatting Dates and Times���������������������������������������������������������������������������������������� 607
Using Predefined Formatters�������������������������������������������������������������������������������������������������������������� 607
Using the format() Method of Datetime Classes��������������������������������������������������������������������������������� 609
Using User-Defined Patterns�������������������������������������������������������������������������������������������������������������� 610

xvii
■ Contents

Using Locale Specific Formats����������������������������������������������������������������������������������������������������������� 615


Using the DateTimeFormatterBuilder Class���������������������������������������������������������������������������������������� 617
Parsing Dates and Times���������������������������������������������������������������������������������������������� 618
Legacy Datetime Classes��������������������������������������������������������������������������������������������� 621
The Date Class������������������������������������������������������������������������������������������������������������������������������������ 621
The Calendar Class����������������������������������������������������������������������������������������������������������������������������� 622
The add() Method�������������������������������������������������������������������������������������������������������������������������������� 623
The roll() Method�������������������������������������������������������������������������������������������������������������������������������� 624
Interoperability with Legacy Datetime Classes������������������������������������������������������������ 625
Summary���������������������������������������������������������������������������������������������������������������������� 629

■Chapter 17: Formatting Data����������������������������������������������������������������������������� 631
Formatting Dates���������������������������������������������������������������������������������������������������������� 631
Using Predefined Date Formats���������������������������������������������������������������������������������������������������������� 632
Using Custom Date Formats��������������������������������������������������������������������������������������������������������������� 635
Parsing Dates������������������������������������������������������������������������������������������������������������������������������������� 637
Formatting Numbers���������������������������������������������������������������������������������������������������� 639
Using Predefined Number Formats���������������������������������������������������������������������������������������������������� 640
Using Custom Number Formats���������������������������������������������������������������������������������������������������������� 641
Parsing Numbers�������������������������������������������������������������������������������������������������������������������������������� 642
printf-Style Formatting������������������������������������������������������������������������������������������������� 643
The Big Picture����������������������������������������������������������������������������������������������������������������������������������� 643
The Details������������������������������������������������������������������������������������������������������������������������������������������ 646
Referencing an Argument Inside a Format Specifier�������������������������������������������������������������������������� 648
Using Flags in a Format Specifier������������������������������������������������������������������������������������������������������� 652
Conversion Characters����������������������������������������������������������������������������������������������������������������������� 653
Summary���������������������������������������������������������������������������������������������������������������������� 667

■Chapter 18: Regular Expressions���������������������������������������������������������������������� 669
What Is a Regular Expression?������������������������������������������������������������������������������������� 669
Metacharacters������������������������������������������������������������������������������������������������������������ 672
Character Classes�������������������������������������������������������������������������������������������������������� 673
Predefined Character Classes������������������������������������������������������������������������������������������������������������ 674

xviii
■ Contents

More Powers to Regular Expressions��������������������������������������������������������������������������� 674


Compiling Regular Expressions���������������������������������������������������������������������������������������������������������� 674
Creating a Matcher����������������������������������������������������������������������������������������������������������������������������� 676
Matching the Pattern�������������������������������������������������������������������������������������������������������������������������� 676

Querying a Match��������������������������������������������������������������������������������������������������������� 679


Beware of Backslashes������������������������������������������������������������������������������������������������ 679
Quantifiers in Regular Expressions������������������������������������������������������������������������������ 680
Matching Boundaries��������������������������������������������������������������������������������������������������� 681
Groups and Back Referencing�������������������������������������������������������������������������������������� 682
Using Named Groups���������������������������������������������������������������������������������������������������� 688
Resetting the Matcher�������������������������������������������������������������������������������������������������� 690
Final Words on E-Mail Validations�������������������������������������������������������������������������������� 691
Find-and-Replace Using Regular Expressions������������������������������������������������������������� 691
Streams of Matched Results���������������������������������������������������������������������������������������� 695
Summary���������������������������������������������������������������������������������������������������������������������� 697

■Chapter 19: Arrays�������������������������������������������������������������������������������������������� 701
What Is an Array?��������������������������������������������������������������������������������������������������������� 701
Arrays Are Objects�������������������������������������������������������������������������������������������������������� 703
Accessing Array Elements�������������������������������������������������������������������������������������������� 704
Length of an Array�������������������������������������������������������������������������������������������������������� 705
Initializing Array Elements�������������������������������������������������������������������������������������������� 706
Beware of Reference Type Arrays�������������������������������������������������������������������������������� 708
Explicit Array Initialization�������������������������������������������������������������������������������������������� 709
Limitations of Using Arrays������������������������������������������������������������������������������������������ 710
Simulating Variable-Length Arrays������������������������������������������������������������������������������� 714
Passing an Array as a Parameter��������������������������������������������������������������������������������� 717
Array Parameter Reference���������������������������������������������������������������������������������������������������������������� 722
Elements of the Array Parameter�������������������������������������������������������������������������������������������������������� 723
The Object Referred by the Array Parameter Elements���������������������������������������������������������������������� 724

xix
■ Contents

Command-Line Arguments������������������������������������������������������������������������������������������� 726


Multi-Dimensional Arrays��������������������������������������������������������������������������������������������� 730
Accessing Elements of a Multi-Dimensional Array����������������������������������������������������������������������������� 734
Initializing Multi-Dimensional Arrays�������������������������������������������������������������������������������������������������� 734

Enhanced for Loop for Arrays��������������������������������������������������������������������������������������� 735


Array Declaration Syntax���������������������������������������������������������������������������������������������� 736
Runtime Array Bounds Checks������������������������������������������������������������������������������������� 737
What Is the Class of an Array Object?�������������������������������������������������������������������������� 738
Array Assignment Compatibility����������������������������������������������������������������������������������� 740
Converting an ArrayList/Vector to an Array������������������������������������������������������������������ 742
Performing Array Operations���������������������������������������������������������������������������������������� 743
Converting Arrays to Another Type������������������������������������������������������������������������������������������������������ 745
Searching an Array����������������������������������������������������������������������������������������������������������������������������� 746
Comparing Arrays������������������������������������������������������������������������������������������������������������������������������� 746
Copying Arrays������������������������������������������������������������������������������������������������������������������������������������ 748
Filling Arrays��������������������������������������������������������������������������������������������������������������������������������������� 748
Computing Hash Code������������������������������������������������������������������������������������������������������������������������ 749
Performing Parallel Accumulation������������������������������������������������������������������������������������������������������ 749
Sorting Arrays������������������������������������������������������������������������������������������������������������������������������������� 750

Summary���������������������������������������������������������������������������������������������������������������������� 750

■Chapter 20: Inheritance������������������������������������������������������������������������������������� 755
What Is Inheritance?���������������������������������������������������������������������������������������������������� 755
Inheriting Classes��������������������������������������������������������������������������������������������������������� 756
The Object Class Is the Default Superclass������������������������������������������������������������������ 759
Inheritance and Hierarchical Relationship������������������������������������������������������������������� 759
What Is Inherited by a Subclass?��������������������������������������������������������������������������������� 760
Upcasting and Downcasting����������������������������������������������������������������������������������������� 762
The instanceof Operator����������������������������������������������������������������������������������������������� 766

xx
■ Contents

Binding������������������������������������������������������������������������������������������������������������������������� 768
Early Binding�������������������������������������������������������������������������������������������������������������������������������������� 769
Late Binding���������������������������������������������������������������������������������������������������������������������������������������� 772

Method Overriding�������������������������������������������������������������������������������������������������������� 775


Method Overriding Rule #1����������������������������������������������������������������������������������������������������������������� 777
Method Overriding Rule #2����������������������������������������������������������������������������������������������������������������� 777
Method Overriding Rule #3����������������������������������������������������������������������������������������������������������������� 777
Method Overriding Rule #4����������������������������������������������������������������������������������������������������������������� 777
Method Overriding Rule #5����������������������������������������������������������������������������������������������������������������� 778
Method Overriding Rule #6����������������������������������������������������������������������������������������������������������������� 779

Accessing Overridden Method������������������������������������������������������������������������������������� 782


Method Overloading����������������������������������������������������������������������������������������������������� 784
Inheritance and Constructors��������������������������������������������������������������������������������������� 788
Method Hiding�������������������������������������������������������������������������������������������������������������� 796
Field Hiding������������������������������������������������������������������������������������������������������������������ 798
Disabling Inheritance��������������������������������������������������������������������������������������������������� 802
Abstract Classes and Methods������������������������������������������������������������������������������������� 803
Method Overriding and Generic Method Signatures���������������������������������������������������� 811
Typo Danger in Method Overriding������������������������������������������������������������������������������� 813
Is-a, has-a, and part-of Relationships�������������������������������������������������������������������������� 814
No Multiple Inheritance of Classes������������������������������������������������������������������������������� 817
Summary���������������������������������������������������������������������������������������������������������������������� 817

■Chapter 21: Interfaces��������������������������������������������������������������������������������������� 823
What Is an Interface?��������������������������������������������������������������������������������������������������� 823
Proposed Solution #1������������������������������������������������������������������������������������������������������������������������� 826
Proposed Solution #2������������������������������������������������������������������������������������������������������������������������� 827
Proposed Solution #3������������������������������������������������������������������������������������������������������������������������� 828
An Ideal Solution��������������������������������������������������������������������������������������������������������������������������������� 828

xxi
■ Contents

Declaring an Interface�������������������������������������������������������������������������������������������������� 833


Declaring Interface Members��������������������������������������������������������������������������������������� 834
Constant Fields Declarations�������������������������������������������������������������������������������������������������������������� 834
Methods Declarations������������������������������������������������������������������������������������������������������������������������� 836
Nested Type Declarations������������������������������������������������������������������������������������������������������������������� 846

An Interface Defines a New Type���������������������������������������������������������������������������������� 848


Implementing an Interface������������������������������������������������������������������������������������������� 851
Implementing Interface Methods��������������������������������������������������������������������������������� 855
Implementing Multiple Interfaces�������������������������������������������������������������������������������� 858
Implementing an Interface Partially����������������������������������������������������������������������������� 861
The Supertype-Subtype Relationship��������������������������������������������������������������������������� 863
Interface Inheritance���������������������������������������������������������������������������������������������������� 864
The Superinterface-Subinterface Relationship������������������������������������������������������������ 870
Inheriting Conflicting Implementations������������������������������������������������������������������������ 870
The Superclass Always Wins�������������������������������������������������������������������������������������������������������������� 871
The Most Specific Superinterface Wins���������������������������������������������������������������������������������������������� 873
The Class Must Override the Conflicting Method������������������������������������������������������������������������������� 874

The instanceof Operator����������������������������������������������������������������������������������������������� 875


Marker Interfaces��������������������������������������������������������������������������������������������������������� 879
Functional Interfaces���������������������������������������������������������������������������������������������������� 880
Comparing Objects������������������������������������������������������������������������������������������������������� 880
Using the Comparable Interface��������������������������������������������������������������������������������������������������������� 880
Using the Comparator Interface���������������������������������������������������������������������������������������������������������� 883

Polymorphism—One Object, Many Views�������������������������������������������������������������������� 887


Dynamic Binding and Interfaces���������������������������������������������������������������������������������� 889
Summary���������������������������������������������������������������������������������������������������������������������� 890

■Chapter 22: Enum Types������������������������������������������������������������������������������������ 895
What Is an Enum Type?������������������������������������������������������������������������������������������������ 895
The Superclass of an Enum Type��������������������������������������������������������������������������������� 899

xxii
■ Contents

Using Enum Types in switch Statements��������������������������������������������������������������������� 903


Associating Data and Methods to Enum Constants������������������������������������������������������ 903
Associating a Body to an Enum Constant��������������������������������������������������������������������� 905
Comparing Two Enum Constants���������������������������������������������������������������������������������� 910
Nested Enum Types������������������������������������������������������������������������������������������������������ 911
Implementing an Interface to an Enum Type���������������������������������������������������������������� 913
Reverse Lookup for Enum Constants��������������������������������������������������������������������������� 914
Range of Enum Constants�������������������������������������������������������������������������������������������� 914
Summary���������������������������������������������������������������������������������������������������������������������� 916

■Chapter 23: Java Shell��������������������������������������������������������������������������������������� 921
What Is the Java Shell?������������������������������������������������������������������������������������������������ 922
The JShell Architecture������������������������������������������������������������������������������������������������ 923
Starting the JShell Tool������������������������������������������������������������������������������������������������ 924
Exiting the JShell Tool�������������������������������������������������������������������������������������������������� 927
What Are Snippets and Commands?���������������������������������������������������������������������������� 927
Evaluating Expressions������������������������������������������������������������������������������������������������ 929
Listing Snippets������������������������������������������������������������������������������������������������������������ 931
Editing Snippets����������������������������������������������������������������������������������������������������������� 935
Rerunning Previous Snippets��������������������������������������������������������������������������������������� 937
Declaring Variables������������������������������������������������������������������������������������������������������� 937
Import Statements�������������������������������������������������������������������������������������������������������� 940
Method Declarations���������������������������������������������������������������������������������������������������� 944
Type Declarations��������������������������������������������������������������������������������������������������������� 945
Setting the Execution Environment������������������������������������������������������������������������������ 948
No Checked Exceptions������������������������������������������������������������������������������������������������ 950
Auto-Completion���������������������������������������������������������������������������������������������������������� 950
Snippets and Commands History��������������������������������������������������������������������������������� 954
Reading JShell Stack Trace������������������������������������������������������������������������������������������ 955

xxiii
■ Contents

Reusing JShell Sessions���������������������������������������������������������������������������������������������� 956


Resetting the JShell State�������������������������������������������������������������������������������������������� 958
Reloading the JShell State������������������������������������������������������������������������������������������� 958
Configuring JShell�������������������������������������������������������������������������������������������������������� 961
Setting the Snippet Editor������������������������������������������������������������������������������������������������������������������� 961
Setting Feedback Mode���������������������������������������������������������������������������������������������������������������������� 962
Creating Custom feedback Modes������������������������������������������������������������������������������������������������������ 965
Setting Up Startup Snippets��������������������������������������������������������������������������������������������������������������� 969
Using JShell Documentation���������������������������������������������������������������������������������������� 972
The JShell API��������������������������������������������������������������������������������������������������������������� 974
Creating a JShell�������������������������������������������������������������������������������������������������������������������������������� 975
Working with Snippets����������������������������������������������������������������������������������������������������������������������� 976
Handling Snippet Events��������������������������������������������������������������������������������������������������������������������� 978
An Example����������������������������������������������������������������������������������������������������������������������������������������� 978
Summary���������������������������������������������������������������������������������������������������������������������� 982

■Appendix A: Character Encodings��������������������������������������������������������������������� 985
ASCII����������������������������������������������������������������������������������������������������������������������������� 986
8-Bit Character Sets����������������������������������������������������������������������������������������������������� 990
Universal Multiple-Octet Coded Character Set (UCS)��������������������������������������������������� 991
UCS-2������������������������������������������������������������������������������������������������������������������������������������������������� 992
UCS-4������������������������������������������������������������������������������������������������������������������������������������������������� 992
UTF-16 (UCS Transformation Format 16)�������������������������������������������������������������������������������������������� 992
UTF-8 (UCS Transformation Format 8)������������������������������������������������������������������������������������������������ 993

Java and Character Encodings������������������������������������������������������������������������������������� 994



■Appendix B: Documentation Comments������������������������������������������������������������ 997
Writing Documentation Comments������������������������������������������������������������������������������ 998
List of Block and Inline Tags��������������������������������������������������������������������������������������� 1000
@author <author-name(s)>������������������������������������������������������������������������������������������������������������� 1001
@deprecated <explanation-text>���������������������������������������������������������������������������������������������������� 1001
@exception <class-name> <description>�������������������������������������������������������������������������������������� 1001
@param <parameter-name> <description>����������������������������������������������������������������������������������� 1002

xxiv
■ Contents

@return <description>��������������������������������������������������������������������������������������������������������������������� 1002


@see <reference>��������������������������������������������������������������������������������������������������������������������������� 1002
@serial <field-description or include/exclude>������������������������������������������������������������������������������� 1003
@serialData <data-description>������������������������������������������������������������������������������������������������������ 1004
@serialField <field-name> <field-type> <field-description>��������������������������������������������������������� 1004
@since <description>���������������������������������������������������������������������������������������������������������������������� 1005
@throws <class-name> <description>������������������������������������������������������������������������������������������� 1005
@version <version-text>����������������������������������������������������������������������������������������������������������������� 1006
{@code <text>}�������������������������������������������������������������������������������������������������������������������������������� 1006
{@docRoot}��������������������������������������������������������������������������������������������������������������������������������������� 1006
{@inheritDoc}����������������������������������������������������������������������������������������������������������������������������������� 1006
{@link <package.class#member> <label>}������������������������������������������������������������������������������������ 1008
{@linkplain <package.class#member> <label>}����������������������������������������������������������������������������� 1008
{@literal <text>}������������������������������������������������������������������������������������������������������������������������������� 1008
{@value <package.class#field>}������������������������������������������������������������������������������������������������������ 1008
@hidden������������������������������������������������������������������������������������������������������������������������������������������� 1009
{@index <keyword> <description>}������������������������������������������������������������������������������������������������ 1009
@provides <service-type> <description>����������������������������������������������������������������� 1010
@uses <service-type> <description>����������������������������������������������������������������������� 1010
Documenting Packages���������������������������������������������������������������������������������������������� 1010
com/jdojo/utility/package-info.java file�������������������������������������������������������������������������������������������� 1011
com/jdojo/utility/package.html file��������������������������������������������������������������������������������������������������� 1011
Overview Documentation������������������������������������������������������������������������������������������� 1012
Including Unprocessed Files in Documentation��������������������������������������������������������� 1012
Skipping Source Files Processing������������������������������������������������������������������������������ 1012
An Example of Documentation Comments����������������������������������������������������������������� 1012
Running the javadoc Tool������������������������������������������������������������������������������������������� 1015
Generated Documentation Files��������������������������������������������������������������������������������� 1017
Viewing Generated HTML Documentation������������������������������������������������������������������ 1017
Searching Javadoc����������������������������������������������������������������������������������������������������� 1019
Summary�������������������������������������������������������������������������������������������������������������������� 1020

Index������������������������������������������������������������������������������������������������������������������� 1023
xxv
About the Author

Kishori Sharan works as a senior software engineer lead at IndraSoft,


Inc. He earned a master’s of science degree in computer information
systems from Troy State University, Alabama. He is a Sun-certified Java 2
programmer and has over 20 years of experience in developing enterprise
applications and providing training to professional developers using the
Java platform.

xxvii
About the Technical Reviewer

Wallace Jackson has been writing for leading multimedia publications about his work in new media content
development since the advent of Multimedia Producer Magazine nearly two decades ago. He has authored
a half-dozen Android book titles for Apress, including four titles in the popular Pro Android series. Wallace
received his undergraduate degree in business economics from the University of California at Los Angeles
and a graduate degree in MIS design and implementation from the University of Southern California. He is
currently the CEO of Mind Taffy Design, a new media content production and digital campaign design and
development agency.

xxix
Acknowledgments

I would like to thank my family members and friends for their encouragement and support: my mom
Pratima Devi; my elder brothers, Janki Sharan and Dr. Sita Sharan; my nephews, Gaurav and Saurav;
my sister Ratna; my friends Karthikeya Venkatesan, Rahul Nagpal, Ravi Datla, Mahbub Choudhury,
Richard Castillo; and many more friends not mentioned here.
My wife, Ellen, was always patient when I spent long hours at my computer desk working on this book.
I want to thank her for all of her support in writing this book.
My special thanks to my friend Preethi Vasudev for offering her valuable time for providing solutions
to the exercises in this book. She likes programming challenges—particularly Google Code Jam. I bet she
enjoyed solving the exercises in each chapter of this book.
My sincere thanks are due to the wonderful team at Apress for their support during the publication of
this book. Thanks to Mark Powers, the Editorial Operations Manager, for providing excellent support. Last
but not least, my sincere thanks to Steve Anglin, the Lead Editor at Apress, for taking the initiative for the
publication of this book.

xxxi
Another Random Scribd Document
with Unrelated Content
South Carolina—Mr. Kean, aye; Mr. Huger, aye.
Georgia—Mr. Few, aye; Mr. Pierce, aye.
So it was resolved in the affirmative." (Page 754, volume 4.)

The bare reading of these passages from the Journals of the


Congress of the old confederation, shows how erroneous Mr.
Webster was in these portions of his speech:

"At the foundation of the constitution of these new


northwestern States, we are accustomed, sir, to praise the
lawgivers of antiquity; we help to perpetuate the fame of Solon
and Lycurgus; but I doubt whether one single law of any
lawgiver, ancient or modern, has produced effects of more
distinct, marked, and lasting character, than the ordinance of
'87. That instrument, was drawn by Nathan Dane, then, and
now, a citizen of Massachusetts. It was adopted, as I think I
have understood, without the slightest alteration; and certainly
it has happened to few men to be the authors of a political
measure of more large and enduring consequence. It fixed, for
ever, the character of the population in the vast regions
northwest of the Ohio, by excluding from them involuntary
servitude. It impressed on the soil itself, while it was yet a
wilderness, an incapacity to bear up any other than free men. It
laid the interdict against personal servitude, in original compact,
not only deeper than all local law, but deeper, also, than all local
constitutions. Under the circumstances then existing, I look
upon this original and seasonable provision, as a real good
attained. We see its consequences at this moment, and we shall
never cease to see them, perhaps, while the Ohio shall flow. It
was a great and salutary measure of prevention. Sir, I should
fear the rebuke of no intelligent gentleman of Kentucky, were I
to ask whether if such an ordinance could have been applied to
his own State, while it yet was a wilderness, and before Boon
had passed the gap of the Alleghany, he does not suppose it
would have contributed to the ultimate greatness of that
commonwealth? It is, at any rate, not to be doubted, that where
it did apply it has produced an effect not easily to be described,
or measured in the growth of the States, and the extent and
increase of their population. Now, sir, this great measure again
was carried by the north, and by the north alone. There were,
indeed, individuals elsewhere favorable to it; but it was
supported as a measure, entirely by the votes of the northern
States. If New England had been governed by the narrow and
selfish views now ascribed to her, this very measure was, of all
others, the best calculated to thwart her purposes. It was, of all
things, the very means of rendering certain a vast emigration
from her own population to the west. She looked to that
consequence only to disregard it. She deemed the regulation a
most useful one to the States that would spring up on the
territory, and advantageous to the country at large. She adhered
to the principle of it perseveringly, year after year, until it was
finally accomplished.
"An attempt has been made to transfer, from the North to the
South, the honor of this exclusion of slavery from the
northwestern territory. The journal, without argument or
comment, refutes such attempt. The cession by Virginia was
made, March, 1784. On the 19th of April following, a committee,
consisting of Messrs. Jefferson, Chase, and Howell, reported a
plan for a temporary government of the territory, in which was
this article: 'that, after the year 1800, there shall be neither
slavery, nor involuntary servitude in any of the said States,
otherwise than in punishment of crimes, whereof the party shall
have been convicted.' Mr. Speight, of North Carolina, moved to
strike out this paragraph. The question was put, according to
the form then practised: 'Shall these words stand, as part of the
plan,' &c.? New Hampshire, Massachusetts, Rhode Island,
Connecticut, New York, New Jersey, and Pennsylvania—seven
States, voted in the affirmative. Maryland, Virginia, and South
Carolina, in the negative. North Carolina was divided. As the
consent of nine States was necessary, the words could not
stand, and were struck out accordingly. Mr. Jefferson voted for
the clause, but was overruled by his colleagues.
"In March, the next year [1785], Mr. King of Massachusetts,
seconded by Mr. Ellery of Rhode Island, proposed the formerly
rejected article, with this addition: 'And that this regulation shall
be an article of compact, and remain a fundamental principle of
the constitutions between the thirteen original States, and each
of the States described in the resolve,' &c. On this clause, which
provided the adequate and thorough security, the eight northern
States at that time voted affirmatively, and the four southern
States negatively. The votes of nine States were not yet
obtained, and thus, the provision was again rejected by the
southern States. The perseverance of the north held out, and
two years afterwards the object was attained."

This is shown to be all erroneous in relation to this ordinance. It


was not first drawn by Mr. Dane, but by Mr. Jefferson, and that
nearly two years before Mr. Dane came into Congress. It was not
passed by the North alone, but equally by the South—there being
but eight States present at the passing, and they equally of the
North and the South—and the South voting unanimously for it, both
as States and as individual members, while the North had one
member against it. It was not baffled two years for the want of nine
States; if so, and nine States had been necessary, it would not have
been passed when it was, and never by free State votes alone.
There were but eight States (both Northern and Southern) present
at the passing; and there were not nine free States in the
confederacy at that time. There were but thirteen in all: and the half
of these, as nearly as thirteen can be divided, were slave States. The
fact is, that the South only delayed its vote for the antislavery clause
in the ordinance for want of the provision in favor of recovering
fugitives from service. As soon as that was added, she took the lead
again for the ordinance—a fact which gives great emphasis to the
corresponding provision in the constitution.
Mr. Webster was present when I read these extracts, and said
nothing. He neither reaffirmed his previous statement, that Mr. Dane
was the author of the ordinance, and that "this great measure was
carried by the North, and by the North alone." He said nothing; nor
did he afterwards correct the errors of his speech: and they now
remain in it; and have given occasion to a very authentic newspaper
contradiction of his statement, copied, like my statement to the
Senate, from the Journals of the old Congress. It was by Edward
Coles, Esq., formerly of Virginia, and private secretary to President
Madison, afterwards governor of the State of Illinois, and now a
citizen of Pennsylvania, resident of Philadelphia. He made his
correction through the National Intelligencer, of Washington City;
and being drawn from the same sources it agrees entirely with my
own. And thus the South is entitled to the credit of originating and
passing this great measure—a circumstance to be remembered and
quoted, as showing the South at that time in taking the lead in
curtailing and restricting the existence of slavery. The cause of Mr.
Webster's mistakes may be found in the fact that the ordinance was
three times before the old Congress, and once (the third time) in the
hands of a committee of which Mr. Dane was a member. It was first
reported by a committee of three (April, 1784) of which two were
from slave states, (Mr. Jefferson of Virginia and Mr. Chase of
Maryland,) Mr. Howard, of Rhode Island; and this, as stated, was
nearly two years before Mr. Dane became a member. The antislavery
clause was then dropped, there being but six States for it. The next
year, the antislavery clause, with some modification, was moved by
Mr. Rufus King, and sent as a proposition to a committee: but did
not ripen into a law. Afterwards the whole ordinance was passed as
it now stands, upon the report of a committee of six, of whom Mr.
Dane was one; but not the chairman.
Closely connected with this question of authorship to which Mr.
Webster's remarks give rise, was another which excited some warm
discussion—the topic of slavery—and the effect of its existence or
non-existence in different States. Kentucky and Ohio were taken for
examples, and the superior improvement and population of Ohio
were attributed to its exemption from the evils of slavery. This was
an excitable subject, and the more so because the wounds of the
Missouri controversy, in which the North was the undisputed
aggressor, were still tender, and hardly scarred over. Mr. Hayne
answered with warmth and resented as a reflection upon the slave
States this disadvantageous comparison. I replied to the same topic
myself, and said:

"I was on the subject of slavery, as connected with the


Missouri question, when last on the floor. The senator from
South Carolina [Mr. Hayne] could see nothing in the question
before the Senate, nor in any previous part of the debate, to
justify the introduction of that topic. Neither could I. He thought
he saw the ghost of the Missouri question brought in among us.
So did I. He was astonished at the apparition. I was not: for a
close observance of the signs in the West had prepared me for
this development from the East. I was well prepared for that
invective against slavery, and for that amplification of the
blessings of exemption from slavery, exemplified in the condition
of Ohio, which the senator from Massachusetts indulged in, and
which the object in view required to be derived from the
Northeast. I cut the root of that derivation by reading a passage
from the Journals of the old Congress; but this will not prevent
the invective and encomium from going forth to do their office;
nor obliterate the line which was drawn between the free State
of Ohio and the slave State of Kentucky. If the only results of
this invective and encomium were to exalt still higher the
oratorical fame of the speaker, I should spend not a moment in
remarking upon them. But it is not to be forgotten that the
terrible Missouri agitation took its rise from the "substance of
two speeches" delivered on this floor; and since that time,
antislavery speeches, coming from the same political and
geographical quarter, are not to be disregarded here. What was
said upon that topic was certainly intended for the north side of
the Potomac and Ohio; to the people, then, of that division of
the Union, I wish to address myself, and to disabuse them of
some erroneous impressions. To them I can truly say, that
slavery, in the abstract, has but few advocates or defenders in
the slave-holding States, and that slavery as it is, an hereditary
institution descended upon us from our ancestors, would have
fewer advocates among us than it has, if those who have
nothing to do with the subject would only let us alone. The
sentiment in favor of slavery was much weaker before those
intermeddlers began their operations than it is at present. The
views of leading men in the North and the South were
indisputably the same in the earlier periods of our government.
Of this our legislative history contains the highest proof. The
foreign-slave trade was prohibited in Virginia, as soon as the
Revolution began. It was one of her first acts of sovereignty. In
the convention of that State which adopted the federal
constitution, it was an objection to that instrument that it
tolerated the African slave-trade for twenty years. Nothing that
has appeared since has surpassed the indignant denunciations
of this traffic by Patrick Henry, George Mason, and others, in
that convention.
"Sir, I regard with admiration, that is to say, with wonder, the
sublime morality of those who cannot bear the abstract
contemplation of slavery, at the distance of five hundred or a
thousand miles off. It is entirely above, that is to say, it affects a
vast superiority over the morality of the primitive Christians, the
apostles of Christ, and Christ himself. Christ and the apostles
appeared in a province of the Roman empire, when that empire
was called the Roman world, and that world was filled with
slaves. Forty millions was the estimated number, being one-
fourth of the whole population. Single individuals held twenty
thousand slaves. A freed man, one who had himself been a
slave, died the possessor of four thousand—such were the
numbers. The rights of the owners over this multitude of human
beings was that of life and death, without protection from law or
mitigation from public sentiment. The scourge, the cross, the
fish-pond, the den of the wild beast, and the arena of the
gladiator, was the lot of the slave, upon the slightest expression
of the master's will. A law of incredible atrocity made all slaves
responsible with their own lives for the life of their master; it
was the law that condemned the whole household of slaves to
death, in case of the assassination of the master—a law under
which as many as four hundred have been executed at a time.
And these slaves were the white people of Europe and of Asia
Minor, the Greeks and other nations, from whom the present
inhabitants of the world derive the most valuable productions of
the human mind. Christ saw all this—the number of the slaves—
their hapless condition—and their white color, which was the
same with his own; yet he said nothing against slavery; he
preached no doctrines which led to insurrection and massacre;
none which, in their application to the state of things in our
country, would authorize an inferior race of blacks to
exterminate that superior race of whites, in whose ranks he
himself appeared upon earth. He preached no such doctrines,
but those of a contrary tenor, which inculcated the duty of
fidelity and obedience on the part of the slave—humanity and
kindness on the part of the master. His apostles did the same.
St. Paul sent back a runaway slave. Onesimus, to his owner,
with a letter of apology and supplication. He was not the man to
harbor a runaway, much less to entice him from his master;
and, least of all, to excite an insurrection."

This allusion to the Missouri controversy, and invective against the


free States for their part in it, brought a reply from Mr. Webster,
showing what their conduct had been at the first introduction of the
slavery topic in the Congress of the United States, and that they
totally refused to interfere between master and slave in any way
whatever. This is what he said:

"When the present constitution was submitted for the


ratification of the people, there were those who imagined that
the powers of the government which it proposed to establish
might, perhaps, in some possible mode, be exerted in measures
tending to the abolition of slavery. This suggestion would, of
course, attract much attention in the southern conventions. In
that of Virginia, Governor Randolph said:
"'I hope there is none here who, considering the subject in
the calm light of philosophy, will make an objection dishonorable
to Virginia—that, at the moment they are securing the rights of
their citizens, an objection is started, that there is a spark of
hope that those unfortunate men now held in bondage may, by
the operation of the general government, be made free.'
"At the very first Congress, petitions on the subject were
presented, if I mistake not, from different States. The
Pennsylvania society for promoting the abolition of slavery, took
a lead, and laid before Congress a memorial, praying Congress
to promote the abolition by such powers as it possessed. This
memorial was referred, in the House of Representatives, to a
select committee consisting of Mr. Foster of New Hampshire; Mr.
Gerry of Massachusetts, Mr. Huntington of Connecticut; Mr.
Lawrence of New-York; Mr. Sinnickson of New Jersey; Mr.
Hartley of Pennsylvania, and Mr. Parker of Virginia; all of them,
sir, as you will observe, northern men, but the last. This
committee made a report, which was committed to a committee
of the whole house, and there considered and discussed on
several days; and being amended, although in no material
respect, it was made to express three distinct propositions on
the subject of slavery and the slave-trade. First, in the words of
the constitution, that Congress could not, prior to the year
1808, prohibit the migration or importation of such persons as
any of the States, then existing, should think proper to admit.
Second, that Congress had authority to restrain the citizens of
the United States from carrying on the African slave-trade, for
the purpose of supplying foreign countries. On this proposition,
our laws against those who engage in that traffic, are founded.
The third proposition, and that which bears on the present
question, was expressed in the following terms:
"'Resolved, That Congress have no authority to interfere in
the emancipation of slaves, or in the treatment of them in any
of the States; it remaining with the several States alone to
provide rules and regulations therein, which humanity and true
policy may require.'
"This resolution received the sanction of the House of
Representatives so early as March, 1790. And now, sir, the
honorable member will allow me to remind him, that not only
were the select committee who reported the resolution, with a
single exception, all northern men, but also that of the members
then composing the House of Representatives, a large majority,
I believe nearly two thirds, were northern men also.
"The house agreed to insert these resolutions in its journal,
and, from that day to this, it has never been maintained or
contended that Congress had any authority to regulate, or
interfere with, the condition of slaves in the several States. No
northern gentleman, to my knowledge, has moved any such
question in either house of Congress.
"The fears of the South, whatever fears they might have
entertained, were allayed and quieted by this early decision;
and so remained, till they were excited afresh, without cause,
but for collateral and indirect purposes. When it became
necessary, or was thought so, by some political persons, to find
an unvarying ground for the exclusion of northern men from
confidence and from lead in the affairs of the republic, then,
and not till then, the cry was raised, and the feeling
industriously excited, that the influence of northern men in the
public councils would endanger the relation of master and slave.
For myself I claim no other merit than that this gross and
enormous injustice towards the whole North, has not wrought
upon me to change my opinions, or my political conduct. I hope
I am above violating my principles, even under the smart of
injury and false imputations. Unjust suspicions and undeserved
reproach, whatever pain I may experience from them, will not
induce me, I trust, nevertheless, to overstep the limits of
constitutional duty, or to encroach on the rights of others. The
domestic slavery of the South I leave where I find it—in the
hands of their own governments. It is their affair, not mine. Nor
do I complain of the peculiar effect which the magnitude of that
population has had in the distribution of power under this
federal government. We know, sir, that the representation of the
states in the other house is not equal. We know that great
advantage, in that respect, is enjoyed by the slaveholding
States; and we know, too, that the intended equivalent for that
advantage, that is to say, the imposition of direct taxes in the
same ratio, has become merely nominal; the habit of the
government being almost invariably to collect its revenues from
other sources, and in other modes. Nevertheless, I do not
complain: nor would I countenance any movement to alter this
arrangement of representation. It is the original bargain, the
compact—let it stand: let the advantage of it be fully enjoyed.
The Union itself is too full of benefit to be hazarded in
propositions for changing its original basis. I go for the
constitution as it is, and for the Union as it is. But I am resolved
not to submit, in silence, to accusations, either against myself
individually, or against the North, wholly unfounded and unjust;
accusations which impute to us a disposition to evade the
constitutional compact, and to extend the power of the
government over the internal laws and domestic condition of
the States. All such accusations, wherever and whenever made,
all insinuations of the existence of any such purposes, I know,
and feel to be groundless and injurious. And we must confide in
southern gentlemen themselves; we must trust to those whose
integrity of heart and magnanimity of feeling will lead them to a
desire to maintain and disseminate truth, and who possess the
means of its diffusion with the southern public; we must leave it
to them to disabuse that public of its prejudices. But, in the
mean time, for my own part, I shall continue to act justly,
whether those towards whom justice is exercised, receive it with
candor or with contumely."
This is what Mr. Webster said on the subject of slavery; and
although it was in reply to an invective of my own, excited by the
recent agitation of the Missouri question, I made no answer
impugning its correctness; and must add that I never saw any thing
in Mr. Webster inconsistent with what he then said; and believe that
the same resolves could have been passed in the same way at any
time during the thirty years that I was in Congress.
But the topic which became the leading feature of the whole
debate; and gave it an interest which cannot die, was that of
nullification—the assumed right of a state to annul an act of
Congress—then first broached in our national legislature—and in the
discussion of which Mr. Webster and Mr. Hayne were the champion
speakers on opposite sides—the latter understood to be speaking
the sentiments of the Vice-President, Mr. Calhoun. This new turn in
the debate was thus brought about: Mr. Hayne, in the sectional
nature of the discussion which had grown up, made allusions to the
conduct of New England during the war of 1812; and especially to
the assemblage known as the Hartford Convention, and to which
designs unfriendly to the Union had been attributed. This gave Mr.
Webster the rights both of defence and of retaliation; and he found
material for the first in the character of the assemblage, and for the
second in the public meetings which had taken place in South
Carolina on the subject of the tariff—and at which resolves were
passed, and propositions adopted significant of resistance to the act;
and, consequently, of disloyalty to the Union. He, in his turn, made
allusions to these resolves and propositions, until he drew out Mr.
Hayne into their defence, and into an avowal of what has since
obtained the current name of "Nullification;" although at the time
(during the debate) it did not at all strike me as going the length
which it afterwards avowed; nor have I ever believed that Mr. Hayne
contemplated disunion, in any contingency, as one of its results. In
entering upon the argument, Mr. Webster first summed up the
doctrine, as he conceived it to be avowed, thus:
"I understand the honorable gentleman from South Carolina
to maintain, that it is a right of the State legislature to interfere,
whenever, in their judgment, this government transcends its
constitutional limits, and to arrest the operation of its laws.
"I understand him to maintain this right, as a right existing
under the constitution; not as a right to overthrow it, on the
ground of extreme necessity, such as would justify violent
revolution.
"I understand him to maintain an authority, on the part of the
States, thus to interfere, for the purpose of correcting the
exercise of power by the general government, of checking it,
and of compelling it to conform to their opinion of the extent of
its powers.
"I understand him to maintain that the ultimate power of
judging of the constitutional extent of its own authority is not
lodged exclusively in the general government, or any branch of
it; but that, on the contrary, the States may lawfully decide for
themselves, and each State for itself, whether, in a given case,
the act of the general government transcends its power.
"I understand him to insist that, if the exigency of the case, in
the opinion of any State government, require it, such State
government may, by its own sovereign authority, annul an act of
the general government, which it deems plainly and palpably
unconstitutional."

Mr. Hayne, evidently unprepared to admit, or fully deny, the


propositions as broadly laid down, had recourse to a statement of
his own; and, adopted for that purpose, the third resolve of the
Virginia resolutions of the year 1798—reaffirmed in 1799. He rose
immediately and said that, for the purpose of being clearly
understood, he would state that his proposition was in the words of
the Virginia resolution; and read it—
"That this Assembly doth explicitly and peremptorily declare,
that it views the powers of the federal government as resulting
from the compact, to which the States are parties, as limited by
the plain sense and intention of the instrument constituting that
compact, as no farther valid than they are authorized by the
grants enumerated in that compact; and that, in case of a
deliberate, palpable, and dangerous exercise of other powers,
not granted by the said compact, the States who are parties
thereto have the right, and are in duty bound, to interpose, for
arresting the progress of the evil, and for maintaining, within
their respective limits, the authorities, rights, and liberties,
appertaining to them."

Thus were the propositions stated, and argued—each speaker


taking his own proposition for his text; which in the end, (and as the
Virginia resolutions turned out to be understood in the South
Carolina sense) came to be identical. Mr. Webster, at one point,
giving to his argument a practical form, and showing what the South
Carolina doctrine would have accomplished in New England if it had
been acted upon by the Hartford Convention, said:

"Let me here say, sir, that, if the gentleman's doctrine had


been received and acted upon in New England, in the times of
the embargo and non-intercourse, we should probably not now
have been here. The government would, very likely, have gone
to pieces, and crumbled into dust. No stronger case can ever
arise than existed under those laws; no States can ever
entertain a clearer conviction than the New England States then
entertained; and if they had been under the influence of that
heresy of opinion, as I must call it which the honorable member
espouses, this Union would, in all probability, have been
scattered to the four winds. I ask the gentleman, therefore, to
apply his principles to that case; I ask him to come forth and
declare, whether, in his opinion, the New England States would
have been justified in interfering to break up the embargo
system, under the conscientious opinions which they held upon
it? Had they a right to annul that law? Does he admit or deny?
If that which is thought palpably unconstitutional in South
Carolina, justifies that State in arresting the progress of the law,
tell me, whether that which was thought palpably
unconstitutional also in Massachusetts, would have justified her
in doing the same thing? Sir, I deny the whole doctrine. It has
not a foot of ground in the constitution to stand on. No public
man of reputation ever advanced it in Massachusetts, in the
warmest times, or could maintain himself upon it there at any
time."

He argued that the doctrine had no foundation either in the


constitution, or in the Virginia resolutions—that the constitution
makes the federal government act upon citizens within the States,
and not upon the States themselves, as in the old confederation:
that within their constitutional limits the laws of Congress were
supreme—and that it was treasonable to resist them with force: and
that the question of their constitutionality was to be decided by the
Supreme Court. On this point, he said:

"The people, then, sir, erected this government. They gave it


a constitution; and in that constitution they have enumerated
the powers which they bestow on it. They have made it a
limited government. They have defined its authority. They have
restrained it to the exercise of such powers as are granted; and
all others, they declare, are reserved to the States or to the
people. But, sir, they have not stopped here. If they had, they
would have accomplished but half their work. No definition can
be so clear as to avoid possibility of doubt; no limitation so
precise as to exclude all uncertainty. Who then shall construe
this grant of the people? Who shall interpret their will, where it
may be supposed they have left it doubtful? With whom do they
repose this ultimate right of deciding on the powers of the
government? Sir, they have settled all this in the fullest manner.
They have left it with the government itself, in its appropriate
branches. Sir, the very chief end, the main design, for which the
whole constitution was framed and adopted was, to establish a
government that should not be obliged to act through State
agency, or depend on State opinion and State discretion. The
people had had quite enough of that kind of government under
the confederacy. Under that system, the legal action, the
application of law to individuals, belonged exclusively to the
States. Congress could only recommend; their acts were not of
binding force, till the States had adopted and sanctioned them.
Are we in that condition still? Are we yet at the mercy of State
discretion, and State construction? Sir, if we are, then vain will
be our attempt to maintain the constitution under which we sit.
But, sir, the people have wisely provided, in the constitution
itself, a proper, suitable mode and tribunal for settling questions
of constitutional law. There are, in the constitution, grants of
powers to Congress, and restrictions on these powers. There
are, also, prohibitions on the States. Some authority must,
therefore, necessarily exist, having the ultimate jurisdiction to
fix and ascertain the interpretation of these grants, restrictions,
and prohibitions. The constitution has, itself, pointed out,
ordained, and established, that authority. How has it
accomplished this great and essential end? By declaring, sir, that
'the constitution, and the laws of the United States made in
pursuance thereof, shall be the supreme law of the land, any
thing in the constitution or laws of any State to the contrary
notwithstanding.'
"This, sir, was the first great step. By this, the supremacy of
the constitution and laws of the United States is declared. The
people so will it. No State law is to be valid which comes in
conflict with the constitution or any law of the United States.
But who shall decide this question of interference? To whom lies
the last appeal? This, sir, the constitution itself decides also, by
declaring 'that the judicial power shall extend to all cases arising
under the constitution and laws of the United States.' These two
provisions, sir, cover the whole ground. They are, in truth, the
key-stone of the arch. With these, it is a constitution; without
them it is a confederacy. In pursuance of these clear and
express provisions, Congress established, at its very first
session, in the Judicial Act, a mode for carrying them into full
effect, and for bringing all questions of constitutional power to
the final decision of the Supreme Court. It then, sir, became a
government. It then had the means of self-protection; and, but
for this, it would, in all probability, have been now among things
which are past. Having constituted the government, and
declared its powers, the people have farther said, that, since
somebody must decide on the extent of these powers, the
government shall itself decide; subject, always, like other
popular governments, to its responsibility to the people. And
now, sir, I repeat, how is it that a State legislature acquires any
power to interfere? Who or what gives them the right to say to
the people, 'we, who are your agents and servants for one
purpose, will undertake to decide that your other agents and
servants, appointed by you for another purpose, have
transcended the authority you gave them?' The reply would be,
I think, not impertinent: who made you judge over another's
servants? To their own masters they stand or fall."

With respect to the Virginia resolutions, on which Mr. Hayne relied,


Mr. Webster disputed the interpretation put upon them—claimed for
them an innocent and justifiable meaning—and exempted Mr.
Madison from the suspicion of having penned a resolution asserting
the right of a State legislature to annul an act of Congress, and
thereby putting it in the power of one State to destroy a form of
government which he had just labored so hard to establish. To this
effect he said:

"I wish now, sir, to make a remark upon the Virginia


resolutions of 1798. I cannot undertake to say how these
resolutions were understood by those who passed them. Their
language is not a little indefinite. In the case of the exercise, by
Congress, of a dangerous power, not granted to them, the
resolutions assert the right, on the part of the State, to
interfere, and arrest the progress of the evil. This is susceptible
of more than one interpretation. It may mean no more than
that the States may interfere by complaint and remonstrance; or
by proposing to the people an alteration of the federal
constitution. This would all be quite unobjectionable; or, it may
be, that no more is meant than to assert the general right of
revolution, as against all governments, in cases of intolerable
oppression. This no one doubts; and this, in my opinion, is all
that he who framed the resolutions could have meant by it: for I
shall not readily believe that he (Mr. Madison) was ever of
opinion that a State, under the constitution, and in conformity
with it, could, upon the ground of her own opinion of its
unconstitutionality, however clear and palpable she might think
the case, annul a law of Congress, so far as it should operate on
herself, by her own legislative power."

Mr. Hayne, on his part, disclaimed all imitation of the Hartford


Convention; and gave (as the practical part of his doctrine) the
pledge of forcible resistance to any attempt to enforce
unconstitutional laws. He said:

"Sir, unkind as my allusion to the Hartford Convention has


been considered by its supporters, I apprehend that this
disclaimer of the gentleman will be regarded as 'the unkindest
cut of all.' When the gentleman spoke of the Carolina
conventions of Colleton and Abbeville, let me tell him that he
spoke of that which never had existence, except in his own
imagination. There have, indeed, been meetings of the people
in those districts, composed, sir, of as high-minded and patriotic
men as any country can boast; but we have had no 'convention'
as yet; and when South Carolina shall resort to such a measure
for the redress of her grievances, let me tell the gentleman that,
of all the assemblies that have ever been convened in this
country, the Hartford Convention is the very last we shall
consent to take as an example; nor will it find more favor in our
eyes, from being recommended to us by the senator from
Massachusetts. Sir, we would scorn to take advantage of
difficulties created by a foreign war, to wring from the federal
government a redress even of our grievances. We are standing
up for our constitutional rights, in a time of profound peace; but
if the country should, unhappily, be involved in a war to-
morrow, we should be found flying to the standard of our
country—first driving back the common enemy, and then
insisting upon the restoration of our rights.
"The gentleman has called upon us to carry out our scheme
practically. Now, sir, if I am correct in my view of this matter,
then it follows, of course, that the right of a State being
established, the federal government is bound to acquiesce in a
solemn decision of a State, acting in its sovereign capacity, at
least so far as to make an appeal to the people for an
amendment to the constitution. This solemn decision of a State
(made either through its legislature, or a convention, as may be
supposed to be the proper organ of its sovereign will—a point I
do not propose now to discuss) binds the federal government,
under the highest constitutional obligation, not to resort to any
means of coercion against the citizens of the dissenting State.
How, then, can any collision ensue between the federal and
State governments, unless, indeed, the former should determine
to enforce the law by unconstitutional means? What could the
federal government do, in such a case? Resort, says the
gentleman, to the courts of justice. Now, can any man believe
that, in the face of a solemn decision of a State, that an act of
Congress is 'a gross, palpable, and deliberate violation of the
constitution,' and the interposition of its sovereign authority to
protect its citizens from the usurpation, that juries could be
found ready merely to register the decrees of the Congress,
wholly regardless of the unconstitutional character of their acts?
Will the gentleman contend that juries are to be coerced to find
verdicts at the point of the bayonet? And if not, how are the
United States to enforce an act solemnly pronounced to be
unconstitutional? But, if the attempt should be made to carry
such a law into effect, by force, in what would the case differ
from an attempt to carry into effect an act nullified by the
courts, or to do any other unlawful and unwarrantable act?
Suppose Congress should pass an agrarian law, or a law
emancipating our slaves, or should commit any other gross
violation of our constitutional rights, will any gentleman contend
that the decision of every branch of the federal government, in
favor of such laws, could prevent the States from declaring
them null and void, and protecting their citizens from their
operation?
"Sir, if Congress should ever attempt to enforce any such
laws, they would put themselves so clearly in the wrong, that no
one could doubt the right of the State to exert its protecting
power.
"Sir, the gentleman has alluded to that portion of the militia of
South Carolina with which I have the honor to be connected,
and asked how they would act in the event of the nullification of
the tariff law by the State of South Carolina? The tone of the
gentleman, on this subject, did not seem to me as respectful as
I could have desired. I hope, sir, no imputation was intended."
[Mr. Webster: "Not at all; just the reverse."]
"Well, sir, the gentleman asks what their leaders would be
able to read to them out of Coke upon Littleton, or any other
law book, to justify their enterprise? Sir, let me assure the
gentleman that, whenever any attempt shall be made from any
quarter, to enforce unconstitutional laws, clearly violating our
essential rights, our leaders (whoever they may be) will not be
found reading black letter from the musty pages of old law
books. They will look to the constitution, and when called upon,
by the sovereign authority of the State, to preserve and protect
the rights secured to them by the charter of their liberties, they
will succeed in defending them, or 'perish in the last ditch.'"
I do not pretend to give the arguments of the gentlemen, or even
their substance, but merely to state their propositions and their
conclusions. For myself, I did not believe in any thing serious in the
new interpretation given to the Virginia resolutions—did not believe
in any thing practical from nullification—did not believe in forcible
resistance to the tariff laws from South Carolina—did not believe in
any scheme of disunion—believed, and still believe, in the patriotism
of Mr. Hayne: and as he came into the argument on my side in the
article of the public lands, so my wishes were with him, and I helped
him where I could. Of this desire to help, and disbelief in disunion, I
gave proof, in ridiculing, as well as I could, Mr. Webster's fine
peroration to liberty and union, and really thought it out of place—a
fine piece of rhetoric misplaced, for want of circumstances to justify
it. He had concluded thus:

"When my eyes shall be turned to behold, for the last time,


the sun in heaven, may I not see him shining on the broken and
dishonored fragments of a once glorious Union; on States
dissevered, discordant, belligerent; on a land rent with civil
feuds, or drenched, it may be, in fraternal blood! Let their last
feeble and lingering glance, rather, behold the gorgeous ensign
of the republic, now known and honored throughout the earth,
still full high advanced, its arms and trophies streaming in their
original lustre, not a stripe erased or polluted, nor a single star
obscured, bearing for its motto no such miserable interrogatory
as, What is all this worth? Nor those other words of delusion
and folly, Liberty first, and Union afterwards; but every where,
spread all over in characters of living light, blazing on all its
ample folds, as they float over the sea and over the land, and in
every wind under the whole heavens, that other sentiment, dear
to every true American heart—Liberty and Union, now and for
ever, one and inseparable!"

These were noble sentiments, oratorically expressed, but too


elaborately and too artistically composed for real grief in presence of
a great calamity—of which calamity I saw no sign; and therefore
deemed it a fit subject for gentle castigation: and essayed it thus:

"I proceed to a different theme. Among the novelties of this


debate, is that part of the speech of the senator from
Massachusetts which dwells with such elaboration of
declamation and ornament, upon the love and blessings of
union—the hatred and horror of disunion. It was a part of the
senator's speech which brought into full play the favorite
Ciceronian figure of amplification. It was up to the rule in that
particular. But, it seemed to me, that there was another rule,
and a higher, and a precedent one, which it violated. It was the
rule of propriety; that rule which requires the fitness of things to
be considered; which requires the time, the place, the subject,
and the audience, to be considered; and condemns the delivery
of the argument, and all its flowers, if it fails in congruence to
these particulars. I thought the essay upon union and disunion
had so failed. It came to us when we were not prepared for it;
when there was nothing in the Senate, nor in the country to
grace its introduction; nothing to give, or to receive, effect to, or
from, the impassioned scene that we witnessed. It may be, it
was the prophetic cry of the distracted daughter of Priam,
breaking into the council, and alarming its tranquil members
with vaticinations of the fall of Troy; but to me, it all sounded
like the sudden proclamation for an earthquake, when the sun,
the earth, the air, announced no such prodigy; when all the
elements of nature were at rest, and sweet repose pervading
the world. There was a time, and you, and I, and all of us, did
see it, sir, when such a speech would have found, in its delivery,
every attribute of a just and rigorous propriety! It was at a time,
when the five-striped banner was waving over the land of the
North! when the Hartford Convention was in session! when the
language in the capitol was, "Peaceably, if we can; forcibly, if we
must!" when the cry, out of doors, was, "the Potomac the
boundary; the negro States by themselves! The Alleghanies the
boundary; the Western savages by themselves! The Mississippi
the boundary, let Missouri be governed by a prefect, or given up
as a haunt for wild beasts!" That time was the fit occasion for
this speech; and if it had been delivered then, either in the hall
of the House of Representatives, or in the den of the Hartford
Convention, or in the highway among the bearers and followers
of the five-striped banner, what effects must it not have
produced! What terror and consternation among the plotters of
disunion! But, here, in this loyal and quiet assemblage, in this
season of general tranquillity and universal allegiance, the whole
performance has lost its effect for want of affinity, connection,
or relation, to any subject depending, or sentiment expressed,
in the Senate; for want of any application, or reference, to any
event impending in the country."

I do not quote this passage for any thing that I now see out of
place in that peroration; but for a quite different purpose—for the
purpose of showing that I was slow to believe in any design to
subvert this Union—that at the time of this great debate (February
and March, 1830) I positively discredited it, and publicly proclaimed
my incredulity. I did not want to believe it. I repulsed the belief. I
pushed aside every circumstance that Mr. Webster relied on, and
softened every expression that Mr. Hayne used, and considered him
as limiting (practically) his threatened resistance to the tariff act, to
the kind of resistance which Virginia made to the alien and sedition
laws—which was an appeal to the reason, judgment and feelings of
the other States—and which had its effect in the speedy repeal of
those laws. Mr. Calhoun had not then uncovered his position in
relation to nullification. I knew that Mr. Webster was speaking at him
in all that he said to Mr. Hayne: but I would believe nothing against
him except upon his own showing, or undoubted evidence. Although
not a favorite statesman with me, I felt admiration for his high
intellectual endowments, and respect for the integrity and purity of
his private life. Mr. Hayne I cordially loved; and believed, and still
believe, in the loyalty of his intentions to the Union. They were both
from the South—that sister Carolina, of which the other was my
native State, and in both of which I have relatives and hereditary
friends—and for which I still have the affections which none but the
wicked ever lose for the land of their birth: and I felt as they did in
all that relates to the tariff—except their remedy. But enough for the
present. The occasion will come, when we arrive at the practical
application of the modern nullification doctrine, to vindicate the
constitution from the political solecism of containing within itself a
suicidal principle, and to vindicate the Virginia resolutions, and their
authors (and, in their own language), from the "anarchical and
preposterous" interpretation which has been put upon their words.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like