Lambda Expression (JAVA PROGRAMMING GUIDE)

Lambda expression was added in jdk 8 which has enhanced the power of java. One of the advantages of lambda expression is ability to easily take advantage of parallel processing capabilities of multi-core environments.

before understanding lambda expression we will need to understand  what is functional interface ?simply put functional interface is a interface that contains one and only abstract method.for example : interface Runnable which has only one abstract method run().

lets create a functional interface.

 

public interface returnNum {
    double getVal();
    
}

the interface is returnNum and it has one and only abstract value getVal();

so what is lambda expression?

A lambda expression is method without a name or simply you can say unnamed method or anonymous method. These are not regular methods in java because these anonymous method are not executed on their own , rather they need to implement a method defined by functional interface. so the consequence is formation of anonymous class. They are also called closures.

 

The lambda expression introduces  its operator as lambda operator or arrow operator and is denoted by “->”. it can be said as “goes to” or “becomes”. here is an example of lambda expression

() -> {double num = 100.23423423423;
return num;
};

this is similar  to :


double returnNum(){
     double num = 100.23423423423;
     return num;
}

binding both functional interface and lamda expression

 

lets create a interface returnNum() which contains abstract method int getNumber();

 

interface returnNum {
int getNum();


}

while creating functional interface , here is the rule you  need to follow.

  • only one abstract method is allowed

 

 

now lets create a class lamda expression


class LamdaExpression {

 
    public static void main(String[] args) {
        
           
        returnNum number =() -> 123;
        System.out.println(number.getNum());
         //returnNum anotherNum =() -> "hello"; //this is wrong 
        //because our abstract method can only support
        // integer data type not other data types.
        
        
    }
    
}

here an object is created for interface returnNum as number and that instance of a object initiates the abstract method int getNum(); or sets its value to 123.

Note:

  • () ->123;

    is similar to

 

int getNum(){
return 123;
}
  •  lambda expression can only take those data types defined by abstract method. for example:

int getNum(); //this will  only support integer data types

inside lamdbaExpression class

//returnNum num =() -> "hello"  //this is wrong.

 

Here is all the code for this tutorial :



public interface returnNum {
//only one abstract method is allowed
//overriding methods are allowed
       int getNum();
       
}
class LamdaExpression {

 
    public static void main(String[] args) {
        
           
        returnNum number =() -> 123;
        System.out.println(number.getNum());
        
        //returnNum anotherNum =() -> "hello"; //this is wrong 
        //because our abstract method can only support
        // integer data type not other data types.
        
    }
    
}

Here  is the follow up tutorial describing Block lambda expressions and features of expression lambdas.

 

3 Comments

Leave a Comment

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