O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Java Lambdas

Book Description

Understand and utilize Java Lambdas

About This Book

  • Take a deep dive into one of the single most important additions to modern Java
  • Master Java lambdas, and fully understand functions, classes, and scope.
  • Improve your programming skills, which will enable you to write cleaner and more dynamic code

Who This Book Is For

Learning Java Lambdas is for developers looking to upgrade their Java skills and familiarize themselves with one of the most important new features of Java. This book is not for absolute beginners and will be more suited to professionals who are already comfortable with Java coding. You should have a basic knowledge of Java before reading this book.

What You Will Learn

  • What a lambda is and how it differs from other Java features
  • How to use lambdas effectively in your own projects
  • The use of method references and advanced scoping
  • The difference between lambdas and closures
  • The differences in bytecode produced when using lambdas

In Detail

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.

Style and approach

This book is a deep dive into one of the core new features of the Java language – Lambdas. It covers them in great details, making sure that you fully understand how lambdas work, and how they can be put to use in your own programs.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. Learning Java Lambdas
    1. Learning Java Lambdas
    2. Credits
    3. About the Author
    4. www.PacktPub.com
      1. Why subscribe?
    5. Customer Feedback
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the color images of this book
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Introduction
      1. The road to modern Java
        1. Modern Java features
    8. 2. Lambdas Introduction
      1. λs in functional programming
        1. 1930s and the lambda calculus
        2. 1950s and LISP
        3. What is a lambda
      2. Functions vs classes
        1. Lambdas in modern Java
        2. Some theoretical differences
        3. Functions vs classes
        4. Some concrete differences
          1. Capture semantics
          2. Shadowed variables
        5. Summary
      3. λ basic syntax
        1. Syntax breakdown
        2. Summary
    9. 3. Lambdas in Depth
      1. Functional interfaces
        1. @FunctionalInterface
        2. Extension
        3. Other interface improvements
        4. Summary
      2. Type inference improvements
        1. Java type inference
        2. Target-typing for lambdas
          1. Type parameters in method calls
          2. Type parameters in chained method calls
      3. Method references
        1. Example
        2. Method reference types
        3. Constructor reference
        4. Static method reference
        5. Instance method reference of particular object (in this case, a closure)
        6. Instance method reference of a arbitrary object whose instance is supplied later (lambda)
        7. Summary
      4. Scoping
        1. Effectively final
          1. Circumventing final
      5. Exception handling
        1. Using a callback
        2. Dealing with exceptions when writing lambdas
        3. As a caller (dealing with exceptions when calling lambdas)
      6. Lambdas vs closures
        1. Basic differences
        2. Other differences
        3. Summary
      7. Bytecode recap
      8. Descriptors
      9. Converting a method signature
      10. Code examples
      11. Example 1
      12. Example 2
      13. Example 3
      14. Example 4
      15. Example 4 (with method reference)
      16. Example 5
      17. Summary
    10. A. Bytecode
      1. WaitFor
      2. Example 1
      3. Example 2
      4. Example 3
      5. Example 4
      6. Example 4 (with Method Reference)
      7. Example 5