Difference between Anonymous Inner Class and Lambda Expression | Code Factory

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

  • 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 :

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Generics upper bound and lower bound in java and android kotlin

Jupyter Notebook Culling in Kubeflow

Grafana as a Yet Another Tool for Technical Monitoring of Software Products We Build

How I Am Learning Machine Learning — Week 2: Python and Pandas (Part Two)

Lab 7: DC Motors

How To… create a proposal

Rabbit MQ Disaster Recovery(DR) Strategy

Implementing gitops the ansible way

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

OOPS in Java

Trending things to know in Java

Artificial Intelligence and many more things to know in 2022 by use of JAVA

Main principles in Java OOP (Object-Oriented Programming)