Explain Java 8 Lambda Expression | What is Lambda Expression | Example Java Code Demo | InterviewDOT
Channel: Interview DOT
Date: 2020-03-09

Click here - https://www.youtube.com/channel/UCd0U_xlQxdZynq09knDszXA?sub_confirmation=1 to get notifications.

Tamil Explain Java 8 Lambda Expression | What is Lambda Expression | Example Java Code Demo | InterviewDOT

Java lambda expressions are new in Java 8. Java lambda expressions are Java's first step into functional programming. A Java lambda expression is thus a function which can be created without belonging to any class. A Java lambda expression can be passed around as if it was an object and executed on demand.

Java lambda expressions are commonly used to implement simple event listeners / callbacks, or in functional programming with the Java Streams API.

Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming, and simplifies the development a lot.

Following are the important characteristics of a lambda expression.

Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.

Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.

Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.

Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.

Here are just a few of the key benefits to using lambda expressions in Java:

Conciseness
Reduction in code bloat
Readability
Elimination of shadow variables
Encouragement of functional programming
Code reuse
Enhanced iterative syntax
Simplified variable scope
Less boilerplate code
JAR file size reductions
Parallel processing opportunities
To get a full understanding of the benefits of lambda expressions in Java and how they provide conciseness and readability, just look at how functional interfaces, that is, an interface that only defines a single method, were implemented prior to lambda expressions. There were basically two approaches that could be used. One approach was to define a separate class that implements the interface, while the second was to use an anonymous inner class.

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface.Runnable, ActionListener, Comparable are some of the examples of functional interfaces.

In Java, a Marker interface is an interface without any methods or fields declaration, means it is an empty interface. Similarly, a Functional Interface is an interface with just one abstract method declared in it. Runnable interface is an example of a Functional Interface.