Posts

Showing posts from September, 2018

Lambda Expression v/s Anonymous Inner Class - Java 8

Image
Lambda Expression is new feature of Java 8. Before Java 8 Anonymous Inner Class was very useful to provide method local implementation of any interface like ActionListner or EventListener etc. With Java 8 if the interface is a Functional Interface we can use Lambda Expression to provide implementation. Both of these can implement an Interface but there are subtle differences between them. Following are to the point differences between them - Anonymous Inner class can implement any interface but Lambda can implement only Functional Interface , the interface with only one abstract method. Anonymous Inner Class can have state variables but Lambda Expression cannot. Lambda Expression is stateless. Lambda Expression can only access final or effectively final variables of containing method. Scope - Anonymous Inner Class creates a new scope whereas Lambda Expression does not. Lambda executes always within scope of surrounding context. To explain it better, in Anonymous Inner Clas