Type of a method #
Terminology. The type (a.k.a. signature) of a method consists of:
- the type(s) of its argument(s), and
- its return type.
Notation. Method types are sometimes described analogously to the domain and codomain of a mathematical function. For instance, consider the following Java method
square:int square(int x){ return x * x; }This method has type:
$\qquad$
int$\to$intSimilarly, we may use:
int$\times$int$\to$intif the method takes twointand returns another,Unit$\times$int$\to$Unitif the method takes aUnitand anint(in that order) and returns aUnit,Unit[]$\to$voidif the method takes an array ofUnits[]and returns nothing,- etc.
Warning. We leave the notion of method type underspecified for now, on purpose. As we will see later on, just like an object, a method may have (or “implement”) several types, due to inheritance.
in Java #
Syntactically, Java does not provide a method typing system like the one above. Instead, Java uses (abuses?) interfaces for this purpose.
Functional interface #
A method type in Java is described with a so-called functional interface.
Syntax. A Java functional interface is an interface with a single (standard) method.
Note. In this definition, “standard” method stands for a method that does not have the default qualifier (“standard” interface methods are also called “abstract”).
Example. The following is a functional interface, which describes the type
Unit$\times$Integer$\to$Unit.interface UnitPerIntegerToUnit { Unit execute(Unit unit, Integer x); }Any method with type
Unit$\times$Integer$\to$Unitautomatically implements this functional interface. For instanceUnitPerIntegerToUnit boost = (Unit unit, Integer extraHealth) -> { unit.health += extraHealth; return unit; };
An instance of a functional interface (which is a method) can be executed by calling the (only) method declared in the interface:
Example (continued).
Unit boostedUnit = boost.execute(new Unicorn(), 5);
The name of a functional interface is irrelevant, as well as the name of its method (and the arguments of this method).
Example. The following functional interface also describes the type
Unit$\times$Integer$\to$Unit.interface Just { Unit because(Unit we, Integer can); }
A functional interface can be annotated with @FunctionalInterface.
This will for instance cause a compilation error if the interface has more than one (non-default) method.
Example.
@FunctionalInterface interface UnitPerIntegerToUnit { Unit execute(Unit unit, Integer x); }
Native functional interfaces #
Java provides native (parameterized) functional interfaces for some frequent method types.
We highlight here a few of them:
Function
#
The functional interface
Function<InputType, OutputType>stands for the typeInputType$\to$OutputType. Its method is calledapply.
Examples.
- A method with type
Unit$\to$Integerimplements the functional interfaceFunction<Unit, Integer>- A method with type
Unit$\to$Unitimplements the functional interfaceFunction<Unit, Unit>
BiFunction
#
The functional interface
BiFunction<InputType1, InputType2, OutputType>stands for the typeInputType1$\times$InputType2$\to$OutputType. Its method is called is also calledapply.
Example. A method with type
Unit$\times$Unit$\to$Integerimplements the functional interfaceBiFunction<Unit, Unit, Integer>
Consumer
#
The functional interface
Consumer<InputType>stands for the typeInputType$\to$void. Its method is calledaccept.
Examples.
- A method with type
Integer$\to$voidimplements the functional interfaceConsumer<Integer>- A method with type
Unit$\to$voidimplements the functional interfaceConsumer<Unit>
BiConsumer
#
This is the analogous of BiFunction, but for consumers.
Supplier
#
The functional interface
Supplier<OutputType>stands for the typevoid$\to$OutputType. Its method is calledget.
Examples.
- A method with type
void$\to$Integerimplements the functional interfaceSupplier<Integer>- A method with type
void$\to$Unitimplements the functional interfaceSupplier<Unit>
Comparator
#
The interface Comparator<InputType> (already seen in the section dedicated to sorting) is functional.
Its method is called compare, and has type InputType $\times$ InputType $\to$ int.