Java Lambda Expressions Quickstart

Lambda Expressions were introduced to Java in Java SE 8. These expressions are modeled on the basis of the mathematical concept of Lambda Calculus (also written as λ-calculus).

There are two concepts from λ-calculus that are worth mentioning here:

  1. λ-calculus treats functions "anonymously", without giving them explicit names.
  2. λ-calculus only uses functions of a single input. 

Using the above two principles, Lambda expressions in Java provide a clear and concise way (aka anonymous) to represent one method interface (aka functions of a single input) using an expression.

E.g. the mathematical function f(x) = x + 2

In Lambda Calculus: λx.x+2
In Java: (int x) -> (x + 2)

As you may notice, lambda expressions in Java are written in the format: (Argument List) Arrow Token (Body) 

Here is another example where an anonymous function receives two inputs x and y and returns their sum: (int x, int y) -> (x + y)

Now although the examples above do not show a return statement, you may very well write multiple lines in your Body and return an appropriate result.

The one method interface in Java is called a Functional Interface. Lambda expressions provide an elegant way to implement such one method interfaces.