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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay