What is lambda ?
Java is an object-oriented language. By introducing lambdas in Java 8, the authors of Java tried to add elements of functional programming in Java.
Now you might be wondering what the difference between object-oriented programming and functional programming is?
In object-oriented programming, objects and classes are the main entities. If we create a function then it should exist within a class. A function has no meaning outside the scope of the class object.
In functional programming, functions can exist outside the scope of an object. We can assign them to a reference variable and we can also pass them to other methods as a parameter.
A lambda expression is just an anonymous function. i.e., a function with no name and that is not bound to an identifier. We can pass it to other methods as parameters, therefore, using the power of functional programming in Java.
Lambda is an Anonymous function
- No Name
- No Modifier
- No Return Type Why to use Lambda expressions?
To write functional programming in java
To write more readable, maintainable and concise code
To enable parallel processing.
Let's see how to convert simple function into lambda expression.....
public void show ()
{
S.O.P("Hello"); => Convert this into Lambda Expression
}
public Employee getData(Employee emp)
{
return emp; => Convert this into Lambda Expression
}
Comments