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

Java All in One For Dummies Seventh

Edition Doug Lowe


Visit to download the full and correct content document:
https://ebookmeta.com/product/java-all-in-one-for-dummies-seventh-edition-doug-low
e/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Java All in One For Dummies Fifth Edition Doug Lowe

https://ebookmeta.com/product/java-all-in-one-for-dummies-fifth-
edition-doug-lowe-3/

Java All in One For Dummies Fifth Edition Doug Lowe

https://ebookmeta.com/product/java-all-in-one-for-dummies-fifth-
edition-doug-lowe-2/

Java All in One For Dummies Fifth Edition Doug Lowe

https://ebookmeta.com/product/java-all-in-one-for-dummies-fifth-
edition-doug-lowe/

Networking All in One For Dummies Doug Lowe

https://ebookmeta.com/product/networking-all-in-one-for-dummies-
doug-lowe/
Networking All-in-One For Dummies Doug Lowe

https://ebookmeta.com/product/networking-all-in-one-for-dummies-
doug-lowe-2/

Electronics All-in-One For Dummies Doug Lowe

https://ebookmeta.com/product/electronics-all-in-one-for-dummies-
doug-lowe/

Networking All-in-One For Dummies, 8th Edition Doug


Lowe

https://ebookmeta.com/product/networking-all-in-one-for-
dummies-8th-edition-doug-lowe/

PowerPoint For Dummies Office 2021 Edition Doug Lowe

https://ebookmeta.com/product/powerpoint-for-dummies-
office-2021-edition-doug-lowe/

CODING ALL-IN-ONE FOR DUMMIES 2nd Edition Dummies.

https://ebookmeta.com/product/coding-all-in-one-for-dummies-2nd-
edition-dummies/
Java
®

ALL-IN-ONE

7th Edition

by Doug Lowe
Ещё больше книг по Java в нашем телеграм канале:
https://t.me/javalib
Java® All-in-One For Dummies®, 7th Edition
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com

Copyright © 2023 by John Wiley & Sons, Inc., Hoboken, New Jersey

Media and software compilation copyright © 2023 by John Wiley & Sons, Inc. All rights reserved.

Published simultaneously in Canada

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under
Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher.
Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons,
Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/
go/permissions.

Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related
trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written
permission. Java is a registered trademark of Oracle America, Inc. All other trademarks are the property of their
respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHORS HAVE USED THEIR
BEST EFFORTS IN PREPARING THIS WORK, THEY MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT
TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM
ALL WARRANTIES, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF MERCHANTABILITY
OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES
REPRESENTATIVES, WRITTEN SALES MATERIALS OR PROMOTIONAL STATEMENTS FOR THIS WORK. THE
FACT THAT AN ORGANIZATION, WEBSITE, OR PRODUCT IS REFERRED TO IN THIS WORK AS A CITATION AND/
OR POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE PUBLISHER AND AUTHORS
ENDORSE THE INFORMATION OR SERVICES THE ORGANIZATION, WEBSITE, OR PRODUCT MAY PROVIDE OR
RECOMMENDATIONS IT MAY MAKE. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER
IS NOT ENGAGED IN RENDERING PROFESSIONAL SERVICES. THE ADVICE AND STRATEGIES CONTAINED HEREIN
MAY NOT BE SUITABLE FOR YOUR SITUATION. YOU SHOULD CONSULT WITH A SPECIALIST WHERE APPROPRIATE.
FURTHER, READERS SHOULD BE AWARE THAT WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR
DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. NEITHER THE PUBLISHER
NOR AUTHORS SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING
BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.

For general information on our other products and services, please contact our Customer Care Department within
the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit
https://hub.wiley.com/community/support/dummies.

Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to
media such as a CD or DVD that is not included in the version you purchased, you may download this material at
http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.

Library of Congress Control Number: 2022950700

ISBN 978-1-119-98664-5 (pbk); ISBN 978-1-119-98665-2 (ebk); ISBN 978-1-119-98666-9 (ebk)


Contents at a Glance
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Book 1: Java Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5


CHAPTER 1: Welcome to Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
CHAPTER 2: Installing and Using Java Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
CHAPTER 3: Working with TextPad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Book 2: Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37


CHAPTER 1: Java Programming Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
CHAPTER 2: Working with Variables and Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
CHAPTER 3: Working with Numbers and Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . 97
CHAPTER 4: Making Choices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
CHAPTER 5: Going Around in Circles (or, Using Loops). . . . . . . . . . . . . . . . . . . . . . . . 149
CHAPTER 6: Pulling a Switcheroo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
CHAPTER 7: Adding Some Methods to Your Madness. . . . . . . . . . . . . . . . . . . . . . . . . 195
CHAPTER 8: Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

Book 3: Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . 237


CHAPTER 1: Understanding Object-Oriented Programming . . . . . . . . . . . . . . . . . . . 239
CHAPTER 2: Making Your Own Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
CHAPTER 3: Working with Statics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
CHAPTER 4: Using Subclasses and Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
CHAPTER 5: Using Abstract Classes and Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . 305
CHAPTER 6: Using the Object and Class Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
CHAPTER 7: Using Inner Classes and Anonymous Classes. . . . . . . . . . . . . . . . . . . . . 355
CHAPTER 8: Working with Packages and the Java Module System. . . . . . . . . . . . . . 365

Book 4: Strings and Other Data Types . . . . . . . . . . . . . . . . . . . . . . . 387


CHAPTER 1: Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
CHAPTER 2: Using Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
CHAPTER 3: Working with Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
CHAPTER 4: Using the BigDecimal Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451

Book 5: Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471


CHAPTER 1: Introducing Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
CHAPTER 2: Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
CHAPTER 3: Using the ArrayList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
CHAPTER 4: Using the LinkedList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
CHAPTER 5: Creating Generic Collection Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
CHAPTER 6: Using Maps and Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575

Book 6: Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595


CHAPTER 1: Introducing Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
CHAPTER 2: Using Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
CHAPTER 3: Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
CHAPTER 4: Searching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637

Book 7: Programming Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . 657


CHAPTER 1: Programming Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
CHAPTER 2: Using Functional Programming and Lambda Expressions. . . . . . . . . . 689
CHAPTER 3: Consuming Web Services with HttpClient. . . . . . . . . . . . . . . . . . . . . . . . 705

Book 8: JavaFX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727


CHAPTER 1: Hello, JavaFX!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
CHAPTER 2: Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
CHAPTER 3: Setting the Stage and Scene Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
CHAPTER 4: Using Layout Panes to Arrange Your Scenes. . . . . . . . . . . . . . . . . . . . . . 791
CHAPTER 5: Getting Input from the User. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
CHAPTER 6: Choosing from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
Table of Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Foolish Assumptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Icons Used in This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Beyond the Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

BOOK 1: JAVA BASICS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5


CHAPTER 1: Welcome to Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

What Is Java, and Why Is It So Great?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8


Platform independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Object orientation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Java API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Important Features of the Java Language. . . . . . . . . . . . . . . . . . . . . . . . 11
Type checking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Java Version Insanity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
What’s in a Name?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

CHAPTER 2: Installing and Using Java Tools. . . . . . . . . . . . . . . . . . . . . . . . . 17


Downloading and Installing the Java Development Kit . . . . . . . . . . . . . 18
Downloading the JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Installing the JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Perusing the JDK folders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Setting the JAVA_HOME and path variables. . . . . . . . . . . . . . . . . . . . 20
Confirming Your Java Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Using Java’s Command-Line Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Compiling a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Compiling more than one file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Using Java compiler options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Running a Java program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using the javap command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Using Java Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

CHAPTER 3: Working with TextPad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31


Downloading and Installing TextPad . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Editing Source Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Compiling a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Running a Java Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

Table of Contents v
BOOK 2: PROGRAMMING BASICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
CHAPTER 1: Java Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Looking at the Venerable Hello, World! Program. . . . . . . . . . . . . . . . . . 40


Dealing with Keywords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Working with Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Types of statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
White space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Working with Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Creating Identifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Crafting Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
End-of-line comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Traditional comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Introducing Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . 51
Understanding classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Understanding static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Creating an object from a class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Viewing a program that uses an object . . . . . . . . . . . . . . . . . . . . . . . 54
So what’s the difference?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Importing Java API Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

CHAPTER 2: Working with Variables and Data Types. . . . . . . . . . . . . . 59


Declaring Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Declaring two or more variables in one statement . . . . . . . . . . . . . 61
Declaring class variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Declaring instance variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Declaring local variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Initializing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Initializing variables with assignment statements . . . . . . . . . . . . . . 65
Initializing variables with initializers. . . . . . . . . . . . . . . . . . . . . . . . . . 66
Using Final Variables (Constants) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Working with Primitive Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Integer types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Floating-point types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
The char type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The Boolean type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Using wrapper classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Using reference types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Using inferred variable types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Working with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Declaring and initializing strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Combining strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Converting primitives to strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Converting strings to primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

vi Java All-in-One For Dummies


Converting and Casting Numeric Data. . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Automatic conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Type casting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Thinking Inside the Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Understanding Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Shadowing Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Printing Data with System.out. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Using standard input and output streams . . . . . . . . . . . . . . . . . . . . 87
Using System.out and System.err. . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Getting Input with the Scanner Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Importing the Scanner class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Declaring and creating a Scanner object. . . . . . . . . . . . . . . . . . . . . . 90
Getting input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Getting Input with the JOptionPane Class. . . . . . . . . . . . . . . . . . . . . . . . 93
Using enum to Create Your Own Data Types . . . . . . . . . . . . . . . . . . . . . 94

