Lambda: A lambda has no type of its own, only the target functional interface's type

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.

Code
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));
Output
Function: 10
UnaryOperator: 10
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms