Complete Java 9 Recipes: A Problem-Solution Approach 3rd Edition Josh Juneau PDF For All Chapters
Complete Java 9 Recipes: A Problem-Solution Approach 3rd Edition Josh Juneau PDF For All Chapters
com
https://textbookfull.com/product/java-9-recipes-a-
problem-solution-approach-3rd-edition-josh-juneau/
https://textbookfull.com/product/java-ee-7-recipes-a-problem-
solution-approach-1st-edition-juneau-josh/
https://textbookfull.com/product/java-7-recipes-a-problem-
solution-approach-juneau-josh-beaty-mark-dea-carl-guime-freddy-
oconner-john/
https://textbookfull.com/product/java-8-recipes-2nd-edition-josh-
juneau/
https://textbookfull.com/product/matlab-machine-learning-recipes-
a-problem-solution-approach-3rd-edition-michael-paluszek/
Android Recipes A Problem-Solution Approach Dave Smith
https://textbookfull.com/product/android-recipes-a-problem-
solution-approach-dave-smith/
https://textbookfull.com/product/raku-recipes-a-problem-solution-
approach-j-j-merelo/
https://textbookfull.com/product/c-recipes-a-problem-solution-
approach-1st-edition-shirish-chavan/
https://textbookfull.com/product/javascript-recipes-a-problem-
solution-approach-1st-edition-russ-ferguson/
https://textbookfull.com/product/wxpython-recipes-a-problem-
solution-approach-1st-edition-mike-driscoll/
Java 9
Recipes
A Problem-Solution Approach
—
Third Edition
—
Josh Juneau
Java 9 Recipes
A Problem-Solution Approach
Third Edition
Josh Juneau
Java 9 Recipes: A Problem-Solution Approach
Josh Juneau
Hinckley, Illinois, USA
ISBN-13 (pbk): 978-1-4842-1975-1 ISBN-13 (electronic): 978-1-4842-1976-8
DOI 10.1007/978-1-4842-1976-8
Library of Congress Control Number: 2017943502
Copyright © 2017 by Josh Juneau
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Jonathan Gennick
Development Editor: Laura Berendson
Technical Reviewer: Vinay Kumar
Coordinating Editor: Jill Balzano
Copy Editor: Brendan Frost
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).
SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484219751. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
This book is dedicated to my wife and children.
Contents at a Glance
■
■Chapter 1: Getting Started with Java 9����������������������������������������������������������������� 1
■
■Chapter 2: Java 9 Enhancements������������������������������������������������������������������������ 35
■
■Chapter 3: Strings����������������������������������������������������������������������������������������������� 47
■
■Chapter 4: Numbers and Dates���������������������������������������������������������������������������� 65
■
■Chapter 5: Object-Oriented Java������������������������������������������������������������������������� 99
■
■Chapter 6: Lambda Expressions������������������������������������������������������������������������ 137
■
■Chapter 7: Data Sources and Collections���������������������������������������������������������� 159
■
■Chapter 8: Input and Output������������������������������������������������������������������������������ 195
■
■Chapter 9: Exceptions and Logging������������������������������������������������������������������� 221
■
■Chapter 10: Concurrency����������������������������������������������������������������������������������� 239
■
■Chapter 11: Debugging and Unit Testing����������������������������������������������������������� 267
■
■Chapter 12: Unicode, Internationalization, and Currency Codes����������������������� 285
■
■Chapter 13: Working with Databases���������������������������������������������������������������� 305
■
■Chapter 14: JavaFX Fundamentals�������������������������������������������������������������������� 355
■
■Chapter 15: Graphics with JavaFX�������������������������������������������������������������������� 429
■
■Chapter 16: Media with JavaFX������������������������������������������������������������������������� 471
v
■ Contents at a Glance
■
■Chapter 17: Java Web Applications with JavaServer Faces������������������������������ 499
■
■Chapter 18: Nashorn and Scripting������������������������������������������������������������������� 529
■
■Chapter 19: E-mail��������������������������������������������������������������������������������������������� 553
■
■Chapter 20: JSON and XML Processing������������������������������������������������������������� 563
■
■Chapter 21: Networking������������������������������������������������������������������������������������ 585
■
■Chapter 22: Java Modularity����������������������������������������������������������������������������� 605
Index��������������������������������������������������������������������������������������������������������������������� 615
vi
Contents
■
■Chapter 1: Getting Started with Java 9����������������������������������������������������������������� 1
1-1. Creating a Development Environment���������������������������������������������������������������������� 1
Problem�������������������������������������������������������������������������������������������������������������������������������������������������� 1
Solution�������������������������������������������������������������������������������������������������������������������������������������������������� 1
How It Works������������������������������������������������������������������������������������������������������������������������������������������� 2
1-2. Getting to “Hello, World”������������������������������������������������������������������������������������������� 3
Problem�������������������������������������������������������������������������������������������������������������������������������������������������� 3
Solution�������������������������������������������������������������������������������������������������������������������������������������������������� 3
How It Works������������������������������������������������������������������������������������������������������������������������������������������� 7
1-3. Configuring the CLASSPATH�������������������������������������������������������������������������������������� 9
Problem�������������������������������������������������������������������������������������������������������������������������������������������������� 9
Solution�������������������������������������������������������������������������������������������������������������������������������������������������� 9
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 10
1-4. Organizing Code with Packages����������������������������������������������������������������������������� 11
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 11
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 11
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 12
1-5. Declaring Variables and Access Modifiers�������������������������������������������������������������� 14
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 14
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 14
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 16
vii
■ Contents
viii
■ Contents
Summary������������������������������������������������������������������������������������������������������������������������ 34
■
■Chapter 2: Java 9 Enhancements������������������������������������������������������������������������ 35
2-1. Avoiding Redundancy in Interface Code����������������������������������������������������������������� 35
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 35
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 35
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 36
ix
■ Contents
Summary������������������������������������������������������������������������������������������������������������������������ 46
■
■Chapter 3: Strings����������������������������������������������������������������������������������������������� 47
Compact Strings: Java 9 String Enhancements������������������������������������������������������������� 47
3-1. Obtaining a Subsection of a String������������������������������������������������������������������������� 47
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 47
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 47
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 48
x
■ Contents
Solution 2��������������������������������������������������������������������������������������������������������������������������������������������� 53
Solution 3��������������������������������������������������������������������������������������������������������������������������������������������� 54
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 54
Summary������������������������������������������������������������������������������������������������������������������������ 64
xi
■ Contents
■
■Chapter 4: Numbers and Dates���������������������������������������������������������������������������� 65
4-1. Rounding Float and Double Values to Integers������������������������������������������������������� 66
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 66
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 66
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 66
xii
■ Contents
xiii
■ Contents
Summary������������������������������������������������������������������������������������������������������������������������ 98
xiv
■ Contents
■
■Chapter 5: Object-Oriented Java������������������������������������������������������������������������� 99
5-1. Controlling Access to Members of a Class�������������������������������������������������������������� 99
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 99
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 99
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 100
xv
■ Contents
5-8. Constructing Instances of the Same Class with Different Values������������������������� 112
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 112
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 112
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 115
Summary���������������������������������������������������������������������������������������������������������������������� 136
xvi
■ Contents
■
■Chapter 6: Lambda Expressions������������������������������������������������������������������������ 137
6-1. Writing a Simple Lambda Expression������������������������������������������������������������������� 137
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 137
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 137
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 138
xvii
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 157
■
■Chapter 7: Data Sources and Collections���������������������������������������������������������� 159
7-1. Defining a Fixed Set of Related Constants������������������������������������������������������������ 159
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 159
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 159
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 161
7-5. Safely Enabling Types or Methods to Operate on Objects of Various Types���������� 174
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 174
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 175
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 176
xviii
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 193
■
■Chapter 8: Input and Output������������������������������������������������������������������������������ 195
8-1. Serializing Java Objects���������������������������������������������������������������������������������������� 196
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 196
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 196
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 197
xix
■ Contents
xx
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 219
■
■Chapter 9: Exceptions and Logging������������������������������������������������������������������� 221
9-1. Catching Exceptions��������������������������������������������������������������������������������������������� 222
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 222
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 222
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 223
xxi
■ Contents
xxii
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 237
■
■Chapter 10: Concurrency����������������������������������������������������������������������������������� 239
10-1. Starting a Background Task�������������������������������������������������������������������������������� 239
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 239
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 239
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 240
10-3. Inserting a Key into a Map Only If the Key Is Not Already Present���������������������� 242
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 242
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 242
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 243
xxiii
■ Contents
xxiv
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 266
■
■Chapter 11: Debugging and Unit Testing����������������������������������������������������������� 267
11-1. Understanding Exceptions���������������������������������������������������������������������������������� 267
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 267
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 267
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 268
xxv
■ Contents
■
■Chapter 12: Unicode, Internationalization, and Currency Codes����������������������� 285
12-1. Converting Unicode Characters to Digits������������������������������������������������������������ 285
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 285
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 285
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 287
xxvi
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 304
■
■Chapter 13: Working with Databases���������������������������������������������������������������� 305
13-1. Connecting to a Database����������������������������������������������������������������������������������� 305
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 305
Solution 1������������������������������������������������������������������������������������������������������������������������������������������� 305
Solution 2������������������������������������������������������������������������������������������������������������������������������������������� 306
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 306
xxvii
■ Contents
xxviii
■ Contents
13-11. Joining RowSet Objects When Not Connected to the Data Source������������������� 335
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 335
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 335
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 340
Summary���������������������������������������������������������������������������������������������������������������������� 353
■
■Chapter 14: JavaFX Fundamentals�������������������������������������������������������������������� 355
14-1. Creating a Simple User Interface������������������������������������������������������������������������ 356
Problem���������������������������������������������������������������������������������������������������������������������������������������������� 356
Solution 1������������������������������������������������������������������������������������������������������������������������������������������� 356
Solution 2������������������������������������������������������������������������������������������������������������������������������������������� 357
How It Works��������������������������������������������������������������������������������������������������������������������������������������� 359
xxix
Another random document with
no related content on Scribd:
+ N Y Times 25:10 Jl 18 ’20 1300w
R of Rs 62:112 Jl ’20 40w
“The student who lacks either the time or the training to study Mr
Bergson’s larger and more difficult work will find in this volume of
essays clues not difficult to understand and profitable to follow.”
“The reader into whose hands it may come will not fail to find its
chapters exceedingly interesting, as they review familiar episodes
from what to Americans is an unfamiliar standpoint.”
“This little book has been compiled for the average person in
business, whether executive or stenographer, who wants a statement
in simple and direct form of the elementary things that are essential
to good letters. It is a first-aid manual of style for business
correspondence, suitable for adoption by any commercial concern,
large or small.” (Introd.) Contents of part 1—The letter itself:
Appearance; Substance; Phraseology; Punctuation; Paragraphing;
Abbreviations; Miscellaneous. Contents of part 2—Words, right and
wrong; Some misused words; Verbal vulgarisms; Similar words often
confused; Pronouns: their use and abuse; Miscellaneous.
“‘The witches’ mountain’ is the only one of the three plays included
that conforms to the canons of real drama.”
“These plays have a freshness and vigor of spaces our Wild West
scenarios somehow lack. There are the same conventional gestures,
the same corroborated sentiment from which any informing fire has
gone out. But at least these are reminiscent of authentic instead of
manufactured emotion.” Lola Ridge
“If these plays seem immature rather than naive; crude, rather
than in the spirit of the folk; if Mr Bierstadt seems to have mistaken
the drama inherent in the life and character of the ‘gaucho’ for drama
in the plays that represent him, there is still nothing but gratitude
due him for introducing the ‘gaucho’ to our unromantic world.”
The author warns against making a fetish of history and points out
the difficulty in the way of its infallibility as a teacher. The number
and complexity of the facts, in part hidden, in part incomprehensible,
impede correct judgment. Besides, latent energies may at any time
spring into action to change men’s reactions to given facts. On the
other hand there is a certain fundamental principle on which society
rests and which serves as constant in the interpretation of history. It
is the object of the book to study the past, to give assurance of the
principle and then to see how men have acted and are acting in its
presence. Contents: Unity in government; The family in English
history: an inquiry; Medieval English sovereignty; The old jury;
Becket and the law; Index.
Am Pol Sci R 14:738 N ’20 50w
Booklist 17:10 O ’20
Boston Transcript p6 Ap 28 ’20 220w
[2]
BINDLOSS, HAROLD. Lister’s great
adventure (Eng title, Head of the house). il *$2 (2c)
Stokes
“The heroine and the various members of her family have more
individuality than is usual in this class of literature.”
This story is divided into three distinct parts, the first and third of
which take place in England, and the second in Canada. Creighton
and Stayward are partners in business until Creighton, driven on by
his wife’s extravagances and his daughter’s need of an education,
misappropriates some of the funds and Stayward dissolves the
partnership. Creighton disappears and his wife spreads stories about
Stayward’s cruelty and dishonesty to her husband. The Canadian
part of the story has to do with Geoffrey Lisle, Stayward’s nephew,
who is managing a mine there, and who comes in contact with Tom
Carson, cook and chemist, who helps him defeat the rival mining
company he is working against. Upon his return to England at his
uncle’s death, Geoffrey again meets the girl who has been in his
thoughts ever since he left England, to discover that she is Ruth
Creighton, and theoretically his enemy. The timely discovery of who
Tom Carson really was helps him to win the girl and to clear his
uncle’s name in her eyes.
“Mr Bindloss is one of those writers (all too few) who handle the
adventure story without stressing the adventures to the disadvantage
of all the other parts of the story. In other words, his characterization
is always clear and distinct and worked up with some elaboration,
and he has a quick eye at the description of natural scenery.”
“The story is rather better than many of the author’s recent books,
and his readers will find considerable entertainment in its pages.”
+ Springf’d Republican p11a Mr 7 ’20
300w
The Times [London] Lit Sup p502 S 18
’19 100w
“An exciting tale told with literary excellence beyond the average of
adventure stories.”
“It is all admirably and romantically told. Though we know the tale
of old, it is still alive when the right chronicler takes it up; and Mr
Binns never for a moment lets it flag.”
+ The Times [London] Lit Sup p126 F 19
’20 200w
“In this compact little volume, rich in well selected facts and
information throughout, the author has performed a useful service.”
W. B. Guthrie
“It has been a long time since ‘London lyrics’ first appeared, but
none the less this intimate and accurate character sketch of their
author has a genuine interest and value.” H: L. West
“In reading this book, and noticing how Mr Birrell is always sliding
away from his subject to talk about himself, or about somebody or
something other than Frederick Locker, you ask why he chose
‘Frederick Locker-Lampson: a character sketch’ for the title of a book
that might just as properly have been called ‘Scraps,’ or ‘Chips,’ or
‘Jottings.’ In the end nevertheless, you feel that you have been unfair.
Mr Birrell, in his odd, slipshod way, is a man of letters—at least a
man who delights in letters; and he gives you a faint character sketch
of Frederick Locker-Lampson.”
[2]
BISHOP, CARLTON THOMAS. Structural
drafting and the design of details. il *$5 Wiley 744
20–4714
“On the whole we are inclined to name this the best book on the
subject.”
+ N Y P L New Tech Bks p27 Ap ’20 170w
Pratt p16 O ’20 20w
“‘It is becoming apparent that in spite of all the work which has
been done there has been practically no change in the general
situation, and there has been no solution of the drug problem.’ This
is the conclusion of Dr Ernest S. Bishop, clinical professor of
medicine in the New York polyclinic medical school. Two
outstanding elements appear to Dr Bishop to have received
insufficient consideration in the efforts to solve the narcotic drug
problem. One of these elements is the suffering of the addict: the
other is the nature of the physical disease with which he is afflicted.
Dr Bishop asserts that the exploitation of human weakness and
suffering would be checked on any large scale, if the disease created
by continued administration of opiates were recognized and its
physical demands comprehended and provided for in legitimate and
relatively unobjectionable ways.... Dr Bishop also recommends the
establishment under proper supervision and management of stations
or clinics at which those who for financial or other reasons are
unable to secure honest medical help, may obtain their necessary
opiate at minimum expense without ‘resorting to underworld
associations and illicit commerce.’”—Springf’d Republican
“The real problems of the narcotic drug situation are related to the
origin and prevention of heroin and cocaine addictions and the
treatment and after-care of those so addicted. This book avoids these
questions and is sterile of information on these essential points of
the narcotic drug problem.” Medicus
“Mr Bishop describes his adventures simply and clearly, and his
book is worth reading.”
“With perfect taste and judgment Mr Bishop has stood aside and
allowed the story to be told through Roosevelt’s letters. He has made
an excellent book, important, always readable and often extremely
amusing. With the ‘Autobiography’ and Mr Thayer’s book, the
present work, ‘Theodore Roosevelt and his time’ is one of the three
indispensable books on this subject. With Mr Huneker’s
‘Steeplejack,’ it is one of the two best American biographies of this
year.” E. L. Pearson
“There are a few little errors, nothing of consequence. But the book
is undoubtedly partisan; which does not prevent it from being a
thoroughly good and complete biography.” C: W. Thompson
Reviewed by E. L. Pearson
“These two volumes, as they stand, will serve not only for the
present time but for future generations.”
“For thirty years and more David Bispham has been prominent,
here and abroad, as a baritone of note, a singing actor, and an
advocate of the use of English speech in opera. In these recollections
he has packed into one volume the record of a long and busy life—a
life of many strange and varied experiences. Unlike most men who
have their hour in opera, he has had his in society. He has traveled
far and wide, and mixed with people who were worth knowing and
far-famed in many ways. To this it may be added, unreservedly, that
he has more than an instinctive turn for setting down, in plain but
vivid words, what he would tell.”—Review
“A singer who can write with ease and style is rarer than that rare
bird, the black swan. One artist of the kind is David Bispham.” C: H:
Meltzer
“He has perhaps not grasped the first bitter truth to be learned by
an author that of all the countless incidents which his own mind
makes picturesque in retrospect only those are interesting which he
can make picturesque to others. The bald stretches, however, are
only occasional.”