❮ Previous Next ❯

Lambda Expression Introduction

Java lambda expressions arenew in Java 8. Java lambda expressions areJava's first step into functional programming. A Java lambda expression is thus a function which can be created without belonging to any class. Javaís implementation of lambda expression consist two constructs. The first is the lambda expression itself and second is the functional interface.

A lambda expression is an anonymous method(method having no name). This method is not executed on its own, it is used to implement a method defined by a functional interface. Lambda expressions are also referred as closures.

A functional interface is an interface that contains one and only one abstract method which specifies the intended purpose of the interface. A functional interface represents a single action. For example, we can say the interface Runnable is a functional interface, because it defines only one method, that is run() method. Therefore the run() method defines the action of Runnable. Furthermore the functional interface defines the target type of a lambda expression. A lambda expression can be used only in a context in which its target type is specified. Functional interface is also referred as Single Abstract Method(SAM) type.

Note: A functional interface may specify any public method defined by Object class, like equals() method without affecting its functional interface status.

package java8s;
public interface Demo {
int print();
}
package java8s;
public class LambdaExample {
public static void main(String[] args) {
Demo d;
d=()-> 100;
System.out.println(d.print());
}
}

Output

100

❮ Previous Next ❯










  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved