Java 8 — Anonymous Inner Class vs Lambda Expression | Code Factory
Index Page : Link
Reference Link : Link
Donate : Link
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 */
}
}
Output :
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 */
}
}
Output :
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 :
(1) Anonymous Inner Class that extends concrete class
class Test {
}Test t = new Test() {
};
(2) Anonymous Inner Class that extends abstract class
abstract class Test {
}Test t = new Test() {
};
(3) Anonymous Inner Class that implements an interface which contains multiple methods
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() {
}
};
(4) Anonymous Inner Class that implements an interface which contains only one abstract method
// 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 = () -> {
};
Note : λ-Expression is only possible in last scenario. In first 3 scenario λ-Expression is not possible.
Example 1 :
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();
}
}
Output :
20
10
Example 2 :
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();
}
}
Output :
20
10
- Inside Anonymous inner class we can declare instance variable. Inside Lambda (λ) Expression it is not possible to declare instance variable, whatever variables declared inside Lambda (λ) Expression simply local variables only.
- Inside Anonymous inner class
this
always refers current inner class object. Inside Lambda (λ) Expressionthis
always refers current outer class object only.- Lambda (λ) Expression came to replace Anonymous inner classes is wrong statement.
- Anonymous inner class is more powerfull than Lambda (λ) Expression. A particular case of Anonymous inner class we can replace with Lambda (λ) Expression.
Example 3 :
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();
}
}
CE : local variables referenced from a lambda expression must be final or effectively final
Advantages of Lambda (λ) Expression :
- We can enable functional programming in Java
- We can reduce length of the code so that readability wll be improved
- We can resolve complexity of Anonymous Inner Class until some extent
- We can handle procedures/functions just like values
- We can pass procedures/functions as arguments
- Easier to use updated APIs and libraries
- Enable support for parallel processing