The IntFunction interface is a functional interface from the java.util.function package, introduced in Java 8. It represents an operation that accepts a primitive int value and produces a result of a specified type R.
- The generic type R defines the type of value returned by the function.
- It is useful when an operation works with an int input and produces a result such as String, Double, or another object.
- It is similar to Function<Integer, R>, but IntFunction directly accepts a primitive int value.
Example: Program to use of a lambda expression with IntFunction to calculate half of an integer value.
import java.util.function.IntFunction;
public class GFG {
public static void main(String[] args) {
IntFunction<Double> operation =
value -> value / 2.0;
Double result = operation.apply(3);
System.out.println(result);
}
}
Output
1.5
Explanation: The lambda expression divides the input by 2.0. The apply() method executes the operation for 3 and returns 1.5.
Syntax
The IntFunction interface is defined as:
@FunctionalInterface
public interface IntFunction<R> {
R apply(int value);
}
Here:
- R specifies the return type.
- value is the primitive int passed to the function.
- apply() is the single abstract method of the interface.
apply() Method of IntFunction
The apply() method accepts a primitive int and returns a result of type R.
Syntax:
R apply(int value);
- Parameters: value represents the int input on which the function operates.
- Return Value: It returns the result produced by the function as type R.
When apply() is called, the input value is passed to the function, processed, and the resulting object is returned.
Working flow
The IntFunction interface takes a primitive int as input and returns a result of type R. Its apply() method executes the lambda expression on the given int value and returns the result.

IntFunction with Different Return Types
The generic type R makes IntFunction suitable for operations that produce different types of results. The input remains an int, while the return type changes according to the requirement.
import java.util.function.IntFunction;
public class GFG {
public static void main(String[] args) {
IntFunction<String> text =
value -> "Value: " + value;
IntFunction<Integer> square =
value -> value * value;
IntFunction<Double> half =
value -> value / 2.0;
System.out.println(text.apply(10));
System.out.println(square.apply(5));
System.out.println(half.apply(8));
}
}
Output
Value: 10 25 4.0
Explanation: Each IntFunction accepts an int, but the result types differ. The first returns a String, the second returns an Integer, and the third returns a Double.