Learning Java An Introduction to Real World Programming with Java Marc Loy - The ebook is ready for instant download and access
Learning Java An Introduction to Real World Programming with Java Marc Loy - The ebook is ready for instant download and access
com
https://textbookfull.com/product/learning-java-an-
introduction-to-real-world-programming-with-java-marc-loy/
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/learning-java-an-introduction-to-
real-world-programming-with-java-5th-edition-marc-loy/
textbookfull.com
https://textbookfull.com/product/learning-java-beginning-programming-
with-java-for-dummies-first-edition-john-bach/
textbookfull.com
https://textbookfull.com/product/introduction-to-programming-in-java-
an-interdisciplinary-approach-2nd-edition-robert-sedgewick/
textbookfull.com
https://textbookfull.com/product/java-an-introduction-to-problem-
solving-and-programming-8th-edition-walter-savitch/
textbookfull.com
Learning Network Programming with Java 1st Edition Reese
Richard M
https://textbookfull.com/product/learning-network-programming-with-
java-1st-edition-reese-richard-m/
textbookfull.com
https://textbookfull.com/product/learning-java-with-games-chong-wei-
xu/
textbookfull.com
https://textbookfull.com/product/learning-java-functional-
programming-1st-edition-reese-richard-m/
textbookfull.com
2. 1. A Modern Language
a. Enter Java
i. Java’s Origins
ii. Growing Up
b. A Virtual Machine
c. Java Compared with Other Languages
d. Safety of Design
i. Simplify, Simplify, Simplify…
ii. Type Safety and Method Binding
iii. Incremental Development
iv. Dynamic Memory Management
2
v. Error Handling
vi. Threads
vii. Scalability
e. Safety of Implementation
i. The Verifier
ii. Class Loaders
iii. Security Managers
3. 2. A First Application
a. Java Tools and Environment
3
i. Classes
ii. The main() Method
iii. Classes and Objects
iv. Variables and Class Types
v. HelloComponent
vi. Inheritance
vii. The JComponent Class
viii. Relationships and Finger-Pointing
ix. Package and Imports
x. The paintComponent() Method
c. HelloJava2: The Sequel
i. Instance Variables
ii. Constructors
iii. Events
iv. The repaint() Method
v. Interfaces
a. JDK Environment
b. The Java VM
c. Running Java Applications
i. System Properties
d. The Classpath
i. javap
4
ii. Modules
e. The Java Compiler
f. Trying Java
g. JAR Files
i. File Compression
ii. The jar Utility
iii. The pack200 Utility
h. Building Up
a. Text Encoding
b. Comments
i. Javadoc Comments
i. Statements
ii. Expressions
f. Arrays
i. Array Types
5
ii. Array Creation and Initialization
iii. Using Arrays
iv. Anonymous Arrays
v. Multidimensional Arrays
g. Types and Classes and Arrays, Oh My!
6. 5. Objects in Java
a. Classes
b. Methods
i. Local Variables
ii. Shadowing
iii. Static Methods
iv. Initializing Local Variables
v. Argument Passing and References
vi. Wrappers for Primitive Types
vii. Method Overloading
c. Object Creation
i. Constructors
ii. Working with Overloaded
Constructors
d. Object Destruction
i. Garbage Collection
6
e. Packages
i. Importing Classes
ii. Custom Packages
iii. Member Visibility and Access
iv. Compiling with Packages
f. Advanced Class Design
a. Exceptions
b. Assertions
7
i. Enabling and Disabling Assertions
ii. Using Assertions
c. The Logging API
i. Overview
ii. Logging Levels
iii. A Simple Example
iv. Logging Setup Properties
v. The Logger
vi. Performance
d. Real-World Exceptions
8. 7. Collections and Generics
a. Collections
i. The Collection Interface
ii. Collection Types
iii. The Map Interface
b. Type Limitations
c. Enter Generics
i. Talking About Types
d. “There Is No Spoon”
i. Erasure
ii. Raw Types
8
e. Parameterized Type Relationships
i. Regex Notation
ii. The java.util.regex API
d. Math Utilities
9
i. The java.lang.Math Class
ii. Big/Precise Numbers
i. Thread State
ii. Time-Slicing
10
iii. Priorities
iv. Yielding
d. Thread Performance
e. Concurrency Utilities
11. 10. Desktop Applications
a. Buttons and Sliders and Text Fields, Oh My!
i. Component Hierarchies
ii. Model View Controller Architecture
iii. Labels and Buttons
iv. Text Components
v. Other Components
i. Mouse Events
ii. Action Events
iii. Change Events
iv. Other Events
d. Modals and Pop Ups
i. Message Dialogs
11
ii. Confirmation Dialogs
iii. Input Dialogs
e. Threading Considerations
i. Menus
ii. Preferences
iii. Custom Components and Java2D
iv. JavaFX
g. User Interface and User Experience
12
i. Asynchronous I/O
ii. Performance
iii. Mapped and Locked Files
iv. Channels
v. Buffers
vi. Character Encoders and Decoders
vii. FileChannel
d. Network Programming
e. Sockets
f. More to Explore
13. 12. Programming for the Web
13
iii. The HttpURLConnection
iv. SSL and Secure Web Communications
d. Java Web Applications
e. Servlet Containers
a. Java Releases
i. JCP and JSRs
b. Lambda Expressions
14
15. A. Code Examples and IntelliJ IDEA
i. Installing on Linux
ii. Installing on a macOS
iii. Installing on Windows
15
Learning Java
FIFTH EDITION
16
Learning Java
17
Illustrator: Rebecca Demarest
While the publisher and the authors have used good faith efforts to
ensure that the information and instructions contained in this work
are accurate, the publisher and the authors disclaim all responsibility
for errors or omissions, including without limitation responsibility
for damages resulting from the use of or reliance on this work. Use
of the information and instructions contained in this work is at your
own risk. If any code samples or other technology this work contains
or describes is subject to open source licenses or the intellectual
property rights of others, it is your responsibility to ensure that your
use thereof complies with such licenses and/or rights.
978-1-492-05627-0
[LSI]
18
Preface
19
examples and avoid merely cataloging features. The examples are
simple, but hint at what can be done. We won’t be developing the
next great “killer app” in these pages, but we hope to give you a
starting point for many hours of experimentation and inspired
tinkering that will lead you to develop one yourself.
The last part of this book branches out to discuss Java in the context
of web applications, web services, and request processing, so you
20
should be familiar with the basic ideas behind web browsers,
servers, and documents.
New Developments
This edition of Learning Java is actually the seventh edition—
updated and retitled—of our original, popular Exploring Java. With
each edition, we’ve taken great care not only to add new material
covering additional features, but to thoroughly revise and update the
existing content to synthesize the coverage and add years of real-
world perspective and experience to these pages.
21
syntactic changes and updates to APIs and libraries. We’ve tried to
capture these new features and update every example in this book to
reflect not only the current Java practice, but style as well.
22
Chapters 4 and 5 introduce programming fundamentals,
then describe the Java language itself, beginning with the
basic syntax and then covering classes and objects,
exceptions, arrays, enumerations, annotations, and much
more.
Chapter 6 covers exceptions, errors, and the logging
facilities native to Java.
Chapter 7 covers collections alongside generics and
parameterized types in Java.
Chapter 8 covers text processing, formatting, scanning,
string utilities, and much of the core API utilities.
Chapter 9 covers the language’s built-in thread facilities.
Chapter 10 covers the basics of graphical user interface
(GUI) development with Swing.
Chapter 11 covers Java I/O, streams, files, sockets,
networking, and the NIO package.
Chapter 12 covers web applications using servlets, servlet
filters, and WAR files, as well as web services.
Chapter 13 introduces the Java Community Process and
highlights how to track future changes to Java while
helping you retrofit existing code with new features, such as
the lambda expressions introduced in Java 8.
If you’re like us, you don’t read books from front to back. If you’re
really like us, you usually don’t read the preface at all. However, on
the off chance that you will see this in time, here are a few
suggestions:
23
should at least look at the information in Chapter 3, which
explains how to use the compiler and interpreter. This
should get you started.
Chapters 11 and 12 are the places to head if you are
interested in writing network or web-based applications and
services. Networking remains one of the more interesting
and important parts of Java.
Chapter 10 discusses Java’s graphics features and
component architecture. You should read this if you are
interested in writing desktop graphical Java applications.
Chapter 13 discusses how to stay on top of changes to the
Java language itself, regardless of your particular focus.
Online Resources
There are many online sources for information about Java.
Oracle also maintains the OpenJDK site. This is the primary open
source version of Java and the associated tools. We’ll be using the
OpenJDK for all the examples in this book.
24
And of course, you can check the home page for Learning Java!
25
creatures.
This book is here to help you get your job done. In general, if
example code is offered with this book, you may use it in your
programs and documentation. You do not need to contact us for
permission unless you’re reproducing a significant portion of the
code. For example, writing a program that uses several chunks of
code from this book does not require permission. Selling or
distributing examples from O’Reilly books does require permission.
Answering a question by citing this book and quoting example code
does not require permission. Incorporating a significant amount of
example code from this book into your product’s documentation
26
does require permission.
If you feel your use of code examples falls outside fair use or the
permission given above, feel free to contact us at
[email protected].
NOTE
For more than 40 years, O’Reilly Media has provided technology
and business training, knowledge, and insight to help companies
succeed.
How to Contact Us
27
Please address comments and questions concerning this book to the
publisher:
Sebastopol, CA 95472
707-829-0104 (fax)
We have a web page for this book where we list errata and any
additional information. You can access this page at
https://oreil.ly/Java_5e.
The example code can be found separately on GitHub. There are two
repositories for this book: the main examples and the web examples.
More details on accessing and working the examples is provided in
Appendix A.
For more information about our books, courses, and news, see our
website at http://www.oreilly.com.
28
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
Many people have contributed to putting this book together, both in
its Exploring Java incarnation and in its current form as Learning
Java. Foremost, we would like to thank Tim O’Reilly for giving us
the opportunity to write this book. Thanks to Mike Loukides, the
series editor, whose patience and experience continue to guide us.
Other folks from O’Reilly, including Amelia Blevins, Zan
McQuade, Corbin Collins, and Jessica Haberman, have provided
consistent wisdom and encouragement. We could not have asked for
a more skillful or responsive team of people with whom to work.
Warm thanks to Ron Becker for sound advice and interesting ideas
as seen from the perspective of a layman well removed from the
programming world. Thanks also to James Elliott and Dan Leuck for
their excellent and timely feedback on the technical content of this
edition. As with so many things in the programming world, extra
eyes are indispensible, and we are lucky to have had such attentive
pairs in our corner.
29
Another Random Document on
Scribd Without Any Related Topics
Morison’s journal illustrates vividly how completely the military
authorities dominated the lives of the fort’s inhabitants. Not only
were the soldiers at the mercy of their officers, but civilians, such as
Morison’s nephew, William Morison, were helpless in the face of
military indifference to their problems since there was no civil
authority at Michilimackinac or anywhere else in what is now
Michigan to which they might appeal during this period. Traders
constantly complained at the high-handed actions of the fort’s
commanders who, these traders charged, used their position to gain
great material benefits for themselves and imposed ruinous
regulations on those traders who would not give them a cut of their
profits. The royal government sought to correct these abuses, but
throughout the period of British rule Michilimackinac is said to have
had a reputation as a center of corruption and misrule.
20
“Ensign Johnstone (who was there with his wife) saluted him with
innumberable knocks & kicks.”
21
Remarks December 1769.
[1stly.] That a few days after this unwarrantable Outrage perpetrated
against my dwelling House & person, John Chinn & Forrest Oaks
were so conscious of their Enormous guilt [and] struck with such a
Remorse, that they declared that they would upon their knees
publickly on the Parade beg to be pardoned if that would satisfy for
the Errors they were conscious of haveing committed. This they
expressed in the Audience of William Maxwell, Commissary, Sergt.
McMurray, George McBeath, [and] Henry Williams, traders.
4thly. Upon the 11th do. Ens. Johnstone assumed to encroach upon
my Province by visiting Sergt. McPherson of the Colonel’s Company,
who says he never sent for him Directly or indirectly. He immediately
insisted upon seeing his wound, tho’ he was but newly dressed one
hour before, [and] took off the dressings, &c. After inspection 22
he told my patient for his comfort, it looked very bad, & that he
knew nothing better for him than to bathe it with brandy. [He] set off
directly, & left the tumefyed wound exposed to the Open air, till my
servant was obliged to go & dress him a second time. This among the
rest is one specimen of the Ens. Johnstone’s activity to interfere in
matters which did not concern him. That at the same time he told
Sergt. McPherson if he belonged to the Company, he [would give]
commands. He would immediately confine Dr. Morison in the common
guard house.
N. B.: That in a few days after this Violent Assault, John Chinn and
Forrest Oaks were observed conjointly & severally, loading their guns
& pistoles, which I dare say were intended for bad purposes which
they would in all appearance have prosecuted had not their
proceedings been disapproved of by a certain gentleman in the
Garrison.
One night the ensueing spring, when they carroused heartily at their
Bowl, John Chinn proposed to pay another Visit to the Doctor, which
being disapproved of by one of the principals in the Assault, was
dropped.
Sunday the 9th December 1770. Betwixt the hours of 11 & 12 o’Clock
forenoon, when the Garrison was at Church, Sergt. [Thomas] Carlile
of the general’s company had the guard that day, & being thirsty
stepped over to his own house (which was adjacent & directly
opposite to his guard) for a drink of spruce beer. He no sooner
opened the Door of his room than Ens. Johnstone (who was there
with his wife) saluted him with innumberable knocks & kicks till
(almost in his own words to me) he had knocked his head into
blubber, then kicked him in the private parts (from which Violence his
private parts, particularly one of his testicles, are greatly Swelled, of a
hue black as his Hat). [Johnson gave him] many bumps upon his
head, [and] his jaw bones, as he himself says, [are] so painful that
he can scarce open his mouth, but with great difficulty.
The poor Sergt. immediately returned to his guard & the next 24
day applyed to the commanding officer Capt. Turnbull of this
Fort for his Protection in regard to his most grievious situation, from
the Attrocious and barbarous usage he had sustained by the violent
proceedings of Ens. Johnstone whom he was determined to
prosecute to the outmost for the Violence done to his person, in his
own house.
Tuesday evening the 1st. Jan’y 1771. John Savage, Taylor & Soldier in
the general’s Company, twixt 11 & 12, had the Door of his house
forced open [and was] committed to the guard-house for not
suffering his wife to comply with obscene proposit[ions made] to her.
At this Exploit Ens. Johnstone was one of the chief witnesses. The
prisoner was set at liberty next morning without any crime given
against him.
Monday the 7th Jan’y 1771. Ens. Johnstone decoyed away Sergt.
Carlile’s wife, which he has been contriveing to accomplish many
months before this finishing stroake, & tho’ her husband was like to
break his Heart, and crying out his Eyes on the occasion, Yet no
Remonstrances would be payed the least attention to. Ens. Johnstone
quitted his Room in the officer’s barracks that evening which he
exchanged with Mr. Main for his. [He] sleeped with her that night in
his new room where he lives with her still, without dread or shame,
while the poor Husband is left in such a disconsolate situation that is
not easy to describe. He next morning (poor man) applyed to 26
the Commanding Officer, who gave him no satisfactory redress,
as he did not chuse to interfere either pro or con. Yet the next day
after her Elopement from her husband she had the honnour of dining
with the commanding officer and his mess, who drank tea with her
that evening at her new lodgeings.
Sergt. Carlile had the mortification to see his wife dayly conducted by
one of the mess to dine with the commanding officer, Capt. Turnbul,
& the good Company with him, at which the poor Sergt. was like to
go distracted but could not help himself.
Upon friday the 8th febry. 1771, Ens. Johnstone in presence of the
Commanding officer, Capt. Turnbull, Ens. Strickland, George Main,
William Maxwell, Commissary, [and] George McBeath, trader,
attacked William Morison, my nephew, in a most rude and Violent
manner, without any evident cause, in the billiard Room in the
presence of six witnesses. The Young man (who is sometimes liable
to a fainting Disposition) in the Scuffle he fell down & cryed Murder!
William Maxwell, Commissary of provisions, interposed, by 27
which he received some knocks from Ens. Johnstone without
returning one blow, after which Johnstone passed by [the]
Commissary in a furious manner, & the young man was flat down in a
swoon. Ens. Johnstone raised up his left arm & gave him repeated
thumps opposite to the heart, by which it would appear he intended
to murder the young man.
He is now under care & it is a chance if ever he can get the better of
it. The commanding officer with all those of their Club was present to
all this. Some of them I am told stood firm with their backs to the
room door, I suppose to hinder any from comeing in to the assistance
of the ill-used Young man. At length the noise brought in Mr. Harise,
who can attest to everything he saw. Mr. Harise with one or two more
carryed him for dead to his room, where after untying his stock he
gradually recovered from his trance.
The 23d. feby. 1771. Ens. Johnstone quarrelled with William Maxwell,
Commissary, & revilled [him] (if not gave him a few blows) with
exasperating expressions, both in company of the commanding
officer, who, when words became too high, ordered Ens. Johnstone
to his room, wherein he did not continue above twenty four hours,
for reasons best known to the Commanding Officer & himself.
28
“... a woman who I have the greatest regard for distracted me by her
imprudent behaviour.”
29
III
A Regimental Court-Martial
President—Lieut. Christie
The Prisoner being put to defence denys the crime & says he will
not be tryed by a Regimental Courtmartial, but desires a general
one, & objects to Ens. Johnstone, for reasons he now will not
mention. The Court is of oppinion the prisoner is guilty of the crime
laid to his Charge, therefore do sentence him to be reduced and
serve as private in the Ranks.
N. B.: That upon the 26th febry. 1771. Capt. Turnbull, Commandant,
Issued verbal orders to Sergt. [Mc]Murray of the General’s Company
to Erase the proceedings of the Regimental Court Martial concerning
Sergt. Carlile’s tryal out of the Regimental Orderly book, & if he
could, would get it Erased out of the orderly book of the Lieut.
Colonel’s Company also. Accordingly Sergt. McGann of the Colonel’s
Company did erase it, without any previous notice given to the officer
who commanded the Company. As that officer was not a little
surprized at such uncommon proceedings, he immediately confined
him in the guard-house, who in his own defence told his officer he
received positive orders from the acting Sergt. Major so to do, who
had told him it was the orders of the day by the commanding officer,
Capt. Turnbull. Upon which the officer commanding the Coll’s
Company sent for the acting Sergt. Major who acknowledged to him
he had received orders from the Commanding officer of the Fort to
have the tryall of Sergt. Carlile torn out of the Book of both
Companies doing duty in Garrison. Upon which Declaration the officer
set Sergt. McGann at liberty.
Sir—
I hope You will pardon my takeing this Liberty to trouble You, but
to ease my own mind I cannot avoid it, by the instigation of my
own Notions. I was so imprudent to object to Ens. Johnstone’s
being a member of my Court Martial, altho’ I am now well
convinced that his own Honnour would not allow him to do
anything prejudicial to Justice. The only Excuse I can make to him
& Ens. Strickland is that a Woman who I have the greatest regard
for distracted me by her imprudent behaviour. This, Sir, I hope in
some part will Extenuate my Crimes, & I shall only further beg
leave to Observe that Ens. Johnstone has behaved to me as 32
a good officer, & I have no ground of Complaint against him,
& I am extreamely sorry for, & beg his forgiveness & Ens.
Strickland’s for my past behaveour, which I never will be guilty of
again.
To Capt. Turnbull
Commanding at Michilamackinac
a true Copy
33
IV
A Catalogue of Foul Deeds
34
“... sentenced to Receive 1000 lashes for Desertion.”
Michilamackinack, 10th Aprile 1771. Ens. Johnstone
35
attempted a rape on a girl betwixt nine & ten Years of age. Her
shrieking out Violently alarmed her step-father, Arthur Ross, & others
in the barracks. This Arthur Ross was servant to Ens. Johnstone at
the time, who upon makeing remonstrances the next morning to Ens.
Johnstone of his rude & uncommon behaveour, Received a very
severe blow on the left breast from his master, Ens. Johnstone, which
made him quit his service instantly.
Tuesday the 23d Aprile 1771. We had a kind of horse race here; the
most of the garrison walked out to enjoy the pleasure of the Show.
Ens. Johnstone being appointed as one of the Judges upon the
Occasion, Ens. Strickland’s mare was saddled for him, that he might
be enabled to execute the important office with the more Alacrity.
The racers appeared on the ground expecting Ens. Johnstone with
outmost impatience. [They] sent several messages in quest of him;
his mare was found; but he himself could not, & no wonder for he
was then in a Captain’s Cabin marking barrells of Liquor to be Rolled
over to the Suttler’s house, which was observed to be conveyed in
this manner by Creditable Witnesses. Ens. Johnstone seized this
opportunity very seasonably while the racers waited half one hour for
his Judicial appearance before they could start.
After secureing his Cargoe in this manner he appeared soon after the
racers started. The horse won & the mare lost the race (which is a
surprizing Circumstance) according [to] the prevailing constitution of
this fort.
Thursday 25th. Aprile 1771. A Barrell of Liquor, at least 36
presumed to be, was Rolled over to the Suttler’s about 12
o’clock forenoon. This Barrell was marked Capt G. T. [George
Turnbull] & rolled over by Donaldson [?] of the General’s Company.
5thly. Attempted to draw his sword upon Mr. [Benjamin] Frobisher for
which he was put under arrest.
7thly. Selling of Common Rum to the Soldiers, mixing water with it, &
selling it afterwards at 18. sh. York Currency pr. Gallon.
12thly. Mixing four ounces of Jallap with the water boiled for Punch at
a publick Enter[tain]ment.
16thly. Kicked & Cuffed Sergeants & Soldiers upon the publick 37
Parade, to which the Commanding officer was present at
some of those Irregularitys.
18thly. Kicked and Cuffed Sergt. Carlile in his own house upon a
Sabbath day when the garrison was at Church, & in the Scuffle
hurt one of his stones.
19thly. Some days after that he decoyed away the Sergt’s wife, Mrs.
Carlile, who lived with him upwards of six weeks at Bed & board
&c.
20thly. Vaunted he had Carnal dealings with Mrs. Oldham & three
Years after attempted a Rape upon her daughter betwixt 9 & 10
years of age.
2lst. Was put under arrest by Capt. Turnbull for the space of 24 hours
for Quarrelling with Maxwell the Commissary.
22d. Attempted to sow Discord betwixt George McBeath and his wife
to whom he offered one hundred pounds &c provideing she
would take up with him & quit her husband.
30th May 1771. That evening after Roll Call, a Delinquent, James
Coleman, soldier of the Lieut Colonel’s Company, sentenced to
Receive 1000 Lashes for Desertion, received near five hundred &
would have received more had not I intervened to hinder any more
proceedings as he was not able to endure any more.
Michilimackinac 31. May 1771. For the future the officer of the Day
will visit the men’s barracks every forenoon at 12 o’clock to see
that the Barracks are kept clean & that the sick or lame are
regularly visited by the Surgeon; when prisoners are in that
situation the Sergt. of the Guard will report to the officer of the day
if it should happen that they are not Regularly attended.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com