# What is a target type of lambda expression in Java?

Java 8Object Oriented ProgrammingProgramming

A functional Interface for which a lambda expression has invoked is called target type of lambda expression. It means that if a lambda expression has invoked for some "X" interface then "X" is the target type of that lambda expression. Hence, we conclude that lambda expressions can be used only in those situations where java compiler can determine the Target Type.

In the below example, the target type of lambda expression is BiFunction. An instance of a class is automatically created that implements the functional interface and lambda expression provides an implementation of the abstract method declared by the functional interface.

## Example

interface BiFunction {
R apply(K k , V v);
}
public class LambdaTypeInferenceTest {
public static void main(String[] args) {
BiFunction result = (n1, n2) ->
{ return n1 + n2;}; // lambda expression
Integer n1 = 10;
Integer n2 = 20;
System.out.println("The addition of n1 and n2 is: "+ result.apply(n1, n2));
}
}

## Output

The addition of n1 and n2 is: 30
Published on 20-Dec-2019 10:46:41