Lambda Expression vs Anonymous Inner class java by devs5003 - May 24, 2020July 7, 20220 Last Updated on July 7th, 2022 Table of Contents Toggle Use of Lambda Expression vs Anonymous Inner classDifference 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 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", "Sony", "Blackberry"}; 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 ******************* 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 always refers to current outer class object ie. enclosing class object. “this” keyword inside anonymous inner class always refers to current anonymous Inner class object but not outer class object. Lambda expression can’t extend any class like abstract and concrete classes. In contrast, an Anonymous inner class can extend any class like abstract and concrete classes. Lambda expression can implement an Interface containing single abstract method ie Functional Interface. Moreover, An Anonymous inner class can implement an interface containing any number of abstract methods. Lambda is called anonymous function ie. method without name. However, an Anonymous Inner class is called a class without name. However, a Lambda Expression occupies permanent memory of JVM. Anonymous Inner class occupies memory whenever object is created on demand. For Lambda Expression at the time of compilation, no .class file fill be generated. For Anonymous Inner class at the time of compilation, a separate .class file will be generated. In order to implement a Functional Interface, We must go with Lambda Expressions. In order to operate on multiple methods, We should go with Anonymous Inner classes. We can’t declare instance variables inside Lambda Expressions and thus they don’t have state. Variables declared acts as local variables. However, we can declare instance variables inside Anonymous Inner classes and thus they have state. We can’t instantiate Lambda Expressions. In contrast, we can instantiate Anonymous Inner classes. The Performance of the lambda expression is better as it is pure compile time activity and don’t incur extra cost during runtime. However, Performance of anonymous inner class is lower as requires class loading at runtime. Related