CHAPTER 3: Working with Numbers and Expressions. . . . . . . . . . . . 97


Working with Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Dividing Integers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Combining Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Using the Unary Plus and Minus Operators . . . . . . . . . . . . . . . . . . . . . 103
Using Increment and Decrement Operators. . . . . . . . . . . . . . . . . . . . . 104
Using the Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Using Compound Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . 108
Using the Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Using constants of the Math class . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Working with mathematical functions. . . . . . . . . . . . . . . . . . . . . . . 111
Creating random numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114
Rounding functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Recognizing Weird Things about Java Math. . . . . . . . . . . . . . . . . . . . . .121
Integer overflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Floating-point weirdness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Division by zero. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

CHAPTER 4: Making Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127


Using Simple Boolean Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Using if Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Simple if statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
if-else statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Nested if statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
else-if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Using Mr. Spock’s Favorite Operators (Logical Ones, of Course). . . . 140
Using the ! operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Using the & and && operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Table of Contents vii


Using the | and || operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Using the ^ operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Combining logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Using the Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Comparing Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

CHAPTER 5: Going Around in Circles (or, Using Loops). . . . . . . . . . 149


Using Your Basic while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
The while statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
A counting loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Breaking Out of a Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Looping Forever. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Letting the user decide when to quit . . . . . . . . . . . . . . . . . . . . . . . . 154
Letting the user decide in another way . . . . . . . . . . . . . . . . . . . . . . 155
Using the continue Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Running do-while Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Validating Input from the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Using the Famous for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Understanding the formal format of the for loop . . . . . . . . . . . . . 162
Scoping out the counter variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Counting even numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Counting backward. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Using for loops without bodies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Ganging up your expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Omitting expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Breaking and continuing your for loops. . . . . . . . . . . . . . . . . . . . . .170
Nesting Your Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
A simple nested for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
A guessing game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

CHAPTER 6: Pulling a Switcheroo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177


Battling else-if Monstrosities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Viewing an example else-if program . . . . . . . . . . . . . . . . . . . . . . . . 178
Creating a better version of the example program . . . . . . . . . . . . 179
Using the switch Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Viewing a boring switch example, complete with flowchart. . . . . 182
Putting if statements inside switch statements . . . . . . . . . . . . . . . 184
Creating Character Cases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Matching Two or More Values in a Single Case Group . . . . . . . . . . . . 186
Intentionally Leaving Out a Break Statement. . . . . . . . . . . . . . . . . . . . 187
Switching with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Assigning a Value with a Switch Statement. . . . . . . . . . . . . . . . . . . . . . 191

viii Java All-in-One For Dummies


CHAPTER 7: Adding Some Methods to Your Madness. . . . . . . . . . . 195
The Joy of Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
The Basics of Making Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
An example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Another example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Methods That Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Declaring the method’s return type . . . . . . . . . . . . . . . . . . . . . . . . . 201
Using the return statement to return the value. . . . . . . . . . . . . . . 202
Using a method that returns a type. . . . . . . . . . . . . . . . . . . . . . . . . 203
You gotta have a proper return statement. . . . . . . . . . . . . . . . . . . 203
Trying another version of the guessing-game program . . . . . . . . 205
Methods That Take Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Declaring parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Scoping out parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Understanding pass-by-value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Trying yet another version of the guessing-game program . . . . . 211

CHAPTER 8: Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215


Understanding Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Witnessing an exception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Finding the culprit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
A simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Another example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Handling Exceptions with a Preemptive Strike. . . . . . . . . . . . . . . . . . . 222
Catching All Exceptions at Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Displaying the Exception Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Using a finally Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Handling Checked Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Viewing the catch-or-throw compiler error. . . . . . . . . . . . . . . . . . . 229
Catching FileNotFoundException . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Throwing the FileNotFoundException . . . . . . . . . . . . . . . . . . . . . . . 231
Throwing an exception from main. . . . . . . . . . . . . . . . . . . . . . . . . . 232
Swallowing exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Throwing Your Own Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Using the try-with-resources Statement . . . . . . . . . . . . . . . . . . . . . . . . 235

BOOK 3: OBJECT-ORIENTED PROGRAMMING. . . . . . . . . . . . . . 237


CHAPTER 1: Understanding Object-Oriented Programming. . . 239
What Is Object-Oriented Programming? . . . . . . . . . . . . . . . . . . . . . . . . 240
Understanding Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Objects have identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Objects have type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

Table of Contents ix
Objects have state. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Objects have behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Understanding the Life Cycle of an Object . . . . . . . . . . . . . . . . . . . . . . 245
Working with Related Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Designing a Program with Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Diagramming Classes with UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Drawing classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Drawing arrows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

CHAPTER 2: Making Your Own Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253


Declaring a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Picking class names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Knowing what goes in the class body. . . . . . . . . . . . . . . . . . . . . . . . 255
Seeing where classes live. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Working with Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Understanding fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Understanding instance methods. . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Understanding visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Using Getters and Setters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Overloading Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Creating Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Creating basic constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Creating default constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Calling other constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Finding More Uses for the this Keyword . . . . . . . . . . . . . . . . . . . . . . . . 268
Using Initializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Using Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

CHAPTER 3: Working with Statics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273


Understanding Static Fields and Methods. . . . . . . . . . . . . . . . . . . . . . . 273
Working with Static Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Using Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Counting Instances. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Preventing Instances. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Using Static Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

CHAPTER 4: Using Subclasses and Inheritance. . . . . . . . . . . . . . . . . . . 283


Introducing Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Motorcycles, trains, and automobiles . . . . . . . . . . . . . . . . . . . . . . . 285
Game play. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
A businesslike example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Inheritance hierarchies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Creating Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

x Java All-in-One For Dummies


Overriding Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Protecting Your Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Using the this and super Keywords in Your Subclasses . . . . . . . . . . . 291
Understanding Inheritance and Constructors . . . . . . . . . . . . . . . . . . . 292
Using the final Keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Final methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Final classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Casting Up and Down. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Determining an Object’s Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Poly What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Creating Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Tracing the Throwable hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Creating an exception class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Throwing a custom exception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

CHAPTER 5: Using Abstract Classes and Interfaces. . . . . . . . . . . . . . 305


Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Using Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Creating a basic interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Implementing an interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Using an interface as a type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
More Things You Can Do with Interfaces. . . . . . . . . . . . . . . . . . . . . . . . 312
Adding fields to an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Extending interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Using interfaces for callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Using Additional Interface Method Types. . . . . . . . . . . . . . . . . . . . . . . 318
Two Interfaces That Enable Java Language Features. . . . . . . . . . . . . . 320
Sealing Your Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

CHAPTER 6: Using the Object and Class Classes. . . . . . . . . . . . . . . . . . 327


The Mother of All Classes: Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Every object is an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Object as a type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Methods of the Object class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
Primitives aren’t objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
The toString Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Using toString. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Overriding toString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
The equals Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Using equals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Overriding the equals method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
The clone Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Implementing the clone method. . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Using clone to create a shallow copy. . . . . . . . . . . . . . . . . . . . . . . . 343
Creating deep copies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

Table of Contents xi
The Class Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Using the instanceof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

CHAPTER 7: Using Inner Classes and Anonymous Classes. . . . . . 355


Declaring Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Understanding inner classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Viewing an example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Using Static Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Using Anonymous Inner Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Creating an anonymous class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Creating a program with an anonymous class. . . . . . . . . . . . . . . . 363

CHAPTER 8: Working with Packages and the Java Module


System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Working with Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Importing classes and packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Creating your own packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
An example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Putting Your Classes in a JAR File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
jar command-line options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Archiving a package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Adding a jar to your classpath. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Running a program directly from an archive . . . . . . . . . . . . . . . . . 374
Using Javadoc to Document Your Classes. . . . . . . . . . . . . . . . . . . . . . . 375
Adding Javadoc comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Using the javadoc command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Viewing Javadoc pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Using the Java Module System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Understanding modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
The module-info.java file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Setting up folders for a module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Compiling a module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Creating a modular JAR file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Running a modular JAR file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386

BOOK 4: STRINGS AND OTHER DATA TYPES . . . . . . . . . . . . . . . 387


CHAPTER 1: Working with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Reviewing Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .390
Using the String Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Finding the length of a string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Making simple string modifications . . . . . . . . . . . . . . . . . . . . . . . . . 395
Extracting characters from a string. . . . . . . . . . . . . . . . . . . . . . . . . . 396
Extracting substrings from a string. . . . . . . . . . . . . . . . . . . . . . . . . . 397

xii Java All-in-One For Dummies


Splitting a string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Replacing parts of a string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Stripping a string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Determining Whether a String Is Empty . . . . . . . . . . . . . . . . . . . . . . . . 402
Using the StringBuilder and StringBuffer Classes . . . . . . . . . . . . . . . . 404
Creating a StringBuilder object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Using StringBuilder methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Viewing a StringBuilder example. . . . . . . . . . . . . . . . . . . . . . . . . . . .407
Using the CharSequence Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Using Text Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408

CHAPTER 2: Using Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 411


Creating a Program for Experimenting with Regular Expressions. . . 412
Performing Basic Character Matching. . . . . . . . . . . . . . . . . . . . . . . . . . 415
Matching single characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Using predefined character classes . . . . . . . . . . . . . . . . . . . . . . . . . 415
Using custom character classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Using ranges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Using negation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Matching multiple characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Using escapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422
Using parentheses to group characters. . . . . . . . . . . . . . . . . . . . . . 422
Using the pipe symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Using Regular Expressions in Java Programs . . . . . . . . . . . . . . . . . . . . 425
Understanding the String problem. . . . . . . . . . . . . . . . . . . . . . . . . . 425
Using regular expressions with the String class. . . . . . . . . . . . . . . 426
Using the Pattern and Matcher classes . . . . . . . . . . . . . . . . . . . . . . 427

