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)

Descriptors


Opcodes will often use parameters, these look a little cryptic in the bytecode as they're usually referenced via lookup tables. Internally, Java uses what's called descriptors to describe these parameters.

They describe types and signatures using a specific grammar you'll see throughout the bytecode. You'll often see the same grammar used in compiler or debug output, so it's useful to recap it here.

Here's an example of a method signature descriptor.

Example$1."<init>":(Lcom/foo/Example;Lcom/foo/Server;)V

It's describing the constructor of a class called $1, which we happen to know is the JVM's name for the first anonymous class instance within another class. In this case Example. So we've got a constructor of an anonymous class that takes two parameters, an instance of the outer class com.foo.Example and an instance of com.foo.Server.

Being a constructor, the method doesn't return anything. The V symbol represents void.

Have a look at breakdown of the descriptor syntax below...