Sobes.tech
Junior — Middle

What is the purpose of the @FunctionalInterface annotation in Java?

sobes.tech AI

Answer from AI

The @FunctionalInterface annotation in Java is used to explicitly indicate that an interface is a functional interface, meaning it contains exactly one abstract method. This helps the compiler verify that the interface meets the requirements of a functional interface and prevents accidental addition of extra abstract methods.

Example usage:

@FunctionalInterface
public interface Calculator {
    int calculate(int a, int b);
}

Trying to add a second abstract method will cause a compile-time error. The annotation is not mandatory but is recommended for improved readability and code safety.

What is the purpose of the @FunctionalInterface… - sobes.tech