Preface |
|
xvii | |
Acknowledgments |
|
xix | |
About this book |
|
xxi | |
About the author |
|
xxix | |
About the cover illustration |
|
xxx | |
Introduction |
|
1 | (21) |
|
|
2 | (1) |
|
2 Introduction to OCA Java SE 8 Programmer I Certification |
|
|
2 | (1) |
|
3 The importance of OCA Java SE 8 Programmer I Certification |
|
|
2 | (2) |
|
4 Comparing OCA Java exam versions |
|
|
4 | (4) |
|
5 Next step: OCP Java SE 8 Programmer II (1Z0-809) exam |
|
|
8 | (1) |
|
6 Complete exam objectives, mapped to book chapters, and readiness checklist |
|
|
8 | (2) |
|
|
10 | (10) |
|
|
10 | (8) |
|
|
18 | (2) |
|
8 The testing engine used in the exam |
|
|
20 | (2) |
|
|
22 | (70) |
|
1.1 The structures of a Java class and a source code file |
|
|
23 | (13) |
|
Structure of a Java class |
|
|
24 | (8) |
|
Structure and components of a Java source code file |
|
|
32 | (4) |
|
1.2 Executable Java applications |
|
|
36 | (5) |
|
Executable Java classes versus non-executable Java classes |
|
|
36 | (1) |
|
|
37 | (2) |
|
Run a Java program from the command line |
|
|
39 | (2) |
|
|
41 | (10) |
|
|
42 | (1) |
|
Defining classes in a package using the package statement |
|
|
42 | (3) |
|
Using simple names with import statements |
|
|
45 | (2) |
|
Using packaged classes without using the import statement |
|
|
47 | (1) |
|
Importing a single member versus all members of a package |
|
|
48 | (1) |
|
The import statement doesn't import the whole package tree |
|
|
49 | (1) |
|
Importing classes from the default package |
|
|
50 | (1) |
|
|
50 | (1) |
|
1.4 Java access modifiers |
|
|
51 | (13) |
|
|
52 | (1) |
|
|
53 | (1) |
|
Protected access modifier |
|
|
54 | (3) |
|
Default access (package access) |
|
|
57 | (4) |
|
|
61 | (1) |
|
Access modifiers and Java entities |
|
|
62 | (2) |
|
|
64 | (8) |
|
|
65 | (1) |
|
|
66 | (1) |
|
|
67 | (5) |
|
1.6 Features and components of Java |
|
|
72 | (2) |
|
Valid features and components of Java |
|
|
72 | (2) |
|
Irrelevant features and components of Java |
|
|
74 | (1) |
|
|
74 | (1) |
|
|
75 | (4) |
|
1.9 Sample exam questions |
|
|
79 | (5) |
|
1.10 Answers to sample exam questions |
|
|
84 | (8) |
|
2 Working with Java data types |
|
|
92 | (55) |
|
|
93 | (12) |
|
|
95 | (1) |
|
|
96 | (6) |
|
Category: character (unsigned integer) |
|
|
102 | (2) |
|
Confusion with the names of the primitive data types |
|
|
104 | (1) |
|
|
105 | (1) |
|
Valid and invalid identifiers |
|
|
105 | (1) |
|
2.3 Object reference variables |
|
|
106 | (5) |
|
What are object reference variables'? |
|
|
107 | (2) |
|
Differentiating between object reference variables and primitive variables |
|
|
109 | (2) |
|
|
111 | (14) |
|
|
112 | (3) |
|
|
115 | (4) |
|
|
119 | (2) |
|
|
121 | (2) |
|
|
123 | (2) |
|
|
125 | (7) |
|
Class hierarchy of wrapper classes |
|
|
125 | (1) |
|
Creating objects of the wrapper classes |
|
|
125 | (1) |
|
Retrieving primitive values from the wrapper classes |
|
|
126 | (1) |
|
Parsing a string value to a primitive type |
|
|
127 | (1) |
|
Difference between using the value of method and constructors of wrapper classes |
|
|
128 | (1) |
|
Comparing objects of wrapper classes |
|
|
128 | (2) |
|
|
130 | (2) |
|
|
132 | (1) |
|
|
132 | (4) |
|
2.8 Sample exam questions |
|
|
136 | (4) |
|
2.9 Answers to sample exam questions |
|
|
140 | (7) |
|
3 Methods and encapsulation |
|
|
147 | (74) |
|
|
149 | (9) |
|
|
149 | (2) |
|
|
151 | (1) |
|
|
152 | (1) |
|
|
153 | (2) |
|
Overlapping variable scopes |
|
|
155 | (3) |
|
|
158 | (8) |
|
|
159 | (1) |
|
|
160 | (1) |
|
|
161 | (2) |
|
|
163 | (3) |
|
3.3 Create methods with arguments and return values |
|
|
166 | (8) |
|
|
168 | (1) |
|
|
169 | (3) |
|
|
172 | (2) |
|
3.4 Create an overloaded method |
|
|
174 | (4) |
|
|
175 | (2) |
|
|
177 | (1) |
|
|
177 | (1) |
|
3.5 Constructors of a class |
|
|
178 | (10) |
|
User-defined constructors |
|
|
178 | (5) |
|
|
183 | (2) |
|
|
185 | (3) |
|
3.6 Accessing object fields |
|
|
188 | (6) |
|
|
188 | (1) |
|
Read and write object fields |
|
|
189 | (3) |
|
Calling methods on objects |
|
|
192 | (2) |
|
3.7 Apply encapsulation principles to a class |
|
|
194 | (3) |
|
|
195 | (1) |
|
|
195 | (2) |
|
3.8 Passing objects and primitives to methods |
|
|
197 | (5) |
|
Passing primitives to methods |
|
|
198 | (1) |
|
Passing object references to methods |
|
|
199 | (3) |
|
|
202 | (1) |
|
|
203 | (4) |
|
3.11 Sample exam questions |
|
|
207 | (5) |
|
3.12 Answers to sample exam questions |
|
|
212 | (9) |
|
4 Selected classes from the Java API and arrays |
|
|
221 | (101) |
|
4.1 Welcome to the world of the String class |
|
|
223 | (16) |
|
|
223 | (4) |
|
The class String is immutable |
|
|
227 | (3) |
|
Methods of the class String |
|
|
230 | (5) |
|
String objects and operators |
|
|
235 | (1) |
|
Determining equality of Strings |
|
|
236 | (3) |
|
4.2 Mutable strings: StringBuilder |
|
|
239 | (8) |
|
The StringBuilder class is mutable |
|
|
239 | (1) |
|
Creating StringBuilder objects |
|
|
240 | (1) |
|
Methods of class StringBuilder |
|
|
241 | (6) |
|
A quick note on the class StringBuffer |
|
|
247 | (1) |
|
|
247 | (11) |
|
|
248 | (1) |
|
|
249 | (1) |
|
|
250 | (2) |
|
|
252 | (2) |
|
Combining array declaration, allocation, and initialization |
|
|
254 | (1) |
|
Asymmetrical multidimensional arrays |
|
|
255 | (1) |
|
Arrays of type interface, abstract class, and class Object |
|
|
256 | (2) |
|
|
258 | (1) |
|
|
258 | (15) |
|
|
259 | (2) |
|
Adding elements to an ArrayList |
|
|
261 | (2) |
|
Accessing elements of an ArrayList |
|
|
263 | (2) |
|
Modifying the elements of an ArrayList |
|
|
265 | (1) |
|
Deleting the elements of an ArrayList |
|
|
266 | (1) |
|
Other methods of ArrayList |
|
|
267 | (6) |
|
4.5 Comparing objects for equality |
|
|
273 | (5) |
|
The method equals in the class Java.lang.Object |
|
|
273 | (1) |
|
Comparing objects of a user-defined class |
|
|
273 | (2) |
|
Incorrect method signature of the equals method |
|
|
275 | (1) |
|
Contract of the equals method |
|
|
276 | (2) |
|
4.6 Working with calendar data |
|
|
278 | (19) |
|
|
279 | (3) |
|
|
282 | (3) |
|
|
285 | (1) |
|
|
286 | (5) |
|
|
291 | (6) |
|
|
297 | (2) |
|
|
299 | (10) |
|
4.9 Sample exam questions |
|
|
309 | (4) |
|
4.10 Answers to sample exam questions |
|
|
313 | (9) |
|
|
322 | (62) |
|
5.1 The if, if-else, and ternary constructs |
|
|
324 | (14) |
|
The if construct audits flavors |
|
|
324 | (4) |
|
|
328 | (1) |
|
Implications of the presence and absence of {} in if-else constructs |
|
|
328 | (3) |
|
Appropriate versus inappropriate expressions passed as arguments to an if statement |
|
|
331 | (1) |
|
|
332 | (2) |
|
|
334 | (4) |
|
|
338 | (8) |
|
Create and use a switch statement |
|
|
339 | (1) |
|
Comparing a switch statement with multiple if-else constructs |
|
|
339 | (2) |
|
Arguments passed to a switch statement |
|
|
341 | (2) |
|
Values passed to the label case of a switch statement |
|
|
343 | (2) |
|
Use of break statements within a switch statement |
|
|
345 | (1) |
|
|
346 | (6) |
|
|
348 | (1) |
|
|
349 | (1) |
|
|
349 | (1) |
|
Optional parts of a for statement |
|
|
350 | (1) |
|
|
351 | (1) |
|
5.4 The enhanced for loop |
|
|
352 | (6) |
|
Iteration with enhanced for loop |
|
|
352 | (3) |
|
Limitations of the enhanced for loop |
|
|
355 | (1) |
|
|
356 | (2) |
|
5.5 The while and do-while loops |
|
|
358 | (4) |
|
|
358 | (2) |
|
|
360 | (2) |
|
While and do-while block, expression, and nesting rules |
|
|
362 | (1) |
|
5.6 Comparing loop constructs |
|
|
362 | (2) |
|
Comparing do-while and while loops |
|
|
362 | (1) |
|
Comparing for and enhanced for loops |
|
|
363 | (1) |
|
Comparing for and while loops |
|
|
364 | (1) |
|
5.7 Loop statements: break and continue |
|
|
364 | (4) |
|
|
364 | (2) |
|
|
366 | (1) |
|
|
367 | (1) |
|
|
368 | (1) |
|
|
369 | (3) |
|
5.10 Sample exam questions |
|
|
372 | (5) |
|
5.11 Answers to sample exam questions |
|
|
377 | (7) |
|
6 Working with inheritance |
|
|
384 | (85) |
|
6.1 Inheritance with classes |
|
|
385 | (9) |
|
The need to inherit classes |
|
|
385 | (2) |
|
|
387 | (3) |
|
A derived class contains within it an object of its base class |
|
|
390 | (1) |
|
Which base class members are inherited by a derived class? |
|
|
391 | (1) |
|
Which base class members aren't inherited by a derived class? |
|
|
391 | (1) |
|
Derived classes can define additional properties and behaviors |
|
|
391 | (1) |
|
Abstract base class versus concrete base class |
|
|
392 | (2) |
|
|
394 | (24) |
|
Need for using interfaces |
|
|
396 | (2) |
|
|
398 | (3) |
|
Types of methods in an interface |
|
|
401 | (4) |
|
Implementing a single interface |
|
|
405 | (2) |
|
A class can't extend multiple classes |
|
|
407 | (1) |
|
A class can implement multiple interfaces |
|
|
408 | (3) |
|
|
411 | (3) |
|
Modifying existing methods of an interface |
|
|
414 | (3) |
|
Properties of members of an interface |
|
|
417 | (1) |
|
6.3 Reference variable and object types |
|
|
418 | (6) |
|
Using a variable of the derived class to access its own object |
|
|
418 | (1) |
|
Using a variable of a superclass to access an object of a derived class |
|
|
419 | (1) |
|
Using a variable of an implemented interface to access a derived class object |
|
|
420 | (1) |
|
The need for accessing an object using the variables of its base class or implemented interfaces |
|
|
421 | (3) |
|
|
424 | (3) |
|
How to cast a variable to another type |
|
|
424 | (2) |
|
|
426 | (1) |
|
6.5 Use this and super to access objects and constructors |
|
|
427 | (7) |
|
|
427 | (3) |
|
|
430 | (4) |
|
|
434 | (12) |
|
Polymorphism with classes |
|
|
434 | (5) |
|
Binding of variables and methods at compile time and runtime |
|
|
439 | (2) |
|
Polymorphism with interfaces |
|
|
441 | (5) |
|
6.7 Simple lambda expressions |
|
|
446 | (6) |
|
Comparing passing values with passing code to methods |
|
|
446 | (3) |
|
Syntax of lambda expressions |
|
|
449 | (1) |
|
|
450 | (2) |
|
|
452 | (1) |
|
|
453 | (3) |
|
6.10 Sample exam questions |
|
|
456 | (5) |
|
6.11 Answers to sample exam questions |
|
|
461 | (8) |
|
|
469 | (70) |
|
|
470 | (5) |
|
|
470 | (3) |
|
Why handle exceptions separately? |
|
|
473 | (1) |
|
Does exception handling offer any other benefits? |
|
|
474 | (1) |
|
7.2 Categories of exceptions |
|
|
475 | (4) |
|
Identifying exception categories |
|
|
476 | (1) |
|
Class hierarchy of exception classes |
|
|
476 | (1) |
|
|
477 | (1) |
|
|
478 | (1) |
|
|
478 | (1) |
|
7.3 Creating a method that throws an exception |
|
|
479 | (4) |
|
Create a method that throws a checked exception |
|
|
480 | (1) |
|
|
481 | (1) |
|
Creating a method that throws runtime exceptions or errors |
|
|
481 | (1) |
|
A method can declare to throw all types of exceptions, even if it doesn't |
|
|
482 | (1) |
|
7.4 What happens when an exception is thrown? |
|
|
483 | (20) |
|
Creating try-catch-finally blocks |
|
|
485 | (5) |
|
Using a method that throws a checked exception |
|
|
490 | (1) |
|
Using a method that throws a runtime exception |
|
|
491 | (2) |
|
Using a method that throws an error |
|
|
493 | (1) |
|
Will a finally block execute even if the catch block defines a return statement? |
|
|
493 | (1) |
|
What happens if both a catch and a finally block define return statements? |
|
|
494 | (1) |
|
What happens if a finally block modifies the value returned from a catch block? |
|
|
495 | (1) |
|
Can a try block be followed only by a finally block? |
|
|
496 | (1) |
|
Does the order of the exceptions caught in the catch blocks matter? |
|
|
497 | (2) |
|
Can I rethrow an exception or the error I catch? |
|
|
499 | (1) |
|
Can I declare my methods to throw a checked exception instead of handling it? |
|
|
500 | (1) |
|
I can create nested hops, so can I create nested try-catch blocks too? |
|
|
500 | (2) |
|
|
502 | (1) |
|
7.5 Common exception classes and categories |
|
|
503 | (17) |
|
ArrayIndexOutOfBoundsException and IndexOutOfBoundsException |
|
|
504 | (1) |
|
|
505 | (2) |
|
|
507 | (1) |
|
|
508 | (3) |
|
|
511 | (3) |
|
|
514 | (2) |
|
ExceptionInInitializerError |
|
|
516 | (2) |
|
|
518 | (1) |
|
|
519 | (1) |
|
|
519 | (1) |
|
|
520 | (1) |
|
|
520 | (6) |
|
7.8 Sample exam questions |
|
|
526 | (4) |
|
7.9 Answers to sample exam questions |
|
|
530 | (9) |
|
|
539 | (102) |
|
|
539 | (35) |
|
8.2 Answers to mock exam questions |
|
|
574 | (67) |
Appendix Answers to Twist in the Tale exercises |
|
641 | (18) |
Index |
|
659 | |