Lambda Expressions in java 8 part 1

We all are familiar with the packages and the functionality provided by those packages in previous java versions. In this article we will look into a new features that java 8 has introduced.

I personally strugguled with the idea of lambda function in java.I have always thought of java as the traditional object oriented programming language and initially i couldn't come to pace that they are promoting functional programming.


Why Functional Programming? Does it enable a programmer to do something that traditional oops didn't?

The answer to this question surprisingly is no. By the introduction of functional programming and lambda expressions ,there is no extra feature which is provided to a java programmer . Lamda expressions are just another way of doing the same thing that you can do previously in traditional ways.


Then Why Introduce Lambda Expression?

Lets take an example:-
 
Lets say you have a class named AnimalVoice which has a method speak and that method gives sounds depending on the animal. There are two ways to implement this

Way 1
public class AnimalVoice{
    public void speak(String animal){
         switch (animal) {
             case "Dog" :
                    System.out.println("bark");
             case "Cat" :
                   System.out.println("meow");
        }
    }
}

Way 2
We can have an interface IAnimal which has an abstract method speak(). And concrete classes Dog , Cat implement this interface and provide their own implementation of the method speak().

interface IAnimal {
public void speak();
}

public class Dog implements IAnimal{
    public void speak(){
        System.out.println("bark")
    }
}

public class Cat implements IAnimal{
    public void speak(){
        System.out.println("meow")
    }
}

public class AnimalVoice{
private IAnimal animal;
    public AnimalVoice(IAnimal animal){
      this.animal=animal
   }
    public void speak(){
      animal.speak();
    }
}

Here in 2nd way for every animal there is one action that varies and to implement that action we are building a class which is lots of line of code.This kind of problem is solved by using lambda expressions.

Basics of Lambda Expressions:-

At its very basic Lambda expressions allows us to treat a function as a value. Lambdas allows us to assign a function to a variable. 

Note: I am not saying the return value of a function to a variable but code block itself can be assigned to a variable.

The variable to which the function is assigned can only be of functional interface type.  I will explain what functional interface is later.


Syntax of Lambda Expression:-
Step 1:- Lets assign a function to a variable.
a= public void speak(){                            // a is of type functional interface
       System.out.println("abc");
     }

Step2:-
we can skip access modifier as now the variable that the method is assigned will be used everywhere in code.
we can skip return type as compiler knows whatt the return type of the function is by checking the code.

we don't need function name as the variable that this function is assigned will be used everywhere.

So code in step 1 gets transformed to
a = () {
              System.out.println("abc");                             // a is of type functional interface
         }

Step 3:-
We need to put -> between the () and the curly braces.
  a= () -> {                                                                      // a is of type functional interface   
                 System.out.println("abc");
         }

Step 4:-
 if the function block is a single line of code then the cirly braces can be removed.
    a= () -> System.out.println("abc");                        // a is of type functional interface
               
 Step 5:-
What if this function has arguments?? Lets have a function which takes two variables and returns their product.
a = (int a ,int b) -> return (a*b);           // a is of type functional interface
 
if their is only sinngle line is present we don't need to specify the return keyword as well

a = (int a,int b) -> a*b ;


Let us see what is Functional Interface:-
A functional interface is an interface which has only one abstract method.
You can read this artice to understand functional interfaces in detail.

So , in our previous example IAnimal was a functional interface
Step 3 can be  rewriten  as 
IAnimal a= () -> {                                                                   
                 System.out.println("abc");
         }

Step 4 can be rewritten as
IAnimal    a= () -> System.out.println("abc");


Since a fuctional interface has only one abstract method it treats the lambda as the implementation of that abstract method.

I hope this article will help in understanding the basics of lambda expressions.I will explain about other concepts related to lambda and functional interface in next articles.

Thank you and feel free to ask about any doubts.





Comments

Popular Posts