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 4


package jdk8.byte_code;

import static jdk8.byte_code.Server.HttpServer;
import static jdk8.byte_code.WaitFor.waitFor;

public class Example4 {
    // lambda with arguments
    void example() throws InterruptedException {
        waitFor(new HttpServer(), (server) -> server.isRunning());
    }
}
Classfile Example4.class
   Last modified 08-May-2014; size 1414 bytes
   MD5 checksum 7177f97fdf30b0648a09ab98109a479c
   Compiled from "Example4.java"
 public class jdk8.byte_code.Example4
   SourceFile: "Example4.java"
   InnerClasses:
     public static #21= #2 of #29; //HttpServer=class jdk8/byte_code/Server$HttpServer of class jdk8/byte_code/Server 
     public static final #65= #64 of #67; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles 
  BootstrapMethods:
    0: #32 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:
   #33 (Ljava/lang/Object;)Z
   #34 invokestatic jdk8/byte_code/Example4.lambda$example33 : (L
j
d
k8/b
y
t
e

c

o
d
e/S
e
r
v
e
rHttpServer;)Z
   #35 (Ljdk8/byte_code/Server$HttpServer;)Z
 minor version: 0
 major version: 52
 flags: ACC_PUBLIC, ACC_SUPER
 Constant pool:
 #1 = Methodref         #9.#28     //java/lang/Object."<init>":()V    
 #2 = Class             #30        //jdk8/byte_code/Server$HttpServer   
 #3 = Methodref         #2.#28     //jdk8/byte_code/Server$HttpServer."<init>":()V 
 #4 = InvokeDynamic     #0:#36     // #0:test:()Ljava/util/function/Predicate;   
 #5 = Methodref         #37.#38    // jdk8/byte_code/WaitFor.waitFor:(Ljava/lang/Object;Ljava/util/function/Predicate;)V 
 #6 = Methodref         #2.#39    // jdk8/byte_code/Server$HttpServer.isRunning:()Ljava/lang/Boolean;
 #7 = Methodref         #40.#41   // java/lang/Boolean.booleanValue:()Z   
 #8 = Class             #42       // jdk8/byte_code/Example4 
 #9 = Class             #43       //java/lang/Object 
 #10 = Utf8             <init>
 #11 = Utf8             ()V 
 #12 = Utf8             Code 
 #13 = Utf8             LineNumberTable 
 #14 = Utf8             LocalVariableTable 
 #15 = Utf8             this 
 #16 = Utf8             Ljdk8/byte_code/Example4; 
 #17 = Utf8             example 
 #18 = Utf8             Exceptions 
 #19 = Class            #44       // java/lang/InterruptedException 
 #20 = Utf8             lambda$example$33 
 #21 = Utf8             HttpServer 
 #22 = Utf8             InnerClasses 
 #23 = Utf8             (Ljdk8/byte_code/Server$HttpServer;)Z 
 #24 = Utf8             server 
 #25 = Utf8             Ljdk8/byte_code/Server$HttpServer; 
#26 = Utf8              SourceFile 
#27 = Utf8              Example4.java 
#28 = NameAndType       #10:#11   // "<init>":()V 
#29 = Class             #45       // jdk8/byte_code/Server 
#30 = Utf8              jdk8/byte_code/Server$HttpServer 
#31 = Utf8              BootstrapMethods 
#32 = MethodHandle      #6:#46    // 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; 
#33 = MethodType        #47       // (Ljava/lang/Object;)Z 
#34 = MethodHandle      #6:#48    // invokestatic jdk8/byte_code/Example4.lambda$example33 : (L
j
d
k8/b
y
t
e

c

o
d
e/S
e
r
v
e
rHttpServer;)Z 
#35 = MethodType        #23       // (Ljdk8/byte_code/Server$HttpServer;)Z 
#36 = NameAndType       #49:#50   // test:()Ljava/util/function/Predicate; 
#37 = Class             #51       // jdk8/byte_code/WaitFor 
#38 = NameAndType       #52:#53   // waitFor:(Ljava/lang/Object;Ljava/util/function/Predicate;)V 
#39 = NameAndType       #54:#55   // isRunning:()Ljava/lang/Boolean; 
#40 = Class             #56       // java/lang/Boolean 
#41 = NameAndType       #57:#58   // booleanValue:()Z 
#42 = Utf8              jdk8/byte_code/Example4 
#43 = Utf8              java/lang/Object 
#44 = Utf8              java/lang/InterruptedException 
#45 = Utf8              jdk8/byte_code/Server 
#46 = Methodref         #59.#60   // 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; 
#47 = Utf8              (Ljava/lang/Object;)Z 
#48 = Methodref         #8.#61    // jdk8/byte_code/Example4.lambda$example33 : (L
j
d
k8/b
y
t
e

c

o
d
e/S
e
r
v
e
rHttpServer;)Z 
#49 = Utf8              test 
#50 = Utf8              ()Ljava/util/function/Predicate; 
#51 = Utf8              jdk8/byte_code/WaitFor 
#52 = Utf8              waitFor 
#53 = Utf8           (Ljava/lang/Object;Ljava/util/function/Predicate;)V 
#54 = Utf8              isRunning 
#55 = Utf8              ()Ljava/lang/Boolean; 
#56 = Utf8              java/lang/Boolean 
#57 = Utf8              booleanValue 
#58 = Utf8              ()Z 
#59 = Class             #62       // java/lang/invoke/LambdaMetafactory 
#60 = NameAndType #63:#66 // 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; 
#61 = NameAndType       #20:#23   // lambda$example33 : (L
j
d
k8/b
y
t
e

c

o
d
e/S
e
r
v
e
rHttpServer;)Z 
#62 = Utf8              java/lang/invoke/LambdaMetafactory 
#63 = Utf8              metafactory 
#64 = Class             #68       // java/lang/invoke/MethodHandles$Lookup 
#65 = Utf8              Lookup 
#66 = 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; 
#67 = Class             #69       // java/lang/invoke/MethodHandles 
#68 = Utf8              java/lang/invoke/MethodHandles$Lookup 
#69 = Utf8              java/lang/invoke/MethodHandles
{
 public jdk8.byte_code.Example4();
 descriptor: ()V
 flags: ACC_PUBLIC
 Code: stack=1, locals=1, args_size=1
 0: aload_0 1:
 invokespecial #1                 // Method java/lang/Object."":()V 
 4: return
 LineNumberTable:
 line 9: 0
 LocalVariableTable:
 Start Length Slot Name Signature
     0      5    0 this Ljdk8/byte_code/Example4;
 void example() throws java.lang.InterruptedException;
   descriptor: ()V
   flags:
   Code:
     stack=2, locals=1, args_size=1
         0: new           #2      //class jdk8/byte_code/Server$HttpServer
         3: dup
         4: invokespecial #3      //Method jdk8/byte_code/Server$HttpServer."<init>":()V
         7: invokedynamic #4,  0  //InvokeDynamic #0:test:()Ljava/util/function/Predicate;
        12: invokestatic  #5      // Method jdk8/byte_code/WaitFor.waitFor:(Ljava/lang/Object;Ljava/util/function/Predicate;)V
        15: return
      LineNumberTable:
        line 13: 0
        line 15: 15
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      16     0  this   Ljdk8/byte_code/Example4;
    Exceptions:
      throws java.lang.InterruptedException
}