Difference between Anonymous Inner Class and Lambda Expression | Code Factory

Reference Link : Link

Donate : Link

Anonymous Inner Class

package com.codeFactory;/**
* @author code.factory
*
*/
public class AnonymousClass {
public static void main(String... args) {
Name name = new Name() {

@Override
public void getName() {
System.out.println("Name : " + name);
}
};
name.getName();
}
}
interface Name {
String name = "Code Factory";
void getName();
}

Lambda Expressions

lambda expressions are added in Java 8 and provide below functionalities.

  • Enable to treat functionality as a method argument, or code as data.
  • A function that can be created without belonging to any class.
  • A lambda expression can be passed around as if it was an object and executed on demand.
package com.codeFactory;/**
* @author code.factory
*
*/
public class LambdaExpressions {
public static void main(String... args) {
Name name = (x) -> System.out.println("Name : " + x);
name.getName("Code Factory");
}
}
interface Name {
void getName(String name);
}

Difference :