In JDK8, the @functionalInterface annotation is added to the Callable, Runnable, and Comparator interfaces.
What is a functional interface
The source code
package java.lang;
import java.lang.annotation.*;
/**
* An informative annotation type used to indicate that an interface
* type declaration is intended to be a <i>functional interface</i> as
* defined by the Java Language Specification.
*
* Conceptually, a functional interface has exactly one abstract
* method. Since {@linkplain java.lang.reflect.Method#isDefault()
* default methods} have an implementation, they are not abstract. If
* an interface declares an abstract method overriding one of the
* public methods of {@code java.lang.Object}, that also does
* <em>not</em> count toward the interface's abstract method count
* since any implementation of the interface will have an
* implementation from {@code java.lang.Object} or elsewhere.
*
* <p>Note that instances of functional interfaces can be created with
* lambda expressions, method references, or constructor references.
*
* <p>If a type is annotated with this annotation type, compilers are
* required to generate an error message unless:
*
* <ul>
* <li> The type is an interface type and not an annotation type, enum, or class.
* <li> The annotated type satisfies the requirements of a functional interface.
* </ul>
*
* <p>However, the compiler will treat any interface meeting the
* definition of a functional interface as a functional interface
* regardless of whether or not a {@code FunctionalInterface}
* annotation is present on the interface declaration.
*
* @jls 4.3.2. The Class Object
* @jls 9.8 Functional Interfaces
* @jls 9.4.3 Interface Method Body
* @since 1.8
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}
Copy the code
explain
Through the JDK8 source Javadoc, you can know that this annotation has the following characteristics
- This annotation can only be marked on interfaces that have one and only one abstract method
- Static methods and default methods in the JDK8 interface are not abstract methods
- Since all interfaces inherit java.lang.Object by default, methods in Object are not abstract if the interface’s display declaration overwrites them
- This annotation is not required and does not matter if an interface conforms to the definition of a “functional interface”. Adding this annotation makes it easier for the compiler to check. If you write a non-functional interface, but add @functionInterface, the compiler will report an error.
The following interface is a proper functional interface:
@FunctionalInterface
public interface TestFunctionalInterface {
// Abstract method
public void sub(a);
// Methods in java.lang.Object are not abstract methods
public boolean equals(Object var1);
// Default is not an abstract method
public default void defaultMethod(a){}Static is not an abstract method
public static void staticMethod(a){}}Copy the code
Functions of functional interfaces
First we define a normal interface and a functional interface
What’s the difference in the way the parameters are passed when the two interfaces are used as parameters
As you can see in the figure above, when passing an instance of a functional interface, the IDE prompts us to write it in the red box because there is only one interface. Isn’t it particularly like passing a function (actually passing an implementation of the interface), that’s why it’s called a functional interface
Bottom line: The purpose of a functional interface is simply to make the implementation of the interface write like a function for functional programming in Java 8