New Member (Lambda Expressions) in Java 8
Lambda statement will be another furthermore essential characteristic of
Java which have been incorporated to Java SE 8. Lambda expressions are essential
to define inline implementation of a Functional interface, i.e. , an
interface with a single method only. Lambda expression dispenses with the need of
an anonymous class and provides for a straightforward yet capable practical
modifying proficiency with java. Preceding lambda expression, anonymous inner
class have been the only choice on execute the method. In other words, it is a
substitution of Java anonymous inner class, which is used
if we have to pass some functionality to any method. Java lambda expression may
be dealt as a function, so compiler doesn't make. class file. They need no
access modifier(private, public or protected), no return type and no name.
Syntax:
(parameters) -> {body}
Lambda
operator
Java Lambda Expression comprised
of three segments:
1) Parameters: It could a
chance to be void or non-empty also.
2) Arrow-token: It will be
used to join parameters and body for expression/statement.
3) Body: It holds
expressions and statements for lambda expression.
Example:-
()->9
"takes no value and
returns 9"
Functional Interface
In Java, A marker interface
will be an interface with no methods or fields declaration. Over basic words,
marker interface will be a void interface. Similarly, a Functional Interface is
an interface with only "one abstract method". Lambda expression gives
implementation of Functional interface. It is indicated by an annotation @FunctionalInterface,
an interface type which is used to declare an interface. It is used for
compiler level errors , the point when the interface we have annotated is not a
substantial Functional Interface.
Eg:- java.lang.Runnable is an example of a
Functional interface. There is only one method void run().
@FunctionalInterface
public Interface Runnable
{
public abstract void run();
}
Courtesy: Ms. Nancy Mendiratta (Faculty, IMS Noida)
Comments
Post a Comment