[go: up one dir, main page]

0% found this document useful (0 votes)
12 views3 pages

Functional Interface Lambda Notes

A Functional Interface in Java has exactly one abstract method and is used for lambda expressions and method references. The document provides examples of using anonymous classes and lambda expressions for a greeting service and a calculator, highlighting the differences in syntax and readability. It emphasizes the modern functional style introduced in Java 8 with lambda expressions.

Uploaded by

anup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Functional Interface Lambda Notes

A Functional Interface in Java has exactly one abstract method and is used for lambda expressions and method references. The document provides examples of using anonymous classes and lambda expressions for a greeting service and a calculator, highlighting the differences in syntax and readability. It emphasizes the modern functional style introduced in Java 8 with lambda expressions.

Uploaded by

anup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Functional Interfaces & Lambda Expressions

What is a Functional Interface?

A Functional Interface is an interface that has exactly one abstract method.

It is used as the basis for lambda expressions and method references in Java.

Example:

@FunctionalInterface

interface Greeter {

void greet(String name);

Real-Life Example - Greeting Service

interface Greeter {

void greet(String name);

class Welcome {

void sayHello(Greeter g) {

g.greet("Anup");

public class Demo {

public static void main(String[] args) {

Welcome w = new Welcome();

// Anonymous Class

w.sayHello(new Greeter() {

public void greet(String name) {

System.out.println("Hello, " + name + "! (from Anonymous Class)");

});
Java Functional Interfaces & Lambda Expressions

// Lambda Expression

w.sayHello((name) -> System.out.println("Hi, " + name + "! (from Lambda)"));

Output

Hello, Anup! (from Anonymous Class)

Hi, Anup! (from Lambda)

Summary: Anonymous vs Lambda

| Feature | Anonymous Class | Lambda Expression |

|------------------|-------------------------------------------|-------------------------------|

| Syntax | Long, verbose | Short, clean |

| Readability | Less readable | More readable |

| Java Version | Works in Java 7 and below | Java 8+ only |

| Use Case | Traditional implementation | Modern functional style |

Bonus - Calculator Example

@FunctionalInterface

interface Calculator {

int compute(int a, int b);

public class DemoCalc {

public static void main(String[] args) {

Calculator add = (a, b) -> a + b;

Calculator mul = (a, b) -> a * b;


Java Functional Interfaces & Lambda Expressions

System.out.println("Sum: " + add.compute(10, 5));

System.out.println("Product: " + mul.compute(10, 5));

Output:

Sum: 15

Product: 50

You might also like