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 11 Cookbook

Book Description

Solutions for modular, functional, reactive, GUI, network, and multithreaded programming

Key Features

  • Explore the latest features of Java 11 to implement efficient and reliable code
  • Develop memory-efficient applications, understanding new garbage collection in Java 11
  • Create restful webservices and microservices with Spring boot 2 and Docker

Book Description

For more than three decades, Java has been on the forefront of developing robust software that has helped versatile businesses meet their requirements. Being one of the most widely used programming languages in history, it's imperative for Java developers to discover effective ways of using it in order to take full advantage of the power of the latest Java features. Java 11 Cookbook offers a range of software development solutions with simple and straightforward Java 11 code examples to help you build a modern software system.

Starting with the installation of Java, each recipe addresses various problem by explaining the solution and offering insights into how it works. You'll explore the new features added to Java 11 that will make your application modular, secure, and fast. The book contains recipes on functional programming, GUI programming, concurrent programming, and database programming in Java. You'll also be taken through the new features introduced in JDK 18.3 and 18.9.

By the end of this book, you'll be equipped with the skills required to write robust, scalable, and optimal Java code effectively.

What you will learn

  • Set up JDK and understand what's new in the JDK 11 installation
  • Implement object-oriented designs using classes and interfaces
  • Manage operating system processes
  • Create a modular application with clear dependencies
  • Build graphical user interfaces using JavaFX
  • Use the new HTTP Client API
  • Explore the new diagnostic features in Java 11
  • Discover how to use the new JShell REPL tool

Who this book is for

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

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 files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Java 11 Cookbook Second Edition
  3. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  6. Installation and a Sneak Peek into Java 11
    1. Introduction
    2. Installing JDK 18.9 on Windows and setting up the PATH variable
      1. How to do it...
    3. Installing JDK 18.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. What's new in Java 11?
      1. Getting ready
      2. How to do it...
        1. JEP 318 – Epsilon
        2. JEP 321 – HTTP Client (Standard)
        3. JEP 323 – Local-Variable Syntax for Lambda Parameters
        4. JEP 333 – ZGC
        5. New API
      3. There's more...
    6. Using application class-data sharing
      1. Getting ready
      2. How to do it...
  7. Fast Track to OOP - Classes and Interfaces
    1. Introduction
    2. Implementing object-oriented design (OOD)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Using inner classes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Using inheritance and aggregation
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Aggregation makes the design more extensible
    5. Coding to an interface
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Creating interfaces with default and static methods
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Creating interfaces with private methods
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    8. A better way to work with nulls using Optional
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Using the utility class Objects
      1. Getting ready
      2. How to do it...
      3. How it works...
  8. 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-Project Jigsaw JDK applications
      1. Getting ready
      2. How to do it...
    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 the 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 multi-release 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...
    13. Making your library module-path-friendly
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more... 
    14. How to open a module for reflection
      1. Getting ready
      2. How to do it...
      3. How it works...
  9. Going Functional
    1. Introduction
    2. Using standard functional interfaces
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Creating a functional interface
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Understanding lambda expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Using lambda expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Using method references
      1. Getting ready
      2. How to do it...
        1. Static unbound method reference
        2. Non-static bound method reference
        3. Non-static unbound method reference
        4. Constructor method references
      3. There's more...
    7. Leveraging lambda expressions in your programs
      1. Getting ready
      2. How to do it...
      3. There's more...
  10. Streams and Pipelines
    1. Introduction
    2. Creating immutable collections using the of() and copyOf() factory methods
      1. Getting ready
      2. How to do it...
      3. There's more...
    3. Creating and operating on streams
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using numeric streams for arithmetic operations
      1. Getting ready
      2. How to do it...
      3. There's more...
    5. Completing streams by producing collections
      1. Getting ready
      2. How to do it...
    6. Completing streams by producing maps
      1. Getting ready
      2. How to do it...
    7. Completing streams by producing maps using grouping collectors
      1. Getting ready
      2. How to do it...
      3. There's more...
    8. Creating stream operation pipeline
      1. Getting ready
      2. How to do it...
      3. There's more...
    9. Processing streams in parallel
      1. Getting ready
      2. How to do it...
  11. Database Programming
    1. Introduction
    2. Connecting to a database using JDBC
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Setting up the tables required for DB interactions
      1. Getting ready
      2. How it works...
      3. There's more...
    4. Performing CRUD operations using JDBC
      1. Getting ready
      2. How to do it...
      3. There's more...
    5. Using the Hikari Connection Pool (HikariCP)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Using prepared statements
      1. Getting ready
      2. How to do it...
      3. There's more...
    7. Using transactions
      1. Getting ready
      2. How to do it...
      3. There's more...
    8. Working with large objects
      1. Getting ready
      2. How to do it...
      3. There's more...
    9. Executing stored procedures
      1. Getting ready
      2. How to do it...
      3. There's more...
    10. Using batch operations for a large set of data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    11. Using MyBatis for CRUD operations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    12. Using the Java Persistence API and Hibernate
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. 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...
    3. Different synchronization approaches
      1. Getting ready
      2. How to do it...
      3. There's more...
    4. Immutability as a means of achieving concurrency
      1. Getting ready
      2. How to do it...
      3. There's more...
    5. Using concurrent collections
      1. Getting ready
      2. How to do it...
      3. How it works...
    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...
    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...
  13. 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's 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...
  14. 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...
    8. Monitoring the Spring Boot 2 application using Micrometer and Prometheus
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more
  15. 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's more...
    7. Making an HTTP request using the Unirest HTTP client library
      1. Getting ready
      2. How to do it...
      3. There's more...
  16. Memory Management and Debugging
    1. Introduction
    2. Understanding the G1 garbage collector
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Unified logging for JVM
      1. Getting ready
      2. How to do it...
    4. Using the jcmd command for the JVM
      1. How to do it...
      2. How it works...
    5. Try-with-resources for better resource handling
      1. How to do it...
      2. How it works...
    6. Stack walking for improved debugging
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using the memory-aware coding style
      1. How to do it...
    8. Best practices for better memory usage
      1. How to do it...
    9. Understanding Epsilon, a low-overhead garbage collector
      1. How to do it...
  17. 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...
  18. Working with New Date and Time APIs
    1. Introduction
    2. How to work with time zone-independent date and time instances
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. How to construct time zone-dependent time instances
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. How to create a date-based period between date instances
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. How to create a time-based period between time instances
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. How to represent epoch time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. How to manipulate date and time instances
      1. Getting ready
      2. How to do it… 
      3. There's more…
    8. How to compare date and time
      1. Getting ready
      2. How to do it…
      3. There's more…
    9. How to work with different calendar systems
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    10. How to format dates using the DateTimeFormatter
      1. Getting ready
      2. How to do it…
      3. How it works…
  19. Testing
    1. Introduction
    2. Behavioral testing using Cucumber
      1. How to do it...
      2. How it works...
    3. Unit testing of an API using JUnit
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Unit testing by mocking dependencies
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Using fixtures to populate data for testing
      1. How to do it...
      2. How it works...
    6. Integration testing
      1. Getting ready
      2. How to do it...
      3. How it works...
  20. The New Way of Coding with Java 10 and Java 11
    1. Introduction
    2. Using local-variable type inference
      1. Getting ready
      2. How to do it...
    3. Using local-variable syntax for lambda parameters
      1. Getting ready
      2. How to do it...
  21. 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. Using CSS to the 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...
    6. Creating a pie chart
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Embedding HTML in an application
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    8. Embedding media in an application
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Adding effects to controls
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    10. Using the Robot API
      1. Getting ready
      2. How to do it...
      3. How it works...
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think