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)

Example 3


package jdk8.byte_code;

import static jdk8.byte_code.WaitFor.waitFor;

public class Example3 {
    // simple lambda
    void example() throws InterruptedException {
        waitFor(() -> true);
    }
}
Classfile Example3.class
  Last modified 08-May-2014; size 1155 bytes
  MD5 checksum 22e120de85528efc921bb158588bbaa1
  Compiled from "Example3.java"
public class jdk8.byte_code.Example3
  SourceFile: "Example3.java"
  InnerClasses:
      public static final #50= #49 of #53; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
  BootstrapMethods:
    0: #23 invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
  Method arguments:
    #24 ()Ljava/lang/Boolean;
    #25 invokestatic jdk8/byte_code/Example3.lambda$example$25:()Ljava/lang/Boolean;
    #24 ()Ljava/lang/Boolean;
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
 #1 = Methodref        #6.#21    // java/lang/Object."<init>":()V
 #2 = InvokeDynamic    #0:#26    // #0:isSatisfied:()Ljdk8/byte_code/Condition; 
 #3 = Methodref        #27.#28   // jdk8/byte_code/WaitFor.waitFor:(Ljdk8/byte_code/Condition;)V 
 #4 = Methodref        #29.#30   // java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean; 
 #5 = Class            #31       // jdk8/byte_code/Example3 
 #6 = Class            #32       // java/lang/Object 
 #7 = Utf8             <init> 
 #8 = Utf8             ()V 
 #9 = Utf8             Code 
 #10 = Utf8            LineNumberTable 
 #11 = Utf8            LocalVariableTable 
 #12 = Utf8            this 
 #13 = Utf8            Ljdk8/byte_code/Example3; 
 #14 = Utf8            example 
 #15 = Utf8            Exceptions 
 #16 = Class           #33       // java/lang/InterruptedException 
 #17 = Utf8            lambda$example$25 
 #18 = Utf8            ()Ljava/lang/Boolean; 
 #19 = Utf8            SourceFile 
 #20 = Utf8            Example3.java 
 #21 = NameAndType     #7:#8     // "<init>":()V 
 #22 = Utf8            BootstrapMethods 
 #23 = MethodHandle    #6:#34    // invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; 
 #24 = MethodType      #18       // ()Ljava/lang/Boolean;
 #25 = MethodHandle    #6:#35    // invokestatic jdk8/byte_code/Example3.lambda$example$25:()Ljava/lang/Boolean; 
 #26 = NameAndType     #36:#37   // isSatisfied:()Ljdk8/byte_code/Condition;     
 #27 = Class           #38       // jdk8/byte_code/WaitFor 
 #28 = NameAndType     #39:#40   // waitFor:(Ljdk8/byte_code/Condition;)V 
 #29 = Class           #41       // java/lang/Boolean 
 #30 = NameAndType     #42:#43   // valueOf:(Z)Ljava/lang/Boolean; 
 #31 = Utf8            jdk8/byte_code/Example3 
 #32 = Utf8            java/lang/Object
 #33 = Utf8            java/lang/InterruptedException 
 #34 = Methodref       #44.#45   // java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; 
 #35 = Methodref       #5.#46    // jdk8/byte_code/Example3.lambda$example$25:()Ljava/lang/Boolean; 
 #36 = Utf8            isSatisfied 
 #37 = Utf8            ()Ljdk8/byte_code/Condition;
 #38 = Utf8            jdk8/byte_code/WaitFor
 #39 = Utf8            waitFor
 #40 = Utf8            (Ljdk8/byte_code/Condition;)V 
 #41 = Utf8            java/lang/Boolean 
 #42 = Utf8            valueOf 
 #43 = Utf8            (Z)Ljava/lang/Boolean;
 #44 = Class           #47       // java/lang/invoke/LambdaMetafactory    
 #45 = NameAndType     #48:#52   // metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; 
 #46 = NameAndType     #17:#18  // lambda$example$25:()Ljava/lang/Boolean;  
 #47 = Utf8            java/lang/invoke/LambdaMetafactory 
 #48 = Utf8            metafactory 
 #49 = Class           #54      // java/lang/invoke/MethodHandles$Lookup 
 #50 = Utf8            Lookup 
 #51 = Utf8            InnerClasses 
 #52 = Utf8 (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; 
 #53 = Class           #55        // java/lang/invoke/MethodHandles
 #54 = Utf8            java/lang/invoke/MethodHandles$Lookup 
 #55 = Utf8            java/lang/invoke/MethodHandles 
{ 
public jdk8.byte_code.Example3();
 descriptor: ()V
 flags: ACC_PUBLIC
 Code:
  stack=1, locals=1, args_size=1
    0: aload_0
    1: invokespecial #1           // Method java/lang/Object."<init>":()V 
    4: return
 LineNumberTable:
 line 6: 0
 LocalVariableTable:
 Start Length Slot Name Signature
     0      5    0 this Ljdk8/byte_code/Example3;
void example() throws java.lang.InterruptedException;
    descriptor: ()V
    flags:
    Code:
      stack=1, locals=1, args_size=1
         0: invokedynamic #2,  0    // InvokeDynamic #0:isSatisfied:()Ljdk8/byte_code/Condition;
         5: invokestatic  #3       // Method jdk8/byte_code/WaitFor.waitFor:(Ljdk8/byte_code/Condition;)V
         8: return
      LineNumberTable:
        line 10: 0
        line 11: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  this   Ljdk8/byte_code/Example3;
    Exceptions:
      throws java.lang.InterruptedException
}