100% found this document useful (2 votes)
48 views79 pages

Java All in One For Dummies Fifth Edition Doug Lowe PDF Download

The document provides information about the 'Java All-in-One For Dummies, Fifth Edition' by Doug Lowe, including links for downloading the ebook. It outlines the contents of the book, which covers Java basics, programming techniques, object-oriented programming, JavaFX, web programming, and database management. Additional ebook recommendations are also included.

Uploaded by

slavczhilin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
48 views79 pages

Java All in One For Dummies Fifth Edition Doug Lowe PDF Download

The document provides information about the 'Java All-in-One For Dummies, Fifth Edition' by Doug Lowe, including links for downloading the ebook. It outlines the contents of the book, which covers Java basics, programming techniques, object-oriented programming, JavaFX, web programming, and database management. Additional ebook recommendations are also included.

Uploaded by

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

Java All in One For Dummies Fifth Edition Doug

Lowe install download

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

Download more ebook from https://ebookmeta.com


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

Java 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/

Java All in One For Dummies Seventh Edition Doug Lowe

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

Design and Operation of Production Networks for Mass


Personalization in the Era of Cloud Technology 1st
Edition Dimitris Mourtzis

https://ebookmeta.com/product/design-and-operation-of-production-
networks-for-mass-personalization-in-the-era-of-cloud-
technology-1st-edition-dimitris-mourtzis/
Alaska Sourdough Revised Edition The Real Stuff by a
Real Alaskan Ruth Allman

https://ebookmeta.com/product/alaska-sourdough-revised-edition-
the-real-stuff-by-a-real-alaskan-ruth-allman/

Altered Earth Getting the Anthropocene Right Julia


Adeney Thomas (Editor)

https://ebookmeta.com/product/altered-earth-getting-the-
anthropocene-right-julia-adeney-thomas-editor/

Maryport a Roman Fort and Its Community 1st Edition


David J Breeze

https://ebookmeta.com/product/maryport-a-roman-fort-and-its-
community-1st-edition-david-j-breeze/

Bound by Light 1st Edition Demi Warrik

https://ebookmeta.com/product/bound-by-light-1st-edition-demi-
warrik/

Neolithic Stepping Stones Excavation and Survey Within


the Western Seaways of Britain 2008 2014 1st Edition
Duncan Garrow Fraser Sturt

https://ebookmeta.com/product/neolithic-stepping-stones-
excavation-and-survey-within-the-western-seaways-of-
britain-2008-2014-1st-edition-duncan-garrow-fraser-sturt/
Long Live Queer Nightlife: How the Closing of Gay Bars
Sparked a Revolution 1st Edition Amin Ghaziani

https://ebookmeta.com/product/long-live-queer-nightlife-how-the-
closing-of-gay-bars-sparked-a-revolution-1st-edition-amin-
ghaziani/
Java®

ALL-IN-ONE

5th Edition

by Doug Lowe

Java® All-in-One For Dummies®, 5th Edition Published by:


John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-
5774, www.wiley.com Copyright © 2017 by John Wiley & Sons, Inc.,
Hoboken, New Jersey

Published simultaneously in Canada

Library of Congress Control Number: 2017934999

ISBN: 978-1-119-24779-1 (pbk); ISBN 978-1-119-24780-7 (ebk);


ISBN 978-1-119-24781-4 (ebk) Manufactured in the United States of
America

Contents at a Glance

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.................1

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


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

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


. . . . . . . . . . . . . . . . . . 21

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


. . . . . . . . . . . . . . . . . . 35

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


. . . . . . . . . 43

CHAPTER 1: Java Programming Basics . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . 45

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


. . . . . . . . . . . . . . . . 65

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


. . . . . . . . . . . . . . . 99

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


. . . . . . . . . . . . . . . . . . . 129

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


. . . . . . . . . . . . . . 151

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


. . . . . . . . . . . . . . . . . . 179

CHAPTER 7: Adding Some Methods to Your Madness . . . . . . . . . . .


. . . . . . . . . . . . . . 193

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


. . . . . . . . . . . . . . . . . 213

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


. . . . 233
CHAPTER 1: Understanding Object-Oriented Programming . . . . . . .
. . . . . . . . . . . . 235

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


. . . . . . . . . . . . . . . . . 249

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


. . . . . . . . . . . . . . . . . . 267

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


. . . . . . . . . . . . . . . . 277

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


. . . . . . . . . . . . . . . 299

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


. . . . . . . . . . . . . . . . 315

CHAPTER 7: Using Inner Classes, Anonymous Classes,

and Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . 341

CHAPTER 8: Working with Packages and the New Java Module


System . . . . . . . . . . 353

Book 4: Strings, Arrays, and Collections . . . . . . . . . . . . . . . . . .


. . . 373

CHAPTER 1: Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . 375

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


. . . . . . . . . . . . . . . . . . . 393

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


. . . . . . . . . . . . . . . . . 423
CHAPTER 4: Using the LinkedList Class . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . 437

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


. . . . . . . . . . . . . . . 449

CHAPTER 6: Using Bulk Data Operations with Collections . . . . . . . .


. . . . . . . . . . . . . 463

Book 5: Programming Techniques . . . . . . . . . . . . . . . . . . . . . .


. . . . . . 473

CHAPTER 1: Programming Threads . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . 475

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


. . . . . . . . . . . . . . . . 505

CHAPTER 3: Using Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . 523

CHAPTER 4: Working with Dates and Times . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . 539

Book 6: JavaFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . 561

CHAPTER 1: Hello, JavaFX! . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . . . 563

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


. . . . . . . . . . . . . . . . . . 583

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


. . . . . . . . . . . . . . . 603

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


. . . . . . . . . . . . . . 625
CHAPTER 5: Getting Input from the User . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . 659

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


. . . . . . . . . . . . . . . . . 681

Book 7: Web Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . 707

CHAPTER 1: Using Java Web Start . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . 709

CHAPTER 2: Creating Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . . 721

CHAPTER 3: Using JavaServer Pages . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . 741

CHAPTER 4: Using JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . . 757

Book 8: Files and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . 775

CHAPTER 1: Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . 777

CHAPTER 2: Working with File Streams . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . 799

CHAPTER 3: Database for $100, Please . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . 827

CHAPTER 4: Using JDBC to Connect to a Database . . . . . . . . . . . .


. . . . . . . . . . . . . . . . 845

CHAPTER 5: Working with XML . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . . . . 861
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . 881

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

The Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .11

Java versus Other Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .12

Important Features of the Java Language . . . . . . . . . . . . . . . . . . . .


. . . .13

Type checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .13

Automatic memory management . . . . . . . . . . . . . . . . . . . . . . . . . .


. .15

Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .16

On the Downside: Java’s Weaknesses . . . . . . . . . . . . . . . . . . . . . . .


. . . .16

Java Version Insanity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .17

What’s in a Name? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . .19

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


. . . . . . . . . . 21

Downloading and Installing the Java Development Kit . . . . . . . . . . .


. .22

Downloading the JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .22
Installing the JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .23

Perusing the JDK folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .23

Setting the path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .24

Using Java’s Command-Line Tools . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .26

Compiling a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .26

Compiling more than one file . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .27

Using Java compiler options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .28

Running a Java program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .29

Using the javap command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .31

Using Java Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .32

Java SE API Docs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .32

Java Language Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .33

Table of Contents v
CHAPTER 3: Working with TextPad . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . 35

Downloading and Installing TextPad . . . . . . . . . . . . . . . . . . . . . . . .


. . . .35

Editing Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .36

Compiling a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .39

Running a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .40

BOOK 2: PROGRAMMING BASICS . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . 43

CHAPTER 1: Java Programming Basics . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . 45

Looking at the Infamous Hello, World! Program . . . . . . . . . . . . . . . .


. .46

Dealing with Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .49

Working with Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .51

Types of statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .51

White space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .52

Working with Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .53
Creating Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .55

Crafting Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .55

End-of-line comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .56

Traditional comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .56

JavaDoc comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .57

Introducing Object-Oriented Programming . . . . . . . . . . . . . . . . . . .


. . .57

Understanding classes and objects . . . . . . . . . . . . . . . . . . . . . . . . .


.57

Understanding static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .58

Creating an object from a class . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .59

Viewing a program that uses an object . . . . . . . . . . . . . . . . . . . . . .


.60

So what’s the difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .62

Importing Java API Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .63

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


. . . . . . 65
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .66

Declaring two or more variables in one statement . . . . . . . . . . . .


.67

Declaring class variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .67

Declaring instance variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .68

Declaring local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .69

Initializing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .70

Initializing variables with assignment statements . . . . . . . . . . . . .


.71

Initializing variables with initializers . . . . . . . . . . . . . . . . . . . . . . . . .


.72

Using Final Variables (Constants) . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .72

Working with Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .73

Integer types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .75

Floating-point types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .76

The char type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .78
The Boolean type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .79

Wrapper classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .79

Using Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .80

vi Java All-in-One For Dummies

Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .82

Declaring and initializing strings . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .82

Combining strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .83

Converting primitives to strings . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .83

Converting strings to primitives . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .84

Converting and Casting Numeric Data . . . . . . . . . . . . . . . . . . . . . . .


. . . .85

Automatic conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .85

Type casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .86

Thinking Inside the Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .87
Understanding Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .87

Shadowing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .89

Printing Data with System.out . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .90

Using standard input and output streams . . . . . . . . . . . . . . . . . . .


.90

Using System.out and System.err . . . . . . . . . . . . . . . . . . . . . . . . . .


. .92

Getting Input with the Scanner Class . . . . . . . . . . . . . . . . . . . . . . .


