Java Lambda Expression

Introduction

A Java lambda expression is an anonymous method.

The Java lambda method implements a method defined by a functional interface.

A lambda expression results in a form of anonymous class.

Lambda expressions are commonly referred to as closures.

A functional interface is an interface with one and only one abstract method.

The lambda expression introduces a new operator into the Java language.

The new operator is ->.

The left side of the -> specifies parameters required by the lambda expression.

If no parameters are needed, an empty parameter list is used.

On the right side of -> is the lambda body.

The lambda specifies the actions of the lambda expression.

Java has two types of lambda bodies:

  • a single expression
  • a block of code

For example, the following lambda expression:

() -> 123.45 

This lambda expression takes no parameters, thus the parameter list is empty.

It returns the constant value 123.45.

It is similar to the following method:

double myMeth() { return 123.45; } 



PreviousNext

Related