ToIntFunction<T> takes an object and returns a primitive int, while IntFunction<R> takes a primitive int and returns an object; together they cover both directions of crossing the boxing boundary.
ToIntFunction<String> length = String::length;
IntFunction<String> repeatX = n -> "x".repeat(n);
System.out.println("length.applyAsInt(\"hello\") = " + length.applyAsInt("hello"));
System.out.println("repeatX.apply(3) = " + repeatX.apply(3));
length.applyAsInt("hello") = 5
repeatX.apply(3) = xxx
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27