Java Reference
In-Depth Information
Chapter 5
Lambda Expressions
In this chapter, you will learn
What lambda expressions are
Why we need lambda expressions
The syntax for defining lambda expressions
Target typing for lambda expressions
Commonly used built-in functional interfaces
Method and Constructor references
Lexical scoping of lambda expressions
What Is a Lambda Expression?
A lambda expression is an unnamed block of code (or an unnamed function) with a list of formal parameters and a
body. Sometimes a lambda expression is simply called a lambda . The body of a lambda expression can be a block
statement or an expression. An arrow ( -> ) is used to separate the list of parameters and the body. The term “lambda”
in "lambda expression" has its origin in Lambda calculus that uses the Greek letter lambda (l) to denote a function
abstraction. The following are some examples of lambda expressions in Java:
// Takes an int parameter and returns the parameter value incremented by 1
(int x) -> x + 1
// Takes two int parameters and returns their sum
(int x, int y) -> x + y
// Takes two int parameters and returns the maximum of the two
(int x, int y) -> { int max = x > y ? x : y;
return max;
}
// Takes no parameters and returns void
() -> { }
 
Search WWH ::




Custom Search