Java 8 – Lambda Expressions – Intro

Introduction

Project Lambda is one of the most hyped feature of Java 8. It marks the beginning of functional programming in java. The core of project Lambda are Lambda expressions which are nothing but anonymous functions but are much less verbose than the ones we are used to using prior to Java 8.

What Changed?

Let’s take a simple example of how Lambda Expressions make code less verbose and more readable. We will write a simple example to create a thread using the pre-java 8 way and then with Lambda Expressions.

 

We can also use anonymous class here and make the code concise

What we are doing here is creating a Thread instance and passing an instance of Runnable’s subclass, which we are creating on the fly, to it.

 

Now, with Lambda expression, we can greatly simplify this

this piece of code is equivalent to what we saw in our earlier code snippets. It is still implementing the runnable interface and overriding the run method but in a lot simpler way.

you can also do:

and it would work just fine but I personally consider it too much concise that it starts to hurt readability.

Now, you might be wondering, what if, there are multiple statements in the run method. The answer is simple, we just use curly braces followed by a semicolon in that case. See the example below:

 

Usage restriction

As per the early spec of project lambda, Lambda expressions can only appear in places where they will be assigned to a variable whose type is a functional interface.

Functional Interface is an interface which has a single abstract method which is NOT inherited from the Object class.

Side Effects

One interesting side effect of Java’s lambda syntax is that for the first time in Java’s history, we find something that cannot be assigned to a reference of type Object.

The compiler will complain that Object is not a functional interface, though the real problem is that the compiler can’t quite figure out which functional interface this lambda should implement: Runnable, Callable, Comparator or something else? We can help the compiler with, as always, a cast, as shown below.

Lambda Expression Syntax

 

In case your functional interface method takes arguments you can just pass the comma separated list of arguments too. For example,

MyFunctionalInterface mfi = (arg1, arg2) -> {     // Curly braces are optional.
       …method body goes here…
}; // Curly braces are only required for accommodating multiple statements.

Let’s create our own functional interface and understand this.

Custom Functional Interface

We started the tutorial by examining how to use lambda expressions using a built-in functional interface i.e, Runnable, but it is not that we can use lambdas with built-in interfaces only.

In this section, we will write our own functional interface and also demonstrate that how to pass arguments using lambda expressions.

We will create a simple interface which has a single method multiply which takes two arguments.

In our main class we will implement our Functional Interface on the fly and invoke the multiply method using lambda expression and pass two arguments.

References

Open JDK spec: http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html
Oracle Lambda Doc: http://www.oracle.com/technetwork/articles/java/architect-lambdas-part1-2080972.html