Table of Contents
Introduction xxi
The Assessment Test xliii
Chapter 1 Welcome to Java 1
Learning About the Java Environment 2
Major Components of Java 2
Downloading a JDK 3
Identifying Benefits of Java 4
Understanding the Java Class Structure 5
Fields and Methods 5
Comments 6
Classes vs. Files 8
Writing a main() Method 8
Creating a main() Method 9
Passing Parameters to a Java Program 10
Running a Program in One Line 12
Understanding Package Declarations and Imports 13
Wildcards 15
Redundant Imports 15
Naming Conflicts 16
Creating a New Package 18
Compiling and Running Code with Packages 19
Using an Alternate Directory 20
Compiling with JAR Files 22
Creating a JAR File 23
Running a Program in One Line with Packages 24
Ordering Elements in a Class 24
Code Formatting on the Exam 26
Summary 27
Exam Essentials 27
Review Questions 29
Chapter 2 Java Building Blocks 35
Creating Objects 36
Calling Constructors 36
Reading and Writing Member Fields 37
Executing Instance Initializer Blocks 38
Following Order of Initialization 39
Understanding Data Types 40
Using Primitive Types 40
Using Reference Types 44
Distinguishing between Primitives and Reference Types 45
Declaring Variables 46
Identifying Identifiers 46
Declaring Multiple Variables 49
Initializing Variables 50
Creating Local Variables 51
Passing Constructor and Method Parameters 52
Defining Instance and Class Variables 53
Introducing var 53
Managing Variable Scope 59
Limiting Scope 59
Nesting Scope 60
Tracing Scope 60
Applying Scope to Classes 61
Reviewing Scope 62
Destroying Objects 62
Understanding Garbage Collection 63
Tracing Eligibility 64
Summary 67
Exam Essentials 67
Review Questions 69
Chapter 3 Operators 79
Understanding Java Operators 80
Types of Operators 80
Operator Precedence 81
Applying Unary Operators 82
Logical Complement and Negation Operators 83
Increment and Decrement Operators 84
Working with Binary Arithmetic Operators 85
Arithmetic Operators 86
Numeric Promotion 88
Assigning Values 90
Assignment Operator 90
Casting Values 90
Compound Assignment Operators 93
Assignment Operator Return Value 94
Comparing Values 95
Equality Operators 95
Relational Operators 97
Logical Operators 99
Short-Circuit Operators 100
Making Decisions with the Ternary Operator 102
Summary 103
Exam Essentials 104
Review Questions 105
Chapter 4 Making Decisions 113
Creating Decision-Making Statements 114
Statements and Blocks 114
The if Statement 115
The else Statement 116
The switch Statement 119
Writing while Loops 126
The while Statement 126
The do/while Statement 127
Comparing while and do/while Loops 128
Infinite Loops 129
Constructing for Loops 130
The for Loop 131
The for-each Loop 136
Controlling Flow with Branching 141
Nested Loops 141
Adding Optional Labels 142
The break Statement 143
The continue Statement 145
The return Statement 147
Unreachable Code 148
Reviewing Branching 148
Summary 149
Exam Essentials 150
Review Questions 151
Chapter 5 Core Java APIs 161
Creating and Manipulating Strings 162
Concatenation 163
Immutability 164
Important String Methods 165
Method Chaining 171
Using the StringBuilder Class 172
Mutability and Chaining 173
Creating a StringBuilder 174
Important StringBuilder Methods 174
Understanding Equality 177
Comparing equals() and == 177
The String Pool 179
Understanding Java Arrays 180
Creating an Array of Primitives 181
Creating an Array with Reference Variables 183
Using an Array 185
Sorting 186
Searching 187
Comparing 188
Varargs 190
Multidimensional Arrays 190
Understanding an ArrayList 193
Creating an ArrayList 193
Using an ArrayList 195
Wrapper Classes 199
Autoboxing and Unboxing 201
Converting Between array and List 201
Using Varargs to Create a List 203
Sorting 204
Creating Sets and Maps 204
Introducing Sets 204
Introducing Maps 205
Calculating with Math APIs 206
min() and max() 206
round() 207
pow() 207
random() 208
Summary 208
Exam Essentials 209
Review Questions 210
Chapter 6 Lambdas and Functional Interfaces 221
Writing Simple Lambdas 222
Lambda Example 222
Lambda Syntax 225
Introducing Functional Interfaces 227
Predicate 228
Consumer 228
Supplier 229
Comparator 230
Working with Variables in Lambdas 231
Parameter List 231
Local Variables inside the Lambda Body 231
Variables Referenced from the Lambda Body 232
Calling APIs with Lambdas 234
removeIf() 234
sort() 235
forEach() 235
Summary 236
Exam Essentials 237
Review Questions 238
Chapter 7 Methods and Encapsulation 247
Designing Methods 248
Access Modifiers 249
Optional Specifiers 250
Return Type 251
Method Name 252
Parameter List 253
Optional Exception List 253
Method Body 254
Working with Varargs 254
Applying Access Modifiers 256
Private Access 256
Default (Package-Private) Access 257
Protected Access 259
Public Access 263
Applying the static Keyword 264
Designing static Methods and Fields 264
Accessing a static Variable or Method 265
Static vs. Instance 266
static Variables 268
Static Initialization 269
Static Imports 270
Passing Data among Methods 272
Overloading Methods 275
Varargs 277
Autoboxing 277
Reference Types 277
Primitives 279
Generics 279
Arrays 280
Putting It All Together 280
Encapsulating Data 281
Summary 283
Exam Essentials 284
Review Questions 285
Chapter 8 Class Design 295
Understanding Inheritance 296
Single vs. Multiple Inheritance 297
Inheriting Object 298
Creating Classes 299
Extending a Class 299
Applying Class Access Modifiers 301
Accessing the this Reference 302
Calling the super Reference 303
Declaring Constructors 305
Creating a Constructor 305
Default Constructor 306
Calling Overloaded Constructors with this() 308
Calling Parent Constructors with super() 310
Constructors and final Fields 314
Order of Initialization 316
Reviewing Constructor Rules 322
Inheriting Members 323
Calling Inherited Members 323
Inheriting Methods 324
Hiding Variables 336
Understanding Polymorphism 337
Object vs. Reference 339
Casting Objects 340
The instanceof Operator 341
Polymorphism and Method Overriding 342
Overriding vs. Hiding Members 344
Summary 346
Exam Essentials 347
Review Questions 349
Chapter 9 Advanced Class Design 363
Creating Abstract Classes 364
Introducing Abstract Classes 364
Defining Abstract Methods 366
Creating a Concrete Class 370
Reviewing Abstract Class Rules 372
Implementing Interfaces 373
Defining an Interface 373
Inserting Implicit Modifiers 377
Inheriting an Interface 380
Polymorphism and Interfaces 384
Reviewing Interface Rules 386
Introducing Inner Classes 388
Defining a Member Inner Class 388
Using a Member Inner Class 389
Summary 390
Exam Essentials 391
Review Questions 392
Chapter 10 Exceptions 401
Understanding Exceptions 402
The Role of Exceptions 402
Understanding Exception Types 404
Throwing an Exception 407
Recognizing Exception Classes 409
RuntimeException Classes 409
Checked Exception Classes 412
Error Classes 413
Handling Exceptions 414
Using try and catch Statements 414
Chaining catch Blocks 416
Applying a Multi-catch Block 418
Adding a finally Block 421
Finally Closing Resources 424
Throwing Additional Exceptions 430
Calling Methods That Throw Exceptions 432
Declaring and Overriding Methods with Exceptions 434
Printing an Exception 435
Summary 437
Exam Essentials 438
Review Questions 440
Chapter 11 Modules 451
Introducing Modules 452
Exploring a Module 453
Benefits of Modules 454
Creating and Running a Modular Program 456
Creating the Files 457
Compiling Our First Module 458
Running Our First Module 460
Packaging Our First Module 461
Updating Our Example for Multiple Modules 463
Updating the Feeding Module 463
Creating a Care Module 464
Creating the Talks Module 466
Creating the Staff Module 469
Diving into the module-info File 470
exports 470
requires transitive 472
provides, uses, and opens 474
Discovering Modules 475
The java Command 475
The jar Command 478
The jdeps Command 478
The jmod Command 480
Reviewing Command-Line Options 481
Summary 483
Exam Essentials 484
Review Questions 485
Appendix Answers to Review Questions 491
Chapter 1: Welcome to Java 492
Chapter 2: Java Building Blocks 494
Chapter 3: Operators 498
Chapter 4: Making Decisions 501
Chapter 5: Core Java APIs 505
Chapter 6: Lambdas and Functional Interfaces 509
Chapter 7: Methods and Encapsulation 511
Chapter 8: Class Design 513
Chapter 9: Advanced Class Design 518
Chapter 10: Exceptions 522
Chapter 11: Modules 526
Index 529