Book Image

Learning Java Lambdas

By : Toby Weston
Book Image

Learning Java Lambdas

By: Toby Weston

Overview of this book

In this short book, we take an in-depth look at lambdas in Java, and their supporting features. The book covers essential topics, such as functional interfaces and type inference, and the key differences between lambdas and closures. You will learn about the background to functional programming and lambdas, before moving on to understanding the basic syntax of lambdas and what differentiates these anonymous functions from standard anonymous classes. Lastly, you'll learn how to invoke lambdas and look at the bytecode generated. After reading this book, you'll understand lambdas in depth, their background, syntax, implementation details, and how and when to use them. You'll also have a clear knowledge of the difference between functions and classes, and why that's relevant to lambdas. This knowledge will enable you to appreciate the improvements to type inference that drive a lot of the new features in modern Java, and will increase your understanding of method references and scoping.
Table of Contents (10 chapters)

Lambdas vs closures


The terms closure and lambda are often used interchangeably but they are actually distinct. In this section we'll take a look at the differences so you can be clear about which is which.

Below is a table showing the release dates for each major version of Java. Java 5.0 came along in 2004 and included the first major language changes including things like generics support:

Around 2008 to 2010 there was a lot of work going on to introduce closures to Java. It was due to go in to Java 7 but didn't quite make it in time. Instead it evolved into lambda support in Java 8. Unfortunately, around that time, people used the term "closures" and "lambdas" interchangeably and so it's been a little confusing for the Java community since. In fact, there's still a project page on the OpenJDK site for closures and one for lambdas.

From the OpenJDK project's perspective, they really should have been using "lambda" consistently from the start. In fact, the OpenJDK got it so wrong, they...