posted 3 years ago
If you are missing anything, I am missing it to.
For instance, witness this:
jshell> DoubleConsumer z = d -> System.out.println(d );
z ==> $Lambda$20/0x0000000800c09a00@443b7951
jshell> z.accept(1.0);
1.0
jshell> Consumer<Double> zz = d -> System.out.println(d);
zz ==> $Lambda$21/0x0000000800c0aa00@28c97a5
jshell> zz.accept(2.0);
2.0
Auto-boxing and unboxing FTW, but either functional interface can work with either call due to unboxing and auto-boxing...
It is instructive to see why the two answers that really are wrong won't work:
C. Function<Long, Double>
D. Function<String, Double>
The functional interface of Function, regardless of type parameters, does not define a method named applyAsDouble() with any overload at all.
So attempts to call it using that method name will not compile.
The "primitive specializations" for consumer don't need a different name because they aren't returning anything, so they have no need to overload a method on return type (this is why a new name is needed for the primitive specializations for functional interfaces that need to return a value).
They don't need one, they don't have one, calling the same method with a primitive of the appropriate type works just great with auto-boxing as shown above.
Tho the question didn't ask that, notice that the one that is a primitive specialization works when passed a long because primitives can undergo widening conversions, e.g. long --> double
jshell> z.accept(1L);
1.0
whereas it is good to see why this fails (you can't widen and auto-box in one step):
jshell> zz.accept(1L);
| Error:
| incompatible types: long cannot be converted to java.lang.Double
| zz.accept(1L);
| ^^
Note this question didn't ask that, however.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)