SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
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.
The professional programmer’s Deitel® guide to Java® 9 and the powerful Java platform
Written for programmers with a background in another high-level language, this book applies the Deitel signature live-code approach to teaching programming and explores the Java® 9 language and APIs in depth. The book presents concepts in fully tested programs, complete with code walkthroughs, syntax shading, code highlighting and program outputs. It features hundreds of complete Java 9 programs with thousands of lines of proven code, and hundreds of software-development tips that will help you build robust applications.
Start with an introduction to Java using an early classes and objects approach, then rapidly move on to more advanced topics, including JavaFX GUI, graphics, animation and video, exception handling, lambdas, streams, functional interfaces, object serialization, concurrency, generics, generic collections, database with JDBC™ and JPA, and compelling new Java 9 features, such as the Java Platform Module System, interactive Java with JShell (for discovery, experimentation and rapid prototyping) and more. You’ll enjoy the Deitels’ classic treatment of object-oriented programming and the object-oriented design ATM case study, including a complete Java implementation. When you’re finished, you’ll have everything you need to build industrial-strength, object-oriented Java 9 applications.
New Java® 9 FeaturesCore Java Features
Keep in Touch
Please visit the author's site for this book at www.deitel.com/books/Java9FP.
Foreword xxvii
Preface xxix
Before You Begin xlv
Chapter 1: Introduction and Test-Driving a Java Application 1
1.1 Introduction 2
1.2 Object Technology Concepts 3
1.3 Java 6
1.4 A Typical Java Development Environment 8
1.5 Test-Driving a Java Application 11
1.6 Software Technologies 15
1.7 Getting Your Questions Answered 18
Chapter 2: Introduction to Java Applications; Input/Output and Operators 19
2.1 Introduction 20
2.2 Your First Program in Java: Printing a Line of Text 20
2.3 Modifying Your First Java Program 24
2.4 Displaying Text with printf 26
2.5 Another Application: Adding Integers 27
2.6 Arithmetic 30
2.7 Decision Making: Equality and Relational Operators 31
2.8 Wrap-Up 34
Chapter 3: Introduction to Classes, Objects, Methods and Strings 35
3.1 Introduction 36
3.2 Instance Variables, set Methods and get Methods 37
3.3 Account Class: Initializing Objects with Constructors 46
3.4 Account Class with a Balance; Floating-Point Numbers 49
3.5 Primitive Types vs. Reference Types 54
3.6 Wrap-Up 55
Chapter 4: Control Statements: Part 1; Assignment, ++ and -- Operators 56
4.1 Introduction 57
4.2 Control Structures 57
4.3 if Single-Selection Statement 59
4.4 if…else Double-Selection Statement 60
4.5 while Iteration Statement 63
4.6 Counter-Controlled Iteration 65
4.7 Sentinel-Controlled Iteration 68
4.8 Nesting Different Control Statements 72
4.9 Compound Assignment Operators 74
4.10 Increment and Decrement Operators 75
4.11 Primitive Types 78
4.12 Wrap-Up 78
Chapter 5: Control Statements: Part 2; Logical Operators 79
5.1 Introduction 80
5.2 Essentials of Counter-Controlled Iteration 80
5.3 for Iteration Statement 81
5.4 Examples Using the for Statement 85
5.5 do…while Iteration Statement 90
5.6 switch Multiple-Selection Statement 90
5.7 Class AutoPolicy: Strings in switch Statements 97
5.8 break and continue Statements 100
5.9 Logical Operators 102
5.10 Wrap-Up 108
Chapter 6: Methods: A Deeper Look 109
6.1 Introduction 110
6.2 Program Units in Java 110
6.3 static Methods, static Fields and Class Math 111
6.4 Methods with Multiple Parameters 113
6.5 Notes on Declaring and Using Methods 116
6.6 Argument Promotion and Casting 117
6.7 Java API Packages 119
6.8 Case Study: Secure Random-Number Generation 120
6.9 Case Study: A Game of Chance; Introducing enum Types 125
6.10 Scope of Declarations 129
6.11 Method Overloading 132
6.12 Wrap-Up 134
Chapter 7: Arrays and ArrayLists 135
7.1 Introduction 136
7.2 Arrays 137
7.3 Declaring and Creating Arrays 138
7.4 Examples Using Arrays 139
7.5 Exception Handling: Processing the Incorrect Response 148
7.6 Case Study: Card Shuffling and Dealing Simulation 149
7.7 Enhanced for Statement 153
7.8 Passing Arrays to Methods 155
7.9 Pass-By-Value vs. Pass-By-Reference 157
7.10 Case Study: Class GradeBook Using an Array to Store Grades 158
7.11 Multidimensional Arrays 163
7.12 Case Study: Class GradeBook Using a Two-Dimensional Array 167
7.13 Variable-Length Argument Lists 173
7.14 Using Command-Line Arguments 174
7.15 Class Arrays 176
7.16 Introduction to Collections and Class ArrayList 179
7.17 Wrap-Up 182
Chapter 8: Classes and Objects: A Deeper Look 184
8.1 Introduction 185
8.2 Time Class Case Study 185
8.3 Controlling Access to Members 190
8.4 Referring to the Current Object’s Members with the this Reference 191
8.5 Time Class Case Study: Overloaded Constructors 193
8.6 Default and No-Argument Constructors 198
8.7 Notes on Set and Get Methods 199
8.8 Composition 200
8.9 enum Types 203
8.10 Garbage Collection 206
8.11 static Class Members 206
8.12 static Import 210
8.13 final Instance Variables 211
8.14 Package Access 212
8.15 Using BigDecimal for Precise Monetary Calculations 213
8.16 JavaMoney API 216
8.17 Time Class Case Study: Creating Packages 216
8.18 Wrap-Up 220
Chapter 9: Object-Oriented Programming: Inheritance 221
9.1 Introduction 222
9.2 Superclasses and Subclasses 223
9.3 protected Members 225
9.4 Relationship Between Superclasses and Subclasses 226
9.5 Constructors in Subclasses 246
9.6 Class Object 247
9.7 Designing with Composition vs. Inheritance 248
9.8 Wrap-Up 249
Chapter 10: Object-Oriented Programming: Polymorphism and Interfaces 251
10.1 Introduction 252
10.2 Polymorphism Examples 254
10.3 Demonstrating Polymorphic Behavior 255
10.4 Abstract Classes and Methods 257
10.5 Case Study: Payroll System Using Polymorphism 260
10.6 Allowed Assignments Between Superclass and Subclass Variables 274
10.7 final Methods and Classes 274
10.8 A Deeper Explanation of Issues with Calling Methods from Constructors 275
10.9 Creating and Using Interfaces 276
10.10 Java SE 8 Interface Enhancements 285
10.11 Java SE 9 private Interface Methods 287
10.12 private Constructors 287
10.13 Program to an Interface, Not an Implementation 288
10.14 Wrap-Up 290
Chapter 11: Exception Handling: A Deeper Look 291
11.1 Introduction 292
11.2 Example: Divide by Zero without Exception Handling 293
11.3 Example: Handling ArithmeticExceptions and InputMismatchExceptions 295
11.4 When to Use Exception Handling 300
11.5 Java Exception Hierarchy 301
11.6 finally Block 304
11.7 Stack Unwinding and Obtaining Information from an Exception 309
11.8 Chained Exceptions 311
11.9 Declaring New Exception Types 313
11.10 Preconditions and Postconditions 314
11.11 Assertions 315
11.12 try-with-Resources: Automatic Resource Deallocation 317
11.13 Wrap-Up 318
Chapter 12: JavaFX Graphical User Interfaces: Part 1 319
12.1 Introduction 320
12.2 JavaFX Scene Builder 321
12.3 JavaFX App Window Structure 322
12.4 Welcome App—Displaying Text and an Image 323
12.5 Tip Calculator App—Introduction to Event Handling 328
12.6 Features Covered in the Other JavaFX Chapters 346
12.7 Wrap-Up 346
Chapter 13: JavaFX GUI: Part 2 347
13.1 Introduction 348
13.2 Laying Out Nodes in a Scene Graph 348
13.3 Painter App: RadioButtons, Mouse Events and Shapes 350
13.4 Color Chooser App: Property Bindings and Property Listeners 360
13.5 Cover Viewer App: Data-Driven GUIs with JavaFX Collections 366
13.6 Cover Viewer App: Customizing ListView Cells 371
13.7 Additional JavaFX Capabilities 375
13.8 JavaFX 9: Java SE 9 JavaFX Updates 377
13.9 Wrap-Up 379
Chapter 14: Strings, Characters and Regular Expressions 380
14.1 Introduction 381
14.2 Fundamentals of Characters and Strings 381
14.3 Class String 382
14.4 Class StringBuilder 395
14.5 Class Character 402
14.6 Tokenizing Strings 407
14.7 Regular Expressions, Class Pattern and Class Matcher 408
14.8 Wrap-Up 417
Chapter 15: Files, Input/Output Streams, NIO and XML Serialization 418
15.1 Introduction 419
15.2 Files and Streams 419
15.3 Using NIO Classes and Interfaces to Get File and Directory Information 421
15.4 Sequential Text Files 425
15.5 XML Serialization 434
15.6 FileChooser and DirectoryChooser Dialogs 441
15.7 (Optional) Additional java.io Classes 447
15.8 Wrap-Up 450
Chapter 16: Generic Collections 451
16.1 Introduction 452
16.2 Collections Overview 452
16.3 Type-Wrapper Classes 454
16.4 Autoboxing and Auto-Unboxing 454
16.5 Interface Collection and Class Collections 454
16.6 Lists 455
16.7 Collections Methods 463
16.8 Class PriorityQueue and Interface Queue 474
16.9 Sets 475
16.10 Maps 478
16.11 Synchronized Collections 482
16.12 Unmodifiable Collections 482
16.13 Abstract Implementations 483
16.14 Java SE 9: Convenience Factory Methods for Immutable Collections 483
16.15 Wrap-Up 487
Chapter 17: Lambdas and Streams 488
17.1 Introduction 489
17.2 Streams and Reduction 491
17.3 Mapping and Lambdas 494
17.4 Filtering 498
17.5 How Elements Move Through Stream Pipelines 500
17.6 Method References 501
17.7 IntStream Operations 504
17.8 Functional Interfaces 509
17.9 Lambdas: A Deeper Look 510
17.10 Stream<Integer> Manipulations 511
17.11 Stream<String> Manipulations 514
17.12 Stream<Employee> Manipulations 517
17.13 Creating a Stream<String> from a File 528
17.14 Streams of Random Values 531
17.15 Infinite Streams 533
17.16 Lambda Event Handlers 535
17.17 Additional Notes on Java SE 8 Interfaces 535
17.18 Wrap-Up 536
Chapter 18: Recursion 537
18.1 Introduction 538
18.2 Recursion Concepts 538
18.3 Example Using Recursion: Factorials 539
18.4 Reimplementing Class FactorialCalculator Using BigInteger 541
18.5 Example Using Recursion: Fibonacci Series 543
18.6 Recursion and the Method-Call Stack 546
18.7 Recursion vs. Iteration 547
18.8 Towers of Hanoi 549
18.9 Fractals 551
18.10 Recursive Backtracking 561
18.11 Wrap-Up 562
Chapter 19: Generic Classes and Methods: A Deeper Look 563
19.1 Introduction 564
19.2 Motivation for Generic Methods 564
19.3 Generic Methods: Implementation and Compile-Time Translation 566
19.4 Additional Compile-Time Translation Issues: Methods That Use a Type Parameter as the Return Type 569
19.5 Overloading Generic Methods 572
19.6 Generic Classes 573
19.7 Wildcards in Methods That Accept Type Parameters 580
19.8 Wrap-Up 584
Chapter 20: JavaFX Graphics, Animation and Video 585
20.1 Introduction 586
20.2 Controlling Fonts with Cascading Style Sheets (CSS) 587
20.3 Displaying Two-Dimensional Shapes 594
20.4 Polylines, Polygons and Paths 599
20.5 Transforms 604
20.6 Playing Video with Media, MediaPlayer and MediaViewer 606
20.7 Transition Animations 612
20.8 Timeline Animations 618
20.9 Frame-by-Frame Animation with AnimationTimer 621
20.10 Drawing on a Canvas 624
20.11 Three-Dimensional Shapes 628
20.12 Wrap-Up 632
Chapter 21: Concurrency and Multi-Core Performance 634
21.1 Introduction 635
21.2 Thread States and Life Cycle 637
21.3 Creating and Executing Threads with the Executor Framework 640
21.4 Thread Synchronization 644
21.5 Producer/Consumer Relationship without Synchronization 653
21.6 Producer/Consumer Relationship: ArrayBlockingQueue 661
21.7 (Advanced) Producer/Consumer Relationship with synchronized, wait, notify and notifyAll 664
21.8 (Advanced) Producer/Consumer Relationship: Bounded Buffers 670
21.9 (Advanced) Producer/Consumer Relationship: The Lock and Condition Interfaces 678
21.10 Concurrent Collections 685
21.11 Multithreading in JavaFX 687
21.12 sort/parallelSort Timings with the Java SE 8 Date/Time API 699
21.13 Java SE 8: Sequential vs. Parallel Streams 702
21.14 (Advanced) Interfaces Callable and Future 704
21.15 (Advanced) Fork/Join Framework 709
21.16 Wrap-Up 709
Chapter 22: Accessing Databases with JDBC 711
22.1 Introduction 712
22.2 Relational Databases 713
22.3 A books Database 714
22.4 SQL 718
22.5 Setting Up a Java DB Database 727
22.6 Connecting to and Querying a Database 729
22.7 Querying the books Database 734
22.8 RowSet Interface 746
22.9 PreparedStatements 749
22.10 Stored Procedures 761
22.11 Transaction Processing 761
22.12 Wrap-Up 762
Chapter 23: Introduction to JShell: Java 9’s REPL for Interactive Java 763
23.1 Introduction 764
23.2 Installing JDK 9 766
23.3 Introduction to JShell 766
23.4 Command-Line Input in JShell 777
23.5 Declaring and Using Classes 778
23.6 Discovery with JShell Auto-Completion 782
23.7 Exploring a Class’s Members and Viewing Documentation 784
23.8 Declaring Methods 790
23.9 Exceptions 792
23.10 Importing Classes and Adding Packages to the CLASSPATH 793
23.11 Using an External Editor 795
23.12 Summary of JShell Commands 797
23.13 Keyboard Shortcuts for Snippet Editing 803
23.14 How JShell Reinterprets Java for Interactive Use 803
23.15 IDE JShell Support 804
23.16 Wrap-Up 804
Chapter 24: Java Persistence API (JPA) 820
24.1 Introduction 821
24.2 JPA Technology Overview 822
24.3 Querying a Database with JPA 823
24.4 Named Queries; Accessing Data from Multiple Tables 830
24.5 Address Book: Using JPA and Transactions to Modify a Database 835
24.6 Web Resources 843
24.7 Wrap-Up 844
Chapter 25: ATM Case Study, Part 1: Object-Oriented Design with the UML 845
25.1 Case Study Introduction 846
25.2 Examining the Requirements Document 846
25.3 Identifying the Classes in a Requirements Document 854
25.4 Identifying Class Attributes 860
25.5 Identifying Objects’ States and Activities 865
25.6 Identifying Class Operations 868
25.7 Indicating Collaboration Among Objects 875
25.8 Wrap-Up 882
Chapter 26: ATM Case Study Part 2: Implementing an Object-Oriented Design 886
26.1 Introduction 887
26.2 Starting to Program the Classes of the ATM System 887
26.3 Incorporating Inheritance and Polymorphism into the ATM System 892
26.4 ATM Case Study Implementation 898
26.5 Wrap-Up 921
Chapter 27: Java Platform Module System 923
27.1 Introduction 924
27.2 Module Declarations 929
27.3 Modularized Welcome App 932
27.4 Creating and Using a Custom Module 942
27.5 Module-Dependency Graphs: A Deeper Look 948
27.6 Migrating Code to Java 9 951
27.7 Resources in Modules; Using an Automatic Module 955
27.8 Creating Custom Runtimes with jlink 959
27.9 Services and ServiceLoader 963
27.10 Wrap-Up 973
Chapter 28: Additional Java 9 Topics 975
28.1 Introduction 976
28.2 Recap: Java 9 Features Covered in Earlier Chapters 977
28.3 New Version String Format 977
28.4 Regular Expressions: New Matcher Class Methods 978
28.5 New Stream Interface Methods 980
28.6 Modules in JShell 983
28.7 JavaFX 9 Skin APIs 984
28.8 Other GUI and Graphics Enhancements 985
28.9 Security Related Java 9 Topics 986
28.10 Other Java 9 Topics 987
28.11 Items Removed from the JDK and Java 9 990
28.12 Items Proposed for Removal from Future Java Versions 991
28.13 Wrap-Up 992
Appendix A: Operator Precedence Chart 994
Appendix B: ASCII Character Set 996
Appendix C: Keywords and Reserved Words 997
Appendix D: Primitive Types 998
Appendix E: Bit Manipulation 999
E.1 Introduction 999
E.2 Bit Manipulation and the Bitwise Operators 999
E.3 BitSet Class 1009
Appendix F: Labeled break and continue Statements 1012
F.1 Introduction 1012
F.2 Labeled break Statement 1012
F.3 Labeled continue Statement 1013
Index 1015