EARTH WEEK
Go paperless and save 40% on eligible eBooks, 70% on video now through April 26. Shop the sale.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB
The open industry format known for its reflowable content and usability on supported mobile devices.
PDF
The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB
The open industry format known for its reflowable content and usability on supported mobile devices.
PDF
The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
The professional programmer's Deitel® guide to Java with integrated generative AI
Written for programmers with a background in another high-level language, in Java for Programmers: with Generative AI, Fifth Edition, you'll learn modern Java development hands on using the latest Java idioms and features and genAIs.
In the context of 200+ real-world code examples, you'll quickly master Java fundamentals then move on to arrays, strings, regular expressions, JSON/CSV processing with the Jackson library, private- and public-key cryptography, classes, inheritance, polymorphism, interfaces, dependency injection, exceptions, generic collections, custom generics, functional programming with lambdas and streams, JavaFX GUI, graphics and multimedia, platform threads, virtual threads, structured concurrency, scoped values, building API-based Java genAI apps, database with JDBC and SQLite, the Java Platform Module System and JShell for Python-like interactivity.
Features:
Want to stay in touch with the Deitels?
Reviewer Comments
"The future of Java programming is here, and this new edition of Deitel is leading the charge! By embracing genAI head-on, the authors are potentially revolutionizing programming education. Through its integrative approach to the use and study of genAI, this book is positioned to be the leading book in modern Java and its applications. Indeed, I expect that it should be widely adopted by instructors who want to ingrain in their students an appreciation for the critical role that Java will play in data science, machine learning, artificial intelligence, and cybersecurity.
"The book's innovative and forward-thinking use of genAI facilitates reader engagement and inspires readers to think critically about the benefits and limitations of AI as a programming aid. Chapter 19 could become everyone's favorite new Java book chapter--the generative AI API-based code examples are interesting and fun.
"All audiences of this book should read the Preface--there's so much to get excited about! It demonstrates, with refreshing transparency and honesty, how much love and care went into the reinvention of an already outstanding Java book by bringing it into a new frontier of what it means to be a programmer in today's world. Bravo! Your Preface statement: 'GenAI has created an ultra-high-level programming capability that will leverage your Java learning experience and ability to produce robust, top-quality Java software quickly, conveniently and economically.' is a great conclusion to the Preface intro--really helps justify the use of genAI!"
--Brian Canada, Professor of Computational Science, University of South Carolina Beaufort
"After reading your whole book, it was fun to read the Preface that wraps everything up at a high level. You have done some amazing work here, and I'm glad to have been a small part of it as a reviewer! I especially appreciate how difficult it must have been to make sure everything was as up to date as possible with the speed at which things change in this field, and the deftness with which you incorporated all the focus on GenAI and data science that's in this book."
--Emily Navarro, Ph.D., Continuing Lecturer, Department of Informatics, University of California, Irvine
"The generative AI exercises are awesome and reflect the way modern developers work! They are fun and let the reader explore and learn about AI by using AI--how meta. This allows readers to expand their knowledge and get a feel for the AIs' code-related capabilities."
--Jeanne Boyarsky, CodeRanch, Java Champion
Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details. (Note: eBooks are 4-color and print books are black and white.)
Preface xxv
Before You Begin lix
Chapter 1: Intro, Test-Driving a Java Application, and Generative AI 1
1.1 Introduction 2
1.2 Hardware 4
1.3 Java 7
1.4 A Brief Review of Object Orientation 8
1.5 Java Application Programming Interface (API) and Open-Source Libraries 11
1.6 Typical Java Program Development Environment 12
1.7 Test-Driving a Java Program with the Java Development Kit (JDK) 15
1.8 Internet, World Wide Web, the Cloud and IoT 18
1.9 Metaverse 22
1.10 Software Development Technologies 25
1.11 Data Analytics and Data Science 27
1.12 How Big Is Big Data? 28
1.13 AI--at the Intersection of Computer Science and Data Science 32
1.14 Generative AI 35
1.15 Wrap-Up 41
Chapter 2: Intro to Java Programming 47
2.1 Introduction 48
2.2 Your First Program in Java: Printing a Line of Text 48
2.3 Modifying Your First Program 54
2.4 Displaying Text with printf 56
2.5 Another Program: Adding Integers 57
2.6 Arithmetic 60
2.7 Decision Making: Equality and Relational Operators 62
2.8 Objects-Natural Case Study: Creating and Using Objects of the Java API's String Class 65
2.9 Wrap-Up 68
Chapter 3: Control Statements: Part 1 69
3.1 Introduction 70
3.2 Control Structures 70
3.3 if Single-Selection Statement 73
3.4 if...else Double-Selection Statement 74
3.5 while Iteration Statement 77
3.6 Counter-Controlled Iteration 79
3.7 Sentinel-Controlled Iteration 83
3.8 Nested Control Statements 87
3.9 Compound Assignment Operators 91
3.10 Increment and Decrement Operators 91
3.11 Primitive Types 94
3.12 Objects-Natural Case Study: Super-Sized Integers 94
3.13 Wrap-Up 98
Chapter 4: Control Statements: Part 2 99
4.1 Introduction 100
4.2 Essentials of Counter-Controlled Iteration 100
4.3 for Iteration Statement 101
4.4 Examples Using the for Statement 104
4.5 Summing the Even Integers from 2 to 20 with the for Statement 105
4.6 Calculating Compound Interest with the for Statement 106
4.7 do...while Iteration Statement 109
4.8 switch Multiple-Selection Statement 111
4.9 break and continue Statements 116
4.10 Logical Operators 118
4.11 Objects-Natural Case Study: Precise Monetary Calculations with Java API Class BigDecimal 124
4.12 Wrap-Up 128
Chapter 5: Methods 131
5.1 Introduction 132
5.2 Declaring Methods 132
5.3 Notes on Declaring and Using Methods 136
5.4 Case Study: Die Rolling Simulation with Random-Number Generation 137
5.5 Case Study: A Game of Chance; Introducing enums 142
5.6 Scope of Declarations 147
5.7 Method Overloading 150
5.8 Class Math: static Methods and Variables 152
5.9 Java API Packages 154
5.10 Method-Call Stack and Activation Records 156
5.11 Argument Promotion and Casting 159
5.12 Objects-Natural Case Study: Java Date/Time API 161
5.13 Wrap-Up 167
Chapter 6: Arrays and ArrayLists 169
6.1 Introduction 170
6.2 Primitive Types vs. Reference Types 171
6.3 Arrays 171
6.4 Declaring and Creating Arrays 172
6.5 Creating and Initializing an Array 173
6.6 Array Initializers 175
6.7 Calculating Array Element Values 176
6.8 Totaling Array Elements 177
6.9 Intro to Visualization: Using a Bar Chart to Display Array Data Graphically 177
6.10 Using Array Elements as Counters 179
6.11 Analyzing Survey Results 181
6.12 Exception Handling 183
6.13 Enhanced for Statement: Totaling Array Elements 184
6.14 Passing Arrays to Methods 186
6.15 Pass-By-Value vs. Pass-By-Reference 188
6.16 Multidimensional Arrays 189
6.17 Variable-Length Argument Lists 193
6.18 Command-Line Arguments 194
6.19 Class Arrays 197
6.20 Objects-Natural Case Study: Intro to Collections and Class ArrayList 199
6.21 Wrap-Up 204
Chapter 7: Strings, NLP and Regex: Generative AI Foundations 207
7.1 Introduction 208
7.2 Fundamentals of Characters and Strings 209
7.3 Class String 210
7.4 Class StringBuilder 225
7.5 Class Character 232
7.6 Tokenizing Strings 237
7.7 Intro to Natural Language Processing (NLP)--at the Root of Generative AI 238
7.8 Objects-Natural Case Study: Intro to Regular Expressions in NLP 240
7.9 Objects-Natural Security Case Study: pMa5tfEKwk59dTvC04Ft1IFQz9mEXnkfYXZwxk4ujGE= 252
7.10 Wrap-Up 260
Chapter 8: Real-World Modeling with Custom Classes 261
8.1 Introduction 262
8.2 Instance Variables, set Methods and get Methods 263
8.3 Default and Explicit Instance Variable Initialization 271
8.4 Account Class: Initializing Objects with Constructors 271
8.5 Account Class with a Balance 274
8.6 Case Study: Card Shuffling and Dealing Simulation 278
8.7 Case Study: Time Class 283
8.8 Controlling Access to Members 286
8.9 Referring to the Current Object's Members with the this Reference 287
8.10 Case Study: Time Class Overloaded Constructors 289
8.11 Default and No-Argument Constructors 296
8.12 Notes on Set and Get Methods 296
8.13 Composition 297
8.14 enum Types 300
8.15 Garbage Collection 303
8.16 static Class Members 303
8.17 static Import 307
8.18 final Instance Variables 308
8.19 Package Access 309
8.20 record Classes 310
8.21 Wrap-Up 315
Chapter 9: Real-World Modeling with Inheritance, Polymorphism & Interfaces 317
9.1 Introduction 318
9.2 Superclasses and Subclasses 320
9.3 Relationship Between Superclasses and Subclasses 322
9.4 Class Object 332
9.5 Intro to Polymorphism: Polymorphic Video Game 332
9.6 Demonstrating Polymorphic Behavior 333
9.7 abstract Classes and Methods 336
9.8 Case Study: Payroll System Using Polymorphism 338
9.9 final Methods and Classes 347
9.10 Issues with Constructors Calling Instance Methods 348
9.11 Creating and Using Interfaces 349
9.12 Other Interface Features 357
9.13 Program to an Interface, Not an Implementation 359
9.14 sealed Classes and Interfaces 367
9.15 private Constructors 370
9.16 protected Members 370
9.17 Wrap-Up 372
Chapter 10: Exception Handling: A Deeper Look 375
10.1 Introduction 376
10.2 Example: Divide by Zero without Exception Handling 378
10.3 Example: Handling ArithmeticExceptions and InputMismatchExceptions 380
10.4 Java Exception Hierarchy 384
10.5 Checked vs. Unchecked Exceptions 386
10.6 finally Block 388
10.7 Stack Unwinding and Obtaining Information from an Exception 390
10.8 Chained Exceptions 394
10.9 Declaring Custom Exceptions 396
10.10 Preconditions and Postconditions 397
10.11 Assertions 398
10.12 try-with-Resources Statement: Automatic Resource Deallocation 399
10.13 Unnamed Variables in catch Handlers 401
10.14 Wrap-Up 402
Chapter 11: Files, I/O Streams, JSON Serialization & CSV Files 405
11.1 Introduction 406
11.2 Files and Streams 407
11.3 Using NIO Classes and Interfaces to Get File and Directory Information 409
11.4 Sequential Text Files 413
11.5 Case Study: JavaScript Object Notation (JSON) Serialization 418
11.6 Case Study: Processing a JSON Response from a Web Service 424
11.7 Case Study: Creating and Reading CSV Files 432
11.8 Case Study: Reading and Analyzing a CSV File Containing the Titanic Disaster Dataset 437
11.9 Objects-Natural Security Case Study: RSA Public-Key Cryptography 445
11.10 Wrap-Up 452
Chapter 12: Generic Collections 455
12.1 Introduction 456
12.2 Collections Overview 457
12.3 Type-Wrapper Classes 458
12.4 Boxing and Unboxing 459
12.5 Lists 459
12.6 Collections Methods 468
12.7 Class PriorityQueue and Interface Queue 480
12.8 Hash Tables 481
12.9 Sets 483
12.10 Maps 486
12.11 Convenience Factory Methods for Creating Immutable Collections 489
12.12 Concurrent Collections 492
12.13 Wrap-Up 492
Chapter 13: Generic Classes and Methods: A Deeper Look 495
13.1 Introduction 496
13.2 Motivation for Generic Methods 496
13.3 Generic Methods: Implementation and Compile-Time Translation 498
13.4 Additional Compile-Time Translation Issues: Methods That Use a Type Parameter as the Return Type 501
13.5 Overloading Generic Methods 505
13.6 Generic Classes 505
13.7 Wildcards in Methods That Accept Type Parameters 513
13.8 Wrap-Up 517
Chapter 14: Functional Programming with Lambdas & Streams 519
14.1 Introduction 520
14.2 Streams and Reduction 521
14.3 Mapping and Lambdas 525
14.4 Filtering 528
14.5 How Elements Move Through Stream Pipelines 530
14.6 Method References 531
14.7 IntStream Operations 534
14.8 Functional Interfaces 540
14.9 Lambdas: A Deeper Look 541
14.10 Stream<Integer> Manipulations 542
14.11 Stream<String> Manipulations 546
14.12 Stream<Employee> Manipulations 548
14.13 Creating a Stream<String> from a File 558
14.14 Streams of Random Values 561
14.15 Infinite Streams 562
14.16 Additional Notes on Interfaces 564
14.17 Wrap-Up 564
Chapter 15: JavaFX Graphical User Interfaces: Part 1 567
15.1 Introduction 568
15.2 JavaFX Scene Builder 569
15.3 JavaFX Application Window Structure 570
15.4 Welcome Application: Displaying Text and an Image 571
15.5 Tip Calculator Application: Intro to Event Handling 577
15.6 Features Covered in the Other JavaFX Chapters 594
15.7 Wrap-Up 594
Chapter 16: JavaFX GUI: Part 2 597
16.1 Introduction 598
16.2 Laying Out Nodes in a Scene Graph 598
16.3 Painter Application: RadioButtons, Mouse Events and Shapes 600
16.4 Color Chooser Application: Property Bindings and Property Listeners 610
16.5 Cover Viewer Application: Data-Driven GUIs with JavaFX Collections 616
16.6 Cover Viewer Application: Customizing ListView Cells 621
16.7 FileChooser and DirectoryChooser Dialogs 625
16.8 Other JavaFX Capabilities and JavaFX Accessibility 631
16.9 JavaFX Updates 633
16.10 JavaFX Resources and Libraries 634
16.11 Wrap-Up 636
Chapter 17: JavaFX Graphics and Multimedia 639
17.1 Introduction 640
17.2 Controlling Fonts with Cascading Style Sheets (CSS) 641
17.3 Displaying Two-Dimensional Shapes 649
17.4 Polylines, Polygons and Paths 655
17.5 Transforms 660
17.6 Playing Video with Media, MediaPlayer and MediaView 662
17.7 Transition Animations 669
17.8 Timeline Animations 675
17.9 Frame-by-Frame Animation with AnimationTimer 677
17.10 CSS Transitions 680
17.11 Drawing on a Canvas 685
17.12 Three-Dimensional Shapes 690
17.13 FXGL: A Brief Intro to Game Programming with JavaFX 694
17.14 Wrap-Up 695
Chapter 18: Concurrency: Platform Threads to Virtual Threads 697
18.1 Introduction 698
18.2 sort/parallelSort Timings with the Date/Time API 700
18.3 Sequential vs. Parallel Streams 703
18.4 Creating and Executing Platform Threads with the Executor Framework 707
18.5 Project Loom Overview 711
18.6 Creating and Executing Virtual Threads with the Executor Framework 712
18.7 Profiling Platform vs. Virtual Threads 717
18.8 Structured Concurrency and Scoped Values 723
18.9 Thread Synchronization Overview 731
18.10 Producer/Consumer Relationship with ArrayBlockingQueue 733
18.11 Multithreading in JavaFX 741
18.12 Wrap-Up 753
Chapter 19: Building API-Based Java Generative AI Applications 757
19.1 Introduction 758
19.2 OpenAI APIs 760
19.3 Setting Up a Java Environment 763
19.4 Text Generation Via Chat Completions 764
19.5 Speech Synthesis and Speech Recognition 783
19.6 Image Generation 786
19.7 Video 789
19.8 Moderation 794
19.9 Class OpenAIUtilities 797
19.10 Wrap-Up 812
Chapter 20: Accessing Databases with JDBC and SQLite 829
20.1 Introduction 830
20.2 Relational Databases 831
20.3 Setting Up the SQLite RDBMS 832
20.4 A books Database 832
20.5 Connecting to and Querying a Database with JDBC 842
20.6 Querying the books Database 846
20.7 PreparedStatements 852
20.8 Stored Procedures 864
20.9 Transaction Processing 864
20.10 Wrap-Up 865
Chapter 21: Java Platform Module System 867
21.1 Introduction 868
21.2 Module Declarations 873
21.3 Modularized Welcome App 876
21.4 Creating and Using a Custom Module 885
21.5 Module-Dependency Graphs: A Deeper Look 890
21.6 Migrating Code to Modules 894
21.7 Resources in Modules 898
21.8 Creating Custom Runtimes with jlink 902
21.9 Services and ServiceLoader 905
21.10 Wrap-Up 915
Chapter 22: Recursion and Big O 919
22.1 Introduction 920
22.2 Recursion Concepts 920
22.3 Recursion Example: Factorials 921
22.4 Recursion Example: Fibonacci Series 924
22.5 Recursion vs. Iteration 927
22.6 Towers of Hanoi 929
22.7 Fractals 931
22.8 Recursive Backtracking 941
22.9 Big O Notation 942
22.10 Common Big O Notations 943
22.11 Wrap-Up 943
Appendix A: Introduction to JShell for Interactive Java 945
A.1 Introduction 946
A.2 Introduction to JShell 948
A.3 Command-Line Input in JShell 959
A.4 Declaring and Using Classes 960
A.5 Discovery with JShell Auto-Completion 964
A.6 Exploring a Class's Members and Viewing Documentation 966
A.7 Declaring Methods 971
A.8 Exceptions 973
A.9 Importing Classes and Adding Packages to the CLASSPATH 974
A.10 Using an External Editor 977
A.11 Summary of JShell Commands 977
A.12 Keyboard Shortcuts for Snippet Editing 983
A.13 How JShell Reinterprets Java for Interactive Use 983
A.14 IDE JShell Support 984
A.15 Wrap-Up 984
Appendix B: Formatted Output 1001
B.1 Introduction 1002
B.2 Output with printf 1002
B.3 Integer Formatting 1003
B.4 Floating-Point Number Formatting 1004
B.5 String and Character Formatting 1005
B.6 Other Conversion Characters 1006
B.7 Field Widths and Precisions 1007
B.8 Formatting Flags 1009
B.9 Argument Indices for Explicit Positioning in Format Strings 1012
B.10 Escape Sequences 1013
B.11 Formatting Strings in Memory 1014
B.12 Wrap-Up 1015
Appendix C: Number Systems 1017
C.1 Introduction 1018
C.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers 1021
C.3 Converting Octal and Hexadecimal Numbers to Binary Numbers 1022
C.4 Converting from Binary, Octal or Hexadecimal to Decimal 1022
C.5 Converting from Decimal to Binary, Octal or Hexadecimal 1024
C.6 Negative Binary Numbers: Two's Complement Notation 1025
Index 1027