CHAPTER 3: Working with Dates and Times . . . . . . . . . . . . . . . . . . . . . . 429


Pondering How Time Is Represented. . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Picking the Right Date and Time Class for Your Application. . . . . . . . 431
Using the now Method to Create a Date-Time Object. . . . . . . . . . . . . 432
Using the parse Method to Create a Date-Time Object. . . . . . . . . . . . 434
Using the of Method to Create a Date-Time Object. . . . . . . . . . . . . . . 435
Using the Month enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Using the ZoneId class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Using the ZoneOffset class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Looking Closer at the LocalDate Class. . . . . . . . . . . . . . . . . . . . . . . . . . 438
Extracting Information about a Date . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Comparing Dates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Calculating with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Formatting Dates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Looking at a Fun Birthday Calculator. . . . . . . . . . . . . . . . . . . . . . . . . . . 446

Table of Contents xiii


CHAPTER 4: Using the BigDecimal Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Seeing Why Java Can’t Add. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Double trouble. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Another example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
BigDecimal to the Rescue! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Creating BigDecimal Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Doing BigDecimal Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Rounding BigDecimal Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
The fundamentals of scale. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
How to round. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Comparing BigDecimal Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
Converting BigDecimals to Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Revisiting Sales Tax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467

BOOK 5: DATA STRUCTURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471


CHAPTER 1: Introducing Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . 473
Imagining Models of Computation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Musing about word RAM memory . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Considering the CPU in the word RAM model. . . . . . . . . . . . . . . . .476
Defining Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Quantifying Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Analyzing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Looking at Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Doubling Down with Doubly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . 492
Surmising Stacks, Queues, and Deques. . . . . . . . . . . . . . . . . . . . . . . . . 494
Musing about Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Checking Out Hash Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Tackling Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Checking Out Binary Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

CHAPTER 2: Using Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503


Understanding Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Initializing an Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Using for Loops with Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Solving Homework Problems with Arrays. . . . . . . . . . . . . . . . . . . . . . . 507
Using the Enhanced for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Using Arrays with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Using Varargs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Using Two-Dimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Creating a two-dimensional array. . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Accessing two-dimensional array elements . . . . . . . . . . . . . . . . . . 515
Initializing a two-dimensional array . . . . . . . . . . . . . . . . . . . . . . . . . 516

xiv Java All-in-One For Dummies


Using jagged arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Going beyond two dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Working with a Fun but Complicated Example: A Chessboard. . . . . . 520
Using the Arrays Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Filling an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Copying an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Sorting an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Searching an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .531
Comparing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Converting arrays to strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532

CHAPTER 3: Using the ArrayList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535


Understanding the ArrayList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Creating an ArrayList Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Adding Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Accessing Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Printing an ArrayList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Using an Iterator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Updating Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Deleting Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546

CHAPTER 4: Using the LinkedList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549


Understanding the LinkedList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Creating a LinkedList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Adding Items to a LinkedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
Retrieving Items from a LinkedList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Updating LinkedList Items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Removing LinkedList Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559

CHAPTER 5: Creating Generic Collection Classes. . . . . . . . . . . . . . . . . 561


Why Generics?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Creating a Generic Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
A Generic Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Using Wildcard-Type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
A Generic Queue Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570

CHAPTER 6: Using Maps and Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575


Mastering Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Using the HashMap Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Creating a hash map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Adding elements to a hash map. . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Retrieving elements from a hash map. . . . . . . . . . . . . . . . . . . . . . . 582
Replacing entries from a hash map . . . . . . . . . . . . . . . . . . . . . . . . . 583

Table of Contents xv
Removing Entries from a Hash Map. . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
Treating a hash map as a collection. . . . . . . . . . . . . . . . . . . . . . . . . 585
Using lambda expressions with maps . . . . . . . . . . . . . . . . . . . . . . . 587
Using the TreeMap Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Introducing red-black trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Looking at TreeMap methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Creating and using a TreeMap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .592
More ways to play with a TreeMap. . . . . . . . . . . . . . . . . . . . . . . . . . 593

BOOK 6: ALGORITHMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595


CHAPTER 1: Introducing Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
What Is an Algorithm?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
Classifying Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Brute-force algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Divide-and-conquer algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . .601
Backtracking algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Dynamic algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Some Fun Algorithm Challenges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Fizz buzz. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Roman numerals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Spelling out dollar amounts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Determining if a tic-tac-toe game is over. . . . . . . . . . . . . . . . . . . . . 609
Scheduling a tournament. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610

CHAPTER 2: Using Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613


Calculating the Classic Factorial Example . . . . . . . . . . . . . . . . . . . . . . . 613
The nonrecursive solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
The recursive solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .614
Displaying Directories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Playing the Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620

CHAPTER 3: Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625


Looking at the Bubble Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Introducing the Quicksort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Using the sort method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Using the partition method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634

CHAPTER 4: Searching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637


Creating a Data Structure Worth Searching . . . . . . . . . . . . . . . . . . . . . 638
Looking at Linear Searching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Using a Binary Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647

xvi Java All-in-One For Dummies


BOOK 7: PROGRAMMING TECHNIQUES . . . . . . . . . . . . . . . . . . . . 657
CHAPTER 1: Programming Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Understanding Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Understanding the Thread class. . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Extending the Thread class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
Creating and starting a thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Implementing the Runnable Interface. . . . . . . . . . . . . . . . . . . . . . . . . . 664
Using the Runnable interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Creating a class that implements Runnable . . . . . . . . . . . . . . . . . . 665
Using the CountDownApp class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Creating Threads That Work Together. . . . . . . . . . . . . . . . . . . . . . . . . . 669
Using an Executor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Synchronizing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Creating a Lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Coping with Threadus Interruptus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Finding out whether you’ve been interrupted . . . . . . . . . . . . . . . . 681
Aborting the countdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683

CHAPTER 2: Using Functional Programming and Lambda


Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
Introducing Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Introducing Functional Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Using Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Passing Parameters via Lambda Expressions. . . . . . . . . . . . . . . . . . . . 694
Using Block Lambda Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Using the java.util.function Package. . . . . . . . . . . . . . . . . . . . . . . . . . . . 698

CHAPTER 3: Consuming Web Services with HttpClient . . . . . . . . . 705


Introducing Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
Understanding HTTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
Diving into HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Looking at a simple HTTP exchange. . . . . . . . . . . . . . . . . . . . . . . . . 713
Getting Started with Java’s HTTP Client Library. . . . . . . . . . . . . . . . . . . 714
HttpClient. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
HttpRequest. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
HttpResponse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Using the send method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Putting It All Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
The HTTP Tester Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723

Table of Contents xvii


BOOK 8: JAVAFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
CHAPTER 1: Hello, JavaFX!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Perusing the Possibilities of JavaFX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Getting Ready to Run JavaFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
Looking at a Simple JavaFX Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Importing JavaFX Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Extending the Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
Launching the Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Overriding the start Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Creating a Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Handling an Action Event. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Creating a Layout Pane. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Making a Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
Setting the Stage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
Examining the Click Counter Program. . . . . . . . . . . . . . . . . . . . . . . . . . 746

CHAPTER 2: Handling Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751


Examining Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
Handling Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
Implementing the EventHandler Interface . . . . . . . . . . . . . . . . . . . . . . 755
Handling Events with Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Handling Events with Anonymous Inner Classes . . . . . . . . . . . . . . . . . 761
Using Lambda Expressions to Handle Events. . . . . . . . . . . . . . . . . . . . 763

CHAPTER 3: Setting the Stage and Scene Layout. . . . . . . . . . . . . . . . . 769


Examining the Stage Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
Examining the Scene Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
Switching Scenes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Creating an Alert Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
Exit, Stage Right. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Creating a Close button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
Handling the CloseRequest event. . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788

CHAPTER 4: Using Layout Panes to Arrange Your Scenes . . . . . . 791


Working with Layout Panes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792
Introducing five JavaFX layout panes . . . . . . . . . . . . . . . . . . . . . . . . 792
Creating layout panes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792
Combining layout panes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
Using the HBox Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794

xviii Java All-in-One For Dummies


Spacing Things Out. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
Adding Space with Margins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
Adding Space by Growing Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
Using the VBox Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
Aligning Nodes in a Layout Pane. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
Using the Flow Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Using the Border Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
Using the GridPane Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Sketching out a plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Creating a grid pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
Working with grid pane constraints . . . . . . . . . . . . . . . . . . . . . . . . . 815
Examining a grid pane example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817

CHAPTER 5: Getting Input from the User. . . . . . . . . . . . . . . . . . . . . . . . . . 825


Using Text Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Validating Numeric Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .833
Using Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
Using Radio Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

CHAPTER 6: Choosing from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841


Using Choice Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
Creating a choice box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
Setting a default value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
Getting the selected item. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
Working with Observable Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
Listening for Selection Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
Using Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
Creating combo boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
Getting the selected item. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
Handling combo box events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
Using List Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
Creating a list view. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
Getting the selected items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
Using Tree Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
Building a tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Creating a TreeView control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861
Getting the selected node . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Looking at a complete program that uses a tree view. . . . . . . . . . 864

INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869

Table of Contents xix


Introduction
W
elcome to Java All-in-One For Dummies, 7th Edition — the one Java book
that’s designed to replace an entire shelf full of the dull, tedious titles
you’d otherwise have to buy. This book contains all the basic informa-
tion you need to know to get going with Java programming, starting with writing
statements and using variables and ending with techniques for writing programs
that use advanced Java features. Along the way, you find plenty of not-so-basic
information about programming user interfaces, working with classes and objects,
data structures, and algorithms.

