1Z0-819 · Question #106
Which interface in the java.util.function package can return a primitive type?
The correct answer is A. ToDoubleFunction. ToDoubleFunction<T> is correct because its functional method applyAsDouble(T value) returns a primitive double, not a boxed Double object - this is precisely the design intent of the ToXxxFunction family in java.util.function. Function<T,R> and BiFunction<T,U,R> both use…
Question
Options
- AToDoubleFunction
- BFunction
- CBiFunction
- DLongConsumer
How the community answered
(24 responses)- A83% (20)
- B8% (2)
- C4% (1)
- D4% (1)
Explanation
ToDoubleFunction<T> is correct because its functional method applyAsDouble(T value) returns a primitive double, not a boxed Double object - this is precisely the design intent of the ToXxxFunction family in java.util.function.
Function<T,R> and BiFunction<T,U,R> both use generic return types (R), which must be object types - Java generics cannot represent primitives, so these always return a reference type, not a primitive.
LongConsumer does accept a primitive long, but it's a Consumer - its accept(long value) method returns void, meaning it produces no value at all.
Memory tip: Look for the To prefix paired with a primitive type name (ToDouble, ToInt, ToLong) - these are the interfaces that return primitives. Meanwhile, interfaces starting with the primitive name directly (like LongConsumer, IntSupplier) describe what they accept or supply, not necessarily what they return.
Topics
Community Discussion
No community discussion yet for this question.