. . . . .92

Importing the Scanner class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .93

Declaring and creating a Scanner object . . . . . . . . . . . . . . . . . . . . .


.94

Getting input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .94

Getting Input with the JOptionPane Class . . . . . . . . . . . . . . . . . . . .


. . . .96

Using enum to Create Your Own Data Types . . . . . . . . . . . . . . . . . .


. . .97

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


. . . . . 99

Working with Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .99
Dividing Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .102

Combining Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .104

Using the Unary Plus and Minus Operators . . . . . . . . . . . . . . . . . . .


. .105

Using Increment and Decrement Operators . . . . . . . . . . . . . . . . . . .


. .106

Using the Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .108

Using Compound Assignment Operators . . . . . . . . . . . . . . . . . . . . .


. .110

Using the Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .111

Using constants of the Math class . . . . . . . . . . . . . . . . . . . . . . . . .


.112

Working with mathematical functions . . . . . . . . . . . . . . . . . . . . . .


.113

Creating random numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.116

Rounding functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .119

Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .121

Recognizing Weird Things about Java Math. . . . . . . . . . . . . . . . . . .


. . .124
Integer overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .125

Floating-point weirdness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .125

Division by zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .126

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


. . . . . . . . . . . . . . 129

Using Simple Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .130

Using if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .132

Simple if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .132

if-else statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .134

Table of Contents vii

Nested if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .135

else-if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .139

Using Mr. Spock’s Favorite Operators (Logical Ones, of Course) . . .


.141

Using the ! operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .142
Using the & and && operators . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.143

Using the | and || operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.144

Using the ^ operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .145

Combining logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.146

Using the Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .148

Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .149

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


. . . . 151

Using Your Basic while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .152

The while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .152

A counting loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .152

Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .154

Looping Forever . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .154

Letting the user decide when to quit . . . . . . . . . . . . . . . . . . . . . . .


.156
Letting the user decide in another way . . . . . . . . . . . . . . . . . . . . .
.157

Using the continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .157

Running do-while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .159

Validating Input from the User . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .161

Using the Famous for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .163

Understanding the formal format of the for loop . . . . . . . . . . . . .164

Scoping out the counter variable . . . . . . . . . . . . . . . . . . . . . . . . . .


.166

Counting even numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .167

Counting backward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .168

Using for loops without bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.169

Ganging up your expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.170

Omitting expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .171

Breaking and continuing your for loops. . . . . . . . . . . . . . . . . . . . .


.172
Nesting Your Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .173

A simple nested for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .173

A guessing game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .174

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


. . . . . . . . . . . . 179

Battling else-if Monstrosities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .179

Viewing an example else-if program . . . . . . . . . . . . . . . . . . . . . . .


.180

Creating a better version of the example program . . . . . . . . . . . .181

Using the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .183

Viewing a boring switch example, complete with flowchart . . . . .184

Putting if statements inside switch statements . . . . . . . . . . . . . .


.185

Creating Character Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .186

Intentionally Leaving Out a Break Statement . . . . . . . . . . . . . . . . . .


. .188

Switching with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .191

viii Java All-in-One For Dummies


CHAPTER 7: Adding Some Methods to Your Madness . . . . . . .
. . . . 193

The Joy of Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .194

The Basics of Making Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .195

An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .196

Another example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .197

Methods That Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .199

Declaring the method’s return type . . . . . . . . . . . . . . . . . . . . . . . .


.199

Using the return statement to return the value . . . . . . . . . . . . . .


.200

Using a method that returns a type . . . . . . . . . . . . . . . . . . . . . . . .


.201

You gotta have a proper return statement . . . . . . . . . . . . . . . . . .


.201

Trying another version of the guessing-game program . . . . . . . .203

Methods That Take Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .206

Declaring parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .207
Scoping out parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .208

Understanding pass-by-value . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.209

Trying yet another version of the guessing-game program . . . . .210

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


. . . . . . . . . . . . 213

Understanding Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .214

Witnessing an exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .215

Finding the culprit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .215

Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .216

A simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .217

Another example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .218

Handling Exceptions with a Preemptive Strike . . . . . . . . . . . . . . . . .


. .220

Catching All Exceptions at Once . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .221

Displaying the Exception Message . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .223
Using a finally Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .223

Handling Checked Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .226

Viewing the catch-or-throw compiler error . . . . . . . . . . . . . . . . . .


.226

Catching FileNotFoundException . . . . . . . . . . . . . . . . . . . . . . . . . .
.227

Throwing the FileNotFoundException . . . . . . . . . . . . . . . . . . . . . .


.228

Throwing an exception from main . . . . . . . . . . . . . . . . . . . . . . . . .


.229

Swallowing exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .229

Throwing Your Own Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .231

Catching Multiple Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .232

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


. 233

CHAPTER 1: Understanding Object-Oriented

Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 235

What Is Object-Oriented Programming? . . . . . . . . . . . . . . . . . . . . .


. . .235
Understanding Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .237

Objects have identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .237

Objects have type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .238

Table of Contents ix

Objects have state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .239

Objects have behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .240

Understanding the Life Cycle of an Object . . . . . . . . . . . . . . . . . . . .


. .240

Working with Related Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .241

Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .242

Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .242

Designing a Program with Objects . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .243

Diagramming Classes with UML . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .244

Drawing classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .245
Drawing arrows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .246

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


. . . . . . . . . . . 249

Declaring a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .249

Picking class names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .250

Knowing what goes in the class body . . . . . . . . . . . . . . . . . . . . . . .


.250

Seeing where classes go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .251

Working with Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .253

Understanding fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .253

Understanding methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.254

Understanding visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .254

Using Getters and Setters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .255

Overloading Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .257

Creating Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .259
Creating basic constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .259

Creating default constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.260

Calling other constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .262

Finding More Uses for the this Keyword . . . . . . . . . . . . . . . . . . . . . .


. .263

Using Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .265

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


. . . . . . . . . . . . . 267

Understanding Static Fields and Methods . . . . . . . . . . . . . . . . . . . .


. . .267

Working with Static Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .269

Using Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .269

Counting Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .270

Preventing Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .273

Using Static Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .274

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


. . . . . . . 277
Introducing Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .277

Motorcycles, trains, and automobiles . . . . . . . . . . . . . . . . . . . . . .


.279

Game play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .279

A businesslike example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .280

Inheritance hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .280

Creating Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .281

Overriding Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .283

x Java All-in-One For Dummies

Protecting Your Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .284

Using this and super in Your Subclasses . . . . . . . . . . . . . . . . . . . . .


. . .284

Understanding Inheritance and Constructors . . . . . . . . . . . . . . . . . .


.286

Using final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . .287

Final methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .287
Final classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .288

Casting Up and Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .289

Determining an Object’s Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .290

Poly What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .292

Creating Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .294

Tracing the Throwable hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . .


.294

Creating an exception class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .296

Throwing a custom exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.296

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


. . . . . 299

Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .299

Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .302

Creating a basic interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .303

Implementing an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .304
Using an interface as a type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .305

More Things You Can Do with Interfaces . . . . . . . . . . . . . . . . . . . . .


. . .306

Adding fields to an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.306

Extending interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .306

Using interfaces for callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.307

Using Default Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .311

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


. . . . . . . 315

The Mother of All Classes: Object . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .316

Every object is an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .316

Object as a type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .316

Methods of the Object class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .317

Primitives aren’t objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .319

The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .319
Using toString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .320

Overriding toString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .321

The equals Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .322

Using equals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .323

Overriding the equals method . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.324

The clone Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .328

Implementing the clone method . . . . . . . . . . . . . . . . . . . . . . . . . . .


.329

Using clone to create a shallow copy . . . . . . . . . . . . . . . . . . . . . . .


.332

Creating deep copies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .333

The Class Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .338

Table of Contents xi

CHAPTER 7: Using Inner Classes, Anonymous

Classes, and Lambda Expressions . . . . . . . . . . . . . . . . . . . 341

Declaring Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .342
Understanding inner classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.342

Viewing an example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .343

Using Static Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .345

Using Anonymous Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .347

Creating an anonymous class . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.347

Creating a program with an anonymous class . . . . . . . . . . . . . . .


.349

Using Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .351

CHAPTER 8: Working with Packages and the New Java

Module System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 353

Working with Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .354

Importing classes and packages . . . . . . . . . . . . . . . . . . . . . . . . . . .


.354

Creating your own packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .355

An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .357
Putting Your Classes in a JAR File . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .358

jar command-line options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .359

Archiving a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .360

Adding a jar to your classpath . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .361

Running a program directly from an archive . . . . . . . . . . . . . . . .


.362

Using JavaDoc to Document Your Classes . . . . . . . . . . . . . . . . . . . .


. . .363

Adding JavaDoc comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .363

Using the javadoc command . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .366

Viewing JavaDoc pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .367

Using the Java Module System . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .368

Understanding modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .369

The module-info.java file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .369

Setting up folders for a module . . . . . . . . . . . . . . . . . . . . . . . . . . .


.371
Compiling a module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .371

Creating a modular JAR file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .372

BOOK 4: STRINGS, ARRAYS, AND COLLECTIONS . . . . . . . . . .


373

CHAPTER 1: Working with Strings . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . 375

Reviewing Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .376

Using the String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .378

Finding the length of a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .380

Making simple string modifications . . . . . . . . . . . . . . . . . . . . . . . .


.380

Extracting characters from a string . . . . . . . . . . . . . . . . . . . . . . . . .


.381

Extracting substrings from a string . . . . . . . . . . . . . . . . . . . . . . . . .


.382

Splitting a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .384

Replacing parts of a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .386

xii Java All-in-One For Dummies


Using the StringBuilder and StringBuffer Classes . . . . . . . . . . . . . . .
.387

Creating a StringBuilder object . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.388

Using StringBuilder methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .388

Viewing a StringBuilder example. . . . . . . . . . . . . . . . . . . . . . . . . . .


.390

Using the CharSequence Interface . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .391

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


. . . . . . . . . . . . . . . . 393

Understanding Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .393

Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .394

Initializing an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .395

Using for Loops with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .396

Solving Homework Problems with Arrays . . . . . . . . . . . . . . . . . . . . .


. .397

Using the Enhanced for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .400

Using Arrays with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .401
Using Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .401

Creating a two-dimensional array . . . . . . . . . . . . . . . . . . . . . . . . . .


.402

Accessing two-dimensional array elements . . . . . . . . . . . . . . . . .


.403

Initializing a two-dimensional array . . . . . . . . . . . . . . . . . . . . . . . .


.405

Using jagged arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .405

Going beyond two dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.407

Working with a Fun but Complicated Example: A Chessboard . . . . .


.408

Using the Arrays Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .416

Filling an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .417

Copying an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .417

Sorting an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .419

Searching an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .419

Comparing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . .420
Converting arrays to strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .420

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


. . . . . . . . . . . 423

Understanding the ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .424

Creating an ArrayList Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .427

Adding Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .428

Accessing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .429

Printing an ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .430

Using an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .430

Updating Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .432

Deleting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .434

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


. . . . . . . . . . 437

Understanding the LinkedList Class . . . . . . . . . . . . . . . . . . . . . . . . .


. . .438

Creating a LinkedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .442
Adding Items to a LinkedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .443

Retrieving Items from a LinkedList . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .445

Updating LinkedList Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .446

Removing LinkedList Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .447

Table of Contents xiii

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


. . . . . . 449

Why Generics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .450

Creating a Generic Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .451

A Generic Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .453

Using Wildcard-Type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .457

A Generic Queue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .458

Using the Diamond Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .462

CHAPTER 6: Using Bulk Data Operations with Collections . . .


. . 463
Looking At a Basic Bulk Data Operation . . . . . . . . . . . . . . . . . . . . . .
. .464

Looking Closer at the Stream Interface . . . . . . . . . . . . . . . . . . . . . .


. . .467

Using Parallel Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .470

BOOK 5: PROGRAMMING TECHNIQUES . . . . . . . . . . . . . . . . . .


. . 473

CHAPTER 1: Programming Threads . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . 475

Understanding Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .476

Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .477

Understanding the Thread class . . . . . . . . . . . . . . . . . . . . . . . . . . .


.477

Extending the Thread class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .478

Creating and starting a thread . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.480

Implementing the Runnable Interface . . . . . . . . . . . . . . . . . . . . . . .


. . .480

Using the Runnable interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.481

Creating a class that implements Runnable . . . . . . . . . . . . . . . . .


.481
Using the CountDownApp class . . . . . . . . . . . . . . . . . . . . . . . . . . .
.483

Creating Threads That Work Together . . . . . . . . . . . . . . . . . . . . . . .


. . .485

Using an Executor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .489

Synchronizing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .491

Creating a Lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .496

Coping with Threadus Interruptus . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .497

Finding out whether you’ve been interrupted . . . . . . . . . . . . . . .


.498

Aborting the countdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .499

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


. . . . . . . . . . 505

Creating a Program for Experimenting with Regular Expressions . .


.506

Performing Basic Character Matching . . . . . . . . . . . . . . . . . . . . . . .


. . .509

Matching single characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.509

Using predefined character classes . . . . . . . . . . . . . . . . . . . . . . . .


.509
Using custom character classes . . . . . . . . . . . . . . . . . . . . . . . . . . .
.512

Using ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .513

Using negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .514

Matching multiple characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.514

Using escapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .516

Using parentheses to group characters . . . . . . . . . . . . . . . . . . . . .


.516

Using the pipe symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .518

xiv Java All-in-One For Dummies

Using Regular Expressions in Java Programs . . . . . . . . . . . . . . . . . .


. .519

Understanding the String problem . . . . . . . . . . . . . . . . . . . . . . . . .


.519

Using regular expressions with the String class . . . . . . . . . . . . . .


.520

Using the Pattern and Matcher classes . . . . . . . . . . . . . . . . . . . . .


.521

CHAPTER 3: Using Recursion . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . 523
Calculating the Classic Factorial Example . . . . . . . . . . . . . . . . . . . . .
. .523

The nonrecursive solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .524

The recursive solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .524

Displaying Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .526

Writing Your Own Sorting Routine . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .530

Understanding how Quicksort works . . . . . . . . . . . . . . . . . . . . . . .


.530

Using the sort method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .531

Using the partition method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .532

Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .534

CHAPTER 4: Working with Dates and Times . . . . . . . . . . . . . .


. . . . . . . . 539

Pondering How Time is Represented . . . . . . . . . . . . . . . . . . . . . . . .


. . .540

Picking the Right Date and Time Class for Your Application . . . . . . .
.541

Using the now Method to Create a Date-Time Object . . . . . . . . . . . .


.542
Using the parse Method to Create a Date-Time Object . . . . . . . . . . .
.544

Using the of Method to Create a Date-Time Object . . . . . . . . . . . . . .


.545

Using the Month enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.545

Using the ZoneId class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .547

Using the ZoneOffset class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .548

Looking Closer at the LocalDate Class . . . . . . . . . . . . . . . . . . . . . . .


. . .548

Extracting Information About a Date . . . . . . . . . . . . . . . . . . . . . . . .


. . .550

Comparing Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .551

Calculating with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .552

Formatting Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .554

Looking at a Fun Birthday Calculator . . . . . . . . . . . . . . . . . . . . . . . .


. . .556

BOOK 6: JAVAFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . 561

CHAPTER 1: Hello, JavaFX! . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . . . . . . . . . . . 563
Perusing the Possibilities of JavaFX . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .564

Looking at a Simple JavaFX Program . . . . . . . . . . . . . . . . . . . . . . . .


. . .566

Importing JavaFX Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .567

Extending the Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .568

Launching the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .569

Overriding the start Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .570

Creating a Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .572

Handling an Action Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .573

Table of Contents xv

Creating a Layout Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .574

Making a Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .576

Setting the Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .576

Examining the Click Counter Program . . . . . . . . . . . . . . . . . . . . . . .


. . .577
CHAPTER 2: Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . 583

Examining Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .584

Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .585

Implementing the EventHandler Interface . . . . . . . . . . . . . . . . . . . .


. .587

Handling Events with Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . .


. . .591

Handling Events with Anonymous Inner Classes . . . . . . . . . . . . . . . .


.593

Using Lambda Expressions to Handle Events . . . . . . . . . . . . . . . . . .


. .596

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


. . . . . . . 603

Examining the Stage Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .604

Examining the Scene Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .607

Switching Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .609

Creating an Alert Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .613

Exit, Stage Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . . .618
Creating a Close button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .619

Handling the CloseRequest event . . . . . . . . . . . . . . . . . . . . . . . . . .


.621

Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .622

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


. . 625

Working with Layout Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .626

Introducing five JavaFX layout panes . . . . . . . . . . . . . . . . . . . . . . .


.626

Creating layout panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .626

Combining layout panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. .628

Using the HBox Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .628

Spacing Things Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .630

Adding Space with Margins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .632

Adding Space by Growing Nodes . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .633

Using the VBox Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .635
Aligning Nodes in a Layout Pane . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . .637

Making Nodes the Same Width . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .638

Using the Flow Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .639

Using the Border Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .643

Using the GridPane Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . .645

Sketching out a plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .646

Creating a grid pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . .647

Working with grid pane constraints . . . . . . . . . . . . . . . . . . . . . . . .


.650

Examining a grid pane example . . . . . . . . . . . . . . . . . . . . . . . . . . .


.652

xvi Java All-in-One For Dummies

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


. . . . . . . . . 659

Using Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . . .660

Validating Numeric Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


. . . . .667
Other documents randomly have
different content
She was still, however, a favourite ship, taking 520 passengers out
and bringing home 125,000 ounces of gold under Captain Clarke.
On her sixth voyage she for the first time got into trouble as she
parted her tow rope when leaving the Mersey and got aground off the
Huskisson Dock, after first colliding with a barque at anchor in the river.
However she came off on the flood without damage and sailed for
Melbourne on 7th December, 1855, arriving out on 26th February, an 83-
day passage. In 1856 she went out in 89 days, leaving Liverpool 5th
September.
Her most serious mishap was on her passage home in 1861, when she
collided with an iceberg on 4th March. Her bowsprit was carried away,
bow stove in and foremast sprung; in fact, so seriously was she
damaged that she was very near being abandoned. Eventually, however,
she managed to struggle into Valparaiso after a month of incessant
pumping. Here she was repaired and, continuing her voyage, at length
arrived at Liverpool on 21st August, 183 days out from Melbourne.
Though Messrs. James Baines sold her to another Liverpool firm in the
early sixties, she still continued regularly in the Melbourne trade, and as
late as 1867 I find another fine passage to her account, which is thus
described by Captain Coates in his Good Old Days of Shipping:—“Captain
Labbet, of Brisbane, once told me that in January, 1867, he took
passage home in the steamship Great Britain. The Marco Polo left at the
same time and was soon lost sight of. A week later the look-out man of
the Great Britain reported a sail right ahead, and shortly afterwards
expressed his belief that it was the Marco Polo, in which ship he had
previously sailed. His opinion, however, was scoffed at; on the ship
being neared he proved to have been right. She was again distanced
and the Great Britain made what was esteemed a good passage. On
taking the pilot off Cork, the first question asked was:—“Have you seen
the Marco Polo?” The reply came:—“Yes, she passed up 8 days ago.” She
had made the passage in 76 days.

st Notable Clippers of 1853.


The Marco Polo was followed across the Atlantic by numerous
other Nova Scotian built ships from the yards of W. & R. Wright and
Smith.
The most notable of these were the Ben Nevis, which arrived during
the summer of 1852, and the Star of the East, Miles Barton, Guiding Star
and Indian Queen, which arrived at Liverpool in 1853. All these ships
were intended to lower the colours of Marco Polo, but not one of them
succeeded in doing so, though they made some very good passages.

en Nevis.”
The Ben Nevis was the first ship owned by Pilkington & Wilson.
She was, however, too short and deep for her tonnage, her
measurements being:—
Length over all 181 feet.
Beam 38 feet 6 inches.
Depth of hold 28 feet.
Registered tonnage 1420.

Commanded by Captain Heron, she sailed for Melbourne on 27th


September, 1852, with 600 passengers, a cabin passage in her costing
£25, and she took 96 days going out.

e “Star of the East.”


A far more worthy ship to compete with the Marco Polo was the
Star of the East, which arrived in Liverpool on 5th March, 1853, 20 days
out from St. John’s against strong N.E. winds. She was built by W. &. R.
Wright, her dimensions being:—
Length of keel 206 feet.
Length over all 237 „
Beam 40 feet 10 in.
Depth of hold 22 feet.
Registered tonnage 1219 tons.

The following are some of her spar measurements:—


Mainmast—extreme length 84 feet; diameter 41 inches.
Main topmast—extreme length 53 feet; diameter 19 inches.
Main topgallant mast—extreme length 75 feet; diameter 14 inches.
Bowsprit and jibboom—outboard 55 feet.
Mainyard 89 „
Main topsail yard 70 „
Main topgallant yard 52 „
Main royal yard 36 „
Main skysail yard 27 „
Sail area (studding sails excepted) 5500 yards.

At the time of her launch she was considered the finest ship ever built
at St. John’s. On her arrival in Liverpool she was at once bought by Mr.
James Beazley, having cost him when ready for sea £22,683. She loaded
for Australia in the Golden Line, and went out to Melbourne in 76 days
under Captain Christian, late of Beazley’s Constance. From Melbourne
she went to Sydney and loaded across to Shanghai; then sailing from
Shanghai in the favourable monsoon, arrived home in 104 days, 4 of
which were spent anchored off Gutztaff Island in a typhoon. The whole
voyage only occupied 9 months 27 days, and she cleared £8018 clear
profit. Her second voyage on the same route she did still better, clearing
£8920.

e “Miles Barton.”
The Miles Barton measured:—
Length 175 feet.
Beam 35 „
Depth 22 „
Registered tonnage 963 tons.

She also was bought by James Beazley and loaded in the Golden Line.
On her maiden voyage she went out to Melbourne in 82 days, and
followed up this performance with two trips of 76 days each.

e “Guiding Star.”
Arrived in Liverpool in October, 1852, and was at once
chartered by the Golden Line for £12,000, considered a huge sum in
those days. Her life, however, was not a long one, as she was lost with
all hands between January and April, 1854, and it was generally
supposed that she became embayed and back-strapped by a huge ice
island in about 44° S., 25° W.
Tragic encounters with ice were by no means unusual in the fifties
when every passage maker was trying to follow out Maury’s instructions
by running far down into southern latitudes in search of strong fair
winds.

e “Indian Queen.”
The Indian Queen, 1041 tons, the most notable Black Baller
launched in 1853, and advertised as Marco Polo’s sister ship, was a very
fast vessel, her first voyage to Australia being made in 6 months 11
days, and in 1855 she came home from Hobart in 78 days. In 1859 she
narrowly escaped the fate of Guiding Star. On 13th March, 1859, she
sailed from Melbourne for Liverpool under Captain Brewer, with 40
passengers and the usual cargo of wool and gold dust. All went well
until she was half way to the Horn, when on the 27th March the weather
became thick with a strong N.W. wind and heavy westerly swell.
On the 31st March she was in 58° S., 151° W. by account; the day
was wet, foggy and very cold and the ship logged a steady 12 knots
with the wind strong at N.W. At 2 a.m. on the following morning those
below were aroused by a violent shock, the crash of falling spars and a
grinding sound along the port side, and the first of the frightened
passengers to arrive on the poop found the ship lying broadside to
broadside with an immense iceberg. All her spars and sails above the
lower masts were hanging over the starboard side, the foremast was
broken off close to the deck and was held at an angle by its rigging, the
mainyard was in half, the bowsprit was washing about under the bows,
and though the mizen topmast was still standing the topsail yard was in
two, broken in the slings.
The night was dark and rainy and at first the watch below and
passengers thought that all was lost. They found no one at the wheel,
the port life-boat gone, and not a soul on the poop, but they were
somewhat reassured by the appearance of the carpenter who had been
sounding the pumps and pronounced the ship to be making no water.
Then the second mate appeared aft and announced that the captain,
mate and most of the crew had gone off in the port life-boat. Apparently
there had been a disgraceful panic which involved even the captain, who
actually left his own son, an apprentice, behind on the ship.
However those who had been so shamefully deserted began to buckle
to with a will, headed by the second mate, Mr. Leyvret, and the cool-
headed carpenter, a man named Thomas Howard. Passengers, cooks,
stewards and those of the crew left on board were promptly divided into
watches, the captain’s son was sent to the wheel, and whilst some set
about clearing up the raffle of gear and getting things ship-shape as far
as possible, others shovelled the ice, which lay in masses on the decks,
overboard.
With some difficulty the crossjack was backed and the head of the
spanker hauled in. At the same time the boat was perceived tossing in
the swell on the port beam and apparently endeavouring to regain the
ship, and faint cries for help could be heard against the wind. She
seemed to be without oars and with sea after sea washing over, she was
soon swept past the ship by the back wash off the ice and lost sight of
in the fog never to be seen again. The ship, though, with the backed
crossjack, began to drift along the side of the berg and presently
dropped clear of it into smoother water to leeward.
Day now began to break and all hands set about cutting away the
wreck, but the mainyard and the rest of the raffle hanging from the
stump of the mainmast was hardly clear before the terrible cry of “Ice to
leeward!” arose and a huge berg appeared looming out of the mist. The
crossjack was at once braced up, the spanker set and the foresail
trimmed in some fashion or other, then in a tense silence the survivors
watched the ship slowly forge ahead and, dragging the wreck of masts
and spars and torn sails along with her, weather the new danger by a
bare 100 yards. And scarcely had she done so when the foremast fell
crashing on to the long-boat, the other boats having been already stove
in by falling spars. The next business was to get the wreck of the
foremast over the side and clear of the ship. Here the carpenter
displayed the greatest coolness and skill, being ably backed up by the
second mate and the 4 seamen left on board. With the last of the wreck
overside, time was found to muster the survivors, when it was
discovered that the captain, chief mate and 15 men had been lost in the
port life-boat, leaving behind the second mate, carpenter, bosun, 4
A.B.’s, 1 O.S. and 2 boys, besides the cooks, stewards, doctor, purser,
and passengers who numbered 30 men, 3 women and 7 children.
A course was now steered for Valparaiso, some 3800 miles away. It
was not until the 7th April that the ship got finally clear of the scattered
ice, but on the 3rd the wind came out of the south and with a lower
stunsail and main staysail set on the main, the ship began to make 3 or
4 knots through the water.
One iceberg of huge size and square like a mountainous box was only
just cleared before it broke in two, the smaller portion bursting into the
sea like an avalanche, and sweeping a huge wave in front of it, did not
bring up until it was 2 to 3 miles away from the rest of the berg. The
last ice was seen in 54° S., it being reckoned that the accident had
happened in 60° S.
As soon as 49° S. was reached, a direct course was shaped for
Valparaiso. Sheers were now rigged and a topmast secured to the stump
of the foremast, then topsail yards were crossed on the jury foremast
and mainmast, which improved the ship’s progress another knot. In this
condition the Indian Queen slowly wandered north, weathering out gale
after gale. On the 7th May a welcome sail was sighted. This proved to
be the New Bedford whaler La Fayette, whose captain boarded them,
offered them every assistance and corrected their longitude, which was
3° out. On the following day the French man-of-war Constantine
appeared and promised to convoy them in. On the 9th May land was
made some 20 miles south of Valparaiso, and on the morning of the
10th, as the crippled Indian Queen approached the Bay, the boats of
H.M.S. Ganges, 84 guns, came out to her aid and towed her in to the
Roads, where she anchored safely, just 40 days after her collision with
the iceberg.

e Famous “Sovereign of the Seas.”


My notes on the emigrant ships sailing from Liverpool in 1853
would not be complete without some mention of the celebrated
American clipper Sovereign of the Seas. This ship was built by Donald
Mackay for the American Swallowtail Line and at the time of her launch,
June, 1852, was hailed as the largest merchant ship in the world, her
measurements being:—
Length of keel 245 feet.
Length between perpendiculars 258 „
Length over all 265 „
Beam 44 „
Depth 23 „
Tonnage (American Register) 2421 tons.

Her lower masts from deck to cap were:—


Foremast 89 feet; mainmast 93 feet; mizen 82 feet.

Her lower yards measured in length:—


Foreyard 80 feet; mainyard 90 feet; crossjack yard 70 feet.

And her topsail yards:—


Fore topsail yard 63 feet; main 70 feet; mizen 56 feet.

She spread 12,000 yards of canvas in her working suit.


On her maiden voyage she carried a crew of 105 men and boys,
including 2 bosuns, 2 carpenters, 2 sailmakers, 3 stewards, 2 cooks, 80
A.B.’s and 10 boys before the mast. She was commanded by Donald
Mackay’s younger brother, Captain Lauchlan Mackay, one of the best
known skippers in the United States.
Loading 2950 tons of cargo and receiving 84,000 dollars freight, she
sailed from New York for San Francisco on 4th August, 1852; and
considering the season of the year, she made a wonderful run south,
crossing the equator in 25 days and reaching 50° S. in 48 days.
“SOVEREIGN OF THE SEAS.”
Larger image (204 kB)

She was nine days making the passage of the Horn from 50° S. to 50°
S.; but shortly after rounding the Horn she carried away her fore and
main topmasts and sprang her foreyard. Captain Mackay, however, kept
the seas and refitted his ship in 14 days, during the whole of which time
he is said to have remained on deck, snatching what little sleep he
allowed himself in a deck chair. The Sovereign of the Seas in spite of this
mishap arrived in San Francisco only 103 days out, and this was
considered the best passage ever made at such an unfavourable season
of the year.
From San Francisco she went across to Honolulu in ballast and there
loaded a cargo of sperm oil; it being the custom of American whalers to
call in there and leave their oil for transhipment so as to clear their holds
for a fresh catch.
The Sovereign of the Seas left Honolulu on 13th February, 1853, for
New York, and once again made a most remarkable passage in spite of
a sprung fore topmast, jury fore topgallant mast and a weak crew—no
doubt a large number of her original crew deserted in San Francisco in
the hope of reaching the gold diggings, but more probably only to be
shanghaied on some homeward bounder.
Like all Mackay’s wonderful creations, the Sovereign of the Seas was
at her best in the roaring forties, and on the run to the Horn she made
3144 miles in 10 days, her best 24-hour runs being:—
March 11 332 miles.
„ 12 312 „
„ 16 396 „
„ 17 311 „
„ 18 411 „
„ 19 360 „

During this time she had strong quartering winds and a heavy
following sea, which drove her at times as much as 19 knots through the
water.
After rounding the Horn, she had the usual weather up through the
tropics, and arrived at New York on 6th May, 1853, having made the
record passage of 82 days from Honolulu.
As she was considered to be too big for either the San Francisco or
China trades, she was at once loaded for Liverpool, there to take part in
the booming Australian emigrant trade.
And crossing the Western Ocean she once more made an
extraordinary passage, as the following epitome shows:—
June 18 —Sailed from New York, passed Sandy Hook at 6.30 p.m.
„ 24 —Sighted Cape Race at 6 a.m.
„ 26 —Becalmed on the Banks.
„ 28 —Distance run 344 miles—ship close-hauled under single reefed topsails.
„ 30 —Distance run 340 miles, under all sail to skysails and royal stunsails off
Cape Clear at 6 a.m.
July 2—Anchored in the Mersey at 10.30 p.m.
Passage New York to Liverpool, from dock to anchorage, 13 days 22 hours 50 minutes,
and 5 days 17 hours from the Banks of Newfoundland.
Donald Mackay crossed the Atlantic on the ship and spent his whole
time watching her every movement, and it was probably the experience
gained on this passage which had much to do with the wonderful
success of his later vessels.
On her arrival in Liverpool the Sovereign of the Seas was at once
chartered by the Black Ball Line. Captain Lauchlan Mackay, however, did
not remain in her, but returned to New York, his place being taken by
Captain Warner, who had been in the ship since she was launched.
Captain Warner sailed from Liverpool on 7th September, 1853, with 25
first cabin, 40 second cabin passengers and a cargo valued at £200,000,
and wrote the following account of his passage to the Liverpool Mercury:

I arrived here after a long and tedious passage of 77 days, having experienced only
light and contrary winds the greater part of the passage. I have had but two chances.
The ship ran in four consecutive days 1275 miles; and the next run was 3375 miles in
12 days. These were but moderate chances. I was 31 days to the equator and carried
skysails 65 days; set them on leaving Liverpool and never shortened them for 35 days.
I crossed the equator in 26° 30′, and went to 53° 30′ S., but found no strong winds. I
think if I had gone to 58° S. I would have had wind enough: but the crew were
insufficiently clothed and about one half disabled, together with the first mate. At any
rate we have beaten all and every one of the ships that sailed with us, and also the
famous English clipper Gauntlet 10 days on the passage, although the Sovereign of the
Seas was loaded down to 23½ feet.

Sovereign of the Seas’ passage was, in fact, an exceedingly good one,


considering all things, but there was not much glory attached to beating
the little Gauntlet, which only measured 693 tons register and was built
of iron.
The Sovereign of the Seas sailed from Melbourne with the mails and a
very large consignment of gold dust; but amongst her crew she had
shipped some old lags, who attempted a mutiny in order to seize the
ship and get away with the gold. However, Captain Warner succeeded in
suppressing these rascals without bloodshed and kept them in irons for
the rest of the passage.
The Sovereign of the Seas made the splendid time of 68 days
between Melbourne and Liverpool; but after this one voyage for the
Black Ball she seems to have returned to her original owners, who put
her into the Shanghai trade for a voyage or two before selling her to a
Hamburg firm.

st Outward Passages for 1853-4, Anchorage to Anchorage.


Date Arrived
Ship. Port from. Date Left. Dys.
Melbourne.
Try Bristol Oct. 12, ’52 Jan. 12, ’53 92
Alipore London „ 16, „ „ 19, „ 95
Marian Moore Liverpool Nov. 15, „ Feb. 15, „ 92
Kent London Jan. 27, ’53 Apl. 20, „ 83
Eagle Liverpool Feb. 22, „ May. 13, „ 80
Marco Polo „ Mar. 14, „ „ 29, „ 76
Bothnia „ „ 5, „ June 3, „ 90
Ganges London „ 23, „ „ 22, „ 91
Osmanli Liverpool Apl. 16, „ July 4, „ 79
Indian Queen „ May 17, „ Aug. 8, „ 82
Gibson Craig London June 4, „ „ 22, „ 79
Star of the East Liverpool July 7, „ Sept. 23, „ 78
Statesman S’thampton „ 10, „ Oct. 5, „ 87
Tasmania Liverpool „ 23, „ „ 23, „ 92
Mobile „ Aug. 16, „ Nov. 16, „ 92
Sovereign of the Seas „ Sept. 7, „ „ 26, „ 80
Chimera „ „ 17, „ Dec. 17, „ 92
Neleus „ Oct. 5, „ „ 24, „ 80
Flying Dragon London „ 14, „ „ 30, „ 77
Kent „ „ 26, „ Jan. 12, ’54 78
Marco Polo Liverpool Nov. 8, „ „ 31, „ 84
Salem „ Dec. 7, „ Feb. 28, „ 83
Essex „ „ 9, „ Mar. 12, „ 92
Marlborough London Jan. 1, ’54 „ 19, „ 77
Indian Queen Liverpool „ 29, „ Apl. 21, „ 84
Crest of the Wave „ Feb. 14, „ „ 28, „ 73

54—The Year of the Big Ships.


The result of Sovereign of the Seas’ visit to Liverpool and that
of her builder and designer Donald Mackay was a further order to
America and Nova Scotia for still bigger ships.
In fact, Donald Mackay returned to Boston with James Baines’
commission to build the famous quartette, Lightning, Champion of the
Seas, James Baines and Donald Mackay, which were shortly to astonish
the world. Against these the White Star Line put forward the equally big
White Star and Red Jacket, two vessels which both in strength, beauty
and speed were worthy to be ranked on equal terms with the great
Black Ballers.
Only two wooden ships were ever launched in England which could
compare in size with these six giants. One of these was the ill-fated
Schomberg and the other the beautiful Sobraon, which, however, had
iron frames and was not launched until the palmy days of the gold rush
were over. Both came from the famous yard of Hall, of Aberdeen.
Schomberg was, of course, wrecked on her maiden passage, but
Sobraon, though never as hard sailed as the great Black Ball and White
Star ships, made equally good passages, and being built of the finest
Malabar teak retained her speed right up to the end of her long and
successful career.
In comparing the measurements of the American built, Nova Scotian
built and Aberdeen built ships the most noticeable point is the greater
beam of the Nova Scotians and the greater length of the British.
This is well shown by the following table:—
Lightning 5.54 beams to length.
Red Jacket 5.54 beams to length.
American Built
Champion of the Seas 5.55 beams to length.
James Baines 5.70 beams to length.
Donald Mackay 5.72 beams to length.

British Built Schomberg 5.82 beams to length.


Sobraon 6.80 beams to length.

Nova Scotian Built Marco Polo 4.86 beams to length.


White Star 4.84 beams to length.

rrying On.
Perhaps no ships ever sailed the seas which held on to their
canvas longer than these great Black Ball and White Star clippers; and
yet the carrying away of spars and sails, which was so common an
occurrence with the earlier American clippers and also with the early
British iron clippers, was quite rare on these big emigrant ships.
There is no difficulty, however, in finding reasons for their freedom
from dismasting and heavy casualties aloft, their designers and builders
had learnt something by the dismastings and constant losses of spars
which overtook their earlier ships, and thus no ships were more
scientifically stayed than these big ships, at the same time in their outfit
we find hemp rigging and wooden spars in their highest state of
efficiency. Strength of gear had for some time been one of the chief
problems that a clipper ship builder had to contend with, and in the
rigging of these six famous ships we see this problem finally mastered.
Topsails, topgallant sails and even royals were diagonally roped from
clew to earing. The rope used for standing rigging was the very best
procurable and of immense thickness; for instance, Lightning’s lower
rigging, fore and main stays and backstays were of 11½ inch Russian
hemp; whilst in regard to spars, here are the diameters in inches of
some of James Baines’ masts and yards:—
Mainmast 42 inches in diameter.
Main topmast 21 inches in diameter.
Main topgallant mast 16 inches in diameter.
Main royal mast 14 inches in diameter.
Mainyard 26 inches in diameter.
Main topsail yard 21 inches in diameter.
Main skysail yard 8 inches in diameter.

vantages of a Light Load Line and High Side.


But added to their greater strength aloft these great clippers
had another advantage over their older sisters in the Californian trade.
They sailed on a lighter load line and showed a higher side. Four or
five hundred emigrants made them dry and buoyant instead of wet and
hard mouthed. Besides being very easy in a sea-way, these big emigrant
clippers were extraordinarily steady ships without any tendency to heavy
quick rolling. This is easily proved from their logs, for one constantly
reads that their passengers were able to enjoy dancing on the poop
when the ships were running 15 and 16 knots before the strong gales
and big seas of easting weather.
Speaking at a dinner given in Melbourne in honour of Captain Enright,
Mr. Alexander Young, a veteran voyager to and from the Antipodes, who
had just travelled out in the Lightning, remarked:—“I have much
pleasure in adding my slight testimony to her well-earned fame by
stating that she is the driest and easiest ship I have ever sailed in. I
assure you, ladies and gentlemen, that we scarcely shipped a bucketful
of water all the passage, and when going 16 knots an hour there was
scarcely any more motion than we feel at the present moment.”
And here are other proofs of the Lightning’s steadiness taken from the
Lightning Gazette, a newspaper published on board:—
9th February, 1855.—14 knots upon a bowline with the yards braced sharp up and
while going at this extraordinary rate she is as dry as possible, seldom shipping a
spoonful of water. During the greater part of the day the carpenter was employed on a
stage below the fore chains, where he worked as easily as if it had been calm.
18th March, 1857.—The wind increases a little towards evening and we make 15 to
17 knots an hour, yet the ship is so steady that we danced on the poop with the
greatest ease (Lat. 42° 34′ S., Long. 17° 04′ W.)
21st February, 1855.—During this time the ship was going 16 knots an hour and in
the saloon the motion was so slight that we thought she had only a light breeze.

amples of Carrying Sail.


Two or three quotations also from the log books and shipboard
newspapers may be of interest to show the power of these ships to
carry sail in heavy weather and strong winds.
Here are two days from the log of the James Baines when running her
easting down in 1856:—
16th June.—Lat. 43° 39′ S., Long. 101° E.; Bar. 29.80°. Wind, S.W. to W.S.W.
Commences with fresh breezes and squalls of sleet, 8 a.m., more moderate. Noon,
sighted a ship ahead; at 1 p.m. was alongside of her and at 2 p.m. she was out of
sight astern. James Baines was going 17 knots with main skysail set, the Libertas, for
such was her name, was under double-reefed topsails.
18th June.—Lat. 42° 47′ S., Long. 115° 54′ E. Bar. 29.20°. Wind, W. to S.W. First
part breeze freshening. At 6 p.m. wind S.W. and freshening. At 8.30 p.m. in all
starboard studding sails; ship going 21 knots with main skysail set. Midnight, fresh gale
and fine clear night. 8 a.m., wind and weather the same. Noon, less wind attended
with snow squalls. Distance 420 miles.
Then in the Lightning Gazette I find the following entries:—
15th January, 1855.—Lat. 39° 42′ N., Long. 19° 25′ E. Wind. S.S.E., strong breezes
and cloudy, with occasional squalls and showers; the ship going 13 knots close-hauled.
In the morning we passed a ship outward bound with topgallant sails in and exchanged
colours with a Swedish brig homeward bound—this vessel was under close-reefed
topsails, while we were carrying three royals and main skysail.
26th February, 1855.—Lat. 45° 48′ S.; Long., 16° 55′ E. Wind, N.N.W., course, S.E.
Another wet uncomfortable day; thick mist and small rain. The barometer had been
falling for a day or two back and went down half an inch last night. The change took
place at 4 p.m., when the wind suddenly shifted to the west and soon afterwards to
S.W., from whence it blew hard with squalls and occasional showers of hail and snow.
At 8 p.m. it backed again to west, where it remained all night, blowing a fresh gale, the
ship running 16 and occasionally 18 knots per hour with main skysail and topgallant
studding sails set.
27th February, 1855.—Lat. 46° 22′ S., Long. 26° 15′ E. Wind, west, course S.E. All
last night it blew a fresh gale with heavy squalls and occasional showers of hail and
snow, the sea running high. From noon yesterday till noon to-day, we ran down 9
degrees and 20 miles of longitude and 34 miles of latitude, making 390 geographical
miles or 450 English miles direct course in the 24 hours, giving an average of 16¼
knots or 18¾ statute miles per hour. During 6 hours in the morning the ship logged 18
knots per hour with royals, main skysail and topgallant studding sails set, the wind
blowing a fresh gale from the westward.
21st October, 1855.—Lat. 36° 4′ S., Long. 24° 52′ W. During the afternoon the wind
chopped round and blew strongly from the S.W. At 5 p.m. sighted a large ship on our
weather quarter, sailing under double-reefed topsails and we apprehend they must
have taken us for the Flying Dutchman seen occasionally in these latitudes, for
notwithstanding the strong breeze we could be observed carrying our skysails with
studding sails ‘low and aloft.’
14th March, 1857.—Lat. 34° 47′ S., Long. 35° 06′ W. The breeze a splendid one. A
barque on the port beam about 3, homeward bound. The wind was as fair for her as
wind could be, yet she had no royals set. We formed a striking contrast to her, for we—
on a wind—had all sail set up to main skysail.
20th March, 1857.—Lat. 43° S., Long. 0° 55′ E. We have made during the last 47
hours the greatest run that perhaps ship ever made, yet all the time we have carried
our main skysail and all sorts and conditions of studding sails.

raordinary 24-hour Runs.


I have quoted the above passages to show the way in which a
Black Baller could carry sail either with a fresh favouring gale or in a
strong head wind. This is sufficiently astonishing in itself, but what
amazes most present day sailors and compels many of them to be
incredulous are such statements as the much quoted one concerning
James Baines—“Ship going 21 knots with main skysail set.”
This and other log book statements have been looked upon by many
as far-fetched exaggerations, but, after careful study of the subject,
during which I have pricked off the different voyages on a track chart, I
have come to the conclusion that these amazing performances were in
no way a stretching of the imagination.
To begin with, I will give the main arguments advanced against them
by the sceptics.
The late Mr. J. N. Barry, writing in an Australian paper, remarks:—
Where American records are concerned much caution must be observed in taking
their feats of speed for granted. Our cousins had a canny fashion of, no matter where
they might be sailing, always reckoning 60 miles to a degree of longitude whilst doing
their easting, so that a day’s run of, say, 240 miles upon a parallel of 45°, would by this
means give the distance covered as exactly 100 miles in excess of what it should be.

Another nautical writer remarks:—


The skippers of many of the celebrated Black Ball clippers were not above adopting
this mode of calculation, viz., 60 miles to a degree of longitude, but while it gave some
wonderful results for a single 24 hours, it did not as a matter of fact make their
passages any more rapid.

And I have had letters scoffing at the Black Ball records, remarking
that their skippers were a leery lot and provided “palatable pabulum for
the proud passengers.”
I will now try and show that these arguments were altogether too
sweeping, and if they may possibly have applied to certain individuals,
they are by no means fair to the greater number of the skippers.
In the first place, not one of the Black Ball or White Star ships was
commanded by an American, and though the accusation was levelled at
Americans, it was evidently done in the belief that the American built
Australian clippers were commanded by Americans.
In the second place, such men as Anthony Enright, of the Lightning,
James Nicol Forbes, of the Marco Polo, Charles McDonald, of the James
Baines, Sam Reid, of the Red Jacket, Captain Pryce, R.N.R., of the
Donald Mackay, and Alexander Newlands, of the Champion of the Seas,
were known and respected all over the world as leading men in their
profession, occupying a position in the Mercantile Marine which would
correspond with that of Orient and P. & O. commanders nowadays,
whilst their performances were very much more widely known, thus
such elementary cheating as giving 60 miles to a degree in the roaring
forties would have been exposed at once.
The greatest 24-hour run ever accomplished by a sailing ship was one
of 436 nautical miles made by the Lightning when crossing the Atlantic
on her maiden passage. The second greatest run was also made by the
Lightning. This was 430 miles when running her easting down bound out
to Australia in 1857, and on the following day her run was 360. This
wonderful performance drew the following letter from Captain Enright to
his passengers, and I think it will dispose of the 60 miles to a degree
accusation, at any rate as far as the Lightning and her commander are
concerned:—
21st March, 1857.
Ladies and Gentlemen,—I cannot help informing you of the extraordinary run we have
made during the last 48 hours—or rather allowing for change of time, 46 hours and 48
minutes. During this time we have run, by thoroughly good and trustworthy
observation, no less than 790 knots or 920 statute miles, being an average of nearly 17
knots or more than 19½ statute miles per hour. Yesterday our noble ship made no less
than 430 knots amounting to an average during the 24 (23½) hours of more than 18
knots. Our change of longitude has amounted to 18 degrees, each degree being equal
to 44 miles.
I firmly believe this to be the greatest performance a sailing ship has ever
accomplished.
I hope this information will in some degree compensate you for the inconvenience
which the heavy weather has occasioned you.
And I remain, Ladies and Gentlemen,
Very faithfully yours,
A. Enright, Commander.