You can (and probably should, eventually) buy separate books on each of these
topics. It won’t take long before your bookshelf is bulging with 10,000 or more
pages of detailed information about every imaginable nuance of Java program-
ming. But before you’re ready to tackle each of those topics in depth, you need to
get a bird’s-eye picture. That’s what this book is about.

And if you already own 10,000 pages or more of Java information, you may be
overwhelmed by the amount of detail and wonder, “Do I really need to read
1,200 pages about JSP just to create a simple web page? And do I really need a
six-pound book on JavaFX?” Truth is, most 1,200-page programming books have
about 200 pages of really useful information — the kind you use every day —
and about 1,000 pages of excruciating details that apply mostly if you’re writ-
ing guidance-control programs for nuclear missiles or trading systems for the
New York Stock Exchange.

The basic idea here is that I’ve tried to wring out the 100-or-so most useful pages
of information on these different Java programming topics: setup and configu-
ration, basic programming, object-oriented programming, advanced program-
ming techniques, JavaFX, file and database programming, web programming, and
animation and game programming. Thus you get a nice, trim book.

So whether you’re just getting started with Java programming or you’re a seasoned
pro, you’ve found the right book.

Introduction 1
About This Book
Java All-in-One For Dummies, 7th Edition, is a reference for all the great things
(and maybe a few not-so-great things) that you may need to know when you’re
writing Java programs. You can, of course, buy a huge 1,200-page book on each
of the programming topics covered in this book. But then, who would carry them
home from the bookstore for you? And where would you find the shelf space to
store them? And when will you find the time to read them?

In this book, all the information you need is conveniently packaged for you
in-between one set of covers. And all of the information is current for the newest
release of Java, known as JDK 19. This book doesn’t pretend to be a comprehen-
sive reference for every detail on every possible topic related to Java program-
ming. Instead, it shows you how to get up and running fast so that you have more
time to do the things you really want to do. Designed using the easy-to-follow
For Dummies format, this book helps you get the information you need without
laboring to find it.

Java All-in-One For Dummies, 7th Edition, is a big book made up of eight smaller
books — minibooks, if you will. Each of these minibooks covers the basics of
one key element of programming, such as installing Java and compiling and
running programs, or using basic Java statements, or using JavaFX to write GUI
applications.

Whenever one big thing is made up of several smaller things, confusion is always
a possibility. That’s why this book has multiple access points. At the beginning
is a detailed table of contents that covers the entire book. Then each minibook
begins with a minitable of contents that shows you at a miniglance what chapters
are included in that minibook. Useful running heads appear at the top of each page
to point out the topic discussed on that page. And handy thumb tabs run down
the side of the pages to help you find each minibook quickly. Finally, a compre-
hensive index lets you find information anywhere in the entire book.

Foolish Assumptions
You and I have never met, so it is difficult for me to make any assumptions
about why you are interested in this book. However, let’s start with a few basic
assumptions:

»» You own or have access to a relatively modern computer. The examples


were created on a Windows computer, but you can learn to program in Java
just as easily on a Mac or Linux computer.

2 Java All-in-One For Dummies


»» You’re an experienced computer user. In other words, I assume that you
know the basics of using your computer, such as starting programs and
working with the file system.

»» You’re interested in learning how to write programs in the Java


language. Since that’s what this book teaches, it’s a fair assumption.

I do not make any assumptions about any previous programming experience in


Java or in any other programming language. Nor do I make any assumptions about
why you want to learn about Java programming. There are all sorts of valid rea-
sons for learning Java. Some want to learn Java for professional reasons; maybe
you want to become a professional Java programmer, or maybe you are a C# or
C++ programmer who occasionally needs to work in Java. On the other hand,
maybe you think programming in Java would make an interesting hobby.

Regardless of your motivation, I do assume that you are a reasonably intelligent


person. You don’t have to have a degree in advanced physics, or a degree in any-
thing at all for that matter, to master Java programming. All you have to be is
someone who wants to learn and isn’t afraid to try.

Icons Used in This Book


Like any For Dummies book, this book is chock-full of helpful icons that draw your
attention to items of particular importance. You find the following icons through-
out this book:

Danger, Will Robinson! This icon highlights information that may help you avert
disaster.

Did I tell you about the memory course I took?

Pay special attention to this icon; it lets you know that some particularly useful
tidbit is at hand.

Hold it — overly technical stuff is just around the corner. Obviously, because this
is a programming book, almost every paragraph of the next 900 or so pages could
get this icon. So I reserve it for those paragraphs that go into greater depth, down
into explaining how something works under the covers — probably deeper than
you really need to know to use a feature, but often enlightening.

Introduction 3
Beyond the Book
In addition to the material in the print or e-book you’re reading right now,
this product also comes with some access-anywhere goodies on the web. Check
out the free Cheat Sheet for more on Java. To get this Cheat Sheet, simply go to
www.dummies.com and type Java All-in-One For Dummies Cheat Sheet in the
Search box.

Visit www.dummies.com/go/javaaiofd7e to dive even deeper into Java. You can


find and download the code used in the book at that link. You can also download a
bonus minibook covering how to use Java with files and databases.

Where to Go from Here


This isn’t the kind of book you pick up and read from start to finish, as if it were
a cheap novel. If I ever see you reading it at the beach, I’ll kick sand in your face.
Beaches are for reading romance novels or murder mysteries, not programming
books. Although you could read straight through from start to finish, this book
is a reference book, the kind you can pick up, open to just about any page, and
start reading. You don’t have to memorize anything in this book. It’s a “need-to-
know” book: You pick it up when you need to know something. Need a reminder
on the constructors for the ArrayList class? Pick up the book. Can’t remember
the goofy syntax for anonymous inner classes? Pick up the book. After you find
what you need, put the book down and get on with your life.

4 Java All-in-One For Dummies


1
Java Basics
Contents at a Glance
CHAPTER 1: Welcome to Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

CHAPTER 2: Installing and Using Java Tools . . . . . . . . . . . . . . . . . . . . . 17

CHAPTER 3: Working with TextPad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Ещё больше книг по Java в нашем телеграм канале:


https://t.me/javalib
IN THIS CHAPTER

»» Finding out about programming

»» Scoping out Java

»» Understanding Java’s
incomprehensible version
numbers

Chapter 1
Welcome to Java

T
his chapter is a gentle introduction to the world of Java. In the next few
pages, you find out what Java is, where it came from, and where it’s going.
You also discover some of the unique strengths of Java, as well as some of
its weaknesses. Also, you see how Java compares with other popular programming
languages such as C, C++, and C#.

By the way, I assume in this chapter that you have at least enough background to
know what computer programming is all about. That doesn’t mean that I assume
you’re an expert or professional programmer. It just means that I don’t take the
time to explain such basics as what a computer program is, what a program-
ming language is, and so on. If you have absolutely no programming experience,
I suggest that you pick up a copy of Java For Dummies, 7th Edition, or Beginning
Programming with Java For Dummies, 5th Edition, both by Barry Burd (Wiley).

Throughout this chapter, you find little snippets of Java program code, plus a few
snippets of code written in other languages, including C, C++, and Basic. If you
don’t have a clue what this code means or does, don’t panic. I just want to give you
a feel for what Java programming looks like and how it compares with program-
ming in other languages.

All the code listings used in this book are available for download at www.dummies.
com/go/javaaiofd7e.

CHAPTER 1 Welcome to Java 7


Another random document with
no related content on Scribd:
“Yes,” she returned; “I have a swelled face.”
“How sorry I am!” said the soldier sympathetically, “for I came this
afternoon in the hope of hearing you sing.”
“And so you shall,” returned the girl kindly. “You shall not go away
disappointed.” And, taking the bandage from her face, she sang
song after song to the fascinated General.
The progress of the courtship was swift, and the marriage was
celebrated with great magnificence in the palatial abode of Queen
Maria Cristina in Paris, with the attendance of representatives of the
most distinguished families of France and Spain.
When General Narvaez returned to Madrid he became Prime
Minister of Spain.
Unfortunately, the marriage did not prove a happy one, and,
indeed, it would have been difficult for anyone to live peacefully with
the irascible Spaniard. This irascibility was seen at the funeral of
General Manso de Zuñiga, who had died in the expedition against
Prim, in the mountains of Toledo. General Narvaez was chief
mourner on the occasion, as the deceased officer had been husband
of Doña Valentina Bouligni, a lady of great importance at this epoch,
with whom he was connected; and the Bishop of Pharsalia was
master of the ceremonies.
At a certain point in the function the order was given to kneel.
But, probably absorbed in some knotty State question, the Duke of
Valencia still stood. Upon this the Bishop quickly approached the
grandee, and said:
“Kneel down, kneel down!”
“But I don’t want to kneel,” returned the General petulantly, and
so he remained standing for the rest of the service.
G E N E R A L N A RVA E Z

