OCP Oracle Certified Professional Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide (Khalid A. Mughal Vasily A. Strelnikov) (Z-Library)
JavaAuthor:Khalid A. Mughal & Vasily A. Strelnikov
No description
Tags
Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
OCP Oracle Certified Professional Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide
Page
3
This page intentionally left blank
Page
4
OCP Oracle Certified Professional Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide Khalid A. Mughal Vasily A. Strelnikov Boston • Columbus • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo
Page
5
Cover image: your/Shutterstock Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital let- ters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omis- sions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The views expressed in this book are those of the author and do not necessarily reflect the views of Oracle. Oracle America Inc. does not make any representations or warranties as to the accuracy, adequacy or completeness of any information contained in this work, and is not responsible for any errors or omissions. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419. For government sales inquiries, please contact governmentsales@pearsoned.com. For questions about sales outside the U.S., please contact intlcs@pearson.com. Visit us on the Web: informit.com Library of Congress Control Number: 2022951639 Copyright © 2023 Pearson Education, Inc. All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval sys- tem, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appro- priate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearson.com/permissions. ISBN-13: 978-0-13-799364-2 ISBN-10: 0-13-799364-1 ScoutAutomatedPrintCode
Page
6
Pearson’s Commitment to Diversity, Equity, and Inclusion Pearson is dedicated to creating bias-free content that reflects the diversity of all learners. We embrace the many dimensions of diversity, including but not limited to race, ethnicity, gender, socioeconomic status, ability, age, sexual orientation, and religious or political beliefs. Education is a powerful force for equity and change in our world. It has the poten- tial to deliver opportunities that improve lives and enable economic mobility. As we work with authors to create content for every product and service, we acknowl- edge our responsibility to demonstrate inclusivity and incorporate diverse schol- arship so that everyone can achieve their potential through learning. As the world's leading learning company, we have a duty to help drive change and live up to our purpose to help more people create a better life for themselves and to cre- ate a better world. Our ambition is to purposefully contribute to a world where: • Everyone has an equitable and lifelong opportunity to succeed through learning. • Our educational products and services are inclusive and represent the rich diversity of learners. • Our educational content accurately reflects the histories and experiences of the learners we serve. • Our educational content prompts deeper discussions with learners and motivates them to expand their own learning (and worldview). While we work hard to present unbiased content, we want to hear from you about any con- cerns or needs with this Pearson product so that we can investigate and address them. • Please contact us with concerns about any potential bias at https://www.pearson.com/report-bias.html.
Page
7
This page intentionally left blank
Page
8
To the loving memory of my mother, Zubaida Begum, and my father, Mohammed Azim. And to the future generation: Tobias Albert, Ronja Johanne and Serine Begum—with all my love. —K.A.M.
Page
9
This page intentionally left blank
Page
10
ix Contents Overview Figures xxi Tables xxv Examples xxix Foreword xxxvii Preface xxxix 1 Basics of Java Programming 1 2 Basic Elements, Primitive Data Types, and Operators 29 3 Declarations 97 4 Control Flow 151 5 Object-Oriented Programming 189 6 Access Control 323 7 Exception Handling 363 8 Selected API Classes 423 9 Nested Type Declarations 489 10 Object Lifetime 531 11 Generics 563 12 Collections, Part I: ArrayList<E> 643
Page
11
x CONTENTS OVERVIEW 13 Functional-Style Programming 673 14 Object Comparison 741 15 Collections: Part II 781 16 Streams 879 17 Date and Time 1023 18 Localization 1095 19 Java Module System 1161 20 Java I/O: Part I 1231 21 Java I/O: Part II 1285 22 Concurrency: Part I 1365 23 Concurrency: Part II 1419 24 Database Connectivity 1511 25 Annotations 1555 26 Secure Coding 1599 A Taking the Java SE 17 and Java SE 11 Developer Exams 1615 B Exam Topics: Java SE 17 Developer 1623 C Exam Topics: Java SE 11 Developer 1629 D Annotated Answers to Review Questions 1635 E Mock Exam: Java SE 17 Developer 1709 F Annotated Answers to Mock Exam 1737 G Java Logging API Overview 1747 Index 1753
Page
12
xi Contents Figures xxi Tables xxv Examples xxix Foreword xxxvii Preface xxxix Writing This Book xxxix About This Book xl Using This Book xli Book Website xliii Request for Feedback xliii About the Authors xliv Acknowledgments xliv 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 24 Review Questions 26
Page
13
xii CONTENTS 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 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 4 Control Flow 151 4.1 Selection Statements 152 4.2 The switch Statement 155 4.3 The switch Expression 164 Review Questions 170
Page
14
CONTENTS xiii 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 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 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
Page
15
xiv CONTENTS 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 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 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 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
Page
16
CONTENTS xv 10.9 Constructing Initial Object State 555 Review Questions 558 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 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 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
Page
17
xvi CONTENTS 13.13 Method and Constructor References 724 13.14 Contexts for Defining Lambda Expressions 733 Review Questions 735 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 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 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 17 Date and Time 1023 17.1 Date and Time API Overview 1024 17.2 Working with Dates and Times 1027
Page
18
CONTENTS xvii 17.3 Using Temporal Units and Temporal Fields 1044 17.4 Working with Instants 1049 17.5 Working with Periods 1057 17.6 Working with Durations 1064 17.7 Working with Time Zones and Daylight Savings 1072 17.8 Converting Date and Time Values to Legacy Date 1088 Review Questions 1089 18 Localization 1095 18.1 Using Locales 1096 18.2 Properties Files 1100 18.3 Bundling Resources 1102 Review Questions 1112 18.4 Core API for Formatting and Parsing of Values 1115 18.5 Formatting and Parsing Number, Currency, and Percentage Values 1116 18.6 Formatting and Parsing Date and Time 1127 18.7 Formatting and Parsing Messages 1139 Review Questions 1153 19 Java Module System 1161 19.1 Making the Case for Modules 1163 19.2 The Modular JDK 1164 19.3 Module Basics 1168 19.4 Overview of Module Directives 1177 19.5 Creating a Modular Application 1179 19.6 Compiling and Running a Modular Application 1186 19.7 Creating JAR Files 1189 19.8 Open Modules and the opens Directive 1191 19.9 Services 1196 19.10 Creating Runtime Images 1204 19.11 Categories of Modules 1205 19.12 Migrating to Modules 1209 19.13 Exploring Modules 1211 19.14 Summary of Selected Operations with the JDK Tools 1218 Review Questions 1223 20 Java I/O: Part I 1231 20.1 Input and Output 1233 20.2 Byte Streams: Input Streams and Output Streams 1234 20.3 Character Streams: Readers and Writers 1241 20.4 The Console Class 1256 Review Questions 1259 20.5 Object Serialization 1261 Review Questions 1277
Page
19
xviii CONTENTS 21 Java I/O: Part II 1285 21.1 Characteristics of a Hierarchical File System 1287 21.2 Creating Path Objects 1289 21.3 Working with Path Objects 1294 21.4 Operations on Directory Entries 1304 21.5 Reading and Writing Files Using Paths 1314 21.6 Managing File Attributes 1321 21.7 Creating Directory Entries 1339 21.8 Stream Operations on Directory Entries 1345 Review Questions 1355 22 Concurrency: Part I 1365 22.1 Threads and Concurrency 1367 22.2 Runtime Organization for Thread Execution 1369 22.3 Creating Threads 1370 Review Questions 1378 22.4 Thread Lifecycle 1380 22.5 Thread Issues 1408 Review Questions 1415 23 Concurrency: Part II 1419 23.1 Utility Classes TimeUnit and ThreadLocalRandom 1421 23.2 The Executor Framework 1423 23.3 The Fork/Join Framework 1447 23.4 Writing Thread-Safe Code 1451 23.5 Special-Purpose Synchronizers 1470 23.6 Synchronized Collections and Maps 1477 23.7 Concurrent Collections and Maps 1482 Review Questions 1504 24 Database Connectivity 1511 24.1 Introduction to Relational Databases 1512 24.2 Introduction to JDBC 1517 24.3 Establishing a Database Connection 1519 24.4 Creating and Executing SQL Statements 1522 24.5 Processing Query Results 1533 24.6 Customizing Result Sets 1539 24.7 Discovering Database and ResultSet Metadata 1543 24.8 Implementing Transaction Control 1545 Review Questions 1548 25 Annotations 1555 25.1 Basics of Annotations 1557 25.2 Declaring Annotation Types 1558
Page
20
CONTENTS xix 25.3 Applying Annotations 1563 25.4 Meta-Annotations 1567 25.5 Selected Standard Annotations 1577 25.6 Processing Annotations 1587 Review Questions 1593 26 Secure Coding 1599 26.1 Application Security Overview 1600 26.2 Security Threat Categories 1602 26.3 Java Security Policies 1608 26.4 Additional Security Guidelines 1610 Review Questions 1611 A Taking the Java SE 17 and Java SE 11 Developer Exams 1615 A.1 Preparing for the Exam 1615 A.2 Registering for the Online Proctored Exam 1616 A.3 How the Online Proctored Exam Is Conducted 1618 A.4 The Questions 1619 B Exam Topics: Java SE 17 Developer 1623 C Exam Topics: Java SE 11 Developer 1629 D Annotated Answers to Review Questions 1635 E Mock Exam: Java SE 17 Developer 1709 F Annotated Answers to Mock Exam 1737 G Java Logging API Overview 1747 G.1 Purpose of the Logging API 1747 G.2 Configuring Logging 1748 G.3 Writing Log Messages 1749 G.4 Applying Guarded Logging 1751 G.5 Summary 1751 Index 1753
Comments 0
Loading comments...
Reply to Comment
Edit Comment