Java Reference
In-Depth Information
text in which a target type is specified. One other thing: a functional interface is sometimes
referred to as a SAM type , where SAM stands for Single Abstract Method .
Let's now look more closely at both lambda expressions and functional interfaces.
NOTE
A functional interface may specify any public method defined by Object , such as equals(
) , without affecting its “functional interface” status. The public Object methods are con-
sidered implicit members of a functional interface because they are automatically imple-
mented by an instance of a functional interface.
Lambda Expression Fundamentals
The lambda expression introduces a new syntax element and operator into the Java lan-
guage. The new operator, sometimes referred to as the lambda operator or the arrow oper-
ator , is -> . It divides a lambda expression into two parts. The left side specifies any para-
meters required by the lambda expression. On the right side is the lambda body , which spe-
cifies the actions of the lambda expression. Java defines two types of lambda bodies. One
type consists of a single expression, and the other type consists of a block of code. We will
begin with lambdas that define a single expression.
At this point, it will be helpful to look at a few examples of lambda expressions before
continuing. Let's begin with what is probably the simplest type of lambda expression you
can write. It evaluates to a constant value and is shown here:
This lambda expression takes no parameters, thus the parameter list is empty. It returns the
constant value 98.6. The return type is inferred to be double . Therefore, it is similar to the
following method:
Of course, the method defined by a lambda expression does not have a name.
A slightly more interesting lambda expression is shown here:
Search WWH ::




Custom Search