Differences between anonymous inner classes and Lambda expression
| Anonymous Inner class | Lambda Expression |
| It's a class without name | It's a method without name (anonymous function) |
| Anonymous inner class can extend abstract and concrete classes |
Lambda expression can't extend abstract and concrete classes |
| Anonymous inner class can implement an interface that contains any number of abstract methods |
Lambda expression can implement an interface which contains single abstract method (Functional Interface) |
| Inside anonymous inner class we can declare instance variables. |
Inside Lambda expression we can't declare instance variables, whatever the variables declared are simply acts as local variables. |
| Anonymous inner classes can be instantiated |
Lambda expressions can't be instantiated |
| Inside anonymous inner class "this" always refers current anonymous inner class object but not outer class Object. |
Inside Lambda expression "this" always refers current outer class object. That is enclosing class object. |
| Anonymous inner class is the best choice if we want to handle multiple methods. |
Lambda expression is the best choice if we want to handle interface with single abstract method (Functional Interface). |
| In the case of anonymous inner class at the time of compilation a separate dot class file will be generated (outerclass$1.class) |
At the time of compilation no dot class file will be generated for Lambda expression. It simply converts in to private method outer class. |
| Memory allocated on demand whenever we are creating an object |
Reside in permanent memory of JVM (Method Area). |