Introduction to Lambdas in Java
Lambdas were introduced in Java 8, expanding functional programming in the language. They also made code easier to read and maintain. Lambdas played a vital role in reducing boilerplate codes and focusing on behavior rather than structure. But before jumping directly into lambdas, let’s understand a few things.
Anonymous inner class
An anonymous inner class in Java defines and instantiates a class or interface in a single step. Since it is defined within a class and assigned to a variable it doesn’t need to have a name so it’s named “anonymous”. Let’s make it clear with an example.
Let’s take a simple example of adding two numbers. I know there are many ways but let us focus on without an anonymous inner class first.
Let’s create an interface with an abstract method that takes two integer parameters and returns the integer.
public interface Addition {
int add(int a, int b);
}
Now, let the class implement the interface and add the method body.
public class AddtionImpl implements Addition{
@Override
public int add(int a, int b) {
return a + b;
}
}
Finally, to add two numbers, we create an object of AdditionImpl that extends Addition and calls the add method.
public class LambdasDemo {
public static void main(String[] args) {
Addition addition = new AddtionImpl();
int sum = addition.add(10, 20);
System.out.println("sum = " + sum);
}
}
sum = 30 //output
That’s one way of doing things. The problem here is that it has a lot of boilerplate codes. Let’s try to reduce those codes by using an anonymous inner class.
The interface remains the same but instead of creating AddtionImpl class to add the method body let us define and instantiate it within our main class.
public class LambdasDemo {
public static void main(String[] args) {
Addition addition = new Addition() {
public int add(int a, int b) {
return a + b;
}
};
int sum = addition.add(10, 20);
System.out.println("sum = " + sum);
}
}
sum = 30 //output
Here instead of creating an extra class we defined and instantiated the Addition interface using an anonymous inner class. We don’t need to explicitly name the class.
An anonymous inner class can be used to define and instantiate both interface and class. It is usually created for quick, localized use, such as handling events or creating a single implementation of an interface. However one of the major drawbacks is that it has limited reusability compared to creating a separate class since this anonymous class cannot be referenced elsewhere.
Lambdas
Okay, now things get even more interesting. We have reduced a lot of boilerplate by removing an entire class (AdditionImpl). But what if we could eliminate the extra boilerplate code, such as the anonymous inner class?
To remove the extra boilerplate code Java 8 introduced the concept of lambdas. First let’s look at an example.
public class LambdasDemo {
public static void main(String[] args) {
Addition addition = (a, b) -> a + b;
int sum = addition.add(10, 20);
System.out.println("sum = " + sum);
}
}
sum = 30 //ouput
The code has been reduced to a single line. Instead of implementing an interface, we’re passing a block of code – a function without even a name just (a, b) as parameters. In lambda expression -> separates the parameter from the body, which is just some code that is run when someone calls the add method.
Happy Coding!