What Is Lambda Expression?

This section describes what is a lambda expression, which is a shorthand form of an anonymous class that implements a single abstract method interface (also called functional interface).

What Is Lambda Expression? A Lambda Expression is a shorthand form of a special anonymous class that implements an interface with only one abstract method. Lambda Expressions are introduced in Java 8.

An interface with only one abstract method is also called a Functional Interface.

Here is the syntax of a lambda expression:

(parameter_list) -> {method_body} 

The above lambda expression syntax is actually a shorthand form of the following anonymous class syntax:

new interface_name() { 
   return_type method_name(parameter_list) {method_body}

The table below provides you a comparison of each element between the anonymous class syntax and the lambda expression syntax:

Anonymous class      |  Lambda expression
new                  |                   
interface_name() {   |  
   return_type       |
   method_name       |
   (parameter_list)  |  (parameter_list)
                     |  ->
   {method_body}     |  {method_body}
}                    |

Here is what I see from the above comparison:

Since a lambda expression is just a shorthand form of a special anonymous class, it acts in the same way as an anonymous class with some minor differences as summarized below:

See the next section for a simple example of a lambda expression.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

Lambda Expressions and Method References

What Is Lambda Expression?

 LambdaCalculator.java - Lambda Expression Example

 Lambda Expression Syntax Options

 Lambda Expression as Method Reference

 Method Reference Example - LambdaMethodRefernce.java

 Lambda Expression Stream Pipeline Operations

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials


 PDF Printing Version