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 2


package jdk8.byte_code;

public interface Server {

  Boolean isRunning();

  public class HttpServer implements Server {
    @Override
    public Boolean isRunning() {
      return false;
    }
  }
}


package jdk8.byte_code;

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

public class Example2 {

    // anonymous class (closure)
    void example() throws InterruptedException {
        Server server = new HttpServer();
        waitFor(new Condition() {
            @Override
            public Boolean isSatisfied() {
                return !server.isRunning();
            }
        });
    }
}
Classfile Example2.class
 Last modified 08-May-2014; size 775 bytes
 MD5 checksum 2becf3c32e2b08abc50465aca7398c4b
 Compiled from "Example2.java"
 public class jdk8.byte_code.Example2
 SourceFile: "Example2.java"
 InnerClasses:
     #4; //class jdk8/byte_code/Example2$1
     public static #27= #2 of #25; //HttpServer=class jdk8/byte_code/Server$HttpServer of class jdk8/byte_code/Server
 minor version: 0
 major version: 52
 flags: ACC_PUBLIC, ACC_SUPER
 Constant pool:
 #1 = Methodref     #8.#24     // java/lang/Object."<init>":()V
 #2 = Class         #26        // jdk8/byte_code/Server$HttpServer
 #3 = Methodref     #2.#24     // jdk8/byte_code/Server$HttpServer."<init>":()V    
 #4 = Class         #28        // jdk8/byte_code/Example2$1 
 #5 = Methodref     #4.#29     // jdk8/byte_code/Example2$1."<init>":(Ljdk8/byte_code/Example2;Ljdk8/byte_code/Server;)V 
 #6 = Methodref     #30.#31    // jdk8/byte_code/WaitFor.waitFor:(Ljdk8/byte_code/Condition;)V 
 #7 = Class         #32        // jdk8/byte_code/Example2
 #8 = Class         #33        // java/lang/Object
 #9 = Utf8          InnerClasses
 #10 = Utf8         <init>
 #11 = Utf8         ()V
 #12 = Utf8         Code 
 #13 = Utf8         LineNumberTable 
 #14 = Utf8         LocalVariableTable 
 #15 = Utf8         this 
 #16 = Utf8         Ljdk8/byte_code/Example2; 
 #17 = Utf8         example 
 #18 = Utf8         server 
 #19 = Utf8         Ljdk8/byte_code/Server; 
 #20 = Utf8         Exceptions 
 #21 = Class        #34          // java/lang/InterruptedException 
 #22 = Utf8         SourceFile
 #23 = Utf8         Example2.java 
 #24 = NameAndType  #10:#11      // "<init>":()V 
 #25 = Class        #35          // jdk8/byte_code/Server 
 #26 = Utf8         jdk8/byte_code/Server$HttpServer 
 #27 = Utf8         HttpServer 
 #28 = Utf8         jdk8/byte_code/Example2$1 
 #29 = NameAndType  #10:#36      // "<init>":(Ljdk8/byte_code/Example2;Ljdk8/byte_code/Server;)V 
 #30 = Class        #37          // jdk8/byte_code/WaitFor 
 #31 = NameAndType  #38:#39      // waitFor:(Ljdk8/byte_code/Condition;)V
 #32 = Utf8         jdk8/byte_code/Example2 
 #33 = Utf8         java/lang/Object
 #34 = Utf8         java/lang/InterruptedException
 #35 = Utf8         jdk8/byte_code/Server
 #36 = Utf8         (Ljdk8/byte_code/Example2;Ljdk8/byte_code/Server;)V
 #37 = Utf8         jdk8/byte_code/WaitFor 
 #38 = Utf8         waitFor 
 #39 = Utf8         (Ljdk8/byte_code/Condition;)V 
{
 public jdk8.byte_code.Example2();
 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/Example2;
 void example() throws java.lang.InterruptedException;
    descriptor: ()V
    flags:
    Code:
      stack=4, locals=2, 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: astore_1
         8: new           #4   // class jdk8/byte_code/Example2$1
        11: dup
        12: aload_0
        13: aload_1
        14: invokespecial #5   // Method jdk8/byte_code/Example2$1."<init>":(Ljdk8/byte_code/Example2;Ljdk8/byte_code/Server;)V
        17: invokestatic  #6   // Method jdk8/byte_code/WaitFor.waitFor:(Ljdk8/byte_code/Condition;)V
        20: return
      LineNumberTable:
        line 10: 0
        line 11: 8
        line 17: 20
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      21     0  this   Ljdk8/byte_code/Example2;
            8      13     1 server   Ljdk8/byte_code/Server;
    Exceptions:
      throws java.lang.InterruptedException
}