When she came to Madrid as the wife of the great General, the
Duchess of Valencia was appointed Lady-in-Waiting to Queen
Isabella, and she never failed in her loyalty to the dynasty which was
in power when she came to the country of her adoption by marriage.
Many years later she was in an hotel in Switzerland, where she
purposed making a long stay, when Don Carlos happened to come
to the same hotel, accompanied by his secretary. As the Duchess of
Valencia was unacquainted with the Pretender to the throne of
Spain, she wondered who the imperious-looking new arrival could
be, who was greeted so respectfully by everybody. Her curiosity was
soon satisfied, for the gentleman’s secretary presented himself
before her to say that the Duke of Madrid begged the honour to pay
his respects to her.
The message filled the Duchess with dismay, for, although she
held the Princes of the blood in great respect, she had no intention of
receiving one who disputed the throne with the reigning Queen.
So, summoning all her dignity to her aid, she said, in a tone of icy
politeness:
“Tell the Duke of Madrid that I am very sorry not to have the
honour of receiving his visit, but to-morrow I leave for Paris.”
And in effect the lady left the hotel on the morrow, and thus the
meeting of one of the oldest and most valued Ladies-in-Waiting with
Don Carlos was avoided.
Isabella certainly never expected that she would be dethroned,
for a few weeks before the revolution of September, 1868, the
celebrated General Tacon, Duke of the Union of Cuba, announced
the forthcoming marriage of his daughter Carolina with the Marquis
Villadarías, of the première noblesse, and a perfect type of a
Spanish grandee, and she said: “I congratulate her sincerely on her
engagement; but,” she added sadly, “for myself I am sorry, as I shall
see her no more at Court.” The Queen here referred to the well-
known Carlist opinions of the Marquis Villadarías, which would have
made it impossible to receive the Marchioness at the palace if she
had remained there.
So Isabella II. was dethroned in 1868, and she can truly be said
to have been the victim of circumstances. From the moment King
Ferdinand died his daughter had been the object of intrigue and
ambition. Whilst our Queen Victoria was carefully educated and
drilled in high principles, Isabella was the prey of those who wished
to rise to power by her favour. Ministers made love to the Sovereign
instead of discussing the welfare of the nation; flowery speeches on
patriotism meant merely the gratification of the orator’s vanity to be
remarked by Her Majesty. Personal advancement was the end and
aim of those in the Government, and thus poor Isabella’s
susceptibilities were worked upon to an awful extent.
It is well known that General Serrano, who might have been
thought to have the welfare of his country at heart, gained an undue
influence over the Queen by means of her affections, and fomented
to a great extent the matrimonial differences between her and her
husband. Generous to a degree, Isabella paid the debts of this
courtier twice, and yet it was this same General who was the first to
have her hurled from the royal palace.
When the great Canning visited Madrid, Bulwer Lytton showed
him at a Court ball the many women who were the favourites of the
Ministers, and there was, indeed, hardly a statesman who would not
sacrifice principles to the pleas of his mistress. It was at this Court,
steeped in immorality, that Isabella was brought up with little or no
knowledge of right and wrong, and even in her marriage she was a
victim to the intrigues and ambitions of other Courts of Europe as
well as those of her own. She was, in fact, a scapegoat of the nation.
Harassed and in desperation at being pressed on to a miserable
marriage destitute of all that could justify it, Isabella, after one of
those long and fruitless discussions with her mother, once addressed
a letter to our Queen Victoria; but in a pure Court like that of England
little idea could be formed of the stagnant atmosphere of the Spanish
palace from which the poor young Queen sent forth her plaint.
Beyond the Court raged the stormy discontent of the country, which
had been thwarted for more than thirty years of the fulfilment of its
constitutional rights promised by Ferdinand VII. as the condition of
his return to the throne of Spain.
Whilst Queen Victoria was daily increasing in the knowledge of
constitutional rights which are the base of a Sovereign’s power, poor
Isabella’s Prime Ministers resigned at any moment in pique or
jealousy of some other politician, and the people grew daily more
discontented at finding the Parliament was a farce, and it meant
neither the progress of the land nor the protection of the people.
Bulwer Lytton was constantly sending despatches to England
about the shortcomings of Isabella II. as a woman, but he seemed to
lay no stress on the cause of her failure as a Queen. Under proper
conditions Isabella doubtless would have been a good woman and a
great Queen, but choked with the weeds of intrigue she was lost.
Undisciplined and uneducated, the poor Queen fell a victim to what,
if properly directed, would have been virtues instead of vices.
The marriage to which Isabella was forced by intrigue was, of
course, the greatest evil which could have befallen such an
impulsive, warm-hearted girl, who knew no more how to turn a deaf
ear to a claimant for her favour than to keep her purse shut to the
plea of an unfortunate beggar.
The Right Hon. Henry Lytton Bulwer wrote a little later from the
British Embassy at Madrid to the Court of St. James’s, saying that he
“looked at the Queen’s conduct as the moral result of the alliance
she had been more or less compelled to contract, and he regarded
her rather with interest and pity than blame or reproach.”
Isabel’s natural intuition of our Queen Victoria’s good heart
prompted her letters to her. They were sent by a private hand, and
who knows what evils might have been prevented in the Court of
Spain if the long journey, so formidable in those days, had not placed
the sister-Queens so far apart?
Espartero’s plea for Isabel to marry Don Enrique de Assisi, the
man of her heart, met no support in a Court torn with intrigue, and
the sad, bad story of Isabel doubtless had its source in the tragedy of
an unhappy marriage. At the plea of a persistent wooer, who knew
that the Queen had the right of dissolving a Ministry, a Government
would fall; and as the station of her favourites became lower and
lower, as time went on the ill-regulated Sovereign had a Government
as undependable as her friends.
Treachery was the keynote of the Court of Spain, and some of
the leaders of the revolution were those who had used the
Sovereign’s ignorance and foolhardiness to their own ends. In such
an atmosphere of untruth and treachery such men as Espartero,
Prim, etc., could play no enduring part. Hardly had Espartero swept
the Court clean of the Regency of Queen Maria Cristina than his fall
was encompassed by O’Donnell, his rival. The flagrant falsification of
the Parliamentary election returns—which is still the cankerworm of
the country—was the check to all progress. Count San Luis made a
primitive effort for the reform of the elections; he suggested that the
names of the candidates as deputies should be put in a bag, and
drawn out by a child blindfolded, for the law of chance seemed to
him better than the custom of deception.
Isabella’s acts of generosity are still quoted with admiration at the
royal palace of Madrid by those who served her as Queen.
Four hundred girls owed their marriage dots to Isabella, and it
was the fathers of these four hundred royally endowed brides who
treacherously worked for her expulsion.
One day, hearing the story of the penury of a clever man of
letters, Isabella commanded 20,000 francs to be sent to him. The
administrator of her finances, thinking the Queen could hardly know
how much money this sum represented, had twenty notes of 1,000
francs each changed into small money, and put out on a table by
which she had to pass.
“What is all this money for?” asked Isabella, when she saw it
spread out to view.
“It is the money for the man of letters, and this shows Your
Majesty how large is the sum of 20,000 francs.”
“So much the better,” was the prompt reply; and the courtier saw
it was not by proving the amount of the boon that he could check his
Sovereign in her generous actions.
A Court official at Madrid, who has been sixty years in office at
the palace, told me he often saw Isabella take off her bracelets, and
give them to the beggars who pressed upon her as she crossed the
courtyard of the royal domain.
“And who could help loving her?” said the old courtier, with tears
in his eyes; “I know I could not.”
Caught in the darkness of ignorance and intrigue, Isabella was
naturally enraged at the revolution. When her son Alfonso was
nearly made captive by the Carlists at Lucar, she said: “I would
rather my Alfonso be a prisoner of the Carlists than a captive of the
revolutionists.”
Isabella had a faithful friend in the Marquis of Grizalba, and he
said to Croze:[18]
“It is the loss of faith which causes our woes; the charm of death
has been destroyed with the hope of a hereafter. But Spain will die
like a gentleman.”
[18] The author of “La Vie intime d’Alfonse XIII.”

From September 19, 1868, to 1870 there is no history of the


Court of Spain, as there was no King, and it was not known if there
ever would be one again. Isabella lived, as we know, in Paris, and
her son pursued his education in Vienna, in the Theresan College,
and later at Sandhurst. The young ex-Prince was devoted to society
and to gaiety, and, seeing how his mother was fêted in Paris, he was
often heard to say:
“I should rather like to be a dethroned King and live in Paris with
plenty of money.”
In Spain, meanwhile, Serrano, Duke de la Torre, was enjoying his
long-sought-for ambition of being supreme in the country, whilst
General Prim was President of the Council of Ministers. The
Duchess of la Torre made an ineffectual attempt to gather a Court
around her at La Granja; but a palace made after the essentially
royal abode of Versailles, with its countless well-kept alleys and its
many panoramas of fountains adorned with allegorical scenes and
figures, did not lend itself to anything but the stately entourage of a
royal Court.
Whilst the Republican party grew in power in the Parliament, the
Generals who had made the revolution sighed after a monarchy.
The Duke of Montpensier, the brother-in-law of the ex-Queen,
might have had a good chance as candidate to the throne, and he
was supported by Topete and the three Liberal Generals; but Spain
could not forget his treachery and ingratitude to Isabella by joining
with her enemies against her, and he found he could gain no real
support from the country. And this coldness became more marked
after the tragedy in which he was the chief actor made a dreadful
stain on Court history.
It will be remembered that Prince Henry of Bourbon, the brother
of the ex-King, whom Isabella had personally preferred to the
husband she was finally obliged to accept, and who married, in
1849, Helena, daughter of the Count of Castellvi, had been removed
from his position of a General of the army, to which he had been
appointed by his cousin, and expatriated for a writing which was very
insulting to the Queen.
Having thus associated himself with republicanism, Prince Henry
became the source of many disloyal publications against the Queen
and her Ministers, and when the blow was struck for the
dethronement of Isabella, he openly welcomed the revolution.
The final opinions which caused the tragic ending to his life were
expressed in an article entitled “The Montpensiers,” and this so
enraged the candidate to the throne that he called out the author of
the pamphlet in a duel, and a wave of horror swept over the Court of
Spain when the ex-King’s brother thus met his death at the hand of
the Duke of Montpensier.
The funeral of the Prince was solemnized with all the insignia of
his rank as Lieutenant-Colonel and the owner of the Collar of
Charles III., and with the rites due to a Freemason of high office. He
was buried in the Escorial, and it is said that his remains will be
finally removed from the simple niche where they now lie to the
imposing tomb of “the Infants.”
Another tragedy befell the family of the ex-Queen of Spain in
December, 1871. On May 13, 1868, the Infanta Isabella, the eldest
daughter of Queen Isabella II., married Count Frederick Girgenti,
who created a most favourable impression in the country by the
valiant way he fought in the Battle of Alcolea under the Marquis of
Novaliches.
But the brave young Prince was subject to epileptic fits, and one
day in December, 1871, to the horror of his wife, he shot himself in
Lucerne. The poor man lived for some hours, tended by his
sorrowing wife. But neither love nor science could avail in such a
case, and the Infanta Isabella found herself a widow at the age of
twenty. However, the Infanta never allowed sorrow to kill her
sympathy for her compatriots, and to go to Spain is to find that no
philanthropic scheme or project is considered complete without the
patronage of the Infanta Isabella.
CHAPTER XIII
THE COURT OF SPAIN UNDER ITALIAN SWAY

