Lambda Expressions – Need of Lambda Expressions (Java 8)

When Java released Java 8 back in 2014, it included a number of significant changes. Infact, JDK 8 is said to be the biggest change to the platform since it’s launch nearly 20 years ago. Lambda Expressions are now supported by the Java Language in Java 8.

Before we get into the technicals of Lambda Expressions, let’s talk about why we need Lambda Expressions at all.

Need of Lambda Expressions

Let’s start by looking at an example. Suppose we need to start a new Thread and do some work in that new Thread. To create a new Thread, you need to pass in an object that implements the Runnable Interface. The Thread will execute the run() method defined in a separate Thread. So let’s make a class that implements Runnable and override the run() method.

1
2
3
4
5
6
class Worker implements Runnable {
     public void run() {
        System.out.println("Hello");
     }
     ...
 }

So now to execute this code, we make an instance of Worker class and pass it as an argument to the Thread class constructor and start the Thread. Here’s how.

1
2
Worker w = new Worker();
new Thread(w).start();

Here, we are using an instance of Worker class that implements Runnable interface and overrides the run() method, to tell a Thread what to do. All this code to run the doWork method in a Thread. Or we could also use Anonymous Classes to pass in the Runnable interface into the Thread constructor as shown below.

1
2
3
4
5
new Thread(new Runnable(){
    public void run(){
        System.out.println("Hello");
    }
}).start();

Although Anonymous Classes do provide a better way of creating on the spot instance of the interface, but it has it’s own drawbacks. Some of them are

  1. Bulky syntax
  2. Confusion surrounding the meaning of names and this
  3. Inflexible class-loading and instance-creation semantics
  4. Inability to capture non-final local variables
  5. Inability to abstract over control flow

In both of the above methods, there’s too much code to write. Programmers in general are lazy people and don’t like to type much. So for the ease of such lazy programmers, Java 8 introduces Lambda Expressions<insert background music>.

We know that Runnable interface has just one abstract method(Yes, Java 8 allows you to implement methods within the interface using default keyword for methods. That’s another topic). An interface with exactly one abstract method is called a Functional Interface. For passing an instance of a Functional interface, we can use Lambda Expressions. For example, in above example, we could have just written:

1
new Thread(() -> System.out.println("Hello")).start();

This way is more readable, more abstract and less error-prone. It essentially does the same thing as Anonymous Classes do, but Lambda Expressions is a lot less code to write.

This was just one way in which we can use Lambda Expressions. On a broader note, while writing Multi-Threaded code, thread safety is primary concern and Lambda Expressions provide thread safety with a much simpler and more readable syntax that Anonymous Classes do.

The high-level goal of Project Lambda is to enable programming patterns that require modeling code as data to be convenient and idiomatic in Java.

Share this:
Share
«
»

Leave a Reply