Type of a method

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 an int.

In other words, this is a function with domain int and codomain int. 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 two int and returns another,
  • Unit $\times$ int $\to$ Unit if the method takes a Unit and an int (in that order) and returns a Unit,
  • Unit[] $\to$ void if the method takes an array of Units[] 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 instance

UnitPerIntegerToUnit 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 type InputType $\to$ OutputType. Its method is called apply.

Examples.

  • A method with type Unit $\to$ Integer implements the functional interface Function<Unit, Integer>
  • A method with type Unit $\to$ Unit implements the functional interface Function<Unit, Unit>

BiFunction #

The functional interface BiFunction<InputType1, InputType2, OutputType> stands for the type InputType1 $\times$ InputType2 $\to$ OutputType. Its method is called is also called apply.

Example. A method with type Unit $\times$ Unit $\to$ Integer implements the functional interface BiFunction<Unit, Unit, Integer>

Consumer #

The functional interface Consumer<InputType> stands for the type InputType $\to$ void. Its method is called accept.

Examples.

  • A method with type Integer $\to$ void implements the functional interface Consumer<Integer>
  • A method with type Unit $\to$ void implements the functional interface Consumer<Unit>

BiConsumer #

This is the analogous of BiFunction, but for consumers.

Supplier #

The functional interface Supplier<OutputType> stands for the type void $\to$ OutputType. Its method is called get.

Examples.

  • A method with type void $\to$ Integer implements the functional interface Supplier<Integer>
  • A method with type void $\to$ Unit implements the functional interface Supplier<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.