What is a lambda expression in Java


Lambda Express in Java

The very important features of Lambda Expressions are that they execute in the context of their appearance. So, a similar lambda expression can be executed differently in some other context (i.e. logic will be same but results will be different based on different parameters passed to function).

The benefits involved in functional programming over object oriented programming (OOP). Most OOP languages evolve around objects and instances and treat only them their first class citizens. Another important entity i.e. functions take back seat. This is especially true in java, where functions can’t exist outside an object. A function itself does not mean anything in java, until it is related to some object or instance.

But in functional programming, you can define functions, give them reference variables and pass them as method arguments and much more. JavaScript is a good example of this where you can pass callback methods e.g. to Ajax calls. It’s very useful feature and has been lacking in java from beginning. Now with java 8, we can also use these lambda expressions.

(p, q) -> p + q
(int p, int q) -> p * q
(p, q) -> p - q
() -> 99
(String p) -> System.out.println(p)
p -> 2 * p

 


%d bloggers like this: