0% found this document useful (0 votes)
676 views

Java8 PDF

Lambda expressions in Java 8 allow functionality to be treated as a method argument or code as data. They enable creating a function without belonging to any class. A lambda expression can be passed around and executed on demand. For example, a lambda expression implements a functional interface by automatically implementing its single abstract method. It is passed to print twice an input integer, demonstrating how lambda expressions express instances of functional interfaces.

Uploaded by

XYZ Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
676 views

Java8 PDF

Lambda expressions in Java 8 allow functionality to be treated as a method argument or code as data. They enable creating a function without belonging to any class. A lambda expression can be passed around and executed on demand. For example, a lambda expression implements a functional interface by automatically implementing its single abstract method. It is passed to print twice an input integer, demonstrating how lambda expressions express instances of functional interfaces.

Uploaded by

XYZ Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

28/05/2018 Lambda Expressions in Java 8 - GeeksforGeeks

Lambda Expressions in Java 8


Lambda expressions basically express instances of functional interfaces(An interface with single abstract
method is called functional interface. An example is java.lang.Runnable). lambda expressions implement
the only abstract function and therefore implement functional interfaces

lambda expressions are added in Java 8 and provide below functionalities.

Enable to treat functionality as a method argument, or code as data.


A function that can be created without belonging to any class.
A lambda expression can be passed around as if it was an object and executed on demand.

// Java program to demonstrate lambda expressions
// to implement a user defined functional interface.
 
// A sample functional interface (An interface with
// single abstract method
interface FuncInterface
{
    // An abstract function
    void abstractFun(int x);
 
    // A non­abstract (or default) function
    default void normalFun()
    {
       System.out.println("Hello");
    }
}
 
class Test
{
    public static void main(String args[])
    {
        // lambda expression to implement above
        // functional interface. This interface
        // by default implements abstractFun()
        FuncInterface fobj = (int x)­>System.out.println(2*x);
 
        // This calls above lambda expression and prints 10.
        fobj.abstractFun(5);
    }
}
Run on IDE

Output:

10

Syntax:
https://www.geeksforgeeks.org/lambda-expressions-java-8/ 1/4

You might also like