Core Java for the Impatient 3rd Edition Cay Horstmann download
Core Java for the Impatient 3rd Edition Cay Horstmann download
https://ebookmeta.com/product/core-java-for-the-impatient-3rd-
edition-cay-horstmann/
https://ebookmeta.com/product/modern-javascript-for-the-
impatient-1st-edition-cay-s-horstmann/
https://ebookmeta.com/product/core-java-volume-ii-advanced-
features-cay-s-horstmann/
https://ebookmeta.com/product/core-java-vol-ii-advanced-
feature-12th-edition-cay-s-horstmann/
https://ebookmeta.com/product/law-as-an-instrument-sources-of-
chinese-law-for-authoritarian-legality-shucheng-wang/
Physics of the Apocalypse 2 A LitRPG Apocalypse Saga
1st Edition Dimitrios Gkirgkiris
https://ebookmeta.com/product/physics-of-the-
apocalypse-2-a-litrpg-apocalypse-saga-1st-edition-dimitrios-
gkirgkiris/
https://ebookmeta.com/product/phantom-1st-edition-rory-surtain/
https://ebookmeta.com/product/abolish-the-monarchy-why-we-should-
and-how-we-will-graham-smith/
https://ebookmeta.com/product/rn-mental-health-nursing-review-
module-edition-11-0-ati-nursing-education/
https://ebookmeta.com/product/programming-languages-and-
systems-19th-asian-symposium-aplas-2021-chicago-il-usa-
october-17-18-2021-proceedings-1st-edition-hakjoo-oh-2/
IT Security Controls: A Guide to Corporate Standards
and Frameworks 1st Edition Virgilio Viegas
https://ebookmeta.com/product/it-security-controls-a-guide-to-
corporate-standards-and-frameworks-1st-edition-virgilio-viegas/
About This eBook
ePUB is an open, industry-standard format for eBooks.
However, support of ePUB and its many features varies
across reading devices and applications. Use your device or
app settings to customize the presentation to your liking.
Settings that you can customize often include font, font size,
single or double column, landscape or portrait mode, and
figures that you can click or tap to enlarge. For additional
information about the settings and features on your reading
device or app, visit the device manufacturer’s Web site.
Many titles include programming code or configuration
examples. To optimize the presentation of these elements,
view the eBook in single-column, landscape mode and
adjust the font size to the smallest setting. In addition to
presenting code and configurations in the reflowable text
format, we have included images of the code that mimic the
presentation found in the print book; therefore, where the
reflowable format may compromise the presentation of the
code listing, you will see a “Click here to view code image”
link. Click the link to view the print-fidelity code image. To
return to the previous page viewed, click the Back button on
your device or app.
Core Java for the
Impatient
Third Edition
Core Java for the
Impatient
Third Edition
Cay S. Horstmann
ISBN-13: 978-0-13-805210-2
ISBN-10: 0-13-805210-7
ScoutAutomatedPrintCode
Pearson’s Commitment to
Diversity, Equity, and Inclusion
Pearson is dedicated to creating bias-free content that
reflects the diversity of all learners. We embrace the many
dimensions of diversity, including but not limited to race,
ethnicity, gender, socioeconomic status, ability, age, sexual
orientation, and religious or political beliefs.
Education is a powerful force for equity and change in our
world. It has the potential to deliver opportunities that
improve lives and enable economic mobility. As we work
with authors to create content for every product and
service, we acknowledge our responsibility to demonstrate
inclusivity and incorporate diverse scholarship so that
everyone can achieve their potential through learning. As
the world’s leading learning company, we have a duty to
help drive change and live up to our purpose to help more
people create a better life for themselves and to create a
better world.
Our ambition is to purposefully contribute to a world where:
Everyone has an equitable and lifelong opportunity to
succeed through learning.
Our educational products and services are inclusive and
represent the rich diversity of learners.
Our educational content accurately reflects the histories
and experiences of the learners we serve.
Our educational content prompts deeper discussions
with learners and motivates them to expand their own
learning (and worldview).
While we work hard to present unbiased content, we want to
hear from you about any concerns or needs with this
Pearson product so that we can investigate and address
them.
Please contact us with concerns about any potential bias
at https://www.pearson.com/report-bias.xhtml.
To Chi—the most patient person in my life.
Contents
Preface
Acknowledgments
About the Author
2 OBJECT-ORIENTED PROGRAMMING
2.1 Working with Objects
2.1.1 Accessor and Mutator Methods
2.1.2 Object References
2.2 Implementing Classes
2.2.1 Instance Variables
2.2.2 Method Headers
2.2.3 Method Bodies
2.2.4 Instance Method Invocations
2.2.5 The this Reference
2.2.6 Call by Value
2.3 Object Construction
2.3.1 Implementing Constructors
2.3.2 Overloading
2.3.3 Calling One Constructor from Another
2.3.4 Default Initialization
2.3.5 Instance Variable Initialization
2.3.6 Final Instance Variables
2.3.7 The Constructor with No Arguments
2.4 Records
2.4.1 The Record Concept
2.4.2 Constructors: Canonical, Custom, and
Compact
2.5 Static Variables and Methods
2.5.1 Static Variables
2.5.2 Static Constants
2.5.3 Static Initialization Blocks
2.5.4 Static Methods
2.5.5 Factory Methods
2.6 Packages
2.6.1 Package Declarations
2.6.2 The jar Command
2.6.3 The Class Path
2.6.4 Package Access
2.6.5 Importing Classes
2.6.6 Static Imports
2.7 Nested Classes
2.7.1 Static Nested Classes
2.7.2 Inner Classes
2.7.3 Special Syntax Rules for Inner Classes
2.8 Documentation Comments
2.8.1 Comment Insertion
2.8.2 Class Comments
2.8.3 Method Comments
2.8.4 Variable Comments
2.8.5 General Comments
2.8.6 Links
2.8.7 Package, Module, and Overview
Comments
2.8.8 Comment Extraction
Exercises
6 GENERIC PROGRAMMING
6.1 Generic Classes
6.2 Generic Methods
6.3 Type Bounds
6.4 Type Variance and Wildcards
6.4.1 Subtype Wildcards
6.4.2 Supertype Wildcards
6.4.3 Wildcards with Type Variables
6.4.4 Unbounded Wildcards
6.4.5 Wildcard Capture
6.5 Generics in the Java Virtual Machine
6.5.1 Type Erasure
6.5.2 Cast Insertion
6.5.3 Bridge Methods
6.6 Restrictions on Generics
6.6.1 No Primitive Type Arguments
6.6.2 At Runtime, All Types Are Raw
6.6.3 You Cannot Instantiate Type Variables
6.6.4 You Cannot Construct Arrays of
Parameterized Types
6.6.5 Class Type Variables Are Not Valid in
Static Contexts
6.6.6 Methods May Not Clash after Erasure
6.6.7 Exceptions and Generics
6.7 Reflection and Generics
6.7.1 The Class<T> Class
6.7.2 Generic Type Information in the Virtual
Machine
Exercises
7 COLLECTIONS
7.1 An Overview of the Collections Framework
7.2 Iterators
7.3 Sets
7.4 Maps
7.5 Other Collections
7.5.1 Properties
7.5.2 Bit Sets
7.5.3 Enumeration Sets and Maps
7.5.4 Stacks, Queues, Deques, and Priority
Queues
7.5.5 Weak Hash Maps
7.6 Views
7.6.1 Small Collections
7.6.2 Ranges
7.6.3 Unmodifiable Views
Exercises
8 STREAMS
8.1 From Iterating to Stream Operations
8.2 Stream Creation
8.3 The filter, map, and flatMap Methods
8.4 Extracting Substreams and Combining
Streams
8.5 Other Stream Transformations
8.6 Simple Reductions
8.7 The Optional Type
8.7.1 Producing an Alternative
8.7.2 Consuming the Value If Present
8.7.3 Pipelining Optional Values
8.7.4 How Not to Work with Optional Values
8.7.5 Creating Optional Values
8.7.6 Composing Optional Value Functions
with flatMap
8.7.7 Turning an Optional into a Stream
8.8 Collecting Results
8.9 Collecting into Maps
8.10 Grouping and Partitioning
8.11 Downstream Collectors
8.12 Reduction Operations
8.13 Primitive Type Streams
8.14 Parallel Streams
Exercises
Index
Preface
Java has seen many changes since its initial release in 1996.
The classic book, Core Java, covers, in meticulous detail, not
just the language but all core libraries and a multitude of
changes between versions, spanning two volumes and over
2,000 pages. However, if you just want to be productive
with modern Java, there is a much faster, easier pathway for
learning the language and core libraries. In this book, I don’t
retrace history and don’t dwell on features of past versions.
I show you the good parts of Java as it exists today, so you
can put your knowledge to work quickly.
As with my previous “Impatient” books, I quickly cut to the
chase, showing you what you need to know to solve a
programming problem without lecturing about the
superiority of one paradigm over another. I also present the
information in small chunks, organized so that you can
quickly retrieve it when needed.
Assuming you are proficient in some other programming
language, such as C++, JavaScript, Swift, PHP, or Ruby, with
this book you will learn how to become a competent Java
programmer. I cover all aspects of Java that a developer
needs to know today, including the powerful concepts of
lambda expressions and streams, as well as modern
constructs such as records and sealed classes.
A key reason to use Java is to tackle concurrent
programming. With parallel algorithms and threadsafe data
structures readily available in the Java library, the way
application programmers should handle concurrent
programming has completely changed. I provide fresh
coverage, showing you how to use the powerful library
features instead of error-prone low-level constructs.
Traditionally, books on Java have focused on user interface
programming, but nowadays, few developers produce user
interfaces on desktop computers. If you intend to use Java
for server-side programming or Android programming, you
will be able to use this book effectively without being
distracted by desktop GUI code.
Finally, this book is written for application programmers, not
for a college course and not for systems wizards. The book
covers issues that application programmers need to wrestle
with, such as logging and working with files, but you won’t
learn how to implement a linked list by hand or how to write
a web server.
I hope you enjoy this rapid-fire introduction into modern
Java, and I hope it will make your work with Java productive
and enjoyable.
If you find errors or have suggestions for improvement,
please visit http://horstmann.com/javaimpatient, head for the
errata page, and leave a comment. Be sure to visit that site
to download the runnable code examples that complement
this book.
Cay Horstmann
Berlin
August 2022
About the Author
Cay S. Horstmann is the author of JavaScript for the
Impatient and Scala for the Impatient (both from Addison-
Wesley), is principal author of Core Java, Volumes I and II,
Twelfth Edition (Pearson, 2022), and has written a dozen
other books for professional programmers and computer
science students. He is professor emeritus of computer
science at San Jose State University and is a Java Champion.
Chapter 1
Fundamental
Programming Structures
In this chapter, you will learn about the basic data types and
control structures of the Java language. I assume that you
are an experienced programmer in some other language
and that you are familiar with concepts such as variables,
loops, function calls, and arrays, but perhaps with a different
syntax. This chapter will get you up to speed on the Java
way. I will also give you some tips on the most useful parts
of the Java API for manipulating common data types.
The key points of this chapter are:
Note
You have just seen a // comment that extends to the
end of the line. You can also have multiline comments
between /* and */ delimiters, such as
Click here to view code image
/*
This is the first sample program in Core Java
for the Impatient.
The program displays the traditional greeting
"Hello, World!".
*/
Note
The javac compiler is invoked with the name of a file,
with slashes separating the path segments, and an
extension .java. The java virtual machine launcher is
invoked with the name of a class, with dots separating
the package segments, and no extension.
Note
If your program consists of a single source file, then
you can skip the compilation step and run the program
with the command
Click here to view code image
java ch01/sec01/HelloWorld.java
Note
On Unix-like operating systems, you can turn a Java
file into an executable program by following these
steps:
Tip
At http://horstmann.com/javaimpatient, you can
download the sample code for all chapters of this book
that presents the book’s code snippets in context. The
code is organized so that you can make a single
project that holds all the example programs. I
encourage you to download, run, and study the code
as you follow along with the book content.
Note
The Random class is declared in the java.util package.
To use it in your program, add an import statement,
like this:
package ch01.sec01;
import java.util.Random;
1.1.4 JShell
In Section 1.1.2, “Compiling and Running a Java Program”
(page 4), you saw how to compile and run a Java program.
The JShell program provides a “read-evaluate-print loop”
(REPL) that allows you to experiment with Java code without
compiling and running a program. When you type a Java
expression, JShell evaluates your input, prints the result,
and waits for your next input. To start JShell, simply type
jshell in a terminal window (Figure 1-3).
Figure 1-3 Running JShell
JShell starts with a greeting, followed by a prompt:
Click here to view code image
| Welcome to JShell -- Version 17
| For an introduction type: /help intro
jshell>
Another Random Scribd Document
with Unrelated Content
Herodes, 438
Hierosolymarius, 136
Hilarus, freedman of Cicero, 30
Hipparchus, 124
Hirrus, see Lucilius Hirrus
Hispania, 296, 362
Hortalus, cognomen of Hortensius, 194, 306;
cf. Hortensius
Hortensiana, 288
Hortensius (Q.), orator, 34, 44, 48, 50, 52, 114, 210, 340, 356, 366,
380, 428, 470
Hortensius (Q.), son of the orator, 462
Hypsaeus, see Plautius Hypsaeus
Ialysus, 176
Iconium, 388, 400
Idaeus pastor, 78
Ilium, 206
Interamna, 104
Interamnates, 308
Iphicrates, 114
Isauricum forum, 404
Isocrates, 100, 426
Issus, 390
Italia, 42, 86, 152, 174, 196, 202, 222, 230, 260, 280
Iulia lex, 162, 358, 402, 474
Iulius Caesar (C.), dictator, 4, 30, 34, 74, 106, 110, 116, 13O, 142, 154,
162, 166, 178, 188, 190, 236, 304, 308, 310, 312, 316, 318, 320,
330, 332, 342, 348, 350, 360, 362, 368, 370, 394, 396, 400, 438,
440, 472, 482
Iulius Caesar (L.), consul 64 B.C., 2, 8, 324
Iulius Caesar Strabo Vopiscus (C.), 412
Iunia lex, 136, 316
Iunius Brutus (M.), 190, 380, 384, 403, 408, 410, 412, 414, 416, 418,
420, 422, 442, 448, 450, 452, 456, 458, 460
Iunius Silanus (D.), 2
Iuventas, 78
Iuventius Laterensis (M.), 162, 190
Iuventius Talna, 56
Κικέρων (ὁ μικρός), (i.e. Tullius Cicero (Q.), son of the orator, q.v.),
138, 150
Κικέρων ὁ φιλόσοφος, (i.e. Tullius Cicero (Q.), the orator, q.v.), 144
Κόνων, 466
Κορινθίων (πολιτεία), 112
Κροτωνιάτης, (i.e. T. Annius Milo, q.v.), 464, 466
Κροτωνιατικά, 466
Κύρου παιδεία, 114
Laconicum, 298
Laelius, 170, 172
Laelius Sapiens (C.), 314
Laenius Flaccus (M.), 396, 400, 406, 422, 458
Λαιστρυγονίη, 146
Lamia (L. Aelius), 354
Laodicea, 372, 374, 376, 380, 388, 392, 396, 400, 404, 414, 424,
438, 442, 446, 462, 474
Larinum, 300
Laterensis, see Iuventius Laterensis
Laterium, 290
Latinus ἀττικισμός, 332;
-ae (tribus), 8;
-ni (libri), 96, 98, 112;
-num (commentarium), 90
Lentulus, ship-owner, 20, 22
Lentulus, see also Cornelius Lentulus
Lepreon, 444
Lepta (Q.), 378, 426, 476
Leucata, 356
Leuctrica pugna, 442
Liberalia, 428
Libertas, 266, 324
Licinia lex, 136, 316
Licinius Crassus (M.), 12, 40, 42, 72, 80, 116, 120, 122, 146, 176, 182,
190, 222, 224, 250, 298, 302, 314
Licinius Lucullus Ponticus (L.), 4, 44, 90, 190
Licinius Lucullus (M.), brother of the last, 78, 268
Licinius Macer (C.), 12
Ligur or Ligus (L.), 346
Ligurinus μῶμος, 392
Livineius Regulus (L.), 234
Livineius Trypho (L.), 234
Livius Drusus, 310, 316, 324
Locri, 434
Lollius Palicanus (L.), 2, 80
Lucceius (L. M. f.), 396, 412, 438
Lucceius (L. Q. f.), 10, 14, 26, 46, 74, 110, 288, 296, 300
Lucilius (Sex.), 392
Lucilius Hirrus (C.), 316
Lucretius (Q.), 316
Lucrinenses res, 296
Luculli, 64
Lucullus, see Licinius Lucullus
Lurco, see Aufidius Lurco
Lutatius Catulus (Q.), consul 78 B.C., 34, 54, 94, 148, 190, 192, 284
Lutatius Catulus (Q.), consul 102 B.C., 268, 270, 272
Lycaonia, 374
Lycaonium forum, 404
Lycii, 468
Lycurgei, 36
Nanneiani, 54
Nar, 308
Nares Luc(aniae), 196
Nasica, 110. Cf. Caecilius Metellus Pius Scipio
Natta, 294
Neapolis, 16, 296
Nemus, 440
Nepos, see Caecilius Metellus Nepos (Q.)
Nero, see Claudius Nero
Nicanor, 344, 396
Nigidius Figulus (P.), 112
Ninnius Quadratus (L.), 248
Nonius Sufenas (M.), 306, 428
Numestius (Numerius), 170, 184, 186, 192
Octavius (C.), father of Augustus, 112
Octavius (M.), 402, 436
Ὁμηρικῶς, 48
Opimius, 320
Oppius (C.), 336, 348
Oppius Cornicinus (Cn.), 268
Ὀπούντιοι, 444
Ὀποῦς, 444
Ops, 434
Orodes, 382, 398, 430
Osaces, 390
Otho, see Roscius Otho
Rabiriana domus, 16
Rabirius (C.), 102
Rantius, 322
Reatini, 308
Regulus, see Livineius Regulus
Rex, 58
Rhinton, 94
Rhodii, 362, 366, 368, 476
Rhodos, 100, 110, 384, 474
Rhosica vasa, 428
Roma, 4, 8, 10, 14, 22, 68, 92, 104, 112, 124, 126, 132, 134, 140, 142,
144, 148, 150, 156, 158, 180, 186, 208, 212, 256, 258, 266, 290,
298, 302, 306, 308, 340, 350, 352, 354, 356, 360, 368, 370, 374,
378, 380, 382, 384, 386, 394, 396, 406, 408, 412, 426, 454, 456,
460, 464, 466, 470, 478
Romanus homo, 90;
populus, 54–6;
-na mysteria, 440;
-nae res, 42, 44, 76, 306, 326, 478;
-ni cives, 384, 402, 404;
-ni equites, 78, 290
Romulus, 108
Roscia lex, 168
Roscius Otho (L.), 102
Rosia (rura), 308
Rufio Vestorianus, 340.
See also Sempronius Rufus
Rutilius Rufus (P.), 314
Valerius, 114
Valerius, interpreter, 30
Valerius (P.), 412
Valerius Flaccus (L.), 82, 194
Valerius Messalla (M.), 296, 310, 316, 320, 366, 386
Valerius Messalla Niger (M.), consul 61 B.C., 32, 36, 38, 40, 44, 262
Valerius Triarius (P.), 316, 324
Varius (P.), 4
Varro, see Terentius Varro
Vatinius (P.), 126, 130, 138, 190
Vedianae res, 440
Vedius (P.), 440
Veiento, 322
Velinus lacus 308;
-na tribus, 310
Vennonius (C.), 440, 458
Venus, 176
Venusia, 348
Vestorius (C.), 288, 304, 316, 332, 340, 444, 452
Vettius, 284
Vettius, a broker, 432
Vettius (L.), 188, 190
Vettius Chrysippus, 120
Vibius, 174
Vibo, 198
Visellius, 250
Volaterrani, 84
Volusius (Cn.), 362
Volusius (Q.), 402
Zaleucus, 434
Zoster, 360
W. H. Smith & Son, The Arden Press, Stamford Street, S.E.
TRANSCRIBER’S NOTES
1. P. 52, corrected “ὃππως” to “ὅππως”.
2. P. 88, corrected “ταυτα” to “ταῦτα”.
3. P. 156, corrected “δε” to “δὲ”.
4. P. 359, corrected “ἒρδοι τις ἥν ἕκαστος εἰδείη
τέχνην” to “ἔρδοι τις ἣν ἕκαστος εἰδείη
τέχνην”.
5. Silently corrected typographical errors in
punctuation and spelling in the index which
didn't agree with the text.
6. Retained anachronistic, non-standard, and
uncertain spellings as printed.
*** END OF THE PROJECT GUTENBERG EBOOK CICERO: LETTERS
TO ATTICUS, VOL. 1 OF 3 ***
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.