Which interface in Java is a marker interface for lambda expressions?
Answer:
Explanation:
The FunctionalInterface
annotation in Java is a marker interface that indicates that an interface is intended to be a functional interface, meaning it has exactly one abstract method. This annotation helps to ensure that an interface can be used with lambda expressions and method references. For example:
@FunctionalInterface public interface MyFunction { void apply(); }
While the FunctionalInterface
annotation is not strictly necessary, it serves as a useful documentation and validation tool, helping developers to write more predictable and maintainable code.
Using the FunctionalInterface
annotation ensures that the interface conforms to the rules of functional programming in Java. This provides a clear indication to other developers that the interface is designed to work with lambda expressions, making the code more self-explanatory and easier to use.