Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.function.IntFunction: practical reference

util.function.IntFunction: practical reference: IntFunction<R> is a functional interface that maps a primitive int argument to a reference result R without boxing the input.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.util.function.IntFunction

IntFunction is a functional interface that maps a primitive int argument to a reference result R without boxing the input.

Use it with IntStream.mapToObj and as a size-dependent factory such as the array constructor String[]::new.

The interface has no compose or andThen method and imposes no purity; apply may return null or throw an implementation-defined exception.

For an array factory a negative argument causes NegativeArraySizeException, while an ordinary IntFunction may legally interpret every int under its own contract.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        java.util.function.IntFunction<String> label = value -> "n=" + value;
        System.out.println(label.apply(7));
    }
}

Result

n=7

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources