Java-Lambda Expressions

Lambda Expressions

Key to understanding Java’s implementation of lambda expressions are two constructs. The first is the lambda expression, itself. The second is the functional interface. A lambda expression is, essentially, an anonymous (that is, unnamed) method. However, this method is not executed on its own. Instead, it is used to implement a method defined by a functional interface. Thus, a lambda expression results in a form of anonymous class. Lambda expressions are also known as closures in some other languages.

A functional interface is an interface that contains one and only one abstract method. Normally, this method specifies the intended purpose of the interface. Thus, a functional interface typically represents a single action. For example :

Lambda Expressions

the interface Programming is a functional interface because it defines only one method : language( ). Therefore, language( ) defines the action of Programming. Furthermore, a functional interface defines the target type of a lambda expression. Here is a key point: a lambda expression can be used only in a context in which its target type is specified. One other thing: a functional interface is sometimes referred to as Single Abstract Method(SAM type).

Syntax Of  Lambda Expression

The syntax of a lambda expression includes an argument list, a new character to the language known as the “arrow token” (->), and a body. The following model represents the structure of a lambda expression :

Lambda Expression

The argument list for a lambda expression can include zero or more arguments. If there are no arguments, then an empty set of parentheses can be used. If there is only one argument, then no parentheses are required. Each argument on the list can include an optional type specification. Lambda expression divided into two parts. The left side specifies any parameters required by the lambda expression. On the right side is the lambda body, which specifies the actions of the lambda expression.

Curly braces surround the body of a block, which contains more than a single expression. The curly braces are not necessary if the body consists of a single expression.  The body is simply evaluated and then returned. If the body of the lambda is an expression and not a statement, a return is implicit. On the contrary, if the body includes more than one statement, a return must be specified, and it marks return of control back to the caller.

A lambda expression is not executed on its own. Rather, it forms the implementation of the abstract method defined by the functional interface that specifies its target type. As a result, a lambda expression can be specified only in a context in which a target type is defined. One of these contexts is created when a lambda expression is assigned to a functional interface reference. Other target type contexts include variable initialization, return statements, and method arguments, to name a few. The following code demonstrates a lambda expression that does not contain any arguments :

Lambda Expressions
The Programming interface, which is in use by the lambda, is also known as a functional interface.

Lambda Expressions

Let’s take a look at how this lambda expression works. First, a reference(mylanguage) to the functional interface Programming is declared. Next, a lambda expression is assigned to that interface reference. An object of type Programming is returned from the lambda expression. When a lambda expression occurs in a target type context, an instance of a class is automatically created that implements the functional interface, with the lambda expression defining the behavior of the abstract method declared by the functional interface. When that method is called through the target, the lambda expression is executed. Thus, a lambda expression gives us a way to transform a code segment into an object.

The empty set of parentheses denotes that there are no arguments being passed to the expression. The return is implicit, and the String “Java” is returned from the lambda expression to the invoker. The expression in the example is assigned to a variable identified by mylanguage. Assume that the functional interface, Programming, contains an abstract method identified as language( ), as seen in the code. In this case, the mylanguage.language( ) method can be invoked to return the String.



Lambda-Expression Private Interface Methodss

Program Source

interface Programming {
    String language();
public class Javaapp {

    public static void main(String[] args) {
        Programming mylanguage =()->"Java";
        String str1 = mylanguage.language();
        mylanguage =()->"PHP";
        String str2 = mylanguage.language();
        mylanguage =()->"C++";
        String str3 = mylanguage.language();

Leave a Comment