Lambda Expression of Functional Interface

// method 2 where lambda expression use there is no anonymous inner class
// just creating diff references to perform diff operations
// (diff objects are creating and implementing in objects)

important points:

* @FunctionalInterface it only takes 1 method
* lambda exressions work only when there is @FunctionalInterface
* (->) using this lambda expression can be used
* see example below ,

* the key point is that methods from Object (like toString, hashCode, and equals)
  do not affect the functional interface rules because they are considered to be
present in all interfaces implicitly. The interface Ex is still a valid
functional interface with the @FunctionalInterface annotation because it
only has one abstract method, void code().

***********************************************************************************
@FunctionalInterface // It means it only takes 1 method
interface Ex {
    void code();
}

class A implements Ex {

    public void code() {
        System.out.println("Coding...");
    }

}

public class Functional_Interface {
    public static void main(String[] args) {

        // method 1 but creates 1 extra anonymous inner class
        // Ex ref=new Ex() given there is 1 anonymous class creates which implements Ex
        // new Ex() creating object of that anonymous class
        Ex ref = new Ex() {

            public void code() {
                System.out.println("In new Coding...");
            }

        };
        ref.code();

        // method 2 where lambda expression used, there is no anonymous inner class
        // just creating diff references to perform diff operations (diff objects are
        // creating and implementing in objects)

        /*
         * Ex ref = () -> System.out.println("In new Coding...");
         * 
         * ref.code();
         */
    }
}

***********************************************************************************

Calculator Example : 

Let's consider a more detailed example using a functional interface for a calculator with multiple operations.

Defining the Functional Interface

java

@FunctionalInterface interface Operate { int operation(int a, int b); }

Implementing the Functional Interface with Lambda Expressions

java

public class Calculator { public static void main(String[] args) { Operate add = (a, b) -> a + b; Operate subtract = (a, b) -> a - b; Operate multiply = (a, b) -> a * b; Operate divide = (a, b) -> a / b; System.out.println("Add: " + add.operation(10, 5)); System.out.println("Subtract: " + subtract.operation(10, 5)); System.out.println("Multiply: " + multiply.operation(10, 5)); System.out.println("Divide: " + divide.operation(10, 5)); } }

In this example:

  • The Operate interface is a functional interface with a single abstract method operation.
  • Different lambda expressions provide implementations for addition, subtraction, multiplication, and division operations.
  • Each lambda expression is an instance of the Operate functional interface, implementing the operation method in different ways.

Summary

  • Anonymous Classes: If you use anonymous classes, the compiler generates a separate class for each anonymous class instance. This means four different anonymous classes for addition, subtraction, multiplication, and division.
  • Lambda Expressions: If you use lambda expressions, the compiler optimizes them internally, often without creating separate class files for each instance. Instead, it uses method handles or invokedynamic instructions to achieve the same functionality more efficiently.
        
  • FunctionalInterface only takes one non-overridden method: This means it can have only one abstract method that is not an override of a method from the Object class.
  • FunctionalInterface can take multiple overridden methods: Methods such as equals(), hashCode(), and toString() are considered overrides from the Object class and do not count towards the single abstract method limit.
    • Memory Efficiency: Lambda expressions are more memory-efficient because they avoid creating separate class files for each anonymous class instance.
    • Performance: Lambda expressions are optimized for performance through the use of method handles and invokedynamic instructions.
    • Readability: Lambda expressions provide a more concise and readable way to implement functional interfaces.

    Comments

    Popular posts from this blog

    Collection Framework of Java

    What is DBMS ?

    Compiler vs Interpreter vs JIT Compiler