Instant Ebooks Textbook OCA Java SE 8 Programmer I Certification Guide 1st Edition Mala Gupta Download All Chapters
Instant Ebooks Textbook OCA Java SE 8 Programmer I Certification Guide 1st Edition Mala Gupta Download All Chapters
com
https://textbookfull.com/product/oca-java-
se-8-programmer-i-certification-guide-1st-edition-
mala-gupta/
https://textbookfull.com/product/ocp-java-se-8-programmer-ii-exam-
guide-exam-1z0-809-kathy-sierra/
textbookfull.com
https://textbookfull.com/product/ocp-oracle-certified-professional-
java-se-11-programmer-i-study-guide-exam-1z0-815-jeanne-boyarsky/
textbookfull.com
https://textbookfull.com/product/paramedic-crash-course-with-online-
practice-test-1st-edition-christopher-coughlin-ph-d/
textbookfull.com
Hannah Arendt’s Aesthetic Politics: Freedom and the
Beautiful Jim Josefson
https://textbookfull.com/product/hannah-arendts-aesthetic-politics-
freedom-and-the-beautiful-jim-josefson/
textbookfull.com
https://textbookfull.com/product/food-chemistry-1st-edition-dongfeng-
wang/
textbookfull.com
https://textbookfull.com/product/history-of-management-thought-
genesis-and-development-from-ancient-origins-to-the-present-day-vadim-
i-marshev/
textbookfull.com
https://textbookfull.com/product/antimicrobials-in-
livestock-1-regulation-science-practice-a-european-perspective-lucie-
pokludova/
textbookfull.com
Her Billionaire Hero A Friends to Lovers Second Chance
Romance Irresistible Brothers Book 4 1st Edition Scarlett
King & Michelle Love [King
https://textbookfull.com/product/her-billionaire-hero-a-friends-to-
lovers-second-chance-romance-irresistible-brothers-book-4-1st-edition-
scarlett-king-michelle-love-king/
textbookfull.com
MANNING Mala Gupta
OCA Java SE 8 Programmer I Certification Guide
OCA Java SE 8
Programmer I
Certification Guide
MALA GUPTA
MANNING
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: [email protected]
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
ISBN: 9781617293252
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16
To Dheeraj, my pillar of strength
brief contents
Introduction 1
1 ■ Java basics 22
2 ■ Working with Java data types 92
3 ■ Methods and encapsulation 147
4 ■ Selected classes from the Java API and arrays 221
5 ■ Flow control 322
6 ■ Working with inheritance 384
7 ■ Exception handling 469
8 ■ Full mock exam 539
vii
contents
preface xvii
acknowledgments xix
about this book xxi
about the author xxix
about the cover illustration xxx
Introduction 1
1 Disclaimer 2
2 Introduction to OCA Java SE 8 Programmer I
Certification 2
3 The importance of OCA Java SE 8 Programmer I
Certification 2
4 Comparing OCA Java exam versions 4
5 Next step: OCP Java SE 8 Programmer II (1Z0-809)
exam 8
6 Complete exam objectives, mapped to book chapters, and
readiness checklist 8
7 FAQs 10
FAQs on exam preparation 10 ■
FAQs on taking the exam 18
8 The testing engine used in the exam 20
ix
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
x CONTENTS
1 Java basics 22
1.1 The structures of a Java class and a source code file
Structure of a Java class 24 ■ Structure and components of
23
command line 39
1.3 Java packages 41
The need for packages 42 Defining classes in a package using
■
Java entities 62
1.5 Nonaccess modifiers 64
abstract modifier 65 ■
final modifier 66 ■
static modifier 67
1.6 Features and components of Java 72
Valid features and components of Java 72 ■
Irrelevant features
and components of Java 74
1.7 Summary 74
1.8 Review notes 75
1.9 Sample exam questions 79
1.10 Answers to sample exam questions 84
an array 258
4.4 ArrayList 258
Creating an ArrayList 259 Adding elements to an
■
5 Flow control
5.1
322
The if, if-else, and ternary constructs
The if construct and its flavors 324 Missing else blocks 328
■
324
enhanced for loops 363 Comparing for and while loops 364
■
contains within it an object of its base class 390 Which base class
■
a derived class object 420 The need for accessing an object using
■
Will a finally block execute even if the catch block defines a return
statement? 493 What happens if both a catch and a finally block
■
modifies the value returned from a catch block? 495 Can a try ■
block be followed only by a finally block? 496 Does the order of the ■
create nested loops, so can I create nested try-catch blocks too? 500
Should I handle errors? 502
7.5 Common exception classes and categories 503
ArrayIndexOutOfBoundsException and
IndexOutOfBoundsException 504 ClassCastException 505 ■
xvii
xviii PREFACE
■ Instruction on what’s happening behind the scenes using the actual code from
the Java API source
■ Mastery of both the concepts and the exam
This book is written for developers with a working knowledge of Java. My hope is that
the book will deepen your knowledge and prepare you well for the exam and that you
will pass it with flying colors!
acknowledgments
First and foremost, I thank Dheeraj—my pillar of strength, my best friend, and my
husband. His constant guidance, encouragement, and love kept me going. He helped
me to get started with this book and got me over the goal line.
My sincere gratitude goes to Marjan Bace, publisher at Manning, for giving me the
opportunity to author this book. The Manning team has been wonderful—Michael
Stephens ensured that it was worth it for Manning to have a book on this subject.
Cynthia Kane, my development editor, is like sunshine. Not only did she help me with
the organization of individual chapters and the overall book, but she pulled me through
whenever the task of writing a book became overwhelming. It’s always a pleasure to
work with her. Copyeditor Linda Recktenwald not only applied her magic to sentence
and language constructions but also supplemented her editing with valuable sugges-
tions on technical content.
Technical development editor Francesco Bianchi suggested multiple additions and
modifications, improving the content of this book. Technical proofreader Jean-François
Morin was outstanding in his review. He not only pointed out existing errors but also
suggested multiple improvements to the organization of the contents. Proofreader
Katie Tennant was extremely capable and talented. She reviewed the final manuscript
with great precision.
The technical reviewers on this book did an awesome job of reviewing the con-
tents and sharing their valuable feedback and comments: Andrea Barisone, Andrea
Consentino, Anutosh Ghosh, David Blau, Marty Henderson, Mirsad Vojnikovic, Nicola
Pedot, Sanjiv Kumar, Simona Russo, Travis Nelson, and Ursin Stauss. I would also like
xix
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
xx ACKNOWLEDGMENTS
to thank Nicole Butterfield and Donna Clements, review editors, for managing the
whole review process and meticulously funneling the feedback to make this book better.
Dennis Dalinnik did an outstanding job of converting the black-and-white hand-
drawn illustrations into glorious images. It was amazing to scrutinize the page proofs.
I also thank Dennis for adjusting the images in the final page proofs, which was a lot
of work. Janet Vail and Mary Piergies were awesome in their expertise at turning all
text, code, and images into publishable form. I am also grateful to Candace Gillhoolley
for her efforts in promoting the book.
I thank the MEAP readers for buying the book while it was being developed and
for their suggestions, corrections, and encouragement.
I would also like to thank my former colleagues Harry Mantheakis, Paul Rosenthal,
and Selvan Rajan, whose names I use in coding examples throughout the book. I have
always looked up to them.
I thank my daughters, Shreya and Pavni, who often advised me on the images that
I created for the book. I thank my family for their unconditional support. The book
would have been not been possible without their love and encouragement.
about this book
This book is written for developers with a working knowledge of Java who want to earn
the OCA Java SE 8 Programmer Certification. It uses powerful tools and features to
make reaching your goal of certification a quick, smooth, and enjoyable experience.
This section explains the features used in the book and tells you how to use the book
to get the most out of it as you prepare for the certification exam. More information
on the exam and on how the book is organized is available in the Introduction.
Covered in chapter/
Exam objectives
section
1.3 Create executable Java applications with a main method; run a Java program from Section 1.2
Figure 1 The Introduction to this book provides a list of all exam objectives and the corresponding
chapter and section numbers where they are covered. See the full table in the Introduction (table I.3).
xxi
xxii ABOUT THIS BOOK
The map in the Introduction shows the complete exam objective list mapped to the
relevant chapter and section numbers. You can jump to the relevant section number
to work on a particular exam topic.
Chapter-based objectives
Each chapter starts with a list of the exam objectives covered in that chapter, as shown
in figure 2. This list is followed by a quick comparison of the major concepts and top-
ics covered in the chapter with real-world objects and scenarios.
[1.2] Define the structure of a Java class. Structure of a Java class, with its components:
package and import statements, class declara-
tions, comments, variables, and methods.
Difference between the components of a Java
class and that of a Java source code file.
[1.3] Create executable Java applications with a The right method signature for the main method
main method; run a Java program from the to create an executable Java application.
command line; including console output. The arguments that are passed to the main
method.
Figure 2 An example of the list of exam objectives and brief explanations at the beginning of each
chapter
Section-based objectives
Each main section in a chapter starts by identifying the exam objective(s) that it cov-
ers. Each listed exam topic starts with the exam objective and its subobjective number.
In figure 3, the number “4.4” refers to section 4.4 in chapter 4 (the complete list of
chapters and sections can be found in the table of contents). The number “9.4” pre-
ceding the exam objective refers to the objective’s numbering in the list of exam
objectives on Oracle’s website (the complete numbered list of exam objectives is given
in table I.3 in the Introduction).
4.4 ArrayList
[9.4] Declare and use an ArrayList of a given type
In this section, I’ll cover how to use ArrayList, its commonly used methods, and the
advantages it offers over an array.
The OCA Java SE 8 Programmer I exam covers only one class from the Java Collec-
tion API: ArrayList. The rest of the classes from the Java Collection API are covered
in the OCP Java SE 8 Programmer II exam (exam number 1Z0-809). One of the reasons
Figure 3 An example of the beginning of a section, identifying the exam objective that it covers
ABOUT THIS BOOK xxiii
Exam tips
Each chapter provides multiple exam tips to reemphasize the points that are the most
confusing, overlooked, or frequently answered incorrectly by candidates and that
therefore require special attention for the exam. Figure 4 shows an example.
Notes
All chapters also include multiple notes that draw your attention to points that should
be noted while you’re preparing for the exam. Figure 5 shows an example.
NOTE Although the terms method parameters and method arguments are not the
same, you may have noticed that many programmers use them interchange-
ably. Method parameters are the variables that appear in the definition of a
method. Method arguments are the actual values that are passed to a method
while executing it. In figure 3.15, the variables phNum and msg are method
parameters. If you execute this method as sendMsg("123456", "Hello"),
then the String values "123456" and "Hello" are method arguments. As you
know, you can pass literal values or variables to a method. Thus, method argu-
ments can be literal values or variables.
Sidebars
Sidebars contain information that may not be directly relevant to the exam but that is
related to it. Figure 6 shows an example.
Images
I use a lot of images in the chapters for an immersive learning experience. I believe
that a simple image can help you understand a concept quickly, and a little humor can
help you to retain information longer.
Simple images are used to draw your attention to a particular line of code (as shown
in figure 7).
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
// code to create a new char array and
// replace the desired char with the new char
Figure 7 An example image that draws your attention to a particular line of code
I use pictorial representation of data in arrays (figure 8) and other data types to aid
visualization and understanding.
A
0 0
B
multiStrArr 1 1
null
2
Jan
0
Feb
1
Figure 8 An example pictorial representation
Mar
2 of data in an array
To reinforce important points and help you retain them longer, a little humor has been
added using comic strips (as in figure 9).
Figure 9 An example of a little humor to help you remember that the finally block always executes
Random documents with unrelated
content Scribd suggests to you:
sacred meteor: ‘No more, no more delay from me. I
follow your guidance, and am already in the way by which
you would lead me. Gods of my country! preserve my
house, preserve my grandchild. Yours in this augury—your
shield is stretched over Troy. Yes, my son, I 30
give way, and shrink not from accompanying your flight.’
He said—and by this the blaze is heard louder and louder
through the streets, and the flames roll their hot volumes
nearer. ‘Come then, dear father, take your seat on my
back, my shoulders shall support you, nor shall I feel the 35
task a burden. Fall things as they may, we twain will
share the peril, share the deliverance. Let my little Iulus
walk by my side, while my wife follows our steps at a
distance. You, our servants, attend to what I now say.
As you leave the city there is a mound, where stands an
ancient temple of Ceres all alone, and by it an old cypress,
observed these many years by the reverence of our sires.
This shall be our point of meeting in one place from 5
many quarters. You, my father, take in your hand these
sacred things, our country’s household gods. For me, just
emerged from this mighty war, with the stains of carnage
fresh upon me, it were sacrilege to touch them, till I
have cleansed me in the running stream.’ 10
“It was night and all living things on earth were in the 35
power of sleep, when methought the sacred images of the
gods, the Phrygian household deities, whom I had borne
away with me from Troy, even from the midst of the blazing
town, stood before my eyes as I lay in slumber, clear in
a flood of light, where the full moon was streaming through
the windows of the house. Then they began to address
me thus, and relieve my trouble with words like these:
‘The answer which Apollo has ready to give you when you 5
reach Ortygia, he delivers here, sending us, see, of his own
motion to your very door. We, the followers of you and
your fortune since Dardanland sunk in flame—we,
the comrades of the fleet which you have been guiding over
the swollen main—we it is that will raise to the stars the 10
posterity that shall come after you, and crown your city
with imperial sway. Be it yours to build mighty walls
for mighty dwellers, and not abandon the task of flight for
its tedious length. Change your settlement: it is not this
coast that the Delian god moved you to accept—not in 15
Crete that Apollo bade you sit down. No, there is a
place—the Greeks call it Hesperia[150]—a land old in
story, strong in arms and in the fruitfulness of its soil—the
Œnotrians were its settlers. Now report says that
later generations have called the nation Italian from the 20
name of their leader. That is our true home: thence
sprung Dardanus and father Iasius, the first founder of our
line. Quick! rise, and tell the glad tale, which brooks no
question, to your aged sire; tell him that he is to look for
Corythus[151] and the country of Ausonia. Jupiter bars you 25
from the fields of Dicte.’[152] Thus astonished by visions
and voices of heaven—for sleep it was not: no—methought
I saw them face to face, their wreathed locks and
their features all in full view; and a cold sweat, too,
trickled down my whole frame. I leap from the bed, and 30
direct upturned hand and voice to heaven, and pour on the
hearth the undefiled libation. The sacrifice paid, with
joy I inform Anchises, and expound the whole from first to
last. He owns the double pedigree and the rival ancestors,
and his own new mistake about the two old countries. 35
Then he says: ‘My son, trained in the school of Troy’s
destiny, Cassandra’s was the one voice which used to
chant to me of this chance. Now I recollect, this was the
fortune she presaged as appointed for our line, calling often
for Hesperia, often for the land of Italy. But could anyone
think that Teucrians would ever reach the Hesperian
shore? Could Cassandra’s prophesying in those days gain
any one’s credence? Let us give way to Phœbus, and 5
follow the better course enjoined.’ He said, and with one
consent we gladly obey. So we quit this settlement as we
quitted the last, and leaving a few behind, set sail, and
make our hollow barque fly over the vast world of waters.
“Soon as the ships had gained the mid-sea, and land was 10
no more to be seen, sky on every side, on every side ocean,
then came a murky storm-cloud and stood over my head,
charged with night and winter tempest, and darkness
ruffled the billow’s crest. At once the winds lay the sea in
heaps, and the waters rise mountains high: a scattered 15
fleet, we are tossed upon the vast abyss: clouds enshrouded
the day, and dank night robbed us of the sky, while fire
flashes momently from the bursting clouds. We are
dashed out of our track, and wander blindly over the blind
waters. Nay, even Palinurus owns he cannot tell day 20
from night in a heaven like this, or recollect the footpath
in the watery wilderness. Three dreary suns, blotted by
blinding darkness, we wander on the deep: three nights
with never a star. On the fourth day, at last, land was
first seen to rise, and mountains with curling smoke 25
wreaths to dawn in distant prospect. Down drop the
sails: we rise on our oars: incessantly the crews, straining
every nerve, toss the foam and sweep the blue.