The same lambda expression can be assigned to different functional interfaces as long as the shape matches; the compiler gives the lambda the type of whichever interface it is assigned to, called target typing.
Function<Integer, Integer> asFunction = x -> x * 2;
UnaryOperator<Integer> asOperator = x -> x * 2;
System.out.println("Function: " + asFunction.apply(5));
System.out.println("UnaryOperator: " + asOperator.apply(5));
Function: 10
UnaryOperator: 10
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