In February, 1869, Serrano was chosen head of the Executive


Government, and in June of the same year Serrano, Duke de la
Torre, was appointed Regent until a King should be elected.
To General Prim, whose ideal had always been that of Liberty, it
was not surprising that, in seeking a Sovereign who, it was hoped,
would steer the country through the shoals of self-interest and
stagnation, set up by an autocrat monarchy, his eyes should turn to
Prince Amadeus of Savoy, whose father had led such a splendid
struggle for the freedom of the country from the despotism of
clericalism. A deputation, formed by deputies who subsequently
became Ministers of Alfonso XII., presided over by Ruiz Zorilla, who
was later a pronounced republican, were thus sent to Italy to submit
the offer of the throne of Spain to the Prince of Savoy. Their mission
to the Prince over, they proceeded to the bedroom of his young wife,
who had recently been confined, and there conveyed to her in due
form the invitation to become Queen of Spain.
The claim of the Italian Prince to the throne rested on the royal
decree of Philip V. of Spain, which formed the integral part of the
Treaty of Utrecht, November 5, 1712. This decree set forth the claim
to the throne of Spain through failure of legitimate line by the Duke of
Savoy, and through failure of the male line by Prince Amadeus of
Carignano and his sons, as descendants of the Infanta Catharine,
daughter of Philip II. When the question of the claims to the throne of
Spain was put to the vote, it was found that Amadeus registered 199,
Espartero 8, Alfonso 2, and for a Republic 63.
Castelar used all his eloquence against the Italian candidate.
“Who are these wretched Dukes of Savoy,” he said, “that run like
hungry dogs in the wake of the coach of our Kings?”

EMILIO CASTELAR

Courage was a great characteristic of young Prince Amadeus.


When only twenty-one, in 1866, he saved a wounded soldier’s life by
carrying him out of danger on his own mule, in one of the skirmishes
during the struggle for Italy’s liberty.
The young man’s calmness in the hour of danger was shown in
1867, when the boiler burst on the ship on which he was returning to
Italy, after attending the function of the opening of the Suez Canal.
The Count of Castiglione was killed, and the panic on board
threatened more disaster. But Amadeus was cool and collected. He
calmed the people and insisted on the sailors’ return to their several
duties, and the ship was successfully brought back.
Fortunately, the young Prince was allowed to marry the lady of
his choice, who proved a devoted friend and companion in all the
vicissitudes of their lives.
When Signor Francisco Cassini, the President of the Chamber of
Deputies, told King Victor Emmanuel whom his son wished to marry,
the royal father said: “Do the young people love each other?”
“They idolize each other,” returned the statesman.
“Then very well; they shall be married,” was the reply. “It is not for
me to stand in opposition to the sentiment of my son.”
So the royal couple were married, and thus fate led to Princess
Maria del Pozzo becoming Queen of Spain.
Naturally, Amadeus was not attracted by all he heard of the
country over which he was called to reign. However, when his father
said, “Of course, it is very hot in Spain at this time, and by going
there you would also run the risk of a disagreeable adventure, and
perhaps even get a bit of lead in your ribs,” the natural courage of
the Prince was stimulated, and he declared he would accept the
invitation to the throne, come what may.
As the new King of Spain’s wife had not recovered sufficiently
from her recent confinement to travel, her husband went alone to
Spain. Before starting for his new country, Amadeus said to his
friends:
“I go to fulfil an impossible mission. Spain, now divided into
various parties, will unite against a foreign King, and I shall soon be
obliged to return the crown they offered me.”
A M A D E U S O F S AV O Y, D U K E O F A O S TA , A F T E RWA R D S K I N G O F
S PA I N

However, the Italian Prince knew he had a valiant supporter in


