Lambda Expressions in Java 8 (Part 3)
In this tutorial, we are going to learn more things about Lambda Expressions and execute some examples in Netbeans IDE. We are going to change our code in our First tutorial to explain Lambda Expressions.
So we have,
- The print method which takes a Print Interface in the Main class.
- An implementation of Print Interface called PrintPerform.
- Perform method which prints Welcome on the screen.
- Print Interface.
Now we want to change these things to Lambda Expressions. Let’s say my Lambda Expression is called “myLambdaExpression”. Then add the implementation of perform method to Lambda Expression.
Now you will get an error on the type of the variable. What is the type of myLambdaExpression variable?. It is a variable that contains a Lambda Expression. We cannot just use any function types for this variable. In Java 8, they have a really nice system to declare functions. That is the Interface. So we are using the same construct to declare Lambda Expressions as well.
Step 1
Create a new Interface. You can give any Name you want. I have used MyLambda.
Step 2
Create one method in this interface which has the exact same signature as the Lambda Expression(Here the method signature is void because it does not return any data).
interface MyLambda{
void foo();
}
You can use any name to the method and no input arguments here(There are no arguments in “myLambdaExpression” Lambda Expression that I declared).
Step 3
Declare myLambdaExpression variable as a type of MyLambda.
Now see there are no errors and Java is happy now.
Now let’s take another example in Lambda Expression which has input arguments and return type. Name it as addFunction and implement the body of the lambda function. Now create an interface name AddLambda and add a method called add with two input parameters(int a and int b). Then declare the type of lambda function as the name of the interface(AddLambda).
Now you will get a parameterized Lambda Function with no errors.
If we use 3 input parameters in lambda function. It will not be going to work.
Similarly, if we change the interface signature again it will fail.
So now we know method signature is highly important to match.
Note: If you already have an interface which already matches with this lambda function you can just use that interface.
In our Example, we have used Print Interface which has no arguments and void return type. So we can use that interface as the type of the lambda function.
But if we use another method in that already defined interface it will get an error. The error will say “incompatible types: Print is not a functional interface”. Let’s say we have used another method in Print interface.
So now we know when you are using an interface to declare a Lambda Expression that interface should have one method. That method should be having the same signature as the expression.
Now let me explain the difference between an actual function and the lambda function. Change the code to run the actual perform function. Then you will get the output like this.
Here you created a new class that implemented the Print interface and we provided the logic in that class.
Similarly, run the lambda function. You will get the same output like this.
So this is how you execute the lambda expressions. By calling the interface method on it, just as if it were an instance of the class(Just implementing the function and not implementing the class).
Is Lambda Expression a shortcut for creating Annonymous Inner Classes?
Let me give you an Example. This is going to create an inline instance of the Print Interface.
Now run the perform method in Annonymous Inner Class. You will get an output like this.
The output is the same as the Lambda Function output. Now you have two types of implementation here. First one is Lambda function implementation and the other one is the Inner Class function implementation. Now you will think that lambda expression may be another shortcut for creating Annonymous Inner Classes because of the same implementation in both things. But it isn’t exactly true. There are things that the Inner Class does which is different from the Lambda Expressions do.
We will learn the differences between Annonymous Inner Classes and the Lambda Functions in another tutorial.
Thank you!