This PDF will be accessible from your Account page after purchase and requires PDF reading software, such as Acrobat® Reader®.
The eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Also available in other formats.
Register your product to gain access to bonus material or receive a coupon.
OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830)
Java SE 17 Developer (Exam 1Z0-829) Programmers Guide
A comprehensive guide to the Java programming language and both the Java SE 21 and Java SE 17 Developer certification exams, this unique tutorial and reference features a rigorous introduction to programming in Java in addition to meticulous coverage of the exam objectives you need to review to pass the 1Z0-830 and 1Z0-829 exams.
Fully updated to reflect changes in the latest exams, it features an increased focus on analyzing code scenarios--not just individual language constructs. Each exam objective is thoroughly addressed, along with the newest features and APIs, and best practices for taking the exam. The only book you need to study for Java SE 21 or Java SE 17 Developer certification, it features
Part I provides comprehensive coverage of new topics that have come into Java 21 since the release of Java 17 and are included in the objectives for the new exam: pattern matching for objects, sequenced collections, and virtual threads. Part II focuses on the objectives for the Java SE 17 Developer Exam. Appendices in both parts of this guide provide the relevant references to chapters where the exam objectives are covered.
Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.
Part I: OCP Java SE 21 Developer (Exam 1Z0-830)
Part I: Contents v
Part I: List of Figures ix
Part I: List of Tables xi
Part I: List of Examples xiii
Preface to OCP Java SE 21 Developer (Exam 1Z0-830) xv
Chapter 1: Pattern Matching for Java Objects 1
1.1 Type Pattern Matching with the instanceof Pattern Match Operator 2
1.2 Type Pattern Matching with the Enhanced switch Construct 11
1.3 Record Patterns 20
1.4 Record Pattern Matching with the instanceof Pattern Match Operator 22
1.5 Record Pattern Matching with the Enhanced switch Construct 26
1.6 Summary of Java Patterns 34
1.7 Using Qualified Name for Enum Constants in the switch Construct 34
1.8 Unexpected Failure in Pattern Matching 35
Review Questions 36
Chapter 2: Sequenced Collections and Map 45
2.1 Collections with Well-Defined Encounter Order 46
2.2 Sequenced Collections 47
2.3 Sequenced Lists 53
2.4 Sequenced Sets 54
2.5 Deques 55
2.6 Sequenced Maps 56
2.7 Unmodifiable Sequenced Collections, Sets, and Maps 62
2.8 Sequenced Concurrent Collections 64
2.9 Sequenced Concurrent Maps 67
Review Questions 69
Chapter 3: Virtual Threads 73
3.1 Motivation for Virtual Threads 74
3.2 Virtual Thread Execution Model 74
3.3 Using Thread Class to Create Virtual Threads 76
3.4 Using Thread Builders to Create Virtual Threads 80
3.5 Using Thread Factory to Create Threads 86
3.6 Using Thread Executor Services 88
3.7 Scalability of Throughput with Virtual Threads 90
3.8 Best Practices for Using Virtual Threads 94
Review Questions 100
Appendix A: Exam Topics: Java SE 21 Developer Professional 107
Appendix B: Annotated Answers to Review Questions 113
Appendix C: Miscellaneous API Updates 121
C.1 The String and StringBuilder Classes 121
C.2 Constructing Locales 125
Part I: Index 127
Part II: OCP Java SE 17 Developer (Exam 1Z0-829)
Part II: Contents at a Glance v
Part II: Contents ix
Part II: List of Figures xix
Part II: List of Tables xxiii
Part II: List of Examples xxvii
Foreword xxxv
Preface to OCP Java SE 17 Developer (Exam 1Z0-829) xxxvii
Chapter 1: Basics of Java Programming 1
1.1 The Java Ecosystem 2
1.2 Classes 5
1.3 Objects 8
1.4 Instance Members 9
1.5 Static Members 10
1.6 Inheritance 13
1.7 Aggregation 16
Review Questions 17
1.8 Sample Java Program 19
1.9 Program Output 23
Review Questions 26
Chapter 2: Basic Elements, Primitive Data Types, and Operators 29
2.1 Basic Language Elements 30
2.2 Primitive Data Types 41
2.3 Conversions 43
2.4 Type Conversion Contexts 46
2.5 Precedence and Associativity Rules for Operators 50
2.6 Evaluation Order of Operands 52
2.7 The Simple Assignment Operator = 54
2.8 Arithmetic Operators: *, /, %, +, - 58
2.9 The Binary String Concatenation Operator + 67
2.10 Variable Increment and Decrement Operators: ++, -- 69
Review Questions 71
2.11 Boolean Expressions 74
2.12 Relational Operators: <, <=, >, >= 74
2.13 Equality 75
2.14 Boolean Logical Operators: !, ^, &, | 78
2.15 Conditional Operators: &&, || 80
2.16 Integer Bitwise Operators: ~, &, |, ^ 82
2.17 Shift Operators: <<, >>, >>> 86
2.18 The Conditional Operator ?: 90
2.19 Other Operators: new, [], instanceof, -> 92
Review Questions 93
Chapter 3: Declarations 97
3.1 Class Declarations 99
3.2 Method Declarations 100
3.3 Statements 101
3.4 Variable Declarations 102
3.5 Instance Methods and the Object Reference this 106
3.6 Method Overloading 108
3.7 Constructors 109
3.8 Static Member Declarations 112
Review Questions 115
3.9 Arrays 117
3.10 Parameter Passing 127
3.11 Variable Arity Methods 136
3.12 The main() Method 141
3.13 Local Variable Type Inference 142
Review Questions 147
Chapter 4: Control Flow 151
4.1 Selection Statements 152
4.2 The switch Statement 155
4.3 The switch Expression 164
Review Questions 170
4.4 Iteration Statements 172
4.5 The while Statement 172
4.6 The do-while Statement 173
4.7 The for(;;) Statement 174
4.8 The for(:) Statement 176
4.9 Transfer Statements 179
4.10 Labeled Statements 179
4.11 The break Statement 180
4.12 The continue Statement 182
4.13 The return Statement 184
Review Questions 185
Chapter 5: Object-Oriented Programming 189
5.1 Implementing Inheritance 191
5.2 The Object Reference super 206
5.3 Chaining Constructors Using this() and super() 209
Review Questions 215
5.4 Abstract Classes and Methods 218
5.5 Final Declarations 225
Review Questions 234
5.6 Interfaces 237
Review Questions 257
5.7 Arrays and Subtyping 259
5.8 Reference Values and Conversions 261
5.9 Reference Value Assignment Conversions 261
5.10 Method Invocation Conversions Involving References 265
5.11 Reference Casting and the instanceof Operator 269
5.12 Polymorphism 278
Review Questions 283
5.13 Enum Types 287
5.14 Record Classes 299
5.15 Sealed Classes and Interfaces 311
Review Questions 318
Chapter 6: Access Control 323
6.1 Design Principle: Encapsulation 324
6.2 Java Source File Structure 325
6.3 Packages 326
6.4 Searching for Classes on the Class Path 337
Review Questions 341
6.5 Access Modifiers 345
6.6 Scope Rules 352
6.7 Implementing Immutability 356
Review Questions 360
Chapter 7: Exception Handling 363
7.1 Stack-Based Execution and Exception Propagation 365
7.2 Exception Types 368
7.3 Exception Handling: try, catch, and finally 375
7.4 The throw Statement 386
7.5 The throws Clause 388
Review Questions 392
7.6 The Multi-catch Clause 397
7.7 The try-with-resources Statement 407
7.8 Advantages of Exception Handling 416
Review Questions 417
Chapter 8: Selected API Classes 423
8.1 Overview of the java.lang Package 425
8.2 The Object Class 425
8.3 The Wrapper Classes 429
Review Questions 438
8.4 The String Class 439
8.5 The StringBuilder Class 464
Review Questions 471
8.6 The Math Class 478
8.7 The Random Class 482
8.8 Using Big Numbers 484
Review Questions 487
Chapter 9: Nested Type Declarations 489
9.1 Overview of Nested Type Declarations 491
9.2 Static Member Types 495
9.3 Non-Static Member Classes 501
Review Questions 510
9.4 Local Classes 512
9.5 Static Local Types 519
9.6 Anonymous Classes 521
Review Questions 527
Chapter 10: Object Lifetime 531
10.1 Garbage Collection 533
10.2 Reachable Objects 533
10.3 Facilitating Garbage Collection 536
10.4 Invoking Garbage Collection Programmatically 537
Review Questions 538
10.5 Initializers 540
10.6 Field Initializer Expressions 540
10.7 Static Initializer Blocks 545
10.8 Instance Initializer Blocks 551
10.9 Constructing Initial Object State 555
Review Questions 558
Chapter 11: Generics 563
11.1 Introducing Generics 565
11.2 Generic Types and Parameterized Types 567
11.3 Collections and Generics 578
11.4 Wildcards 579
11.5 Using References of Wildcard Parameterized Types 584
11.6 Bounded Type Parameters 591
11.7 Generic Methods and Constructors 593
11.8 Implementing a Simplified Generic Stack 598
Review Questions 600
11.9 Wildcard Capture 604
11.10 Flexibility with Wildcard Parameterized Types 607
11.11 Type Erasure 613
11.12 Implications for Overloading and Overriding 615
11.13 Limitations and Restrictions on Generic Types 623
Review Questions 636
Chapter 12: Collections, Part I: ArrayList<E> 643
12.1 Lists 644
12.2 Declaring References and Constructing ArrayLists 646
12.3 Modifying an ArrayList<E> 651
12.4 Querying an ArrayList<E> 655
12.5 Iterating Over an ArrayList<E> 657
12.6 Converting an ArrayList<E> to an Array 658
12.7 Creating List Views 659
12.8 Arrays versus ArrayLists 662
Review Questions 667
Chapter 13: Functional-Style Programming 673
13.1 Functional Interfaces 675
13.2 Lambda Expressions 679
13.3 Lambda Expressions and Anonymous Classes 688
Review Questions 693
13.4 Overview of Built-In Functional Interfaces 695
13.5 Suppliers 699
13.6 Predicates 703
13.7 Consumers 709
13.8 Functions 712
13.9 Two-Arity Specialization of Function<T, R>: BiFunction<T, U, R> 717
13.10 Extending Function<T,T>: UnaryOperator<T> 720
13.11 Extending BiFunction<T,T,T>: BinaryOperator<T> 721
13.12 Currying Functions 723
13.13 Method and Constructor References 724
13.14 Contexts for Defining Lambda Expressions 733
Review Questions 735
Chapter 14: Object Comparison 741
14.1 The Objects Class 743
14.2 Implementing the equals() Method 744
14.3 Implementing the hashCode() Method 753
14.4 Implementing the java.lang.Comparable<E> Interface 761
14.5 Implementing the java.util.Comparator<E> Interface 769
Review Questions 774
Chapter 15: Collections: Part II 781
15.1 The Java Collections Framework 783
15.2 Collections 790
15.3 Lists 801
15.4 Sets 804
15.5 Sorted Sets and Navigable Sets 810
15.6 Queues 814
15.7 Deques 821
Review Questions 826
15.8 Maps 830
15.9 Map Implementations 840
15.10 Sorted Maps and Navigable Maps 845
Review Questions 851
15.11 The Collections Class 856
15.12 The Arrays Class 864
Review Questions 874
Chapter 16: Streams 879
16.1 Introduction to Streams 881
16.2 Running Example: The CD Record Class 882
16.3 Stream Basics 884
16.4 Building Streams 890
16.5 Intermediate Stream Operations 905
16.6 The Optional Class 940
16.7 Terminal Stream Operations 946
16.8 Collectors 978
16.9 Parallel Streams 1009
Review Questions 1016
Chapter 17: Date and Time 1025
17.1 Date and Time API Overview 1026
17.2 Working with Dates and Times 1029
17.3 Using Temporal Units and Temporal Fields 1046
17.4 Working with Instants 1051
17.5 Working with Periods 1059
17.6 Working with Durations 1066
17.7 Working with Time Zones and Daylight Savings 1074
17.8 Converting Date and Time Values to Legacy Date 1090
Review Questions 1091
Chapter 18: Localization 1097
18.1 Using Locales 1098
18.2 Properties Files 1102
18.3 Bundling Resources 1104
Review Questions 1114
18.4 Core API for Formatting and Parsing of Values 1117
18.5 Formatting and Parsing Number, Currency, and Percentage Values 1118
18.6 Formatting and Parsing Date and Time 1129
18.7 Formatting and Parsing Messages 1141
Review Questions 1155
Chapter 19: Java Module System 1163
19.1 Making the Case for Modules 1165
19.2 The Modular JDK 1166
19.3 Module Basics 1170
19.4 Overview of Module Directives 1179
19.5 Creating a Modular Application 1181
19.6 Compiling and Running a Modular Application 1188
19.7 Creating JAR Files 1191
19.8 Open Modules and the opens Directive 1193
19.9 Services 1198
19.10 Creating Runtime Images 1206
19.11 Categories of Modules 1207
19.12 Migrating to Modules 1211
19.13 Exploring Modules 1213
19.14 Summary of Selected Operations with the JDK Tools 1220
Review Questions 1225
Chapter 20: Java I/O: Part I 1233
20.1 Input and Output 1235
20.2 Byte Streams: Input Streams and Output Streams 1236
20.3 Character Streams: Readers and Writers 1243
20.4 The Console Class 1258
Review Questions 1261
20.5 Object Serialization 1263
Review Questions 1279
Chapter 21: Java I/O: Part II 1287
21.1 Characteristics of a Hierarchical File System 1289
21.2 Creating Path Objects 1291
21.3 Working with Path Objects 1296
21.4 Operations on Directory Entries 1306
21.5 Reading and Writing Files Using Paths 1316
21.6 Managing File Attributes 1323
21.7 Creating Directory Entries 1341
21.8 Stream Operations on Directory Entries 1347
Review Questions 1357
Chapter 22: Concurrency: Part I 1367
22.1 Threads and Concurrency 1369
22.2 Runtime Organization for Thread Execution 1371
22.3 Creating Threads 1372
Review Questions 1380
22.4 Thread Lifecycle 1382
22.5 Thread Issues 1410
Review Questions 1417
Chapter 23: Concurrency: Part II 1421
23.1 Utility Classes TimeUnit and ThreadLocalRandom 1423
23.2 The Executor Framework 1425
23.3 The Fork/Join Framework 1449
23.4 Writing Thread-Safe Code 1453
23.5 Special-Purpose Synchronizers 1472
23.6 Synchronized Collections and Maps 1479
23.7 Concurrent Collections and Maps 1484
Review Questions 1506
Chapter 24: Database Connectivity 1513
24.1 Introduction to Relational Databases 1514
24.2 Introduction to JDBC 1519
24.3 Establishing a Database Connection 1521
24.4 Creating and Executing SQL Statements 1524
24.5 Processing Query Results 1535
24.6 Customizing Result Sets 1541
24.7 Discovering Database and ResultSet Metadata 1545
24.8 Implementing Transaction Control 1547
Review Questions 1550
Chapter 25: Annotations 1557
25.1 Basics of Annotations 1559
25.2 Declaring Annotation Types 1560
25.3 Applying Annotations 1565
25.4 Meta-Annotations 1569
25.5 Selected Standard Annotations 1579
25.6 Processing Annotations 1589
Review Questions 1595
Chapter 26: Secure Coding 1601
26.1 Application Security Overview 1602
26.2 Security Threat Categories 1604
26.3 Java Security Policies 1610
26.4 Additional Security Guidelines 1612
Review Questions 1613
Appendix A: Taking the Java SE 17 and Java SE 11 Developer Exams 1617
A.1 Preparing for the Exam 1617
A.2 Registering for the Online Proctored Exam 1618
A.3 How the Online Proctored Exam Is Conducted 1620
A.4 The Questions 1621
Appendix B: Exam Topics: Java SE 17 Developer 1625
Appendix C: Exam Topics: Java SE 11 Developer 1631
Appendix D: Annotated Answers to Review Questions 1637
Appendix E: Mock Exam: Java SE 17 Developer 1713
Appendix F: Annotated Answers to Mock Exam 1741
Appendix G: Java Logging API Overview 1751
G.1 Purpose of the Logging API 1751
G.2 Configuring Logging 1752
G.3 Writing Log Messages 1753
G.4 Applying Guarded Logging 1755
G.5 Summary 1755
Part II: Index 1757