I\'m reading a chapter on lambdas in Herbert Schildt\'s \"Java: The Complete Reference\" and there are quite a few references to \"lambda target type\" and \"target type context
You should understand "target type" as the functional interface as which the function is (intended to be) used.
Think about this: what is this lambda expression expected to be and how can it be used?
() -> "123";
As the book notes it, this expression can't be used on its own. It needs to be associated with a functional interface.
Now, what functional interface can be the type of a lambda expression is picked up from the context. This is where the "target type" of the lambda expression means something.
Consider these examples:
Example 1:
void printString(Supplier supplier) {
System.out.println(supplier.get());
}
You can call that with
printString(() -> "123");
In this case, you mean the type of () -> "123"
to be Supplier
. That's the target type of () -> "123"
in this context.
Example 2:
MyInterface myInt = () -> "123";
As you can see, an identical lambda expression was used, but its target type is now MyInterface
.
Likewise, you can declare another functional interface that has the same signature as MyInterface.func()
and assign the very same lambda expression to it. The target type changes in these varied contexts.