A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

what is lambda expression in java

Why do use Lambda Expression?


1. To provide the implementation of Functional interface.

2. Less coding.


*Java Lambda Expression Syntax

(argument-list) -> {body}


package Polymorphism;

interface B
{
    void print();
}

public class Test2 {

    public static void main(String[] args) {
        B  obj = ()->{
           
            System.out.println("I am HB");};
        obj.print();

    }


}

output:
I am HB

Same job can be done by method overload.


package Polymorphism;

interface A
{
    void display();
}

public class Test implements A
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.display();
    }

    @Override
    public void display()
    {
        System.out.println("Name is HB");
       
    }

}


output:

I am HB

LEARN TUTORIALS

.

.