DEV Community

Ratik Mahajan
Ratik Mahajan

Posted on

What is Functional Interface in java

Functional interface are used in java to make java a functional language :

  1. functions to be used as higher order citizens.

I have a question here with you guys, how do call a function in the java.

  1. create a function in the class, create an class object and then call the function from the class object.
  2. create a static function on the class and call the function.
  3. 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 interface

  4. create a function interface, create a function signature .

  5. create an object of this interface and call the funtion with signature mentioned in the function interface.

  6. 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);
}

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
nigel447 profile image
nigel447

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