Search code examples
lambdajava-8functional-programmingmarker-interfaces

Is @FunctionalInterface just a marker interface or more than that?


I guess @FuntionalInterface is present even before Java 8 (although not directly named as @FunctionalInterface the interfaces such as Comparator and Comparable that have single method). Is it just a marker interface or will it trigger compiler checks to make sure that the interface contains one and only one method which can be used in lambda expression ?


Solution

  • It is not even a marker. From the @FunctionalInterface API:

    An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.

    (Highlighting added by me)

    It is additionally annotated with @Documented, thus the presence of this annotation will be present in, for example, javadocs.

    It provides, however, some compile time safety. If the annotated interface has more than one non-default method, we will receive a compilation error.

    Ideone demo