Sub topic 12
Primitive Functional Interfaces and Boxing Costs
The interfaces you have met so far, such as Function<T, R> and Predicate<T>, use generics. Generics only work with object types like Integer. They do not work with primitive types like int. This chapter shows what that limit costs and which interfaces avoid it.
What Boxing Means
A primitive type is one of Java's basic value types, such as int, long, double, or boolean. A wrapper class is an object type that holds one primitive value, such as Integer for int.
Boxing means wrapping a primitive inside its wrapper object. Unboxing means taking the primitive back out. Java does both for you without being asked. This is called autoboxing.
Look at this lambda:
import java.util.function.Predicate;
public class BoxingDemo {
public static void main(String[] args) {
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4)); // true
}
}The value 4 is an int. Predicate<Integer> needs an Integer, so Java boxes 4 into an Integer object. Inside the lambda, n % 2 unboxes it again. One call is cheap. Millions of calls inside a loop create millions of small objects, and that slows the program down.
Interfaces Made for Primitives
The java.util.function package has special versions of the main interfaces for int, long, and double. These are called primitive specializations. They take and return primitives directly, so no boxing happens.
import java.util.function.IntBinaryOperator;
import java.util.function.IntPredicate;
import java.util.function.ToIntFunction;
public class PrimitiveInterfaces {
public static void main(String[] args) {
IntPredicate isPositive = n -> n > 0; // no Integer boxing
IntBinaryOperator sum = (a, b) -> a + b;
ToIntFunction<String> length = String::length;
System.out.println(isPositive.test(-1)); // false
System.out.println(sum.applyAsInt(2, 3)); // 5
System.out.println(length.applyAsInt("stream")); // 6
}
}Notice the method names. When the result is a primitive, the method name says so. IntBinaryOperator uses applyAsInt, not apply.
A Quick Reference
The int versions are listed below. The Long and Double versions follow the same pattern, for example LongPredicate and DoubleUnaryOperator.
| Generic interface | int version | Method |
|---|---|---|
| `Predicate<Integer>` | `IntPredicate` | `test(int)` |
| `Consumer<Integer>` | `IntConsumer` | `accept(int)` |
| `Supplier<Integer>` | `IntSupplier` | `getAsInt()` |
| `UnaryOperator<Integer>` | `IntUnaryOperator` | `applyAsInt(int)` |
| `BinaryOperator<Integer>` | `IntBinaryOperator` | `applyAsInt(int, int)` |
| `Function<Integer, R>` | `IntFunction<R>` | `apply(int)` |
| `Function<T, Integer>` | `ToIntFunction<T>` | `applyAsInt(T)` |
When to Use Them
Use a primitive interface when your lambda works with numbers and runs many times, for example inside a big loop or on a large list. For small or one-off tasks, the generic version is fine. Clear code matters more than tiny speed gains.
Exercise
You need a lambda that takes an int and returns that number squared, as an int, with no boxing. Which interface should you use, and how do you call it?
Show answer
Use IntUnaryOperator, because it takes one int and returns an int. Write IntUnaryOperator square = n -> n * n; and call it with square.applyAsInt(5), which returns 25.
Exercise
Why does calling a Predicate<Integer> with an int value cost more than calling an IntPredicate?
Show answer
Predicate<Integer> needs an Integer object, so Java boxes the int into a new Integer and later unboxes it. IntPredicate accepts the int directly, so it creates no extra object.