Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

9/22/22, 2:30 PM Lambda Expression Vs Anonymous Inner Class | Making Java Easy To Learn

Making Java easy to learn


Java Technology and Beyond

You are here

Home > java >

Lambda Expression Vs Anonymous Inner Class


java by devs5003 - May 24, 2020  0

Table of Contents (Click on links below to navigate) [hide]


1 Use of Lambda Expression vs Anonymous Inner class
2 Difference between Lambda Expression & Anonymous Inner class

Use of Lambda Expression vs Anonymous Inner class


Before going through the topic Lambda Expression vs Anonymous Inner class, we should
have some glimpse on how these two look like. To proceed further we will have an
https://javatechonline.com/lambda-expression-vs-anonymous-inner-class/ 1/7
9/22/22, 2:30 PM Lambda Expression Vs Anonymous Inner Class | Making Java Easy To Learn

example. 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.
First, we will implement it using anonymous inner class, then by using Lambda Expression
in order to have clear picture on both.

public class SortingTest {

public static void main(String[] args) {

String [] mobiles = {"Samsung", "Lenovo", "Xiaomi", "Oppo", "Apple",


System.out.println("************* Using Anonymous Inner class *******


Arrays.sort(mobiles, new Comparator<String>() {

public int compare(String s1, String s2) {

return s1.compareTo(s2);

});

//Using Arrays.stream to covert in Stream

Arrays.stream(mobiles).forEach(System.out::println);

System.out.println("************* Using Lambda Expression ***********


Arrays.sort(mobiles, (s1, s2) -> s1.compareTo(s2));

//Using Stream.of to covert in Stream

Stream.of(mobiles).forEach(System.out::println);

Output :

************* Using Anonymous Inner class ***************

Apple

Blackberry

Lenovo

Oppo

Samsung

Sony

Xiaomi

************* Using Lambda Expression *******************

https://javatechonline.com/lambda-expression-vs-anonymous-inner-class/ 2/7
9/22/22, 2:30 PM Lambda Expression Vs Anonymous Inner Class | Making Java Easy To Learn

Apple

Blackberry

Lenovo

Oppo

Samsung

Sony

Xiaomi

For example, from the above code snippet below code is the implementation of
Comparator interface by using Anonymous Inner class.

new Comparator<String>() { public int compare(String s1, String s2) { return s1.compareTo(s2);

and implementation by using Lambda expressions for the same is as below:

Arrays.sort(mobiles, (s1, s2) -> s1.compareTo(s2));

Difference between Lambda Expression & Anonymous Inner class


Now its turn to find out the differences between them.

Lambda Expression Anonymous Inner class

“this” keyword inside Lambda Expression “this” keyword inside anonymous inner
always refers to current outer class object ie. class always refers to current anonymous
enclosing class object. Inner class object but not outer class
object.

Lambda expression can’t extend any class In contrast, an Anonymous inner class can
like abstract and concrete classes. extend any class like abstract and
concrete classes.

https://javatechonline.com/lambda-expression-vs-anonymous-inner-class/ 3/7
9/22/22, 2:30 PM Lambda Expression Vs Anonymous Inner Class | Making Java Easy To Learn

Lambda Expression Anonymous Inner class

Lambda expression can implement an Moreover, An Anonymous inner class can


Interface containing single abstract method implement an interface containing any
ie Functional Interface. number of abstract methods.

Lambda is called anonymous function ie. However, an Anonymous Inner class is


method without name. called a class without name.

However, a Lambda Expression occupies Anonymous Inner class occupies memory


permanent memory of JVM. whenever object is created on demand.

For Lambda Expression at the time of For Anonymous Inner class at the time of
compilation, no .class file fill be generated. compilation, a separate .class file will be
generated.

In order to implement a Functional Interface, In order to operate on multiple methods,


We must go with Lambda Expressions. We should go with Anonymous Inner
classes.

We can’t declare instance variables inside However, we can declare instance


Lambda Expressions and thus they don’t variables inside Anonymous Inner classes
have state. Variables declared acts as local and thus they have state.
variables.

We can’t instantiate Lambda Expressions. In contrast, we can instantiate


Anonymous Inner classes.

The Performance of the lambda expression is However, Performance of anonymous


better as it is pure compile time activity and inner class is lower as requires class
don’t incur extra cost during runtime. loading at runtime.

 Tagged anonymous inner class anonymous inner class can anonymous inner class in java anonymous
inner class vs lambda Difference between Lambda Expression & Anonymous Inner class java lambda vs
anonymous class Lambda Expression & Anonymous Inner class lambda expression avoid unwanted anonymous
inner class Lambda Expression vs Anonymous Inner class Use of Lambda Expression vs Anonymous Inner class
what is anonymous class in java

 Previous article

https://javatechonline.com/lambda-expression-vs-anonymous-inner-class/ 4/7

You might also like