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 Cookbook

Book Description

A definitive guide to learning the key concepts of modern application development in Java

About This Book

  • Learn the latest features of Java 9
  • Extend your Java knowledge and take your application to new levels by making it fast, secure, and scalable
  • Delve into the intricacies of Modular programming in Java 9

Who This Book Is For

The book is for intermediate to advanced Java programmers who want to make their applications fast, secure, and scalable.

What You Will Learn

  • Set up JDK and know the differences in the JDK 9 installation
  • Implement OO designs using classes and interfaces
  • Manage operating system processes
  • Understand the new modular JDK and modular programming
  • Create a modular application with clear dependencies
  • Build graphical user interfaces using JavaFX
  • Use the new HTTP Client API
  • Learn about the new diagnostic features in Java 9
  • See how to use the new jShell REPL tool
  • Execute ES6-compliant JavaScript code from your Java applications

In Detail

Java is an object-oriented programming language. It is one of the most widely accepted languages because of its design and programming features, particularly in its promise that you can write a program once and run it anywhere.

This cookbook offers a range of software development examples in simple and straightforward Java 9 code, providing step-by-step resources and time-saving methods to help you solve data problems efficiently. Starting with the installation of Java, each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works.

We cover major concepts such as Project Jigsaw and various tools that will enable you to modularize your applications. You will learn new features in the form of recipes that will make your applications modular, secure, and fast.

Style and approach