If further proof is wanted that Captain Enright did not allow 60 miles
to a degree, but only 44 as he states to his passengers, here are the
noon positions found by observation, not account only, from which the
runs can be verified on the chart.
March 18, Lat. 42° 34′ S., Long. 17° 04′ W.
19, Lat. 43° 0′ S., Long. 7° 17′ W.
20, Lat. 43° 0′ S., Long. 0° 55′ E.

The following is a list of all runs of 400 miles and over, which I have
been able to verify.
March 1, 1854.—Lightning 436 miles.
March 19, 1857.—Lightning 430 miles.
February 6, 1855.—James Baines 423 miles.
February 27, 1855.—Donald Mackay 421 miles.
June 18, 1856.—James Baines 420 miles.
February 27, 1854.—Red Jacket 413 miles.
January 27, 1855.—James Baines 407 miles.
July 6, 1854.—Red Jacket 400 miles.

All these performances were made running east, making the day’s
work under 24 hours.
Several other ships claimed runs of over 400 miles, but I have not
included these as I have not sufficient particulars to verify them.
Marco Polo is supposed to have done a run of 428 miles under Captain
McDonald on 7th January, 1854, and Shalimar 420 miles in 1855 on her
first passage to Australia, under Captain Robertson. With this general
account of their powers I must now return to a more detailed
description of the giant clippers themselves.

e “Lightning.”
The Lightning was built by Donald Mackay to the order of
James Baines in the winter of 1853-4 at a cost of £30,000, and on her
arrival in Liverpool was furnished and decorated below at a further cost
of £2000.
“LIGHTNING.”
From a painting.
Larger image (206 kB)

Her measurements were:—


Tonnage (builders) 2096 tons.
(register) 1468 „
(burthen) 3500 „
Length 244 feet.
Beam 44 „
Depth 23 „
Dead rise at half-floor 20 inches.

Her poop was 92 feet long and her saloon 86 feet, whilst she had 8
feet under the beams in her ’tween decks, a most unusual height for
those days.
With regard to design, she was one of the sharpest ships ever
launched. Her model is thus described by Captain H. H. Clark:—“She had
long, concave water-lines and at her load displacement line a cord from
her cut-water to just abaft the fore rigging showed a concavity of 16
inches. Her stem raked boldly forward, the lines of the bow gradually
becoming convex and blending with the sheer line and cut-water, while
the only ornament was a beautiful full-length figure of a young woman
holding a golden thunderbolt in her outstretched hand, the flowing white
drapery of her graceful form and her streaming hair completing the fair
and noble outline of the bow.
“The after-body was long and clean, though fuller than the bow, while
the stern was semi-elliptical in form, with the plank sheer moulding for
its base, and was ornamented with gilded carved work, though this
really added nothing to the beauty of the strong sweeping outline of her
hull.”
The Lightning’s spar and rigging measurements were tremendous:—
Mainmast, deck to truck 164 feet.
Foremast „ „ 151 „
Mizenmast „ „ 115 „
Mainyard „ „ 95 „
Lower stunsail booms 65 „

She spread 13,000 yards of canvas when under all plain sail. Donald
Mackay had her rigged as a three skysail yard ship, but later Messrs.
James Baines fitted her with a moonsail on the main by lengthening the
skysail mast. This was also done in the case of James Baines. And these
two ships had the proud distinction of being perhaps the only two ships
afloat which regularly crossed a moonsail yard.
The Lightning was provided with iron water tanks holding 36,000
gallons of water—a novelty at that date. And in various other ways her
accommodation for passengers was an improvement on anything
attempted before.
The great Bully Forbes was sent out to Boston to superintend her
outfit and take command of her, and he was lucky in finding a valuable
friend and adviser in Captain Lauchlan Mackay, who made the trip to
Liverpool in her as builders’ representative.

e “Red Jacket.”
The Red Jacket, Lightning’s great rival, was designed by
Samuel A. Pook, of Boston, the well-known designer of Game-cock,
Surprise, Northern Light, Ocean Telegraph, Herald of the Morning, and
other famous clipper ships. She was built by George Thomas at
Rockland, Maine, for Messrs. Seacomb & Taylor, and only took the water
a few days before the Lightning.
Her measurements were:—
Tonnage (registered) 2460 tons.
(burthen) 5000 „
Length 260 feet.
Beam 44 „
Depth 26 „

Though her bow and stern were very sharp and beautifully modelled
and she had concave bow lines, she was not so extreme a ship as the
Lightning.
“RED JACKET.”
From an old lithograph.
Larger image (195 kB)

Donald Mackay’s ships were chiefly distinguished for their powerful


workmanlike appearance rather than for delicate beauty—they showed
strength rugged and unmistakable, but the Red Jacket’s strength was
more disguised under graceful curves; for instance, she had the graceful
arched stem and clipper bow of a China ship, whereas Lightning’s stem
was almost straight, with only a very slight curve in it.
Red Jacket was not named after Tommy Atkins, but after a great
Indian chief, and her figure-head was a beautiful representation of this
warrior in all the magnificence of feather head-dress and beaded
buckskins.

ce across the Atlantic between “Lightning” and “Red Jacket.”


The Lightning loaded at Constitution Wharf, Boston, and sailed
for Liverpool on 18th February, 1854, whilst the Red Jacket sailed from
New York on the following day, and great interest was shown in shipping
circles as to which should make the best passage across the Atlantic.
In the end these two magnificent clippers arrived in Liverpool on the
same day, 4th March, their exact times being:—
Red Jacket—Sandy Hook to Rock Light 13 days 1 hour.
Lightning—Boston Light to Rock Light 13 days 19½ hours.

Their 24-hour runs opened the eyes of the packet ship commanders
and in fact the whole world.
The Red Jacket put up runs of 413, 374, 371, 343, and 300 against
the Lightning’s 436, 328, 312 and 306, thus there was little to choose
between the two vessels on this point.
The Boston Daily Atlas of 18th February, 1854, thus describes the
Lightning’s departure from Boston:—
At 2 o’clock the Lightning hove her anchor up, and at 3 o’clock discharged her pilot
off Boston Light. She went down in tow of the steamer Rescue, Captain Hennessy, and
was piloted by Mr. E. G. Martin.
Before the steamer left her, she set her head sails, and fore and mizen topsails, and
had a moderate breeze from W. to S.W. She appeared to go at the rate of 6 knots
under this canvas, though she draws 22 feet of water and has only 23 feet depth of
hold.
We have seen many vessels pass through the water, but never saw one which
disturbed it less. Not a ripple curled before her cut-water, nor did the water break at a
single place along her sides. She left a wake as straight as an arrow and this was the
only mark of her progress. There was a slight swell, and as she rose we could see the
arc of her forefoot rise gently over the seas as she increased her speed. At 5 p.m., two
hours after the pilot left her, the outer telegraph station reported her 30 miles east of
Boston Light with all drawing sails set and going along like a steam boat.

And the following extract from her log book was published in the
Liverpool Albion on her arrival.
Distance.
Feb. 19 Wind, W.S.W. and N.W. moderate 200 miles.
20 „ N.N.E. and N.E. strong breezes with snow 328 „
21 „ E.S.E. with snow storms 145 „
22 „ E.S.E., a gale with high cross sea and rain 114 „
23 „ N., strong gales to E.S.E.; ends moderate 110 „
24 „ S.E., moderate 312 „
25 „ E.S.E. and S.E., fresh breezes with thick weather 285 „
26 „ W.S.W., moderate 295 „
27 „ W.N.W. „ 260 „
28 „ W. and N.W., steady breezes 306 „
March 1 „ South. Strong gales; bore away for the North Channel;
carried away the fore topsail and lost jib; hove the log
several times and found the ship going through the water
at the rate of 18 to 18½ knots; lee rail under water and
rigging slack 436 „

South, first part moderate, latter part light and calm.
2

Light winds and calms.
3
„ Light S.E. winds and calms; at 7 a.m. off Great Orme’s Head. 12 noon
8 off the N.W. lightship.

On 28th February at noon she was in Lat. 52° 38′ N., Long. 22° 45′
W., and her run of 436 nautical miles from that position to her noon
position on 1st March gives her the greatest day’s work ever
accomplished, to the best of my belief, by a sailing ship. The 1st March
entry “Wind south—bore away for the North Channel,” has misled some
nautical critics, who have plotted her as being up with Rathlin Island
when she bore away, without noticing the direction of the wind. The log
is rather ambiguously worded, but her run of 436 miles puts her some
30 miles west of Achill Head—and she then bore away north, bringing
the wind on the starboard quarter. If she had been off Rathlin Island she
would have had to bring the wind on the starboard bow for the course
through the North Channel.
Captain Charles McDonald always hoped to get a day’s run of 500
miles out of the James Baines, and firmly believed she could do it; but
he never succeeded in beating the Lightning’s records.
The Red Jacket, which was under the command of Captain Asa
Eldridge, of American packet ship fame, had strong winds from S.E. to
W.S.W. with rain, snow and hail. As with Lightning, the first half of her
passage was the slowest half and for the first seven days she could only
average 182 miles a day. But with practically the same weather, it is
interesting to compare the performances of the two vessels as they
approached the Irish Coast. Red Jacket’s last six runs were 219, 413,
374, 343, 300, and 371, giving a total of 2020 and an average of 336.
The only vessel that has ever beaten this six-day run is the famous
Cutty Sark, which in 1876, before her wings were clipped, ran 2163
miles in six days in the roaring forties, when outward bound to Sydney.

