Lambda Expression

It is a shorter way of writing an implementation of a method which we can execute later at some point.
Syntax : (arg1,arg2,arg3,…) -> {body of implementing method};

Examples :
Ex1 :

1
2
(int x, int y,int z) -> {  return x + y + z; };
 
(int x, int y,int z) -> {  return x + y + z; };
 

Ex2:

1
2
() -> System.out.println("no arg method");
 
() -> System.out.println("no arg method");
 

Ex3:

1
2
(String s) -> { System.out.println(s); };
 
(String s) -> { System.out.println(s); };
 

Ex4:

1
2
() -> 10
 
() -> 10
 

Ex5:

1
() -> { return 3.14 };
() -> { return 3.14 };

Note: Lambda can be used only for functional interfaces

Even if there is an abstract class with one and only one abstract method also Lambda will not work.
Example
Consider an abstract class as below

1
2
3
4
5
package com.kb.Lambda;
 
public abstract class AbstractClass {
abstract void show();
}
package com.kb.Lambda;

public abstract class AbstractClass {
abstract void show();
}

Create an implementor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.kb.Lambda;
 
public class AbstractClassImplementor {
 
    public static void main(String[] args) {
        AbstractClass abstractClass = new AbstractClass() {
            
            @Override
            void show() {
                System.out.println("show called");
                
            }
        };
 
//New way using lambda
AbstractClass abstractClass1 = () -> System.out.println("show called new way");
 
    }
    
}
package com.kb.Lambda;

public class AbstractClassImplementor {

	public static void main(String[] args) {
		AbstractClass abstractClass = new AbstractClass() {
			
			@Override
			void show() {
				System.out.println("show called");
				
			}
		};

//New way using lambda
AbstractClass abstractClass1 = () -> System.out.println("show called new way");

	}
	
}

This New way will give the compile time error with no doubt.
So lambda expression cannot be used with abstract class even if it is containing single abstarct method.

So it must be used only in case of functional interface.

Key points on Lambda expression are

1)
A lambda expression can have zero, one or more parameters which is decided as per the method signature in functional interface.

2)
The type of the parameters can be explicitly declared or it can be inferred from the context. e.g. (int a,int b) is same as just (a,b)

3)
Empty parentheses should be used to represent an empty set of parameters.ex : () -> 1

4)

If method in functional interface has single parameter, and its type is inferred, it is not mandatory to use parentheses. e.g. a -> return a+a;

5)
If body of lambda expression has single statement, curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.

6)
When there is more than one statement in body than these must be enclosed in curly brackets and the return type of the anonymous function is the same as the type of the value returned within the block, or void if nothing is returned.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on