Course Content
Core Java
About Lesson

Lambda expressions were introduced in Java 8, bringing functional programming capabilities to the language. The Lambda expression is used to provide the implementation of a functional interface. Java lambda expression is treated as a function.

It is an anonymous function that adds functional programming to Java, making it easier to replace anonymous inner classes.

Characteristics

  1. Lambda expressions are essentially anonymous methods.
  2. Lambda expressions can be used only with functional interfaces. A functional interface is an interface that has exactly one abstract method.

Syntax

  1. (parameters) -> expression : Having single statement
  2. (parameters) -> { statement1; statement2;} : Having multiple statements.

-> : used to link the arguments list and the body of expressions.

Example Code

interface MyFunction {
    void print(String message);
}

public class LambdaExample {
    public static void main(String[] args) {
        MyFunction func = (message) -> {
            System.out.println("Message: " + message);
            System.out.println("Length: " + message.length());
        };
        func.print("Hello, Lambda!");
    }
}

// Java Lambda Expression with Multiple Parameters
interface MyFunctionMulti {
    void print(String message, Integer i);
}

public class LambdaExample {
    public static void main(String[] args) {
        MyFunction func = (message, i) -> {
            System.out.println("Message: " + message);
            System.out.println("Length: " + message.length());
            System.out.println("Int: " + i);
        };
        func.print("Hello, Lambda!");
    }
}

Type Inference

Java compiler infers the types of the parameters in a lambda expression from the context, such as the target type of the assignment.

Implementation of Anonymous Class vs Lambda Expression

public class RunnableExample {
    public static void main(String[] args) {
        // Using an anonymous class to implement Runnable
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello, World!");
            }
        };
        
        new Thread(runnable).start();
    }
}

public class RunnableExample {
    public static void main(String[] args) {
        // Using a lambda expression to implement Runnable
        Runnable runnable = () -> System.out.println("Hello, World!");
        
        new Thread(runnable).start();
    }
}

@FunctionalInterface
interface CustomFunctionalInterface {
    String reverse(String s);
}

public class CustomFunctionalInterfaceExample {
    public static void main(String[] args) {
        // Using an anonymous class to implement the reverse method
        CustomFunctionalInterface reverser = new CustomFunctionalInterface() {
            @Override
            public String reverse(String s) {
                return new StringBuilder(s).reverse().toString();
            }
        };
        
        // Invoking the method
        System.out.println(reverser.reverse("hello")); // Output: olleh
    }
}

@FunctionalInterface
interface CustomFunctionalInterface {
    String reverse(String s);
}

public class CustomFunctionalInterfaceExample {
    public static void main(String[] args) {
        // Using a lambda expression to implement the reverse method
        CustomFunctionalInterface reverser = s -> new StringBuilder(s).reverse().toString();
        
        // Invoking the method
        System.out.println(reverser.reverse("hello")); // Output: olleh
    }
}

Scopes

Lambda expressions can access:

  1. Local variables of method: must be effectively final.
  2. Instance and static variables: can access and modify.
@FunctionalInterface
interface MyFunction {
    void print(String message);
}

public class LambdaScopeExample {
    private static String staticVar = "Static Variable";
    private String instanceVar = "Instance Variable";

    public void exampleMethod() {
        String localVar = "Local Variable";
        
        MyFunction func = (message) -> {
            System.out.println(message);
            System.out.println(staticVar);
            System.out.println(instanceVar);
            System.out.println(localVar);
            instanceVar = "t4";
        };
        func.print("Hello, Lambda!");
        func.print("Hello, Lambda!");
    }

    public static void main(String[] args) {
        new LambdaScopeExample().exampleMethod();
    }

Scroll to Top