Before jumping to programming lets clear some points in related with the lamda expression and Anonymous inner class within 2 min of reading.
- Anonymous class is a class without name while lamda expression is a function without name.
2. Since Anonymous class is a class so it can extends abstract class or any concrete class but lamda expression can not extends abstract class or concrete class.
3. Anonymous inner class can implement interface having any number of abstract method but in case of Lamda an interface must have only one single abstract method.
4. It is possible to declare an instance variable in Anonymous inner class but in lamda only local variable scope will reflect in implementation.
5. Annonymous inner class is best to use when we want to implement methods but in lamda ony one abstract method is require, i.e- Funcational interface.
6. In case of Anonymous innerclass separate .class file is generated after compliation but in case of lamda no separate .class file is generated.
7. This is the most important difference, whenever any anonymous class is created, memory is allocated in heap area but in case of lamda memory is allocated in method area of JVM.
Lambdas are not the same thing as anonymous classes in terms of byte code and also in terms of behavior/functioning.