General Prim, who used all his oratory and influence to get the
sympathy of his countrymen on the side of the new-comer. But, as
we know, it was not the fate of the pioneer of Liberty to see the
realization of the scheme which he had hoped would be for the
welfare of the country, and on December 30, 1870, the day on which
Amadeus landed on Spanish soil, Prim was foully murdered by
Spaniards.
Long inquiry and investigation never revealed convincingly the
hand that shot the General in the street. It was supposed by some to
have been a partisan of an unsuccessful candidate to the throne;
others think it was a gipsy, who did it as a deed of outlawry. Be that
as it may, strong suspicion fell upon Señor Paul y Angulo, who
expressed his indignation strongly in the prologue to his paper on
“Revolutionary Truths”:
“The sacrifices that I have made for my country have been no
light matters, and all I have in return is to find myself obliged to leave
my poor country, to be the victim of vile calumniators, and to have to
fly from persecution as if I were some horrible criminal.”
Prim’s death was accompanied with much suffering, for some of
his fingers were so seriously injured by the shot, it was thought that
their amputation would save his life. But the operation was in vain,
and the General died in two days, just as Amadeus landed at
Carthagena.
When the sad news reached the new King, that his ardent
supporter had fallen a martyr to his cause, he said:
“Gentlemen, my duty is clear: I must go on to Madrid.”
Arrived at the Spanish capital on January 2, 1871, where a fall of
snow added gloom to the occasion, Amadeus at once repaired to the
Church of Atocha, to pay his respects to the remains of the man to
whom he mainly owed the throne of Spain.
As the young King gazed at the corpse of the great leader, who
had inspired trust and confidence in all with whom he came in
contact, he prayed for strength to be able to fulfil the hopes which
the Spaniard had directed to himself. With Prim, the pioneer of
Liberty in Spain, young Amadeus, who had fought for the same
cause in his own country, had always felt in sympathy.
Those who had suffered for their religious opinions had looked to
the great soldier as the herald of a new era. Juan Cabrera, the
leader of the Protestant movement, who had fled to Gibraltar for fear
of imprisonment, and there led a life of exile and hardship, hastened
to meet Prim after the coup which put Spain practically in his hands.
“May I return to my country?” asked Cabrera, when he saw him
at Gibraltar.
“Yes, yes, my man,” replied the General, whilst fixing his keen
eyes on the Protestant’s face, worn with study and anxiety, “you can
go back to Spain now with your Bible under your arm.”
And this the preacher of the reformed faith found to be true, for
Spain had no longer to fear the active persecution of those who
resisted the introduction of the Bible into their land.
As Amadeus gazed at the features of the General, set in death,
he sighed deeply in sorrow at his loss, and when he arrived at the
royal palace, the magnificent setting of so many scenes of struggle
for supremacy in the country, he sat down wearily, and said:
“I feel sure that my loyalty will not be able to save Spain from the
fury of contending factions.”
When the new King took up his abode in the fine domain, with
Prim dead, he felt as if he were starting for a voyage on a ship of
which the rudder was lost, for he knew not whom to trust or to whom
to turn for counsel.
But Amadeus was not a man to let himself be enervated by fears
and doubts for the future, although the first few weeks of his
residence in the palace were additionally anxious from the fact of his
wife being ill at Alassio. For the new young Queen of Spain had not
been able to accompany her husband to Spain, as she was not
sufficiently recovered from her confinement; and when, in her desire
to join Amadeus in the new sphere of influence, the journey was
made too soon for her health, she was for some time ill at Alassio.
At last the new Queen was able to undertake the ten days’
journey by sea to Alicante, where she was received with great
delight by the young King.
The bright spring day which saw the arrival at Alicante of Queen
Maria Victoria seemed to augur well for the success of the young
couple. A thrill of pride passed through the young wife when she saw
her husband come out to meet her in a white-and-gold launch, his
face bright with hope, and looking every inch a King. As the bright
barque cut through the sunlit waters, with Amadeus accompanied by
his Ministers, who had come to welcome her in state, she stepped to
the prow with pretty words of greeting on her lips, and when she was
finally taken off from the Italian ship to set foot on Spanish soil, a
storm of cheers burst from the throats of the Italian sailors, to be
echoed by those from the Spanish crews and sight-seers anxious to
welcome the new Sovereign. The bright and intelligent young wife
did indeed seem to bring sunshine to Spain, and in the opening of
the Senate there was a sincerity in the royal speech which found an
echo in the hearts of those who really wished for the welfare of the
country.
“When my feet touched Spanish soil,” said Amadeus, in a voice
which penetrated to every part of the house, “I determined to merge
my ideas, my sentiments, and my interests, in those of the nation
who elected me as its head, and whose independent character
would never submit to foreign and illegitimate intrigues. My sons will
have the good fortune to receive their first impressions of life here;
their first language will be Spanish; their education will be in
accordance with the customs of the nation; they will learn to think
and feel as you think and feel; and we shall unite with imperishable
bonds our own fate with your fate.”
But no patriotic sentiments could entirely extinguish the sparks of
smouldering resentment that a foreigner should be set over Spain.
The pride of the Castilians was wounded, and no salve of
sympathy could prevent the canker caused by such a hurt.
Everything the Italian King and Queen did was purposely
misinterpreted. He was dubbed “King Macaroni,” and this mocking
appellation expressed the resentment of the Court and country.
The very democratic simplicity of the young couple was an
offence to a land which revels in old-world ceremony and stately
Court etiquette, and the clerical party never let the people forget that
it was Victor Emmanuel, the father of their new King, who had
ousted the Pope from his position of political supremacy.
Of course Isabella was very indignant when she heard who had
been elected as ruler at the Court of Spain, and she expressed her
feelings in a torrent of speech.
“The revolution continues,” she said, whilst her eyes blazed with
indignation, “and it has just disavowed the rights of my son, who is
to-day your legitimate King according to all the Spanish constitutions,
by calling to the throne of St. Ferdinand a foreigner, whose merits,
however great, cannot entitle him to be your Sovereign, in the face of
the rights of a whole dynasty, which is the only one that has in its
favour the legitimacy which has been consecrated by the lapse of
ages and by constitutions which it has been a signal folly to
disavow.”
Of course the Bourbon party echoed these sentiments of its ex-
Queen, and Amadeus felt the want of unity which has ever been the
main defect of the country.
People who came into contact with Amadeus at the Court of
Spain admired the sense of his opinions, although the form of their
utterance was not in accordance with that adopted by former rulers
at the palace; for the King had many habits of a nervous man. One
of these was to take hold of a chair when he was talking, and twist
first one of his long legs, and then the other, in and out of the
woodwork as he turned it about before him.
As simplicity was a very marked trait in the young royal couple’s
tastes, they rejected the idea of establishing themselves in the
magnificent apartments used by the ex-Sovereigns, and chose a
simple suite of somewhat small rooms commanding a beautiful view
of the Casa de Campo, and there the King passed the happiest
hours of the day with his wife and children. The young Queen’s fine
intelligence rendered her an able confidante for her consort’s State
difficulties, and she was generally present at the discussions with the
Ministers.
Sunday generally saw an intellectual gathering round the royal
dinner-table, but the admiration of the select few who began to
appreciate the gifts and aims of the young couple did not,
unfortunately, represent the feeling of the country, and it required all
the charity and philosophy of King Amadeus and Queen Maria
Victoria to ignore the half-concealed sneers of those at Court who
mocked at the foreigners and their simple, superior tastes.
Indeed, the Alfonsists never lost an opportunity of testifying their
allegiance to the Bourbon dynasty, and, as they studiously avoided
the royal palace from whence it had been expelled, the Court society
of Madrid presented a strange medley of people who were so little
conversant with the customs of such centres that Amadeus began to
doubt if Madrid had any really good society.
A certain Señor B., who was subsequently a Minister during the
Regency, was invited to a function at the palace. So he went to a
first-rate shirt-maker and ordered a shirt for the occasion. The shirt
came with the fine embroidered cambric frill set out over blue tissue-
paper. So, thinking the blue paper was meant to be worn with the
shirt, Señor B. strutted into the royal presence quite proud of his
attire—paper and all.
So naturally Don Amadeus was constantly saying: “But there is
no society in Madrid.” This remark was repeated in one of the salons
of the aristocracy on the eve of the funeral of Blanca Osma, the
Marchioness of Povar, mother of the present Duke of Arion, who had
been renowned for her beauty and elegance, and, stung at this slight
to their circles, somebody said: “Well, to-morrow Amadeus shall see
whether there be any good society in Madrid, for we will all parade in
front of the windows of the palace after the funeral.” And so they did,
and thus the demonstration of sympathy for the family of Osma and
Malpica became one of political importance.
February 10, 1872, was celebrated at the palace of the Dukes of
Bailen by a magnificent ball. The minuet was danced by ladies in
most beautiful Pompadour dresses, trimmed with handsome lace,
and their hair powdered in the style of the last régime, and the
gentlemen showed their high degree in dress and dignity. This
minuet was repeated in the Palace of the Plaza del Angel by request
of the mother of the Empress Eugénie, and society kept alive the
feeling for the ex-régime by the same sort of fêtes until the day
dawned for the restoration, which doubtless these gatherings aided,
for the little rooms adjoining the salons were the scene of many
councils in the cause of the Bourbons.
One day this feeling of antagonism was expressed in a more
patent and painful form.
It was a hot evening, which the King and Queen had spent
listening to the music in the gardens of the Buen Retiro. The royal
couple was returning to the palace by the Arenal, when suddenly a
vehicle opposed the passage of the carriage by crossing just in front
of it. The coachman checked the horses and cleverly prevented a
collision, and just then a shot was directed towards the royal party.
Upon this the King sprang boldly to his feet, exclaiming:
“Here is the King! Fire at him, not at the others!”
But no further attempts were made at assassination, and the
retinue reached the palace in safety, where the young Queen sought
to still her tremors of anxiety by the sight of her brave young
husband standing sound and well before her.
To the King the late hours of the Court were particularly
disagreeable. At work from six o’clock in the morning, he rang at
eight o’clock for breakfast; astonishment was on the lackey’s face
when answering the summons; he heard that it had never been
customary for their ex-Majesties to be served before eleven o’clock.
So Amadeus, wishing to avoid any friction by insisting on earlier
hours, adopted the habit of going to a café for his early meal after
long application to State matters had made him conscious of the
necessity of breaking his fast.
Thus the maids, who sally forth in Madrid with baskets on their
arms to be filled with necessaries for the household, would often
return and regale the ears of their mistresses with how they had
brushed against His Majesty as they did their business in the market-
place. In one of these peregrinations Amadeus noticed that Castelar,
the leader of the Republican party, raised his hat to him. Surprised at
this sign of respect from the enemy, the young man stopped, and
said he wondered that anybody of Castelar’s opinions should salute
royalty, to which the great orator replied, with all the grace and
charm of an accomplished Castilian:
“My salute was not to royalty, sire, but to the bravest man in
Christendom.”
And it was this bravery which aroused the admiration of Spain.
However, no quality could overcome the country’s rooted prejudice
against “the foreigner,” and when Amadeus had taken his seat on
the throne in the magnificent crimson-and-gold setting of the state
salon of the palace, it was not to take real possession of his subjects’
hearts. There was no antagonism against the Italian King, but utter
indifference for him, which was much more difficult to deal with. He
was unknown to the Spaniards, a mere guest, and the necessity of
forming a Court for his wife was attended with the difficulty of the
ladies of high degree being Alfonsists or Carlists, and thus many of
them considered themselves superior to the lady on the throne.
The ladies of the last régime openly showed the Italian royal
couple that their loyalty was still directed to the Spanish ex-
Sovereigns, by constantly presenting themselves in the Buen Retiro,
and other resorts where they drove or walked, in the white lace
mantillas and other characteristics of costume especially Spanish.
As a counterfoil to these signs of disrespect to those in power,
the ladies who were followers of King Amadeus and his wife
arranged a cortège formed of women of the town, who were all
dressed like the Spanish doñas of high degree, and they were
accompanied by a noisy, bullying sort of fellow who obviously
represented the King’s Chamberlain, the Duke of Sexto.
Thus the feeling of the Court of Spain at this epoch was
manifested in a series of spiteful acts unworthy of people of high
position.
The Court ladies showed little sympathy with the philanthropic
aims of Queen Maria Victoria. The existing Home for the Children of
Laundresses is still a standing proof that the sight of the thousands
of women on their knees by the side of the River Manzanares,
washing linen, had evoked a feeling of pity in the heart of the young
royal mother.
The King found it impossible to take any action for good in his
adopted country. The want of sympathy, and suspicion, which met
every suggestion of the young King, allied with the confusion
reigning in every department of the Government, made progress
unattainable, and the King, having nothing to do with his time in a
serious way, was soon found to be an easy prey to the seductions of
designing Spanish women, and it was not known till some time
afterwards that the Government had to interfere in ridding the Court
of an adventuress who managed to get into the Court circle.
As Queen Maria Victoria wrote to a valued friend in Italy, she
seemed wanting in the essential to make her a good Queen of
Spain, and that was the desire to remain in the country.
Sensitive as the young Sovereign was, she was ever conscious
of the half-concealed looks of scorn of those about her, who
wondered that she preferred the simple customs of a happy
domestic life to the pomp and etiquette of an old Court régime. The
Countess della Alinma and the Marquis of Ulugares sympathized
with Their Majesties’ tastes, but these two friends could not stop the
whispers of discontent and disapprobation reaching them from the
large circles of the great palace.
Much of the rigidity of Court etiquette was abandoned during the
short reign of Amadeus and Maria Victoria. The custom of courtiers
prostrating themselves on their knees before Their Majesties was
abandoned, and, thanks to the good sense of Alfonso XII., it was
never more resumed.
Queen Maria Victoria created an Order which was called by her
name, but it lapsed after her departure from the palace.
We read in “Cosas del Año 1873” (Things of the Year 1873), by
Carlos Frontaura, that many open insults had been levelled at the
Italian Sovereigns during the last few weeks of their reign. At the
Court reception which is always customary on New Year’s Day in
Spain, the Conservative deputies were conspicuous by their
absence, and Generals Serrano, Concha, Infante, Rivero, Allende,
Zabala and Hoyos, Topete, Malcampo, Martinez Espinosa, and the
ex-Ministers Rios, Rosas, etc., all excused themselves from
attending the banquet which took place in the evening.
The Countess of Heredia-Spinola gave a magnificent ball in her
house in Calle Fernando el Santo, and all the guests wore the fleur-
de-lis as a sign of their devotion to the Bourbon family.
Society at the Court of Spain was very different in the year 1872
from what it had been during the late dynasty.
As Napoleon I. said, “You may confer titles and dignities, but you
cannot give that particular cachet which goes with real Court
society.”
The Countess of Campo Alange always said, “Did So-and-so
learn the minuet when he was young?” For if the answer to this
question were in the negative, it showed that the courtier only
belonged to the new dynasty.
The Marquis of San Rafael was then Prime Minister, but when
the Marchioness wished to enter the Queen’s presence she was not
allowed to pass, whereas an arrogant lady of the old aristocracy
quickly forced her way in. The Prime Minister was advised to report
this slight to Amadeus himself. When the King heard of the matter,
he only shrugged his shoulders, and said, “Let them fight it out.”
The King and Queen felt that their days in Spain were numbered,
and it only wanted some incident to put the match to the train of
discontent.
The ostensible cause of the break of the King with the
Government was the appointment to the command of the artillery of
Hidalgo, who five years before had been in command of the
company which had made the insurrection in the barracks of San Gil
in 1866. The King himself did not favour this appointment, but when
Ruiz Zorilla showed him a vote of confidence in the course carried by
the Congress, Amadeus thought it time to resign the crown which
meant nothing but mortification to himself and his wife. So on that
evening (February 11, 1873) the republic was proclaimed, and six
o’clock the following morning saw the sad exit from Spanish Court
life of the Italians who had been so fruitlessly summoned to its circle.
Queen Maria Victoria had also been wounded in her
susceptibilities as a mother. When her second child was born to her
about a fortnight before the proclamation of the republic, the young
Sovereigns naturally expected that the Ministry, Diplomatic Corps,
military dignitaries, and clerical leaders, would be ready to greet the
baby Prince according to the Court etiquette of the country. But the
representatives of the country did not feel sufficient interest in the
birth of “the little foreigner” to hasten to pay him their respects; and
although the red and yellow flags waved triumphantly above the
royal palace, it was several hours before there gathered in the
audience chamber an assembly sufficiently large and august to
receive the presentation of the son of Amadeus and Maria Victoria.
It was hardly a fortnight later when the die was cast, and the
Italians decided to abandon the throne of Spain.
The personal attendants of the Queen wept as they saw her
carried to the entrance of the palace still weak and ill from her recent
confinement. The dethroned young King took the frail form of his wife
in his arms when she was taken from the litter at the foot of the
grand staircase, and, after placing her in the carriage waiting in the
archway, proudly saluted the Guard and stepped in by her side.
In a departure arranged so hurriedly, all the necessary comforts
were forgotten, and the royal invalid was faint for want of
nourishment, which was only attainable after hours of travelling.
Amadeus was grateful indeed for the soup he was at last able to
procure at a little railway-station on the line, and he boldly met the
remarks and curious looks of the people who crowded to see the
royal fugitive as he bore the cup from the restaurant to his wife.
Once in Portugal, Amadeus had nothing more to fear for the
personal safety of the family, and it was from thence they soon sailed
quietly for Italy.
CHAPTER XIV
SOME TRUTHS ABOUT THE REPUBLIC

