Functional interface concept was introduced in JDK 8. This article explains what is functional interface and its use with lambda expressions.
JDK 8 is required for running examples in this program.
What is a Functional Interface ?
A functional interface is an interface that specifies only one abstract method.
A functional interface may also contain one or more static or default methods.
Here are some examples of functional interfaces.
Example 1:
This example contains a functional interface with just an abstract method :
public interface MyInterface { int add(int a, int b); }
Example 2:
Here is another example of a functional interface that along with the abstract method declaration, contains a default method.
public interface MyInterface { int add(int a, int b); default int subtract(int a, int b){ return a-b; } }
Functional interface and lambda expression
A lambda expression can not be executed on its own. It forms the implementation of the abstract method defined by the functional interface that specifies the target type.
The main advantage of a functional interface is that it can be used to assign a lambda expression or it can be used as a method reference.
Lets see how a lambda expression can be used in assignment context.
Here is a lambda expression that displays a message “Hello World”.
() -> {System.out.println(“Hello World”);}
To execute it, first lets create a functional interface with a abstract method hello().
interface MyInter{
void hello();
}
Now, we can assign the lambda expression to the interface reference as follows :
MyInter infVar = () -> {System.out.println(“Hello World”);};
When a lambda expression occurs in a target type context, an instance of a class implementing the functional interface is automatically created.
In that class, the lambda expression defines the behaviour of the abstract method declared by the functional interface.
Now, when that method is called through the target, the lambda expression is executed.
So, the previous lambda expression can be executed by calling :
infVar.hello();
Here is the complete program for this example :
Example 1
package java8; public class FuntionalInterfaceDemo { interface MyInter{ void hello(); } public static void main(String[] args) { // TODO Auto-generated method stub MyInter infVar = () -> {System.out.println("Hello World");}; infVar.hello(); } }
Running this program with produce the following output:
Hello World
Example 2
Here is another example program of using functional interface to to execute a lambda expression. the lambda expression adds two integers and returns their result.
public class LambdaSumDemo { interface MyInter{ void add(int x, int y); } public static void main(String[] args) { // TODO Auto-generated method stub MyInter myInter = (x,y) -> {System.out.println("Sum is " + (x+y));}; myInter.add(10,15); } }
Running this program will produce following output :
Sum is 25
In order for a lambda expression to be used in a target type context, the type of the abstract method and the type of the abstract method must be compatible.
For instance, in previous example, since the abstract method specifies two int parameters, the lambda expression must specify two parameters of type int or can be implicitly inferred as type int.
So, in general :
- The type and number of the lambda expression parameters should be compatible with abstract method parameters.
- their return types must be compatible.
- any exception thrown by the lambda expression must be acceptable to the method.
More on Java 8
Here is a reference for the new features added in JDK 8:
Also, here are some interview questions on the new features added in Java 8:
© 2015 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post
#
#
#
#