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)

Bytecode recap


To start with, let's recap on what we know about bytecode.

To get from source code to machine runnable code. The Java compiler produces bytecode. This is either interpreted by the JVM or re-compiled by the Just-in-time compiler.

When it's interpreted, the bytecode is turned into machine code on the fly and executed. This happens each time the bytecode is encountered but he JVM.

When it's Just-in-time compiled, the JVM compiles it directly into machine code the first time it's encountered and then goes on to execute it.

Both happen at run-time but Just-in-time compilation offer lots of optimizations.

So, Java bytecode is the intermediate representation between source code and machine code.

Note

As a quick side bar: Java's JIT compiler has enjoyed a great reputation over the years. But going back full circle to our introduction, it was John McCarthy that first wrote about JIT compilation way back in 1960. So it's interesting to think that it's not just lambda support that was influenced...