Functional interfaces
A functional interface is an interface with exactly one abstract method. Lambdas and method references implement that method. @FunctionalInterface asks the compiler to keep it that way.
Detailed Explanation
Lambdas need a type. Java did not add a new function type to the type system. It reused interfaces. If an interface has one abstract method, a lambda can be that method. Runnable, Comparator, Callable, and the java.util.function types (Function, Predicate, Consumer, Supplier, UnaryOperator, plus Bi* and primitive specializations) are the everyday set.
Default methods and static methods do not count toward the one abstract method. Methods inherited from Object (equals, hashCode, toString) do not count either, which is why Comparator can declare boolean equals(Object) and still be functional. A second abstract method, even a small helper, makes a lambda illegal.
@FunctionalInterface is optional and useful. If someone adds an abstract method, the compiler fails on the interface instead of on every lambda. Put it on interfaces you intend as lambda targets.
The JDK split primitive specializations (IntFunction, ToIntFunction, IntPredicate) to avoid boxing in hot streams. You do not need to invent TriFunction for application code; a small named interface (interface Rate { int apply(int paise, int bps); }) is clearer than a nested generic.
You reach for a functional interface when you want to pass behaviour: a sort key, a retry policy, a callback. You write your own when the JDK names do not match the domain (Fee, Clock, Mailer) or when you need checked exceptions on the method. You avoid a zoo of Function<A, Function<B, C>> when a named type would document the job.
The cost of using raw Function everywhere is unreadable signatures and lost meaning. The cost of a new interface per call site is noise. Reuse Predicate and Function inside generic utilities. Use domain names at the application boundary. Remember that a lambda's checked exceptions must match the single abstract method; that is why people invent ThrowingFunction.
Code Examples
@FunctionalInterface
interface Fee {
int apply(int paise); // the one abstract method
}
public class Fees {
static int charge(int paise, Fee fee) {
return fee.apply(paise);
}
public static void main(String[] args) {
Fee gst = p -> p + p * 18 / 100;
System.out.println(charge(10000, gst)); // prints: 11800
System.out.println(charge(10000, p -> p)); // prints: 10000
}
}import java.util.function.Function;
import java.util.function.Predicate;
public class JdkTypes {
public static void main(String[] args) {
Predicate<String> blank = String::isBlank;
Function<String, Integer> len = String::length;
System.out.println(blank.test(" ")); // prints: true
System.out.println(len.apply("Java")); // prints: 4
}
}Common Gotchas
A second abstract method kills lambda compatibility
add a `default` method instead, or split the interface.
`@FunctionalInterface` is not required
it is a guard. The compiler already treats any SAM interface as a target type.
`Comparator` is functional despite `equals`
do not add another abstract method to "help"; you will break `list.sort(...)` lambdas.
Checked exceptions need a custom interface
`Function.apply` does not declare `throws IOException`.