Lambda Expressions In Java
Lambda Expressions
Lambda expressions are introduced in Java 8 and it is the biggest feature of Java 8. A lambda expression makes easy functional programming and helps to make the development simple.
Syntax
parameter -> expression body
Important features of a Lambda expression are,
- Type declaration is optional – There is no need to declare the type of a parameter. Java compiler can inference the same from the value of the parameter.
- Parenthesis around parameter is optional − There is no need to declare a single parameter in the parenthesis. Parenthesis is required for the multiple parameters.
- A curly brace is optional−There is no need to use curly braces in an expression body, if the body holds a single statement.
- Return keyword is optional −Java compiler automatically returns the value. If the body has a single expression to return the value and the curly braces are required to show , the expression returns a value.
Let’s see an example of lambda expressions, given below.
Code
- public class LambdaExpressionExample {
- public static void main(String args[]) {
- LambdaExpressionExample le = new LambdaExpressionExample();
- //with type declaration
- MathOperation addition = (int x, int y) - > x + y;
- //without type declaration
- MathOperation subtraction = (x, y) - > x - y;
- System.out.println("90 + 50 = " + le.operate(90, 50, addition));
- System.out.println("150 - 59 = " + le.operate(150, 59, subtraction));
- //with parenthesis
- GreetingService g1 = message - >
- System.out.println("Welcome to " + message);
- //without parenthesis
- GreetingService g2 = (message) - >
- System.out.println("Welcome to " + message);
- g1.sayMessage("Java 8");
- g2.sayMessage("Java 8");
- }
- interface MathOperation {
- int operation(int x, int y);
- }
- interface GreetingService {
- void sayMessage(String message);
- }
- private int operate(int x, int y, MathOperation mathOperation) {
- return mathOperation.operation(x, y);
- }
- }
Output
In the example, mentioned above, lambda expressions are used mainly to define inline implementation of a functional interface, i.e., an interface with a single method only and we have used several types of lambda expressions to define the operation method of MathOperation interface. We define the implementation of sayMessage of GreetingService.
Java Lambda expression removes the need of an anonymous class, gives a very simple and powerful functional programming capability to Java.
Scope
We can refer to the final variable, which is assigned only once, using lambda expression. Lambda expression throws a compilation error, when a variable is assigned a value; second time.
Let’s see scope example, given below.
Code
- public class LambdaExpressionExample {
- final static String print = " Welcome to ";
- public static void main(String args[]) {
- GreetingService g1 = message - >
- System.out.println(print + message);
- g1.sayMessage("Lambda Expression");
- }
- interface GreetingService {
- void sayMessage(String message);
- }
- }
Output
Summary
Thus, we learnt that lambda expression to make it easy functional programmable, which made the development task easier. It is the biggest feature of Java 8 and we also learnt how to use it in Java.