Before going through the comparison of Lambda Expression vs Anonymous Inner class, we should have a glimpse of what these two exactly are.
A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.
Whereas, Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time.
Suppose we have to perform sorting on some set of Strings.
For that we will take an array to given Strings, then we will implement the Comparator interface to sort them.
We will implement it using Lambda Expression, and by using an anonymous inner class in order to have a clear picture of both.
Java Lambda Expressions are the foundation of functional programming in Java.
Since Java version 8, Lambda expressions reduce the code length and code complexity to a greater extent.
Java Lambda Expressions are particular code segments that behave like a regular method.
They are designed to accept a set of parameters as input and return a value as an output.
Unlike methods, a Lambda Expression does not mandatorily require a specific name.
lambda expressions are added in Java 8 and provide the below functionalities.
Enable to treat functionality as a method argument, or code as data.
A function that can be created without belonging to any class.
A lambda expression can be passed around as if it was an object and executed on demand.
Normally we create a class i.e we declare class but, anonymous classes are expressions which means that we define the anonymous class in another expression.
In simple words, the anonymous inner class is a class without names and only one object is created.
Anonymous class is useful when we have to create an instance of the object with overloading methods of a class or interface without creating a subclass of class.
Anonymous can be created in two ways:
Class(can also be Abstract)
In the anonymous class we can declare the following:
As mentioned previously, an anonymous class is an expression.
The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.
The Key Differences
Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code.
An inner anonymous class is more powerful as we can use many methods as we want, whereas lambda expression can only be used where an interface has only a single abstract method.
For Lambda Expression at the time of compilation, no .class file fill be generated.
For the Anonymous Inner class at the time of compilation, a separate .class file will be generated.
The Performance of the lambda expression is better as it is pure compile-time activity and doesn’t incur extra cost during runtime.
However, the Performance of the anonymous inner class is lower as requires class loading at runtime.
If you are coding in Java 8 make sure you use lambda expression instead of anonymous class for implementing Comparable, Comparator, Runnable, Callable, CommandListener, ActionListener, and several other interfaces in Java, which got just one single method.