O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java® 9 for Programmers, Fourth Edition

Book Description

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 Features

  • Java® 9’s Platform Module System

  • Interactive Java via JShell—Java 9’s REPL

  • Collection Factory Methods, Matcher Methods, Stream Methods, JavaFX Updates, Using Modules in JShell, Completable Future Updates, Security Enhancements, Private Interface Methods and many other language and API updates.

  • Core Java Features

  • Classes, Objects, Encapsulation, Inheritance, Polymorphism, Interfaces

  • Composition vs. Inheritance, “Programming to an Interface not an Implementation”

  • Lambdas, Sequential and Parallel Streams, Functional Interfaces with Default and Static Methods, Immutability

  • JavaFX GUI, 2D and 3D Graphics, Animation, Video, CSS, Scene Builder

  • Files, I/O Streams, XML Serialization

  • Concurrency for Optimal Multi-Core Performance, JavaFX Concurrency APIs

  • Generics and Generic Collections

  • Recursion, Database (JDBC™ and JPA)

  • Keep in Touch

  • Contact the authors at: deitel@deitel.com

  • Join the Deitel social media communities
    LinkedIn® at bit.ly/DeitelLinkedIn
    Facebook® at facebook.com/DeitelFan
    Twitter® at twitter.com/deitel
    YouTube™ at youtube.com/DeitelTV

  • Subscribe to the Deitel ® Buzz e-mail newsletter at www.deitel.com/newsletter/subscribe.html

  • For source code and updates, visit: www.deitel.com/books/Java9FP

  • Table of Contents

    1. Title Page
    2. Copyright Page
    3. Detication Page
    4. Contents
    5. Deitel® Series
    6. Trademarks
    7. Foreword
    8. Preface
    9. Before You Begin
    10. 1. Introduction and Test-Driving a Java Application
      1. 1.1 Introduction
      2. 1.2 Object Technology Concepts
      3. 1.3 Java
      4. 1.4 A Typical Java Development Environment
      5. 1.5 Test-Driving a Java Application
      6. 1.6 Software Technologies
      7. 1.7 Getting Your Questions Answered
    11. 2. Introduction to Java Applications; Input/Output and Operators
      1. 2.1 Introduction
      2. 2.2 Your First Program in Java: Printing a Line of Text
      3. 2.3 Modifying Your First Java Program
      4. 2.4 Displaying Text with printf
      5. 2.5 Another Application: Adding Integers
      6. 2.6 Arithmetic
      7. 2.7 Decision Making: Equality and Relational Operators
      8. 2.8 Wrap-Up
    12. 3. Introduction to Classes, Objects, Methods and Strings
      1. 3.1 Introduction
      2. 3.2 Instance Variables, set Methods and get Methods
      3. 3.3 Account Class: Initializing Objects with Constructors
      4. 3.4 Account Class with a Balance; Floating-Point Numbers
      5. 3.5 Primitive Types vs. Reference Types
      6. 3.6 Wrap-Up
    13. 4. Control Statements: Part 1; Assignment, ++ and -- Operators
      1. 4.1 Introduction
      2. 4.2 Control Structures
      3. 4.3 if Single-Selection Statement
      4. 4.4 if...else Double-Selection Statement
      5. 4.5 while Iteration Statement
      6. 4.6 Counter-Controlled Iteration
      7. 4.7 Sentinel-Controlled Iteration
      8. 4.8 Nesting Different Control Statements
      9. 4.9 Compound Assignment Operators
      10. 4.10 Increment and Decrement Operators
      11. 4.11 Primitive Types
      12. 4.12 Wrap-Up
    14. 5. Control Statements: Part 2; Logical Operators
      1. 5.1 Introduction
      2. 5.2 Essentials of Counter-Controlled Iteration
      3. 5.3 for Iteration Statement
      4. 5.4 Examples Using the for Statement
      5. 5.5 do...while Iteration Statement
      6. 5.6 switch Multiple-Selection Statement
      7. 5.7 Class AutoPolicy: Strings in switch Statements
      8. 5.8 break and continue Statements
      9. 5.9 Logical Operators
      10. 5.10 Wrap-Up
    15. 6. Methods: A Deeper Look
      1. 6.1 Introduction
      2. 6.2 Program Units in Java
      3. 6.3 static Methods, static Fields and Class Math
      4. 6.4 Methods with Multiple Parameters
      5. 6.5 Notes on Declaring and Using Methods
      6. 6.6 Argument Promotion and Casting
      7. 6.7 Java API Packages
      8. 6.8 Case Study: Secure Random-Number Generation
      9. 6.9 Case Study: A Game of Chance; Introducing enum Types
      10. 6.10 Scope of Declarations
      11. 6.11 Method Overloading
      12. 6.12 Wrap-Up
    16. 7. Arrays and ArrayLists
      1. 7.1 Introduction
      2. 7.2 Arrays
      3. 7.3 Declaring and Creating Arrays
      4. 7.4 Examples Using Arrays
      5. 7.5 Exception Handling: Processing the Incorrect Response
      6. 7.6 Case Study: Card Shuffling and Dealing Simulation
      7. 7.7 Enhanced for Statement
      8. 7.8 Passing Arrays to Methods
      9. 7.9 Pass-By-Value vs. Pass-By-Reference
      10. 7.10 Case Study: Class GradeBook Using an Array to Store Grades
      11. 7.11 Multidimensional Arrays
      12. 7.12 Case Study: Class GradeBook Using a Two-Dimensional Array
      13. 7.13 Variable-Length Argument Lists
      14. 7.14 Using Command-Line Arguments
      15. 7.15 Class Arrays
      16. 7.16 Introduction to Collections and Class ArrayList
      17. 7.17 Wrap-Up
    17. 8. Classes and Objects: A Deeper Look
      1. 8.1 Introduction
      2. 8.2 Time Class Case Study
      3. 8.3 Controlling Access to Members
      4. 8.4 Referring to the Current Object’s Members with the this Reference
      5. 8.5 Time Class Case Study: Overloaded Constructors
      6. 8.6 Default and No-Argument Constructors
      7. 8.7 Notes on Set and Get Methods
      8. 8.8 Composition
      9. 8.9 enum Types
      10. 8.10 Garbage Collection
      11. 8.11 static Class Members
      12. 8.12 static Import
      13. 8.13 final Instance Variables
      14. 8.14 Package Access
      15. 8.15 Using BigDecimal for Precise Monetary Calculations
      16. 8.16 JavaMoney API
      17. 8.17 Time Class Case Study: Creating Packages
      18. 8.18 Wrap-Up
    18. 9. Object-Oriented Programming: Inheritance
      1. 9.1 Introduction
      2. 9.2 Superclasses and Subclasses
      3. 9.3 protected Members
      4. 9.4 Relationship Between Superclasses and Subclasses
      5. 9.5 Constructors in Subclasses
      6. 9.6 Class Object
      7. 9.7 Designing with Composition vs. Inheritance
      8. 9.8 Wrap-Up
    19. 10. Object--Oriented Programming: Polymorphism and Interfaces
      1. 10.1 Introduction
      2. 10.2 Polymorphism Examples
      3. 10.3 Demonstrating Polymorphic Behavior
      4. 10.4 Abstract Classes and Methods
      5. 10.5 Case Study: Payroll System Using Polymorphism
      6. 10.6 Allowed Assignments Between Superclass and Subclass Variables
      7. 10.7 final Methods and Classes
      8. 10.8 A Deeper Explanation of Issues with Calling Methods from Constructors
      9. 10.9 Creating and Using Interfaces
      10. 10.10 Java SE 8 Interface Enhancements
      11. 10.11 Java SE 9 private Interface Methods
      12. 10.12 private Constructors
      13. 10.13 Program to an Interface, Not an Implementation
      14. 10.14 Wrap-Up
    20. 11. Exception Handling: A Deeper Look
      1. 11.1 Introduction
      2. 11.2 Example: Divide by Zero without Exception Handling
      3. 11.3 Example: Handling ArithmeticExceptions and InputMismatchExceptions
      4. 11.4 When to Use Exception Handling
      5. 11.5 Java Exception Hierarchy
      6. 11.6 finally Block
      7. 11.7 Stack Unwinding and Obtaining Information from an Exception
      8. 11.8 Chained Exceptions
      9. 11.9 Declaring New Exception Types
      10. 11.10 Preconditions and Postconditions
      11. 11.11 Assertions
      12. 11.12 try-with-Resources: Automatic Resource Deallocation
      13. 11.13 Wrap-Up
    21. 12. JavaFX Graphical User Interfaces: Part 1
      1. 12.1 Introduction
      2. 12.2 JavaFX Scene Builder
      3. 12.3 JavaFX App Window Structure
      4. 12.4 Welcome App—Displaying Text and an Image
      5. 12.5 Tip Calculator App—Introduction to Event Handling
      6. 12.6 Features Covered in the Other JavaFX Chapters
      7. 12.7 Wrap-Up
    22. 13. JavaFX GUI: Part 2
      1. 13.1 Introduction
      2. 13.2 Laying Out Nodes in a Scene Graph
      3. 13.3 Painter App: RadioButtons, Mouse Events and Shapes
      4. 13.4 Color Chooser App: Property Bindings and Property Listeners
      5. 13.5 Cover Viewer App: Data-Driven GUIs with JavaFX Collections
      6. 13.6 Cover Viewer App: Customizing ListView Cells
      7. 13.7 Additional JavaFX Capabilities
      8. 13.8 JavaFX 9: Java SE 9 JavaFX Updates
      9. 13.9 Wrap-Up
    23. 14. Strings, Characters and Regular Expressions
      1. 14.1 Introduction
      2. 14.2 Fundamentals of Characters and Strings
      3. 14.3 Class String
      4. 14.4 Class StringBuilder
      5. 14.5 Class Character
      6. 14.6 Tokenizing Strings
      7. 14.7 Regular Expressions, Class Pattern and Class Matcher
      8. 14.8 Wrap-Up
    24. 15. Files, Input/Output Streams, NIO and XML Serialization
      1. 15.1 Introduction
      2. 15.2 Files and Streams
      3. 15.3 Using NIO Classes and Interfaces to Get File and Directory Information
      4. 15.4 Sequential Text Files
      5. 15.5 XML Serialization
      6. 15.6 FileChooser and DirectoryChooser Dialogs
      7. 15.7 (Optional) Additional java.io Classes
      8. 15.8 Wrap-Up
    25. 16. Generic Collections
      1. 16.1 Introduction
      2. 16.2 Collections Overview
      3. 16.3 Type-Wrapper Classes
      4. 16.4 Autoboxing and Auto-Unboxing
      5. 16.5 Interface Collection and Class Collections
      6. 16.6 Lists
      7. 16.7 Collections Methods
      8. 16.8 Class PriorityQueue and Interface Queue
      9. 16.9 Sets
      10. 16.10 Maps
      11. 16.11 Synchronized Collections
      12. 16.12 Unmodifiable Collections
      13. 16.13 Abstract Implementations
      14. 16.14 Java SE 9: Convenience Factory Methods for Immutable Collections
      15. 16.15 Wrap-Up
    26. 17. Lambdas and Streams
      1. 17.1 Introduction
      2. 17.2 Streams and Reduction
      3. 17.3 Mapping and Lambdas
      4. 17.4 Filtering
      5. 17.5 How Elements Move Through Stream Pipelines
      6. 17.6 Method References
      7. 17.7 IntStream Operations
      8. 17.8 Functional Interfaces
      9. 17.9 Lambdas: A Deeper Look
      10. 17.10 Stream<Integer> Manipulations
      11. 17.11 Stream<String> Manipulations
      12. 17.12 Stream<Employee> Manipulations
      13. 17.13 Creating a Stream<String> from a File
      14. 17.14 Streams of Random Values
      15. 17.15 Infinite Streams
      16. 17.16 Lambda Event Handlers
      17. 17.17 Additional Notes on Java SE 8 Interfaces
      18. 17.18 Wrap-Up
    27. 18. Recursion
      1. Outline
      2. 18.1 Introduction
      3. 18.2 Recursion Concepts
      4. 18.3 Example Using Recursion: Factorials
      5. 18.4 Reimplementing Class FactorialCalculator Using BigInteger
      6. 18.5 Example Using Recursion: Fibonacci Series
      7. 18.6 Recursion and the Method-Call Stack
      8. 18.7 Recursion vs. Iteration
      9. 18.8 Towers of Hanoi
      10. 18.9 Fractals
      11. 18.10 Recursive Backtracking
      12. 18.11 Wrap-Up
    28. 19. Generic Classes and Methods: A Deeper Look
      1. 19.1 Introduction
      2. 19.2 Motivation for Generic Methods
      3. 19.3 Generic Methods: Implementation and Compile-Time Translation
      4. 19.4 Additional Compile-Time Translation Issues: Methods That Use a Type Parameter as the Return Type
      5. 19.5 Overloading Generic Methods
      6. 19.6 Generic Classes
      7. 19.7 Wildcards in Methods That Accept Type Parameters
      8. 19.8 Wrap-Up
    29. 20. JavaFX Graphics, Animation and Video
      1. 20.1 Introduction
      2. 20.2 Controlling Fonts with Cascading Style Sheets (CSS)
      3. 20.3 Displaying Two-Dimensional Shapes
      4. 20.4 Polylines, Polygons and Paths
      5. 20.5 Transforms
      6. 20.6 Playing Video with Media, MediaPlayer and MediaViewer
      7. 20.7 Transition Animations
      8. 20.8 Timeline Animations
      9. 20.9 Frame-by-Frame Animation with AnimationTimer
      10. 20.10 Drawing on a Canvas
      11. 20.11 Three-Dimensional Shapes
      12. 20.12 Wrap-Up
    30. 21. Concurrency and Multi-Core Performance
      1. 21.1 Introduction
      2. 21.2 Thread States and Life Cycle
      3. 21.3 Creating and Executing Threads with the Executor Framework
      4. 21.4 Thread Synchronization
      5. 21.5 Producer/Consumer Relationship without Synchronization
      6. 21.6 Producer/Consumer Relationship: ArrayBlockingQueue
      7. 21.7 (Advanced) Producer/Consumer Relationship with synchronized, wait, notify and notifyAll
      8. 21.8 (Advanced) Producer/Consumer Relationship: Bounded Buffers
      9. 21.9 (Advanced) Producer/Consumer Relationship: The Lock and Condition Interfaces
      10. 21.10 Concurrent Collections
      11. 21.11 Multithreading in JavaFX
      12. 21.12 sort/parallelSort Timings with the Java SE 8 Date/Time API
      13. 21.13 Java SE 8: Sequential vs. Parallel Streams
      14. 21.14 (Advanced) Interfaces Callable and Future
      15. 21.15 (Advanced) Fork/Join Framework
      16. 21.16 Wrap-Up
    31. 22. Accessing Databases with JDBC
      1. 22.1 Introduction
      2. 22.2 Relational Databases
      3. 22.3 A books Database
      4. 22.4 SQL
      5. 22.5 Setting Up a Java DB Database
      6. 22.6 Connecting to and Querying a Database
      7. 22.7 Querying the books Database
      8. 22.8 RowSet Interface
      9. 22.9 PreparedStatements
      10. 22.10 Stored Procedures
      11. 22.11 Transaction Processing
      12. 22.12 Wrap-Up
    32. 23. Introduction to JShell: Java 9’s REPL for Interactive Java
      1. 23.1 Introduction
      2. 23.2 Installing JDK 9
      3. 23.3 Introduction to JShell
      4. 23.4 Command-Line Input in JShell
      5. 23.5 Declaring and Using Classes
      6. 23.6 Discovery with JShell Auto-Completion
      7. 23.7 Exploring a Class’s Members and Viewing Documentation
      8. 23.8 Declaring Methods
      9. 23.9 Exceptions
      10. 23.10 Importing Classes and Adding Packages to the CLASSPATH
      11. 23.11 Using an External Editor
      12. 23.12 Summary of JShell Commands
      13. 23.13 Keyboard Shortcuts for Snippet Editing
      14. 23.14 How JShell Reinterprets Java for Interactive Use
      15. 23.15 IDE JShell Support
      16. 23.16 Wrap-Up
    33. 24. Java Persistence API (JPA)
      1. 24.1 Introduction
      2. 24.2 JPA Technology Overview
      3. 24.3 Querying a Database with JPA
      4. 24.4 Named Queries; Accessing Data from Multiple Tables
      5. 24.5 Address Book: Using JPA and Transactions to Modify a Database
      6. 24.6 Web Resources
      7. 24.7 Wrap-Up
    34. 25. ATM Case Study, Part 1: Object-Oriented Design with the UML
      1. 25.1 Case Study Introduction
      2. 25.2 Examining the Requirements Document
      3. 25.3 Identifying the Classes in a Requirements Document
      4. 25.4 Identifying Class Attributes
      5. 25.5 Identifying Objects’ States and Activities
      6. 25.6 Identifying Class Operations
      7. 25.7 Indicating Collaboration Among Objects
      8. 25.8 Wrap-Up
    35. 26. ATM Case Study Part 2: Implementing an Object-Oriented Design
      1. 26.1 Introduction
      2. 26.2 Starting to Program the Classes of the ATM System
      3. 26.3 Incorporating Inheritance and Polymorphism into the ATM System
      4. 26.4 ATM Case Study Implementation
      5. 26.5 Wrap-Up
    36. 27. Java Platform Module System
      1. 27.1 Introduction
      2. 27.2 Module Declarations
      3. 27.3 Modularized Welcome App
      4. 27.4 Creating and Using a Custom Module
      5. 27.5 Module-Dependency Graphs: A Deeper Look
      6. 27.6 Migrating Code to Java 9
      7. 27.7 Resources in Modules; Using an Automatic Module
      8. 27.8 Creating Custom Runtimes with jlink
      9. 27.9 Services and ServiceLoader
      10. 27.10 Wrap-Up
    37. 28. Additional Java 9 Topics
      1. 28.1 Introduction
      2. 28.2 Recap: Java 9 Features Covered in Earlier Chapters
      3. 28.3 New Version String Format
      4. 28.4 Regular Expressions: New Matcher Class Methods
      5. 28.5 New Stream Interface Methods
      6. 28.6 Modules in JShell
      7. 28.7 JavaFX 9 Skin APIs
      8. 28.8 Other GUI and Graphics Enhancements
      9. 28.9 Security Related Java 9 Topics
      10. 28.10 Other Java 9 Topics
      11. 28.11 Items Removed from the JDK and Java 9
      12. 28.12 Items Proposed for Removal from Future Java Versions
      13. 28.13 Wrap-Up
    38. A. Operator Precedence Chart
    39. B. ASCII Character Set
    40. C. Keywords and Reserved Words
    41. D. Primitive Types
    42. E. Bit Manipulation
      1. E.1 Introduction
      2. E.2 Bit Manipulation and the Bitwise Operators
      3. E.3 BitSet Class
    43. F. Labeled break and continue Statements
      1. F.1 Introduction
      2. F.2 Labeled break Statement
      3. F.3 Labeled continue Statement
    44. Index