LAMBDA EXPRESSIONS

Nilgar Sagar
3 min readMay 25, 2021

LAMBDA EXPRESSIONS AND ITS USES

History behind lambda expressions.

In the mathematics there is one concept called as lambda calculus. This Lambda calculus made the big change in the mathematical world ,in 1930’s the first time lambda calculus was introduced by Alonzo Church into the mathematical world. By using the lambda expressions very big and difficult mathematical problems where solved very easily because of benefits of lambda calculus the programmer also started the use of lambda expressions .LISP is first programming language to use lambda expressions.

What are Lambda expressions?

A lambda expression is a short block of code that takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method. Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces.

Lambda expressions are added in Java 8 and provide below functionalities.

1.Enable functional programming in java.

2.write readable, maintainable and concise code.

3.To use API’s very easily and effectively.

4.To enable parallel processing.

5.Enable to treat functionality as a method argument, or code as data.

6.A function that can be created without belonging to any class.

7.A lambda expression can be passed around as if it was an object and executed on demand.

Requirements of lambda expressions:

1.It should be Anonymous ( means the functions should not contain the name).

2.There should not be any return type.

3.There should not be any modifier.

Syntax :

lambda operator -> body

where lambda operator can be:

1.Zero parameter:

() -> System.out.println(“Zero parameter lambda”);

2.One parameter:

(a) -> System.out.println(“The value is: “ + a);

3.Multiple parameters:

(a1, a2) -> System.out.println(“Multiple parameters: “ + a1 + “, “ + a2);

Examples of lambda expression in java:

1.Lets see the normal code without using Lambda expression to print “HELLO WORLD” how much lines it takes and lets compare it with code using the lambda expression .

Normal method:

public void normal()

{

System.out.pritnln(“HELLO WORLD”);

}

This is normal method now to convert this code to Lambda expression there should not be any name ,modifier, and return type . And use indication by using symbol -> which tell that this method is lambda expression. If there is only one statement then there is no need to use curly brackets. So our lambda expression will be as below:

Lambda Expression:

()->System.out.println(“HELO WORLD”);

2.Lets take another example to print multiplication of two integers.

Normal method:

public void normal(int a, int b)

{

System.out.println(a*b);

}

Lambda Expression:

(a,b)->System.out.println(a*b);

When we are using Lambda expression the complier automatically guess the type of parameter you pass automatically know the type whether it is integer ,float ,character or any other.

3.Now let know how to return any value .

Normal method:

public int normal(int a, int b)

{

return a*b;

}

Lambda Expression:

(a,b)->a*b;

Here complier will automatically guess that user want to return the value. There is no need to write return here if you use then you should also use curly brackets even though there is only one statement.

4.Now write code to return cube of given number

Normal method:

public int normal(int a)

{

return a*a*a;

}

Lambda Expression:

a->a*a*a;

when there is only one argument then there is no any need to use parenthesis ().

Important points:

1.The body of a lambda expression can contain zero, one or more statements.

2.When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.

3.When there are more than one statements, then these must be enclosed in curly brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned.

--

--