Lambda expressions

 All we know Lambda expressions are new concept in Java 8.
Why we need it ?
  1. Enable the functional program.
    Really do we need functional programming in java.
    We have been writing the millions of line java code  with OOPS concepts.
    Nothing new to do with functional programing, whatever we have been doing with OOPS same we can achieve with functional programing.
    Why we need the new functional  programming feature in JAVA ?
      - It's readable and maintainable.
      When we are working on client project requirement makes us write millions of lines java code

    It' hard to maintain the complex code.
    So, functional programing came in to the picture to improve the code readability and maintain it in elegant way.


    How can we improve coding style with functional programing in JAVA?

    In Java everything is an object. Whatever we do we have to do in an object way.
    So, we have to create object for class and perform the operation inside a method.

    With Lambda expressions we can pass the behaviour  as parameter.
    Before going to write a code with Lambda expression let me show how we write the code with JAVA7

    interface MleInterface{
        void show();
    }
    
    class MleInterfaceImplementedClass implements MleInterface{
        
    public void show(){
        // Write some operaion.
        System.out.println("I'm from show");
    }
    }
    
    public class HelloWorld{
    
      void showMsg( MleInterface mle){
          
          mle.show();
      }
         public static void main(String []args){
             HelloWorld  obj = new HelloWorld();
             MleInterfaceImplementedClass mleObj = new MleInterfaceImplementedClass();
             /* Some people say this way we are sending the behaviour as parameter.
              Actually, this is not sending the behaviour as a parameter.
              Here we are sending the object which contains the behaviour.
    
         */
             obj.showMsg(mleObj);
         }
    }
    
    


    Now let us see same code how can we write with Lambda expression.

    In above approach we have done some extra steps by creating the class(MleInterfaceImplementedClass) and it object.


    interface MleInterface{
        void show();
    }
    
    
    public class HelloWorld{
    
      void showMsg( MleInterface mle){
          // Write some logic
          mle.show();
      }
         public static void main(String []args){
             HelloWorld  obj = new HelloWorld();
             /* Simplified Lambda expression.  */
             obj.showMsg(() -> System.out.println("I'm from Lambda expression"));
         }
    }
    
    

    Just we are sending the behaviour as a parameter.

    Then question is how compiler knows it's return type ?

       In java8, compiler  is enough smart to determine the type with Lambda expression.

    If you closely see we wrote interface.
    That is not normal interface it's functional interface.
    It means we can't write one more method in the interface.
    If we write it, Lambda expression will become invalid.

    Why? - showMsg   input type is MleInterface.
     When compiler evaluates  Lambda expression  it will checks for the interface show method type and method signature.
    If one more method is there then compile will confuse which method signature to take.

    Ohh.. What if other programmer write some other method inside the interface?
    To avoid this situation we have to write annotation on top of the interface.

    @FunctionalInterface
    

    So that others to get to know that this functional interface we should not write other abstract method.

    What if I want to write some lines of code and send parameters too to showMsg method.

    obj.showMsg((a) -> {
    // Write some more logic.
    System.out.println(a))
    
    };
    
    

    Note: Some people will say both anonymous  class and Lambda expression are same but not the same.
    Lambda expressions are completely different.
  

No comments:

Post a Comment