nerdexam
Oracle

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…

Working with Streams and Lambda Expressions

Question

Which interface in the java.util.function package can return a primitive type?

Options

  • AToDoubleFunction
  • BFunction
  • CBiFunction
  • DLongConsumer

How the community answered

(24 responses)
  • A
    83% (20)
  • B
    8% (2)
  • C
    4% (1)
  • D
    4% (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

#functional interfaces#primitive return types#lambda expressions#java.util.function

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice