Search code examples
javafunctional-interface

Why is java 8 IntSupplier method named getAsInt() and not get()?


Is there any specific reason why it could not have been get() similar to Supplier interface?

Obviously same query applies for other interfaces (DoubleSupplier, BooleanSupplier, etc) too.


Solution

  • Because java.util.function.Supplier is a generic class, it will always return an object matching the generic type specified. The java.util.function.IntSupplier class (also the DoubleSupplier, BooleanSupplier, etc. classes from the same package) returns a primitive type, not an object.

    I believe the getter method is named getAsInt to clarify that what is returned is a primitive int rather than an Integer object.