Java 8 — Anonymous Inner Class vs Lambda Expression | Code Factory

Thread Example using Anonymous Inner Class :

package com.codeFactory.anonymousClass;public class Test {
public static void main(String... args) {
/* Child Thread start */
Runnable r = new Runnable() {
@Override
public void run() {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
}
};
/* Child Thread end */
Thread t = new Thread(r);
t.start();
/* Main Thread start */
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
/* Main Thread end */
}
}
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread

Thread Example using Lambda (λ) Expression :

package com.codeFactory.anonymousClass;public class Test {
public static void main(String... args) {
/* Child Thread start */
Runnable r = () -> {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
};
/* Child Thread end */
Thread t = new Thread(r);
t.start();
/* Main Thread start */
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
/* Main Thread end */
}
}
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread

Anonymous Inner Class != Lambda (λ) Expression :

class Test {

}
Test t = new Test() {

};
abstract class Test {

}
Test t = new Test() {

};
interface Test {
public void m1();
public void m2();
public void m3();
}
Test t = new Test() {
@Override
public void m1() {

}
@Override
public void m2() {

}
@Override
public void m3() {

}
};
// with Anonymous Inner Class
interface Test {
public void m1();
}
Test t = new Test() {
@Override
public void m1() {

}
};
// with λ-Expression
interface Test {
public void m1();
}
Test t = () -> {

};
package com.codeFactory.anonymousClass;interface Interf {
public void m1();
}
public class Test {
int x = 10;

public void m2() {
Interf i = new Interf() {
int x = 20; // instance variable
@Override
public void m1() {
System.out.println(this.x);
System.out.println(Test.this.x);
}
};
i.m1();
}

public static void main(String... args) {
Test t = new Test();
t.m2();
}
}
20
10
package com.codeFactory.anonymousClass;interface Interf {
public void m1();
}
public class Test {
int x = 10;

public void m2() {
Interf i = () -> {
int x = 20;
System.out.println(x);
System.out.println(this.x);
};
i.m1();
}

public static void main(String... args) {
Test t = new Test();
t.m2();
}
}
20
10
package com.codeFactory.anonymousClass;interface Interf {
public void m1();
}
public class Test {
int x = 10;
public void m2() {
int y = 20;
Interf i = () -> {
System.out.println(x); // ✓
System.out.println(y); // ✓
x = 111; // ✓
y = 222; // X
};
i.m1();
}

public static void main(String... args) {
Test t = new Test();
t.m2();
}
}

Advantages of Lambda (λ) Expression :

  1. We can enable functional programming in Java
  2. We can reduce length of the code so that readability wll be improved
  3. We can resolve complexity of Anonymous Inner Class until some extent
  4. We can handle procedures/functions just like values
  5. We can pass procedures/functions as arguments
  6. Easier to use updated APIs and libraries
  7. Enable support for parallel processing

Difference between Anonymous Inner Class and Lambda Expression :

--

--

--

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

Recommended from Medium

Angular 7 + Nginx + Docker + SSL + Jenkins Automatic deployment

What is Memento Design Pattern

How to make Auto Layout more convenient in iOS

Zoo App part 2

Uploading to GitHub using Bash: 1-Minute Guide

UIStackView + UIImageView- distribution, axis, spacing, and clips to bounds and content mode.

Hunting Down an Obscure Avro Bug on a Remote Spark Cluster

Apex Triggers made easy

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

Functional Interfaces in Java

Limit of builder pattern in object composition

Why is there a String array parameter in Java main method?

How to use Switch Expressions in Java!