Each recipe in this book contains a series of complete instructions to perform a core task, plus an explanation of the purpose behind it. Similar methods or further refinements are included with each recipe to provide comprehensive guidance.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Conventions
    6. Reader feedback
    7. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Installation and a Sneak Peek into Java 9
    1. Introduction
    2. Installing JDK 9 on Windows and setting up the PATH variable
      1. How to do it...
    3. Installing JDK 9 on Linux (Ubuntu, x64) and configuring the PATH variable
      1. How to do it...
    4. Compiling and running a Java application
      1. Getting ready
      2. How to do it...
    5. New features in Java 9
      1. How to do it...
        1. JEP 102 -- Process API updates
        2. JEP 110 -- HTTP/2 client
        3. JEP 213 -- milling project coin
        4. JEP 222: jshell -- the Java shell (Read-Eval-Print Loop)
        5. JEP 238 -- multi-release JAR files
        6. JEP 266 -- more concurrency updates
        7. Project Jigsaw
      2. There's more...
    6. Using new tools in JDK 9
      1. Getting ready
      2. How to do it...
        1. jdeprscan
        2. jdeps
        3. jlink
        4. jmod
        5. JShell
    7. Comparing JDK 8 and JDK 9
      1. Getting ready
      2. How to do it...
      3. See also
  3. Fast Track to OOP - Classes and Interfaces
    1. Introduction
    2. Implementing object-oriented design using classes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Using inner classes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using inheritance and composition to make the design extensible
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Composition makes the design more extensible
      4. See also
    5. Coding to an interface
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Creating interfaces with default and static methods
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Creating interfaces with private methods
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Using enums to represent constant entities
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Using the @Deprecated annotation to deprecate APIs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    10. Using HTML5 in Javadocs
      1. Getting ready
      2. How to do it...
  4. Modular Programming
    1. Introduction
    2. Using jdeps to find dependencies in a Java application
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Creating a simple modular application
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Creating a modular JAR
      1. Getting ready
      2. How to do it...
    5. Using a module JAR with pre-JDK 9 applications
      1. Getting ready
      2. How to do it...
      3. See also
    6. Bottom-up migration
      1. Getting ready
      2. How to do it...
        1. Modularizing banking.util.jar
        2. Modularizing math.util.jar
        3. Modularizing calculator.jar
      3. How it works...
    7. Top-down migration
      1. Getting ready
      2. How to do it...
        1. Modularizing the calculator
        2. Modularizing banking.util
        3. Modularizing math.util
    8. Using services to create loose coupling between consumer and provider modules
      1. Getting ready
      2. How to do it...
    9. Creating a custom modular runtime image using jlink
      1. Getting ready
      2. How to do it...
    10. Compiling for older platform versions
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Creating multirelease JARs
      1. How to do it...
      2. How it works...
    12. Using Maven to develop a modular application
      1. Getting ready
      2. How to do it...
  5. Going Functional
    1. Introduction
    2. Understanding and creating a functional interface
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Understanding lambda expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using method references
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    5. Creating and invoking lambda-friendly APIs
      1. Getting ready
      2. How it works...
      3. There's more...
      4. See also
    6. Leveraging lambda expressions in your programs
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
  6. Stream Operations and Pipelines
    1. Introduction
    2. Using the new factory methods to create collection objects
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    3. Creating and operating on streams
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Creating an operation pipeline on streams
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    5. Parallel computations on streams
      1. Getting ready
      2. How to do it...
      3. See also
  7. Database Programming
    1. Introduction
    2. Connecting to a database using JDBC
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    3. Setting up the tables required for DB interactions
      1. Getting ready
      2. How it works...
      3. There's more...
      4. See also
    4. Performing CRUD operations
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    5. Using prepared statements
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    6. Using transactions
      1. Getting ready
      2. How to do it...
      3. There's more...
    7. Working with large objects
      1. Getting ready
      2. How to do it...
      3. There's more...
    8. Executing stored procedures
      1. Getting ready
      2. How to do it...
      3. There's more...
  8. Concurrent and Multithreaded Programming
    1. Introduction
    2. Using the basic element of concurrency - thread
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    3. Different synchronization approaches
      1. Getting ready
      2. How to do it...
      3. There's more...
      4.  See also
    4. Immutability as a means to achieve concurrency
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    5. Using concurrent collections
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using the executor service to execute async tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Using fork/join to implement divide-and-conquer
      1. Getting ready
      2. How to do it...
    8. Using flow to implement the publish-subscribe pattern
      1. Getting ready
      2. How to do it...
  9. Better Management of the OS Process
    1. Introduction
    2. Spawning a new process
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Redirecting the process output and error streams to file
      1. Getting ready
      2. How to do it...
      3. There is more...
    4. Changing the working directory of a subprocess
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Setting the environment variable for a subprocess
      1. How to do it...
      2. How it works...
    6. Running shell scripts
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Obtaining the process information of the current JVM
      1. How to do it...
      2. How it works...
    8. Obtaining the process information of the spawned process
      1. Getting ready
      2. How to do it...
    9. Managing the spawned process
      1. How to do it...
    10. Enumerating live processes in the system
      1. How to do it...
    11. Connecting multiple processes using pipe
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Managing subprocesses
      1. Getting ready
      2. How to do it...
      3. How it works...
  10. GUI Programming Using JavaFX
    1. Introduction
    2. Creating a GUI using JavaFX controls
      1.  Getting ready
      2. How to do it...
      3. How it works...
    3. Using the FXML markup to create a GUI
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also 
    4. Using CSS to style elements in JavaFX
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating a bar chart
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Creating a pie chart
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Creating a line chart
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Creating an area chart
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Creating a bubble chart
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Creating a scatter chart
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Embedding HTML in an application
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    12. Embedding media in an application
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    13. Adding effects to controls
      1. How to do it...
      2. How it works...
      3. There's more...
    14. Using the new TIFF I/O API to read TIFF images
      1. Getting ready
      2. How to do it...
  11. RESTful Web Services Using Spring Boot
    1. Introduction
    2. Creating a simple Spring Boot application
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Interacting with the database
      1. Getting ready
        1. Installing MySQL tools
        2. Creating a sample database
        3. Creating a person table
        4. Populating sample data
      2. How to do it...
      3. How it works...
    4. Creating a RESTful web service
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating multiple profiles for Spring Boot
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Deploying RESTful web services to Heroku
      1. Getting ready 
        1. Setting up a Heroku account
        2. Creating a new app from the UI
        3. Creating a new app from the CLI
      2. How to do it...
      3. There's more...
    7. Containerizing the RESTful web service using Docker
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. Networking
    1. Introduction
    2. Making an HTTP GET request
      1. How to do it...
      2. How it works...
    3. Making an HTTP POST request
      1. How to do it...
    4. Making an HTTP request for a protected resource
      1. How to do it...
      2. How it works...
    5. Making an asynchronous HTTP request
      1. How to do it...
    6. Making an HTTP request using Apache HttpClient
      1. Getting ready
      2. How to do it...
      3. There is more...
    7. Making an HTTP request using the Unirest HTTP client library
      1. Getting ready
      2. How to do it...
      3. There's more...
  13. Memory Management and Debugging
    1. Introduction
    2. Understanding the G1 garbage collector
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Unified logging for JVM
      1. Getting ready
      2. How to do it...
      3. See also
    4. Using the new diagnostic commands for the JVM
      1. How to do it...
      2. How it works...
      3. See also
    5. Try with resources for better resource handling
      1. How to do it...
      2. How it works...
      3. See also
    6. Stack walking for improved debugging
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Some best practices for better memory usage
      1. How it  works...
      2. See also
  14. The Read-Evaluate-Print Loop (REPL) Using JShell
    1. Introduction
    2. Getting familiar with REPL
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Navigating JShell and its commands
      1. How to do it...
    4. Evaluating code snippets
      1. How to do it...
      2. There's more...
    5. Object-oriented programming in JShell
      1. How to do it...
    6. Saving and restoring the JShell command history
      1. How to do it...
    7. Using the JShell Java API
      1. How to do it...
      2. How it works...
  15. Scripting Using Oracle Nashorn
    1. Introduction
    2. Using the jjs command-line tool
      1. Getting ready
      2. How to do it...
      3. There's more...
    3. Embedding the Oracle Nashorn engine
      1. Getting ready
      2. How to do it...
    4. Invoking Java from Oracle Nashorn
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Using the ES6 features implemented in Oracle Nashorn
      1. How to do it... 
  16. Testing
    1. Introduction
    2. Unit testing of an API using JUnit
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Unit testing by mocking dependencies
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using fixtures to populate data for testing
      1. How to do it...
      2. How it works...
      3. See also
    5. Behavioral testing
      1. Getting ready
      2. How to do it...
      3. How it works...