Java Lambda Expressions

When implementation of anonymous class is very simple, for example, an interface contains only one method, then the syntax of anonymous classes may seem unclear. In these situations the functionality is passed as an argument to another method to perform an action, for example, when someone clicks a button. Lambda expressions enable to treat functionality as a method argument, or code as data.

Over the years mainly two features have profoundly reshaped the Java language, fundamentally changing the way that code is written. The first was the addition of generics, added by JDK 5 and the second is the lambda expression, which is added to JDK 8. Lambda expressions significantly enhance Java because of primarily two reasons. First, they add new syntax elements that increase the expressive power of the language. Second, the addition of lambda expressions resulted in new capabilities such as the handling of for-each style operations, and the new stream API, which supports pipeline operations on data.
The more can be found at Chapter 15 of

The lambda expressions mainly consist of two constructs, lambda expression and functional interface.

Lambda Expressions

A lambda expression is, essentially, an anonymous or unnamed method and it is used to implement a method defined by a functional interface. Thus, a lambda expression results in a form of anonymous class.

The lambda expression introduces new operator into the Java language and this operator is called lambda operator or arrow operator(->). It divides a lambda expression 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. The −> can be verbalized as “becomes” or “goes to”.

Let’s begin with the simplest type of lambda expression.

The above lambda expression takes no parameters, so the parameter list is empty. It returns the constant value 125.65. Therefore, it is similar to the following method:

The above lambda expression obtains a pseudo-random value from Math.random(), multiplies it by 50, and returns the result. It, too, does not require a parameter.

The above lambda expression returns true if the value of parameter x is even. Most of the times you don’t need to explicitly specify the type of the parameter, x, because in many cases its type can be inferred.

Functional Interfaces

A functional interface is an interface that specifies only one abstract method. For example, the Runnable interface is a functional interface because it defines only one method: run().

With JDK 8, it is possible to specify default behavior for a method declared in an interface. This is called a default method. An interface method is abstract only if it does not specify a default implementation. Because nondefault interface methods are implicitly abstract, there is no need to use the abstract modifier but if you want you can use abstract modifier.

An example of a functional interface is given below

In this case, the method getMessage() is implicitly abstract, and it is the only method defined by GreetingService. Thus, GreetingService is a functional interface, and its function is defined by getMessage().

As mentioned earlier, a lambda expression is not executed on its own. As a result, a lambda expression can be specified only in a context in which a target type is defined.

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.

Because the lambda expression assigned to greetingService returns the value Hello, welcome to lambda expression!, that is the value obtained when getMessage() is called.

The complete example of the GreetingService lambda expression

Step 1. Create the functional interface with the below source code

Step 2. Create a main class which will call the getMessage()

Step 3. Run the above main class, you will see the below output

Another example would be as shown below

Output from the above example

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.