1873–1874

We have an interregnum in the history of the Court of Spain during


the republic which held rule from February 11, 1873, until the
restoration of the monarchy on December 30, 1874; but those
readers, who like to have some idea of what was passing in Spain
whilst the palace was empty, may be interested in the following
particulars, drawn from a book entitled “Contemporaneous Truths,”
by His Excellency Vicente Lafuente. These truths were republished
by Colonel Figuerola Ferretti[19] in 1898, with an able prologue from
the officer’s pen, to show those malcontents who wished to return to
this form of government how baneful it was for the welfare of the
land.
[19] This Spaniard is connected on his mother’s side with Pope
Pius IX. (Mastai-Ferretti), whilst his father was Figuerola, the patriot
of Cuba.
L I E U T E N A N T- C O L O N E L L U I S D E F I G U E R O L A F E R R E T T I

From a Painting by Miss A. J. Challice, exhibited at the Royal Academy, London

Queen Maria Cristina graciously accepted the book from the


Colonel, who was then a Chamberlain at her Court, and it doubtless
served to disperse the false ideas as to the nature of a Spanish
republic which had arisen in the minds of those who were absent
from the country whilst it held sway.
Twenty-five years had elapsed since Spain adopted the republic,
but, as Figuerola Ferretti reminds his readers, that time had not
obliterated the horrors of that period from those who belonged to that
time.
Those who were inclined to regard a republic as an ideal form of
government were reminded that the fatal night of February 11, 1873,
saw the opening of the Pandora box, whence issued all sorts of
moral and political calamities, which spread like a black cloud over
the Spanish nation in both worlds. With the enthronement of moral
and material disorder, licence and anarchy came from all sides, to
the increase of impiety and corruption of customs, the ruin of
families, the debasement of the public credit, the demoralization of
the forces on sea and land, the loss of honour and national dignity,
and the peril of the independence and integrity of the country both in
the Peninsula and in America.
Such is the picture of the republic from the night of February 11,
1873, until the morning of January 3, 1874, when it was dissolved by
the coup of General Pavia. This opinion is no mere expression of
party rancour, for, as it is founded on the facts and events recorded
in the Gazette and the Journal of the Sessions of the Cortes, which
were noted day by day, they became, under the pen of the historian
Lafuente, the true history which, according to Cicero, is “the light of
truth and the master of life.”
A few quotations from this diary of facts, which Ferretti
republished as an antidote to the anti-dynastic feelings which were
aroused by the loss of Cuba, give some idea of the effect of the
republic on Spain:
“February 16, 1873.—Assassinations in Montilla under shocking
circumstances. Eight houses sacked and burnt; Señor Robobo
assassinated and quartered. Abolition of the oath of loyalty in the
army.
“February 20 and 21.—During these days the theatre of
Barcelona was the scene of dreadful military orgies and acts of
immorality and barbarism. The column of Cabrinati rebelled in Santa
Coloma de Farnés, at the instigation of the republicans, and the cry
of ‘Down with the officers!’ was heard all over Catalonia.
“February 24.—There was a general Carlist rising in Navarre, and
a call to arms of all men between twenty and forty years of age.
“February 28.—The neighbourhood of Madrid, in view of the
prevailing want of discipline and the ease with which dwellings could
be invaded, began organizing armed bands.
“March 15.—The battalion of ‘the Cazadores of Madrid’
committed unspeakable horrors in Falset, and several companies of
Catalonia began a course of pillage and immorality.
“March 17.—General Hidalgo harangued the savage soldiers of
Falset, but he was so hissed that he was obliged to retire, like almost
all the other officers.
“March 18.—A great meeting was held at San Isidro, where the
public commemorated what they called ‘the glories of the Commune
of Paris,’ which they were evidently seeking to imitate.”
The record of March closes with the mention of the occupation of
the churches of Barcelona as barracks and theatres.
April 3 we read: “The republicans of Manresa invade and profane
a church, take possession of the library and rooms of the seminary,
and the town-hall of Tarragona.
“May 13.—An electoral meeting in Barcelona; the popular Mayor
Buxó is wounded by a stone. The voluntary troops of Madrid knock
down and wound the chaplain of the hospital, insult the officials who
seek to release him, and commit various robberies and
assassinations, so that the troops have to be called out against
them.
“June 3.—In Madrid and other places the procession of the
Corpus Christi could not take place on account of the uproars in the
streets. Orgies in the churches of Belen and San José at Barcelona,
and indecent balls, in which the mysteries of our redemption were
mocked at.
“June 16.—Horrible assassinations at Bande (Orense). Sixty
unhappy beings of all ages and both sexes fell victims to this
savagery.”
After three days’ fighting the international incendiaries and
assassins were expelled from Seville, leaving the city stained with
blood and injured by fire.
“September 23.—General Don Manuel Pavia was appointed
Governor of Madrid.”
Carlism was rapidly gaining ground during these months. There
were 8,000 Carlists in Aragon and Valencia, and as many more in
Catalonia, 12,000 in Navarre, and more than that number in the
Basque provinces, thus making more than 40,000 Carlists in all
Spain.
“November 7.—Señor Castelar, the President of the Republic,
was daily losing power in the Congress, where neither eloquence nor
good sense seemed to have any sway over the turbulent spirits.”
When the Corporation of the city became disaffected from the
Government, it seemed to the Governor of Madrid that it was time for
him to assert the power of military rule.
So on December 2, when the chamber of the Congress was
nothing but a scene of riot and disorder, each deputy striving by his
loud voice and violent actions to overpower his fellow, the cultured
Castelar, the head of the republic, whose orations would have
reflected honour on the Areopagus of old, was met by a vote of want
of confidence.

You might also like