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. The type of a method is sometimes described analogously to the type of a mathematical function. For instance, consider the following Java method
square
:int square(int x){ return x * x; }
This method takes as input an
int
, and returns anint
.In other words, this is a function with domain
int
and codomainint
. This is written:$\qquad$
square
$\colon$int
$\to$int
And the type of this function is
$\qquad$
int
$\to$int
Similarly, we may use:
int
$\times$int
$\to$int
if the method takes twoint
and returns another,Unit
$\times$int
$\to$Unit
if the method takes aUnit
and anint
(in that order) and returns aUnit
,Unit[]
$\to$void
if the method takes an array ofUnits[]
and returns nothing,- etc.
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$Unit
automatically 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$Integer
implements the functional interfaceFunction<Unit, Integer>
- A method with type
Unit
$\to$Unit
implements 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$Integer
implements 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$void
implements the functional interfaceConsumer<Integer>
- A method with type
Unit
$\to$void
implements 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$Integer
implements the functional interfaceSupplier<Integer>
- A method with type
void
$\to$Unit
implements 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
.