Lambda Expression and Functional Interface in Java

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

import java.util.*;
@FunctionalInterface
interface MyInter
{
       public int mySquare(int n);  
}

class TestLambda 
{
    public static void main(String args[])
    {
          MyInter M1=(n)->{return(n*n);};
          Scanner scan=new Scanner(System.in);
           int m,x;
           System.out.println("Enter a number");
           m=scan.nextInt();
           x=M1.mySquare(m);
           System.out.println("Square of number is "+x);
    }
}

// @FunctionalInterface
// interface MyInter
// {
//        public int factorial(int n);  
// }

// class TestLambda 
// {
//     public static void main(String args[])
//     {
//            MyInter M1=(n)->{
//                          int f=1;
//                          while(n!=0)
//                          {
//                             f=f*n;
//                             n--;
//                          }
//                 return(f); 
//            };
//          Scanner scan=new Scanner(System.in);
//            int m,x;
//          System.out.println("Enter a number");
//          m=scan.nextInt();
//          x=M1.factorial(m);
//          System.out.println("Factorial is "+x);
//     }
//}


// @FunctionalInterface
// interface MyInter
// {
//        public void add(int a,int b);   
// }

// class TestLambda 
// {
//     public static void main(String args[])
//     {
//            MyInter M1=(a,b)->{System.out.println(a+b);};
//             M1.add(50,10);
//            M1.add(100,20);
//            M1.add(150,20);
//            M1.add(500,40);
//            M1.add(90,10);

//     }
// }


// @FunctionalInterface
// interface MyInter
// {
//        public void a1();   
// }

// class TestLambda 
// {
//     public static void main(String args[])
//     {
//             MyInter M1=()->{System.out.println("Hello Friends How Are You This is lambda expression");};
//             M1.a1();
//     }
// }

 

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses
Image

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *