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 };