Anonymous Inner Class vs Lambda Expression

In this article, we will be going to explore the differences between lambda expression and anonymous inner Class. We will understand when to use one over the other and compare their syntax, behavior using example.

Anonymous Inner Class vs Lambda Expression
Anonymous Inner Class vs Lambda Expression

Introduction

Before going through the topic Lambda Expression and Anonymous Inner class let’s have look each of them.

  • Anonymous classes are like nested classes without a name.
  • Lambda expressions were introduced in Java 8 to promote functional programming. And in some use cases, they’re used as a replacement for an anonymous class.

In Java, there are multiple ways to implement functional interfaces i.e., An interface which contains only one abstract method. Traditionally, anonymous inner classes were used to implement these interfaces. However, from Java 8 onward lambda expression were introduced as a more concise and expressive way to implement functional interfaces.

Anonymous Inner class

Lest take an example of predefine functional interface from java API and understand the syntax of an anonymous inner class:

Runnable reference = new Runnable() {
  @Override
  public void run() {
    System.out.println("Hello World from Anonymous class Thread");
  }
};

Anonymous Inner Class example

In this example, we implement the ` Addition ` interface using anonymous inner classes for addition.

package com.javaocean;

@FunctionalInterface
interface Addition {
  int sum(int a, int b);
}

public class AnonymousExample {
public static void main(String[] args) {
  // Using an anonymous inner class to implement the Addition interface for addition
  Addition add = new Addition() {
  @Override
   public int sum(int a, int b) {
    return a + b;
   }
};

  // Using the anonymous inner classes
  int resultAdd = add.sum(7, 7);
  System.out.println("Addition: " + resultAdd); // Result: 14
  }
}

 

Lambda Functions in Java

Lambda expressions implement functional interfaces, which is an interface with only one abstract method. The lambda expression is a concise way to represent anonymous functions.  And provides an implementation of a functional interface without name. It also provides a way to pass a function as a method argument. 

let’s look at an example lambda expression implementing the Runnable interface.

class LambdaExpression {
 public static void main(String[] args) {
  // Using a lambda expression to implement Runnable interface
  Thread t1 = new Thread( () -> System.out.println("Hello from thread.. using lambda expression"));
  t1.start();
 }
}

 

Syntax of Lambda Functions

Lambda expression is simply the function without any name, the simplest lambda we can denote as
()->{}
The syntax of a lambda function consists of three parts:
(parameters) -> expression or statement block;

  • parameters: If the lambda function takes no parameters, an empty pair of parentheses is used.
  • ->: The arrow operator separates the parameter list from the body of the lambda function.
  • Expression or Body: The body of the lambda function, which can be either a single expression or a statement block enclosed in curly braces.

Example of a Lambda Function

Let’s consider an example of a functional interface called “Addition”, which has a single abstract method ` sum (int a, int b)` that performs a mathematical operation on two integers.

package com.javaocean;

@FunctionalInterface
interface Addition {
  int sum(int a, int b);
}

public class AnonymousExample {
public static void main(String[] args) {
  // Using a lambda function to implement the Calculator interface for addition
  Addition add = (a, b) -> a + b;
  // Using the lambda functions
  int resultAdd = add.sum(7, 7);
  System.out.println("Addition: " + resultAdd); // Result: 14
 }
}

Anonymous Inner class vs Lambda Expression

As discussed above, the anonymous class is used for interfaces and abstract classes, whereas lambda expressions are used only for functional interfaces.  Other important difference between them.

Anonymous Class Lambda Expression
Scope of “this” keyword this keyword refers to the anonymous class itself lambda expression, this refers to its enclosing class
Performance Anonymous class leads to an extra class file on the compilation, which takes additional time during class loading The lambda expression is better compared to the anonymous class in terms of performance.
Instantiate we can instantiate Anonymous Inner classes. We can’t instantiate Lambda Expressions.
Extend Anonymous inner class can extend or implement an interface Lambda expression can’t extend any class like abstract and concrete classes.

 

 

You may find other post on different topic that might be helpful, Please have a look

  1. Factory design pattern in Java
  2. Builder design pattern
  3. Abstract class and Interface
  4. Java 8 new feature
  5. Arrays.asList() and List.of()

Leave a Comment