Java @FunctionalInterface annotation

www.laut‮ut‬ri.com
Java @FunctionalInterface annotation

In Java, the @FunctionalInterface annotation is used to indicate that an interface is a functional interface. A functional interface is an interface that has a single abstract method, which is also known as a functional method.

Here is an example of a functional interface in Java:

@FunctionalInterface
public interface Runnable {
  void run();
}

The @FunctionalInterface annotation is optional, but it can be used to enforce the rule that a functional interface must have exactly one abstract method. If an interface with more than one abstract method is annotated with @FunctionalInterface, the compiler will generate an error.

Functional interfaces are often used in Java to define lambda expressions, which are concise ways of defining a function that can be passed as an argument to a method or stored in a variable.

Here is an example of how to use a lambda expression to implement the Runnable interface:

Runnable task = () -> {
  // code goes here
};
Created Time:2017-11-01 22:29:45  Author:lautturi