Professional Documents
Culture Documents
Instant Download Ebook of Java All in One For Dummies Seventh Edition Doug Lowe Online Full Chapter PDF
Instant Download Ebook of Java All in One For Dummies Seventh Edition Doug Lowe Online Full Chapter PDF
https://ebookmeta.com/product/java-all-in-one-for-dummies-fifth-
edition-doug-lowe-3/
https://ebookmeta.com/product/java-all-in-one-for-dummies-fifth-
edition-doug-lowe-2/
https://ebookmeta.com/product/java-all-in-one-for-dummies-fifth-
edition-doug-lowe/
https://ebookmeta.com/product/networking-all-in-one-for-dummies-
doug-lowe/
Networking All-in-One For Dummies Doug Lowe
https://ebookmeta.com/product/networking-all-in-one-for-dummies-
doug-lowe-2/
https://ebookmeta.com/product/electronics-all-in-one-for-dummies-
doug-lowe/
https://ebookmeta.com/product/networking-all-in-one-for-
dummies-8th-edition-doug-lowe/
https://ebookmeta.com/product/powerpoint-for-dummies-
office-2021-edition-doug-lowe/
https://ebookmeta.com/product/coding-all-in-one-for-dummies-2nd-
edition-dummies/
Java
®
ALL-IN-ONE
7th Edition
by Doug Lowe
Ещё больше книг по Java в нашем телеграм канале:
https://t.me/javalib
Java® All-in-One For Dummies®, 7th Edition
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2023 by John Wiley & Sons, Inc., Hoboken, New Jersey
Media and software compilation copyright © 2023 by John Wiley & Sons, Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under
Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher.
Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons,
Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/
go/permissions.
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related
trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written
permission. Java is a registered trademark of Oracle America, Inc. All other trademarks are the property of their
respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHORS HAVE USED THEIR
BEST EFFORTS IN PREPARING THIS WORK, THEY MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT
TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM
ALL WARRANTIES, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF MERCHANTABILITY
OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES
REPRESENTATIVES, WRITTEN SALES MATERIALS OR PROMOTIONAL STATEMENTS FOR THIS WORK. THE
FACT THAT AN ORGANIZATION, WEBSITE, OR PRODUCT IS REFERRED TO IN THIS WORK AS A CITATION AND/
OR POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE PUBLISHER AND AUTHORS
ENDORSE THE INFORMATION OR SERVICES THE ORGANIZATION, WEBSITE, OR PRODUCT MAY PROVIDE OR
RECOMMENDATIONS IT MAY MAKE. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER
IS NOT ENGAGED IN RENDERING PROFESSIONAL SERVICES. THE ADVICE AND STRATEGIES CONTAINED HEREIN
MAY NOT BE SUITABLE FOR YOUR SITUATION. YOU SHOULD CONSULT WITH A SPECIALIST WHERE APPROPRIATE.
FURTHER, READERS SHOULD BE AWARE THAT WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR
DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. NEITHER THE PUBLISHER
NOR AUTHORS SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING
BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.
For general information on our other products and services, please contact our Customer Care Department within
the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit
https://hub.wiley.com/community/support/dummies.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to
media such as a CD or DVD that is not included in the version you purchased, you may download this material at
http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
Table of Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Foolish Assumptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Icons Used in This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Beyond the Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Table of Contents v
BOOK 2: PROGRAMMING BASICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
CHAPTER 1: Java Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Table of Contents ix
Objects have state. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Objects have behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Understanding the Life Cycle of an Object . . . . . . . . . . . . . . . . . . . . . . 245
Working with Related Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Designing a Program with Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Diagramming Classes with UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Drawing classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Drawing arrows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Table of Contents xi
The Class Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Using the instanceof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Table of Contents xv
Removing Entries from a Hash Map. . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
Treating a hash map as a collection. . . . . . . . . . . . . . . . . . . . . . . . . 585
Using lambda expressions with maps . . . . . . . . . . . . . . . . . . . . . . . 587
Using the TreeMap Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
Introducing red-black trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Looking at TreeMap methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Creating and using a TreeMap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .592
More ways to play with a TreeMap. . . . . . . . . . . . . . . . . . . . . . . . . . 593
INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
You can (and probably should, eventually) buy separate books on each of these
topics. It won’t take long before your bookshelf is bulging with 10,000 or more
pages of detailed information about every imaginable nuance of Java program-
ming. But before you’re ready to tackle each of those topics in depth, you need to
get a bird’s-eye picture. That’s what this book is about.
And if you already own 10,000 pages or more of Java information, you may be
overwhelmed by the amount of detail and wonder, “Do I really need to read
1,200 pages about JSP just to create a simple web page? And do I really need a
six-pound book on JavaFX?” Truth is, most 1,200-page programming books have
about 200 pages of really useful information — the kind you use every day —
and about 1,000 pages of excruciating details that apply mostly if you’re writ-
ing guidance-control programs for nuclear missiles or trading systems for the
New York Stock Exchange.
The basic idea here is that I’ve tried to wring out the 100-or-so most useful pages
of information on these different Java programming topics: setup and configu-
ration, basic programming, object-oriented programming, advanced program-
ming techniques, JavaFX, file and database programming, web programming, and
animation and game programming. Thus you get a nice, trim book.
So whether you’re just getting started with Java programming or you’re a seasoned
pro, you’ve found the right book.
Introduction 1
About This Book
Java All-in-One For Dummies, 7th Edition, is a reference for all the great things
(and maybe a few not-so-great things) that you may need to know when you’re
writing Java programs. You can, of course, buy a huge 1,200-page book on each
of the programming topics covered in this book. But then, who would carry them
home from the bookstore for you? And where would you find the shelf space to
store them? And when will you find the time to read them?
In this book, all the information you need is conveniently packaged for you
in-between one set of covers. And all of the information is current for the newest
release of Java, known as JDK 19. This book doesn’t pretend to be a comprehen-
sive reference for every detail on every possible topic related to Java program-
ming. Instead, it shows you how to get up and running fast so that you have more
time to do the things you really want to do. Designed using the easy-to-follow
For Dummies format, this book helps you get the information you need without
laboring to find it.
Java All-in-One For Dummies, 7th Edition, is a big book made up of eight smaller
books — minibooks, if you will. Each of these minibooks covers the basics of
one key element of programming, such as installing Java and compiling and
running programs, or using basic Java statements, or using JavaFX to write GUI
applications.
Whenever one big thing is made up of several smaller things, confusion is always
a possibility. That’s why this book has multiple access points. At the beginning
is a detailed table of contents that covers the entire book. Then each minibook
begins with a minitable of contents that shows you at a miniglance what chapters
are included in that minibook. Useful running heads appear at the top of each page
to point out the topic discussed on that page. And handy thumb tabs run down
the side of the pages to help you find each minibook quickly. Finally, a compre-
hensive index lets you find information anywhere in the entire book.
Foolish Assumptions
You and I have never met, so it is difficult for me to make any assumptions
about why you are interested in this book. However, let’s start with a few basic
assumptions:
Danger, Will Robinson! This icon highlights information that may help you avert
disaster.
Pay special attention to this icon; it lets you know that some particularly useful
tidbit is at hand.
Hold it — overly technical stuff is just around the corner. Obviously, because this
is a programming book, almost every paragraph of the next 900 or so pages could
get this icon. So I reserve it for those paragraphs that go into greater depth, down
into explaining how something works under the covers — probably deeper than
you really need to know to use a feature, but often enlightening.
Introduction 3
Beyond the Book
In addition to the material in the print or e-book you’re reading right now,
this product also comes with some access-anywhere goodies on the web. Check
out the free Cheat Sheet for more on Java. To get this Cheat Sheet, simply go to
www.dummies.com and type Java All-in-One For Dummies Cheat Sheet in the
Search box.
»» Understanding Java’s
incomprehensible version
numbers
Chapter 1
Welcome to Java
T
his chapter is a gentle introduction to the world of Java. In the next few
pages, you find out what Java is, where it came from, and where it’s going.
You also discover some of the unique strengths of Java, as well as some of
its weaknesses. Also, you see how Java compares with other popular programming
languages such as C, C++, and C#.
By the way, I assume in this chapter that you have at least enough background to
know what computer programming is all about. That doesn’t mean that I assume
you’re an expert or professional programmer. It just means that I don’t take the
time to explain such basics as what a computer program is, what a program-
ming language is, and so on. If you have absolutely no programming experience,
I suggest that you pick up a copy of Java For Dummies, 7th Edition, or Beginning
Programming with Java For Dummies, 5th Edition, both by Barry Burd (Wiley).
Throughout this chapter, you find little snippets of Java program code, plus a few
snippets of code written in other languages, including C, C++, and Basic. If you
don’t have a clue what this code means or does, don’t panic. I just want to give you
a feel for what Java programming looks like and how it compares with program-
ming in other languages.
All the code listings used in this book are available for download at www.dummies.
com/go/javaaiofd7e.
When she came to Madrid as the wife of the great General, the
Duchess of Valencia was appointed Lady-in-Waiting to Queen
Isabella, and she never failed in her loyalty to the dynasty which was
in power when she came to the country of her adoption by marriage.
Many years later she was in an hotel in Switzerland, where she
purposed making a long stay, when Don Carlos happened to come
to the same hotel, accompanied by his secretary. As the Duchess of
Valencia was unacquainted with the Pretender to the throne of
Spain, she wondered who the imperious-looking new arrival could
be, who was greeted so respectfully by everybody. Her curiosity was
soon satisfied, for the gentleman’s secretary presented himself
before her to say that the Duke of Madrid begged the honour to pay
his respects to her.
The message filled the Duchess with dismay, for, although she
held the Princes of the blood in great respect, she had no intention of
receiving one who disputed the throne with the reigning Queen.
So, summoning all her dignity to her aid, she said, in a tone of icy
politeness:
“Tell the Duke of Madrid that I am very sorry not to have the
honour of receiving his visit, but to-morrow I leave for Paris.”
And in effect the lady left the hotel on the morrow, and thus the
meeting of one of the oldest and most valued Ladies-in-Waiting with
Don Carlos was avoided.
Isabella certainly never expected that she would be dethroned,
for a few weeks before the revolution of September, 1868, the
celebrated General Tacon, Duke of the Union of Cuba, announced
the forthcoming marriage of his daughter Carolina with the Marquis
Villadarías, of the première noblesse, and a perfect type of a
Spanish grandee, and she said: “I congratulate her sincerely on her
engagement; but,” she added sadly, “for myself I am sorry, as I shall
see her no more at Court.” The Queen here referred to the well-
known Carlist opinions of the Marquis Villadarías, which would have
made it impossible to receive the Marchioness at the palace if she
had remained there.
So Isabella II. was dethroned in 1868, and she can truly be said
to have been the victim of circumstances. From the moment King
Ferdinand died his daughter had been the object of intrigue and
ambition. Whilst our Queen Victoria was carefully educated and
drilled in high principles, Isabella was the prey of those who wished
to rise to power by her favour. Ministers made love to the Sovereign
instead of discussing the welfare of the nation; flowery speeches on
patriotism meant merely the gratification of the orator’s vanity to be
remarked by Her Majesty. Personal advancement was the end and
aim of those in the Government, and thus poor Isabella’s
susceptibilities were worked upon to an awful extent.
It is well known that General Serrano, who might have been
thought to have the welfare of his country at heart, gained an undue
influence over the Queen by means of her affections, and fomented
to a great extent the matrimonial differences between her and her
husband. Generous to a degree, Isabella paid the debts of this
courtier twice, and yet it was this same General who was the first to
have her hurled from the royal palace.
When the great Canning visited Madrid, Bulwer Lytton showed
him at a Court ball the many women who were the favourites of the
Ministers, and there was, indeed, hardly a statesman who would not
sacrifice principles to the pleas of his mistress. It was at this Court,
steeped in immorality, that Isabella was brought up with little or no
knowledge of right and wrong, and even in her marriage she was a
victim to the intrigues and ambitions of other Courts of Europe as
well as those of her own. She was, in fact, a scapegoat of the nation.
Harassed and in desperation at being pressed on to a miserable
marriage destitute of all that could justify it, Isabella, after one of
those long and fruitless discussions with her mother, once addressed
a letter to our Queen Victoria; but in a pure Court like that of England
little idea could be formed of the stagnant atmosphere of the Spanish
palace from which the poor young Queen sent forth her plaint.
Beyond the Court raged the stormy discontent of the country, which
had been thwarted for more than thirty years of the fulfilment of its
constitutional rights promised by Ferdinand VII. as the condition of
his return to the throne of Spain.
Whilst Queen Victoria was daily increasing in the knowledge of
constitutional rights which are the base of a Sovereign’s power, poor
Isabella’s Prime Ministers resigned at any moment in pique or
jealousy of some other politician, and the people grew daily more
discontented at finding the Parliament was a farce, and it meant
neither the progress of the land nor the protection of the people.
Bulwer Lytton was constantly sending despatches to England
about the shortcomings of Isabella II. as a woman, but he seemed to
lay no stress on the cause of her failure as a Queen. Under proper
conditions Isabella doubtless would have been a good woman and a
great Queen, but choked with the weeds of intrigue she was lost.
Undisciplined and uneducated, the poor Queen fell a victim to what,
if properly directed, would have been virtues instead of vices.
The marriage to which Isabella was forced by intrigue was, of
course, the greatest evil which could have befallen such an
impulsive, warm-hearted girl, who knew no more how to turn a deaf
ear to a claimant for her favour than to keep her purse shut to the
plea of an unfortunate beggar.
The Right Hon. Henry Lytton Bulwer wrote a little later from the
British Embassy at Madrid to the Court of St. James’s, saying that he
“looked at the Queen’s conduct as the moral result of the alliance
she had been more or less compelled to contract, and he regarded
her rather with interest and pity than blame or reproach.”
Isabel’s natural intuition of our Queen Victoria’s good heart
prompted her letters to her. They were sent by a private hand, and
who knows what evils might have been prevented in the Court of
Spain if the long journey, so formidable in those days, had not placed
the sister-Queens so far apart?
Espartero’s plea for Isabel to marry Don Enrique de Assisi, the
man of her heart, met no support in a Court torn with intrigue, and
the sad, bad story of Isabel doubtless had its source in the tragedy of
an unhappy marriage. At the plea of a persistent wooer, who knew
that the Queen had the right of dissolving a Ministry, a Government
would fall; and as the station of her favourites became lower and
lower, as time went on the ill-regulated Sovereign had a Government
as undependable as her friends.
Treachery was the keynote of the Court of Spain, and some of
the leaders of the revolution were those who had used the
Sovereign’s ignorance and foolhardiness to their own ends. In such
an atmosphere of untruth and treachery such men as Espartero,
Prim, etc., could play no enduring part. Hardly had Espartero swept
the Court clean of the Regency of Queen Maria Cristina than his fall
was encompassed by O’Donnell, his rival. The flagrant falsification of
the Parliamentary election returns—which is still the cankerworm of
the country—was the check to all progress. Count San Luis made a
primitive effort for the reform of the elections; he suggested that the
names of the candidates as deputies should be put in a bag, and
drawn out by a child blindfolded, for the law of chance seemed to
him better than the custom of deception.
Isabella’s acts of generosity are still quoted with admiration at the
royal palace of Madrid by those who served her as Queen.
Four hundred girls owed their marriage dots to Isabella, and it
was the fathers of these four hundred royally endowed brides who
treacherously worked for her expulsion.
One day, hearing the story of the penury of a clever man of
letters, Isabella commanded 20,000 francs to be sent to him. The
administrator of her finances, thinking the Queen could hardly know
how much money this sum represented, had twenty notes of 1,000
francs each changed into small money, and put out on a table by
which she had to pass.
“What is all this money for?” asked Isabella, when she saw it
spread out to view.
“It is the money for the man of letters, and this shows Your
Majesty how large is the sum of 20,000 francs.”
“So much the better,” was the prompt reply; and the courtier saw
it was not by proving the amount of the boon that he could check his
Sovereign in her generous actions.
A Court official at Madrid, who has been sixty years in office at
the palace, told me he often saw Isabella take off her bracelets, and
give them to the beggars who pressed upon her as she crossed the
courtyard of the royal domain.
“And who could help loving her?” said the old courtier, with tears
in his eyes; “I know I could not.”
Caught in the darkness of ignorance and intrigue, Isabella was
naturally enraged at the revolution. When her son Alfonso was
nearly made captive by the Carlists at Lucar, she said: “I would
rather my Alfonso be a prisoner of the Carlists than a captive of the
revolutionists.”
Isabella had a faithful friend in the Marquis of Grizalba, and he
said to Croze:[18]
“It is the loss of faith which causes our woes; the charm of death
has been destroyed with the hope of a hereafter. But Spain will die
like a gentleman.”
[18] The author of “La Vie intime d’Alfonse XIII.”
EMILIO CASTELAR
1873–1874