1. Lambda Expression Introduction
Lambda expressions are new feature of Java 8. With Lambda Expression interface implementation becomes more easier compared with earlier releases of Java. In this example, we will learn about Lambda expression and how to use it for Functional Interface. When an interface contains only one abstract method, we call it as functional interface.
2. The Runnable Interface
Java provides an interface called Runnable
which one can use to create threads. Look at the interface definition below:

Explanation
- Shows the Runnable interface declaration.
- The interface contains only one contract method called
run
. This method does not take any arguments and returns nothing. - The interface is marked as Functional Interface via
@FunctionalInterface
. When an interface contains only one method, we call it as Functional Interface.
In this example, we will use this Functional Interface to explore the Lambda Expression.
3. Anonymous Inner Class
Anonymous Inner class is an inner class with no name. They are useful to define the handler functions or to provide the interface implementation on the fly. Now, look at the below example which implements the run
method of the Runnable
interface using anonymous inner class technique:
Explanation
- The outer class contains the
main
method which want to start a thread. The class name isEx002_AnnInClass
. - When we compile the code, we get a class file (Compiled object code) for this outer class.
Thread
constructor takes a class which implements theRunnable
interface. Instead of creating a new class to implement the Runnable interface, we create anonymous inner class. In the step chart above, afternew Runnable()
we open a pair of Curley braces and implement therun
method. The method is inside the nameless inner class (Anonymous class) which simply prints a message in the console window.- In the compiled output, we can see this nameless inner class as
MainClassName
$1.
We can convert this Anonymous Inner class as a Lambda Expression. Let us do that.
4. Lambda Expression
In the below code, we create a thread and provide Runnable implementation as Lambda Expression. Note, Runnable
interface expects to provide implementation for only one method.
Explanation
- Shows the thread creation as well as providing the
run
method override of the Runnable interface. - Java knows that Thread class constructor can take a class which implements the Runnable interface. The parenthesis here states the run method. Note, we do not specify the name ‘run’ here as it is the only method defined in the Runnable interface.
- The arrow mark here states the body of the run method starts.
- There is only one statement in the method body which is a function call to println. Since println returns void, it qualifies as a Lambda Expression (note run method override won’t take any param and returns nothing).
Running both the examples, starts the thread and prints message in the console window.
5. Code Reference
5.1 Anonymous Inner Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Ex002_AnnInClass { public static void main(String[] args) { //Sample 01: Create Thread use Anonymous Inner Class Thread T1 = new Thread(new Runnable() { @Override public void run() { System.out.println("Thread by Inner Class"); } }); //Sample 02: Start the Thread T1.start(); } } |
5.2 Lambda Expression Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Ex003_LambdaExp { public static void main(String[] args) { //Sample 01: Create Thread via Lambda Thread T1 = new Thread( () -> System.out.println("Thread by Lambda") ); //Sample 02: Invoke the Thread T1.start(); } } |
6. Summary
Lambda Expression suits well for the Functional Interfaces. Here, we saw how we used the anonymous inner class to implement thread & how we converted it even more simplified Lambda.
Categories: Java