Thursday, 7 December 2017

What is lambda Expression in java?

Lambda expressions are the important feature of Java 8
A lambda expression is an anonymous or name less function
The Lambda expression is used to provide the implementation of an interface using an expression.
We don't need to define the method again and again for providing the implementation in lambda expression.
Advantages: less code is required
    Syntax:
  (argument-list) -> {body} 
  Note: it’s a method without a declaration, i.e., access modifier, return value declaration, and name.

Example:
interface Subable{  
  int sub(int x,int y); 
                         }  
  
   public class LambdaDemo{  
  public static void main(String[] args) {  
                  // Multiple parameters in lambda expression  
       Subable sb1=(x,y)->(x-y);  
        System.out.println(sb1.sub(150,20));  
        
       // Multiple parameters with data type in lambda expression  
  Subable sb2=(int x,int y)->(x-y);  
  System.out.println(sb2.sub(100,20));  
    }  
}  


No comments:

Post a Comment

Global Infrastructure and its components in AWS

You may operate your workloads wherever and however you choose with the AWS Global Infrastructure, and you'll use the same network, cont...