Java Lambda Expressions

Asinshani Taniya
5 min readNov 11, 2021

--

Java 8 moved initial steps to introducing functional programming features for Java developers, one of those is Java Lambda Expressions.

Here we’re going to learn about what it is in a simple way. Just think that you have the following interface,

As an object-oriented programmer, if you want to invoke this method, you have to override the method in a new class (or any number of classes), pass the arguments and execute.

As an example,

And also, we can use the following way as well,

Anyhow, what if we can invoke calculate method without creating objects, or classes? It is interesting, right?

Let’s think, you introduce a new variable in the Demo class to express that the logic (here I go with multiplication) to calculate method, as following,

We cannot execute this code, it will give you many errors. But we can modify this and execute calculate method from Math interface without creating any objects and classes. Just follow these four steps with me.

Step 1:

If you notice, we did not have data type for our variable, right? For that, let’s use the interface name as our data type. For our example, it is Math.

Step 2:

Here I introduce three things to remove from the above code.

  1. Remove the method name.
  2. Remove the modifier (access or any).
  3. Remove the return type.

Now we have,

Step 3:

We don’t need to introduce argument data types as well, compiler can detect it. So, remove data type of method parameters.

Step 4:

This is the last and the important step. You now have to add a Lambda expression arrow before starts the method body.

Okay, now you can use this to invoke calculate method of Math interface.

To execute, you just want to call the method,

Even, you can use this lambda expression variable as an argument as well.

But we cannot use Lambda expressions to invoke methods from every interface. We can do it when the interface is only a functional interface.

Functional Interface

We name an interface as a Functional Interface when it has only one abstract method. But that interface can have any number of static methods and default methods (methods with logics), it doesn’t matter.

So now you may get the point that why we can remove modifier, return type, method name and also data types of arguments. Because that the compiler can detect this information, due to it can invoke exactly one method.

Let’s think you defined a functional interface and invoke the abstract method using lambda expression.

What if, some years later, someone adds another abstract method to your functional interface?

It will fail your all lambda expressions due to the interface is not now functional. But Java introduces a way to prevent this incident, How?

You can use annotation ‘@FunctionalInterface’ when you want to keep your interface as functional interface.

Note: This is not compulsory for every functional interface.

What if, some years later, someone changes the abstract method name?

I guarantee your Lambda expression not failed for sure. But in the execution you have to change the method name. And we can prevent this using a comment if you want. :-)

What if, when your functional interface extended by a parent interface, and that interface has methods?

  • If the parent interface not have abstract methods then no issues, still you have functional interface with you because other methods doesn’t matter.
  • If the parent interface have the exact same abstract method then also no problem, still you have functional interface with you.
  • If the parent interface have different abstract method then there is an issue, your interface now not functional. Because it has two methods.

What if, when your functional interface has child interfaces, and that interfaces has methods?

No issues at all. Those interfaces extended by your functional interface. Your one is totally safe.

Further more simplicity for Lambda Expression

  • If your lambda expression contain one line of logic, you can remove the curly brackets.
  • If your lambda expression contain one line of logic, and it is a return, you can remove the curly brackets and return keyword as well.
  • If your lambda expression take one argument, one line of logic have, and that one line of logic invoking a function with same number of arguments.

Then you can use the following syntax. And it is called method reference.

There is constructor reference as well,

……………………

Okay, from here I am going to stop my blog. I hope you now understand about lambda expressions. Happy learning… Bye!

--

--