Functional Interface in Java - Quiz

Total: 5 questions

1. 

What is function descriptor.

Function descriptor term is used to describe the signature of the Functional interface abstract method and lambda expression.

2. 

What is a functional interface in Java?

A functional interface is an interface with exactly one abstract method, also called a SAM interface (Single Abstract Method). Such an interface can serve as the target type of a lambda expression or a method reference. The number of default, static, private and private static methods is not limited (the last two since Java 9), and abstract declarations that repeat the public methods of java.lang.Object (equals, hashCode, toString) are not counted. Examples from the JDK: Runnable, Callable, Comparator, Iterable.

3. 

Why does Comparator remain a functional interface although it declares two abstract methods, compare(T, T) and equals(Object)?

Because abstract declarations that repeat the public methods of java.lang.Object are not counted. Every class implementing an interface already inherits equals, hashCode and toString from Object, so a lambda would have nothing to implement there and the compiler excludes such declarations. Effectively Comparator has a single abstract method, compare, which is why the @FunctionalInterface annotation on it is perfectly legal and Comparator<Car> byCost = (a, b) -> a.getCostUSD() - b.getCostUSD(); compiles.

4. 

What is the target type of a lambda expression and where does it come from?

A lambda expression itself carries no information about the interface it implements. The compiler infers that type from the context in which the lambda appears, and this context type is called the target type. It comes from a variable declaration (Searchable s = c -> ...;), a method parameter (list.removeIf(c -> ...)), a method return statement, a cast, a ternary expression or an array initializer. As a consequence, the same lambda body fits different interfaces as long as the parameter list, the return type and the declared exceptions match — the method names play no role. Code without a target type does not compile: both var f = () -> System.out.println("hi"); and Object o = c -> c.getCostUSD() > 20000; are compile errors.

5. 

Which ready-made functional interfaces does the java.util.function package provide?

The main ones are: Predicate<T> with boolean test(T t) — checks a condition; Consumer<T> with void accept(T t) — performs an action and returns nothing; Function<T, R> with R apply(T t) — converts one value into another; Supplier<T> with T get() — supplies a value without arguments; UnaryOperator<T> and BinaryOperator<T> — special cases of Function whose arguments and result share one type; and the two-argument BiFunction<T, U, R>, BiPredicate<T, U>, BiConsumer<T, U>. For primitives there are specializations that avoid boxing: IntPredicate, IntFunction<R>, ToIntFunction<T>, IntUnaryOperator, IntBinaryOperator, IntSupplier, IntConsumer and their long and double counterparts. Functional interfaces also live outside this package: Runnable, Callable<V>, Comparator<T>, Iterable<T> and AutoCloseable.

Page 1 of 1