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


Index Page : Link

Donate : Link

Medium Blog : Link

Applications : 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 (λ) Expression this 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 :

  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 :

Anonymous Inner classLambda Expression
It’s a class without nameIt’s a method without name (anonymous function)
Anonymous inner class can extend Abstract and concrete classesLambda (λ) expression can’t extend Abstract and concrete classes
Anonymous inner class can implement an interface that contains any number of Abstract methodsLambda (λ) expression can implement an interface which contains single abstract method (Functional Interface)
Inside anonymous inner class we can declare instance variablesInside Lambda (λ) Expression we can’t declare instance variables, whatever the variables declared are simply acts as local variables
Anonymous inner classes can be instantiatedLambda (λ) expressions can’t be instantiated
Inside anonymous inner class this always refers current anonymousInner class object but not outer classObjectInside Lambda (λ) Expression this always refers current outer class object. That is enclosing class object
Anonymous inner class is the best choice if we want to handle multiple methodsLambda (λ) expression is the best choice if we want to handle interface with single abstract method (Functional Interface)
In the case of anonymous inner class at the time of compilation a separate dot class file will be generated (outerclass$1.class)At the time of compilation no dot class file will be generated for Lambda (λ) expression. It simply convertsin to private method outer class
Memory allocated on demand whenever we are creating an objectReside in permanent memory of JVM (Method Area)

6 thoughts on “Java 8 – Anonymous Inner Class vs Lambda Expression | Code Factory”

  1. Fantastic goods from you, man. I’ve understand your stuff previous to and you’re just extremely excellent. I actually like what you’ve acquired here, certainly like what you’re stating and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can’t wait to read much more from you. This is actually a terrific site.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s