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 1


package jdk8.byte_code;

import static jdk8.byte_code.WaitFor.waitFor;

@SuppressWarnings("all")
public class Example1 {

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