Java MCQ: Which interface in Java 8 is annotated with @FunctionalInterface?
Answer:
Explanation:
The Runnable
interface in Java is annotated with @FunctionalInterface
in Java 8. A functional interface is an interface that contains exactly one abstract method. These interfaces can be used as the assignment target for lambda expressions or method references.
The @FunctionalInterface
annotation is used to explicitly declare that an interface is intended to be a functional interface. This annotation is not mandatory, but it helps ensure that the interface remains functional by enforcing the single abstract method rule at compile-time.
For example, the Runnable
interface has a single abstract method, run()
, making it a functional interface:
@FunctionalInterface
public interface Runnable {
void run();
}
In Java 8, functional interfaces like Runnable
, Comparator
, Callable
, and others can be implemented using lambda expressions, which provide a more concise and readable way to define the behavior of the interface’s abstract method.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html