Lambda Expression VS Anonymous Inner class

Knoldus Inc.
3 min readMar 23, 2020

As we know that java8 has come up with a lot of new features. lambda Expressions is one of them But before java8, We were having anonymous inner classes. so in this blog we will see how Lambda Expressions in different from Anonymous Inner classes.

We will see some of the features where Anonymous Inner classes is better than Lambda Expressions.

What is Anonymous Inner class?

Anonymous class is an inner class without a name.We can declare and instantiate class at the same time and for which we can create a single object only.

Here are Two Ways we can create Anonymous Inner class:-

  • class
  • interface
class Test {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Child Thread");
}
}
};
Thread t = new Thread(r);
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread");
}
}
}

What is Lambda Expression?

Lambda expressions basically express instances of functional interfaces. It implements the abstract function of a functional Interface.

Few Important points related to Lambda Expression:

  • Body of lambda expression can contain any now of statements zero, one or more.
  • Lambda Expression can be created without belonging to any class.
  • Curly brackets are not required in case of single statement and return type will be that of the body expression.
  • When there are more than one statements, then these must be enclosed in curly brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned.
interface Interf {
public void add(int a, int b);
}

class Test {
public static void main(String[] args) {
Interf i = (a, b) -> System.out.println("The sum is:" + (a + b));
i.add(10, 20);
}
}

Difference Between Anonymous Inner class and Lambda expression

Now we have gone through the basic terms Anonymous Inner class and lambda expression. The next thing is to know the difference between Anonymous Inner class and lambda expression.

  • Anonymous Inner classes can be used in case of more than one abstract method while a lambda expression specifically used for functional interfaces.
  • It can use instance variables and thus can have state, lambdas cannot.
  • Anonymous inner classes have support to access variables from the enclosing context. It has access to all final variables of its enclosing context.

Now we need to know that Lambda Expression is less powerful than Anonymous inner classes.

interface A {
m1();
m2();
}

we can never use Lambda Expression here, as Lambda Expression is only applicable for functional Interfaces but we can use Anonymous inner classes.

A a=new A(){
public void m1()
{}
public void m2()
{}
}

In a case an Interface contains an abstract method that’s the only case where we can replace Anonymous inner classes with Lambda Expression.

Anonymous inner classes != Lambda Expression

Hope this blog will help you.

Happy Blogging!!!!

Knoldus-blog-footer-image

--

--

Knoldus Inc.

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com