java8
Lambdas vs Anonymous Inner Classes (Scope and Performance).
1. Scope
- this keyword: In an anonymous class,
thisrefers to the anonymous class itself. In a lambda,thisrefers to the enclosing class. - Variables: Both can access effectively final local variables.
2. Performance (and Compilation)
- Class Files: Anonymous classes generate a separate
.classfile (e.g.,Main.class). Lambdas do not; they useinvokedynamic(Java 7+ feature) to create a call site. - Memory: Lambdas are generally more memory-efficient as they don't always create a new object instance (depending on whether they capture state).