Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at the introduction of Functional Interfaces and Lambda Expressions in Java 8.
One of the great updates in Java 8 is the addition of Lambda Expression support. Functional interfaces are initialized in a simple way using Lambda expressions.
A Functional interface is an interface which has one and only one method in it.
Let’s look at the simple program to generate a square/cube of a number using Functional Interface and LambdaExpression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.clearurdoubt; /** * @author Sai Gowtham Badvity * Functional Interface is an interface that contains only one method. * */ @FunctionalInterface interface FuncInterface { int test(int i); } public class FuncInterfaceDemo { public static void main(String[] args) { // initialize the Functional Interface using a lambda expression. FuncInterface func = (x) -> x*x; // lambda expression for returning square of a number System.out.println(" 3 * 3 = " + func.test(3)); func = (x) -> x*x*x; // lambda expression for returning cube of a number System.out.println(" 2 * 2 * 2 = " + func.test(2)); } } |
Output:
Happy Learning :).
Please let us know in case of any queries.