Book Image

Java 9 Cookbook

By : Mohamed Sanaulla, Nick Samoylov
Book Image

Java 9 Cookbook

By: Mohamed Sanaulla, Nick Samoylov

Overview of this book

<p>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.</p> <p>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.</p> <p>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.</p>
Table of Contents (22 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

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 about the core programming language as well as common tasks to build a wide variety of software. You will learn new features in the form of recipes, to make your application modular, secure, and fast.

What this book covers

Chapter 1, Installation and Sneak Peek into Java 9, helps you to set up the development environment for running your Java programs and gives a brief overview of the new features and tools in Java 9

Chapter 2, Fast Track to OOP - Classes and Interfaces, covers object-oriented programming principles and design solutions, including inner classes, inheritance, composition, interfaces, enumerations, and the Java 9 changes to Javadocs.

Chapter 3, Modular Programming, introduces jigsaw as a major feature and a huge leap for the Java ecosystem. This chapter demonstrates how to use tools, such as jdeps and jlink, to create simple modular applications, and related artifacts such as modular JARs, and finally how to modularize your pre-Java 9 applications.

Chapter 4, Going Functional, introduces a programming paradigm called functional programming and its applicability in Java 9. Topics covered include functional interfaces, lambda expressions, and lambda-friendly APIs.

Chapter 5, Stream Operations and Pipelines, shows how to leverage streams and chain multiple operations on a collection to create a pipeline, use factory methods to create collection objects, create and operate on streams, and create an operation pipeline on streams, including parallel computations.

Chapter 6, Database Programming, covers both basic and commonly used interactions between a Java application and a database, right from connecting to the database and performing CRUD operations to creating transactions, storing procedures, and working with large objects.

Chapter 7, Concurrent and Multithreaded Programming, presents different ways of incorporating concurrency and some best practices, such as synchronization and immutability. We will also discuss the implementation of some commonly used patterns, such as divide-conquer and publish-subscribe, using the constructs provided by Java.

Chapter 8, Better Management of the OS Process, elaborates on the new API enhancements around the Process API.

Chapter 9, GUI Programming Using JavaFX, shows how to get started with creating JavaFX applications, leverage CSS styling into your applications, build a GUI in a declarative way using FXML, and use the graph, media, and browser components of JavaFX.

Chapter 10, RESTful Webservices Using Spring Boot, deals with creating simple RESTful webservices using Spring boot, deploying them to Heroku, and finally dockerizing Spring boot-based RESTful webservice applications.

Chapter 11, Networking, shows you how to use different HTTP client API libraries, namely the API provided in Java 9 as an incubator module, the Apache HTTP client, and the Unirest HTTP client API.

Chapter 12, Memory Management and Debugging, explores managing the memory of a Java application, including an introduction to the garbage collection algorithm used in Java 9, and some new features, which help in advanced application diagnostics. We'll also show how to manage resources by using the new try-with-resources construct and the new stack walking API.

Chapter 13, The Read-Evaluate-Print Loop (REPL) Using JShell, shows you how to work with the new REPL tool and JShell, provided as part of the JDK.

Chapter 14, Scripting Using Oracle Nashorn, shows how to interoperate between JavaScript and Java using the Oracle Nashorn JavaScript engine and how to use the jjs command-line tool to run JavaScript. It also explores Oracle Nashorn's support for the new ECMAScript 6.

Chapter 15, Testing, explains how to unit-test your APIs before they are integrated with other components, including stubbing dependencies with some dummy data and mocking dependencies. We will also show you how to write fixtures to populate the test data and then how to test your application behavior by integrating different APIs and testing them.

What you need for this book

To run the code examples, you will need a computer with 2GB RAM at least, 10GB free disk space, and Windows or Linux OS. The following software/libraries are required:

  • JDK 9 (for all chapters)
  • PostgreSQL 9.4 DB (for Chapter 6, Database Programming)
  • Junit 4.12 (for Chapter 15, Testing)
  • Mockito 2.7.13 (for Chapter 15, Testing)
  • Maven 3.5.0 (for Chapter 3, Modular Programming)
  • MySQL 5.7.19 DB (for Chapter 10, RESTful Webservices Using Spring Boot)
  • Heroku CLI (for Chapter 10, RESTful Webservices Using Spring Boot)
  • Docker (for Chapter 10, RESTful Webservices Using Spring Boot)

Who this book is for

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

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it…, How it works…, There's more…, and See also). To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "When run, it produces the same value as with an object of the Vehicle class." A block of code is set as follows:

module newfeatures{
  requires jdk.incubator.httpclient;
}

Any command-line input or output is written as follows:

$> vim ~/.bash_aliases

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Right-click on My Computer and then click on Properties."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book 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. You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account. Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Java-9-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

 

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.