What are Lambda Expressions in Java?

Lambda expressions in Java are equivalent to functions (methods) without a name. We can also call them anonymous functions. 

Lambdas have everything that other methods have in Java, such as:

  • Method parameters
  • Method body
  • Return type

Lambda expressions are functions that can be created without belonging to any class. 

One of the main advantages of lambda is that it can be assigned to a variable and passed around like any other variable in Java.

The syntax looks like this:

lambda expressions java

The syntax starts with parentheses (), where we can pass the input parameters. Followed by that, we have an arrow, which separates the input from the method body. Lambda body is provided between the curly braces {}, and here, we will write what lambda is going to do with method parameters. Just like when we write a regular method/function in Java.

Usages of the Lambda expression in Java

Lambda expression provides an implementation of Functional interfaces.
A Functional Interface is an interface that has only one abstract method. These interfaces are annotated with @FunctionalInterface annotation.
We will work with Functional interfaces in upcoming lessons.

Using Lambda expression in Java – examples

Example 1:
Let’s create one Functional interface which will have only one abstract method:

 @FunctionalInterface
 interface Signable { 
   void signDocument();
 }


Now, let’s implement it using an anonymous class, so without the Lambda:

@FunctionalInterface
interface Signable {
  void signDocument();
}

class Test {

  public static void main(String[] args) {

    Signable signable = new Signable() { // anonymous class
      @Override
      public void signDocument() {
        System.out.println("Signing the document...");
      }
    };

    signable.signDocument();
  }
}
Output: Signing the document…

Ok, now let’s replace the anonymous class with the Lambda expression:

@FunctionalInterface
interface Signable {
  void signDocument();
}

class Test {

  public static void main(String[] args) {

    Signable signable = () -> {
      System.out.println("Signing the document...");
    };
    signable.signDocument();
  }
}
Output: Signing the document…
 
Example 2:
Let’s write a Functional interface with a method that accepts two parameters:
 @FunctionalInterface
 interface Drawable {   
  void draw(String shape, int length);
}


Now let’s implement it using Lambda:

@FunctionalInterface
interface Drawable {
    void draw(String shape, int length);
}

class Test {

  public static void main(String[] args) {

    Drawable drawable = (shape, length) -> {
      System.out.println("Drawing shape: " + shape + " with length: " + length);
    };
    drawable.draw("Rectangle", 100);
  }
}

Output:
Drawing shape: Rectangle with length: 100


Example 3:
Now let’s write a Functional interface with a method that returns some value:

 @FunctionalInterface
 interface Summable {
   int sum(int number1, int number2);
}


Let’s implement the Summable interface using Lambda:

@FunctionalInterface
interface Summable {  
  int sum(int number1, int number2);
}

class Test {

  public static void main(String[] args) {

    Summable summable = (num1, num2) -> {
      return num1 + num2;
    };

    System.out.println("The sum is: " + summable.sum(10, 5));
  }
}
Output: The sum is: 15
 
That was all regarding Lambda Expressions in Java. Proceed to the next lesson to learn more about Java Functional Interfaces.
 
Happy coding!

Leave a Reply

Your email address will not be published.