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 (with Method Reference)


package jdk8.byte_code;

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

@SuppressWarnings("all")
public class Example4_method_reference {
    // lambda with method reference
    void example() throws InterruptedException {
        waitFor(new HttpServer(), HttpServer::isRunning);
    }

}
Classfile Example4_method_reference.class
  Last modified 08-May-2014; size 1271 bytes
  MD5 checksum f8aef942361f29ef599adfec7a594948
  Compiled from "Example4_method_reference.java"
public class jdk8.byte_code.Example4_method_reference
 SourceFile: "Example4_method_reference.java"
 InnerClasses:
   public static #23= #2 of #21; //HttpServer=class jdk8/byte_code/Server$HttpServer of class jdk8/byte_code/Server 
   public static final #52= #51 of #56; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles 
  BootstrapMethods:
    0: #26 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:
       #27 (Ljava/lang/Object;)Z 
       #28 invokevirtual jdk8/byte_code/Server$HttpServer.isRunning:()Ljava/lang/Boolean;
       #29 (Ljdk8/byte_code/Server$HttpServer;)Z
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
 #1 = Methodref       #7.#20         //java/lang/Object."<init>":()V    
 #2 = Class           #22            //jdk8/byte_code/Server$HttpServer    
 #3 = Methodref       #2.#20         //  jdk8/byte_code/Server$HttpServer."<init>":()V 
 #4 = InvokeDynamic   #0:#30         // #0:test:()Ljava/util/function/Predicate; 
 #5 = Methodref       #31.#32        // jdk8/byte_code/WaitFor.waitFor:(Ljava/lang/Object;Ljava/util/function/Predicate;)V 
 #6 = Class           #33            // jdk8/byte_code/Example4_method_reference 
 #7 = Class           #34            // java/lang/Object 
 #8 = Utf8            <init> 
 #9 = Utf8            ()V 
 #10 = Utf8           Code 
 #11 = Utf8           LineNumberTable 
 #12 = Utf8           LocalVariableTable 
 #13 = Utf8           this  
 #14 = Utf8           Ljdk8/byte_code/Example4_method_reference; 
 #15 = Utf8           example 
 #16 = Utf8           Exceptions 
 #17 = Class          #35            // java/lang/InterruptedException   
 #18 = Utf8           SourceFile
 #19 = Utf8           Example4_method_reference.java
 #20 = NameAndType    #8:#9         // "<init>":()V
 #21 = Class          #36           // jdk8/byte_code/Server 
 #22 = Utf8           jdk8/byte_code/Server$HttpServer 
 #23 = Utf8           HttpServer 
 #24 = Utf8           InnerClasses 
 #25 = Utf8           BootstrapMethods 
 #26 = MethodHandle   #6:#37        // 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; 
#27 = MethodType      #38           // (Ljava/lang/Object;)Z 
#28 = MethodHandle    #5:#39        // invokevirtual jdk8/byte_code/Server$HttpServer.isRunning:()Ljava/lang/Boolean;
#29 = MethodType      #40           // (Ljdk8/byte_code/Server$HttpServer;)Z 
#30 = NameAndType     #41:#42       // test:()Ljava/util/function/Predicate; 
#31 = Class           #43           //jdk8/byte_code/WaitFor 
#32 = NameAndType     #44:#45       // waitFor:(Ljava/lang/Object;Ljava/util/function/Predicate;)V 
#33 = Utf8            jdk8/byte_code/Example4_method_reference
#34 = Utf8            java/lang/Object 
#35 = Utf8            java/lang/InterruptedException 
#36 = Utf8            jdk8/byte_code/Server 
#37 = Methodref       #46.#47       // 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; 
#38 = Utf8            (Ljava/lang/Object;)Z 
#39 = Methodref       #2.#48        // jdk8/byte_code/Server$HttpServer.isRunning:()Ljava/lang/Boolean; 
#40 = Utf8            (Ljdk8/byte_code/Server$HttpServer;)Z
#41 = Utf8            test 
#42 = Utf8            ()Ljava/util/function/Predicate;
#43 = Utf8            jdk8/byte_code/WaitFor 
#44 = Utf8            waitFor 
#45 = Utf8           (Ljava/lang/Object;Ljava/util/function/Predicate;)V 
#46 = Class           #49           // java/lang/invoke/LambdaMetafactory 
#47 = NameAndType     #50:#53       // 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;
#48 = NameAndType     #54:#55       // isRunning:()Ljava/lang/Boolean; #49 = Utf8            java/lang/invoke/LambdaMetafactory 
#50 = Utf8            metafactory 
#51 = Class           #57           // java/lang/invoke/MethodHandles$Lookup 
#52 = Utf8            Lookup 
#53 = 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; 
#54 = Utf8            isRunning 
#55 = Utf8            ()Ljava/lang/Boolean;
#56 = Class           #58 // java/lang/invoke/MethodHandles 
#57 = Utf8            java/lang/invoke/MethodHandles$Lookup
#58 = Utf8            java/lang/invoke/MethodHandles 
{
 public jdk8.byte_code.Example4_method_reference();
   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 7: 0 
LocalVariableTable:
 Start Length Slot Name Signature
     0      5    0 this Ljdk8/byte_code/Example4_method_reference;  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 11: 0
        line 12: 15
      LocalVariableTable:
      Start Length Slot Name Signature
      0     16     0    this Ljdk8/byte_code/Example4_method_reference;  
    Exceptions:
      throws java.lang.InterruptedException
  }