«Back to Home

Core Java

Topics

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
  1. public class LambdaExpressionExample {  
  2.     public static void main(String args[]) {  
  3.         LambdaExpressionExample le = new LambdaExpressionExample();  
  4.         //with type declaration  
  5.         MathOperation addition = (int x, int y) - > x + y;  
  6.         //without type declaration  
  7.         MathOperation subtraction = (x, y) - > x - y;  
  8.         System.out.println("90 + 50 = " + le.operate(9050, addition));  
  9.         System.out.println("150 - 59 = " + le.operate(15059, subtraction));  
  10.         //with parenthesis  
  11.         GreetingService g1 = message - >  
  12.             System.out.println("Welcome to " + message);  
  13.         //without parenthesis  
  14.         GreetingService g2 = (message) - >  
  15.             System.out.println("Welcome to " + message);  
  16.         g1.sayMessage("Java 8");  
  17.         g2.sayMessage("Java 8");  
  18.     }  
  19.     interface MathOperation {  
  20.         int operation(int x, int y);  
  21.     }  
  22.     interface GreetingService {  
  23.         void sayMessage(String message);  
  24.     }  
  25.     private int operate(int x, int y, MathOperation mathOperation) {  
  26.         return mathOperation.operation(x, y);  
  27.     }  
  28. }  
10

Output

11

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
  1. public class LambdaExpressionExample {  
  2.     final static String print = " Welcome to ";  
  3.     public static void main(String args[]) {  
  4.         GreetingService g1 = message - >  
  5.             System.out.println(print + message);  
  6.         g1.sayMessage("Lambda Expression");  
  7.     }  
  8.     interface GreetingService {  
  9.         void sayMessage(String message);  
  10.     }  
  11. }  
12 
 
Output

13
 
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.