- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a target type of lambda expression in Java?
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
Advertisements
To Continue Learning Please Login
Login with Google