Functional interface are used in java to make java a functional language :
- functions to be used as higher order citizens.
I have a question here with you guys, how do call a function in the java.
- create a function in the class, create an class object and then call the function from the class object.
- create a static function on the class and call the function.
you always have to put it to a class, now you can create a function with the reference of the interface.
steps to create function through functional interfacecreate a function interface, create a function signature .
create an object of this interface and call the funtion with signature mentioned in the function interface.
this function can be passed in java in parameters of the function who would accept the type function interface.
below is the code for the same.
package com.sorting;
public class UseFunctionInterface {
public static void main(String[] args) {
CalculateSquare calculateSquare = (number) -> number * number;
calculateSquare.square(4);
}
}
@FunctionalInterface
interface CalculateSquare {
int square(int number);
}
Top comments (1)
rather than "make java a functional language " would be better to say make java a object / function language" where function values are objects that can be passed around