ed Jacket’s” First Voyage to Australia.


At Liverpool Captain Eldridge handed over his command to
Captain Samuel Reid, who managed to get the Red Jacket away for
Australia, as one of the White Star regular packets, 10 days ahead of
Captain Forbes. The Red Jacket sailed on 4th May, 1854, one day behind
a new Nova Scotian built Black Ball packet named the Mermaid.
On the 10th May the two ships were off Oporto, and kept close to
each other as far as Teneriffe; the N.E. trades were poor and it was a
light weather passage to the line, which was crossed on 29th May by the
Red Jacket, the Mermaid being then in 1° north.
From this point the Red Jacket, steering a more westerly course, had
light and variable winds, whilst the Mermaid was better treated and
reached the latitude of the Cape five days ahead, and still held better
winds, being actually 1397 miles ahead of the Red Jacket on 15th June.
Red Jacket, indeed, did not really get going until 26th June, but from
that date her log is so remarkable that I give it below.
The Red Jacket was in 40° S., 14° E., before there was any need to
touch her topgallant sheets, and Captain Reid was evidently determined
to find wind somehow, with the result that, in spite of it being the depth
of winter, he was not deterred from standing far to the southward on a
Great Circle course. He was rewarded by all the wind he could desire,
but so great was the cold that the ship was put down by the head by the
frozen spindrift which covered her to the mainmast in an icy mantle.
Her log from 26th June, when she first began to feel the benefit of the
westerlies, was as follows:—
Date. Lat. Long. Weather. Dist.
° ′ ° ′
June 26 48 06S 34 44E Var. and stiff rain and sleet. 315
27 50 06 42 19 Wind N.W., fresh and squally with hail, very cold weather. 330
28 50 54 49 16 Wind W.N.W., squalls with hail showers. 263
29 50 34 56 34 Wind N.N.W., squalls, entire fore part of ship covered 286
with ice.
30 52 03 63 50 Wind N.N.W., fresh with hail squalls; very cold, air 19°. 287
July 1 51 39 71 21 Wind N.N.W., fresh, with hail squalls, latter part light, air 286
19°.
2 50 29 72 26 Wind S.W., first part calm, latter part heavy gales and
heavy sea.
3 50 12 80 30 Wind W.S.W., first part heavy gales, latter part fresh 312
breezes, high sea, freezing.
4 49 25 88 30 Wind variable, fresh gales and heavy sea, freezing, rain 300
and sleet.
5 49 13 95 00 Wind N.N.W., first part light and heavy rain, latter stiff, 288
with heavy squalls.
6 48 38 104 15 Wind W.N.W., strong gales and squalls, heavy sea. 400
7 47 25 112 44 Wind variable in strength and direction. 299
8 46 38 119 44 Wind N.N.W., stiff and squalls, with rain. 350
9 45 09 129 18 Wind N.N.W., strong and squally, with rain. 357
10 42 42 134 38 Wind N.N.W., fine weather. 334
11 40 36 139 35 Wind N.W., heavy squalls and rain. 245
12 Wind N.N.W., fine weather. Made King’s Island at 10.50 300
p.m., crossed bar at 11.50 p.m.

Red Jacket made the passage from Rock Light to Port Phillip Heads in
69 days 11 hours 15 minutes; passage under sail 67 days 13 hours, total
distance run 13,880 miles.
The Mermaid, which gained such an advantage over the Red Jacket in
the earlier part of the passage, ran her easting down a good deal further
to the northward, and did not arrive till the 17th July, having made a
passage of 74½ days.
Red Jacket set sail on her homeward passage on 3rd August. She was
not in very good trim this time, being too light and very much down by
the stern, however, she still continued to show her quality, constantly
logging 17 or 18 knots in fresh breezes and 14 and 15 knots when close-
hauled. Only once on the homeward passage were her topsails close-
reefed and only once did she ship any water. This was on the 31st
August in a heavy squall with foresail and fore and main topgallant sails
set.
She rounded the Horn on the 23rd August, only 20 days out, her
week’s work averaging out as follows:
1st week 231 miles per day.
2nd „ 307 „ „
3rd „ 254 „ „

But on the day after she had rounded the Horn, she had a narrow
escape of being embayed by ice, and one of her passengers gave the
following account of her danger to the newspapers:—“On the morning of
24th August, I was roused out of sleep by the noise of shortening sail
and the look-out singing out land. Ice had been seen some time before,
but the solid masses had been supposed in the dark to be land. On
getting out I found we were in smooth water and large masses of ice
floating about us. As the day broke, we found ourselves sailing along a
lake of water not unlike a canal. The ice seemed to extend on every side
in solid fields as far as the eye could reach without any prospect of
getting out, so that we had to follow the channel. All sail was clewed up
except the topsails, and as there was a good breeze we proceeded along
at about 4 or 5 knots. Our situation at this time seemed most appalling,
as we appeared to be getting further into the ice, so that by 10 or 11
o’clock we were almost making up our minds to remain for weeks in this
fearful situation.
“About noon the captain and second mate, who had been on the fore
topsail yard all the morning, discovered clear sea again, to gain which
we had to force a passage through dense masses of ice. It was here she
sustained the principal damage to her stem and copper. We soon got
clear and the rest of the day we saw no traces of ice and were very
thankful we had got off so easily. But to our dismay at 8 p.m. we again
fell in with it. The ship was put about and sail shortened for the night
and we ran back to the clear water in which we had been sailing. At
daybreak sail was made and at 7 a.m. we came up to the ice. At first it
was only large pans much melted, the water having all the appearance
of brine and being quite thick round them. Afterwards large masses of
icebergs presented themselves. In grinding the ship through these, great
difficulty was experienced—very large bergs were also interspersed and
visible all round.
“This day we cleared it again about noon. Icebergs were still,
however, seen both near and in the distance; their appearance was most
grand, the largest being thought to be about 2 miles in circumference
and 100 feet high. It was passed about 4 or 5 miles distant on our
starboard and lee side.
“We hove to again at night. Next day, Saturday, was for the most part
a dead calm and we were carried back with the current. There was not a
breath of wind; a clear sky and beautiful weather, only the air sharp.
Icebergs were, however, still seen. The next day, Sunday, we passed a
number more, which were the last ice seen. One of these was most
grand, being about 200 feet high. We cleared it on our port or windward
side about a mile or less distant. The weather during this period was
clear and fine. Indeed, the day before encountering the ice was
beautiful, a fine light breeze which heightened towards evening and sea
smooth. We were running close-hauled 14 knots an hour steadily during
the night. The sun had set a deep crimson behind a bank of clouds over
against Cape Horn.”
Red Jacket’s next three weeks’ runs averaged:—
4th week, 205 miles per day.
5th week, 237 miles per day. (Mostly light breezes, squalls and rain.)
6th week, 224 miles per day. (Easterly winds.)

The line was crossed on 13th September, the Red Jacket having run
10,243 miles in 42 days, an average of 244 per day. She now had every
hope of beating the record, but, alas, from here on she had nothing but
calms and light head winds which drove her across into 43° W. and she
was 31½ days from the line to port, reaching Liverpool on 15th October,
after a passage of 73 days. This was considered an extraordinary
performance, when allowance was made for the light weather
experienced after crossing the line. During one whole week in the
doldrums she averaged under 100 miles per day, and the two following
weeks she only averaged 142 and 106 miles respectively.
The whole voyage, however, had been a wonderfully fast one. She
had made the trip, out and home, in 5 months 10 days and 22½ hours,
and had actually circumnavigated the globe in 62 days 22 hours,
between 11th June and 2nd September, running 15,991 miles in that
time.
On her homeward passage she ran 14,863 miles, her greatest day’s
work being 376 miles and her average 202¼ miles per day.
She brought home gold dust and sovereigns to the value of £208,044.
She sailed this voyage under the American flag, being only chartered by
the White Star Line, but on her return to Liverpool Messrs. Pilkington &
Wilson bought her for the sum of £30,000.

e “Lightning’s” First Voyage to Australia.


The Lightning, with the famous Bully Forbes in command and
the almost equally famous Bully Bragg as mate, left Liverpool on the
14th May for Melbourne. But unlike the Red Jacket, she had a light
weather passage out, her topgallant sails being carried the whole way.
She crossed the line 25 days out and took 30 days running from the
meridian of the Cape to Port Phillip Heads, arriving off Sandridge Pier on
the afternoon of 31st July, 77 days from Liverpool, her best runs being
348, 332, 329, 311, and 300.
On the morning of the 20th August she left her anchorage at
Melbourne in company with the Mermaid, having gold dust on board to
the value of £1,000,000. The tug dropped her off the Heads at 4 p.m.,
and by the following noon she had done 268 knots. At 4 a.m. on the
24th she passed a large ship supposed to be the Mermaid, and at 10
p.m. on the same day passed the Auckland Islands. From here she had
fresh westerly and south-westerly winds, seldom logging less than 14
and frequently 18½ and 19 knots per hour. Forbes carried on in the
most daring manner, and on the Lightning’s arrival at Liverpool her
passengers told weird stories of Bully Forbes keeping his station at the
break of the poop with a pistol in each hand in order to prevent his
scared crew from letting go the royal halliards.
By 28th August the ship was in 57° 20′ S., but at 11 p.m. on this day
a violent squall from the S.W. carried away the fore topmast stunsail
boom, and a moment later the fore topmast went over the side, the fore
royal, fore topgallant sail and fore topsail being blown out of the bolt
ropes at the same instant.

You might also like