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 :

Difference between Anonymous Inner Class and Lambda Expression :

--

--

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