Java 8 Recipes, 2nd Edition

Java 8 Recipes, 2nd EditionReviews
Author: Josh Juneau
Pub Date: 2014
ISBN: 978-1-4302-6826-0
Pages: 627
Language: English
Format: PDF +code
Size: 10 Mb

Download

Java 8 Recipes offers solutions to common programming problems encountered while developing Java-based applications. Fully updated with the newest features and techniques available, Java 8 Recipes provides code examples involving Lambdas, embedded scripting with Nashorn, the new date-time API, stream support, functional interfaces, and much more. Especial emphasis is given to features such as lambdas that are newly introduced in Java 8. Content is presented in the popular problem-solution format: Look up the programming problem that you want to solve. Read the solution. Apply the solution directly in your own code. Problem solved!

+

New Features of Java 8

Java 8 has introduced a magnitude of new features, making it one of the most significant releases of Java in years. This chapter covers a handful of the new features of Java 8 and includes short examples of each. These recipes are meant to provide a brief overview of the some new features in Java 8 and to get you up and running quickly using them. This is by no means a comprehensive list of the new features that were introduced in Java 8, but more of a precursor of what to expect throughout this book. At the end of each recipe, you’re directed to the chapter in this book, or to an another resource, that covers the topic in more detail.

Java Language Enhancements

The release of Java 8 introduced a number of new features to the language, making it more productive, easier to manage, and more efficient. This section covers some of these new features via a variety of recipes.

Lambda expressions are one of the new features of Java 8 that will have a tremendous impact on productivity and business logic. Lambda expressions are anonymous blocks of code that accept zero or more parameters, encapsulate a series of statements or an expression, and return a result. They are analogous to closures from other languages, being anonymous functions that can be passed into other code as needed.

The syntax of a lambda expression includes an argument list, a new character to the language known as the “arrow token” (->), and a body. The following model represents the structure of a lambda expression:

The argument list for a lambda expression can include zero or more arguments. If there are no arguments, then an empty set of parentheses can be used. If there is only one argument, then no parentheses are required. Each argument in the list can include an optional type specification. If the type of the argument is not specified, then the
type is derived from the current context.

Lambda expressions are built on functional interfaces, which are simply Java interfaces containing a single abstract method. Simply put, the lambda expression itself is an anonymous inner class that implements a functional interface, which can then be assigned to a variable and invoked. This recipe is merely a primer for lambda expressions; to learn more about them, refer to Chapter 6.

In releases of JavaFX prior to JavaFX 8, there was no standard API for printing portions of an application stage. In JavaFX 8, a Print API has been added to standardize the way in which printing features are handled. The API also makes it easy to enable applications with printing functionality using very little code. The API is quite large, as it contains a number of classes, but it is very straight-forward and easy to use.

To enable print functionality for a specified node, start by working with the javafx.print.PrinterJobclass, as it contains all of the functionality for generating a very simple printing task. To send a node to the default system printer, simply invoke PrintJob.createPrinterJob()to return a PrinterJobobject. Once the object has been returned, check to ensure that it is not null, and then call its printPage()method, passing the node to be printed. The excerpt of the solution that contains this functionality is shown in the following lines of code: