Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming, and simplifies the development a lot.
Syntax
A lambda expression is characterized by the following syntax.
parameter -> expression body
Following are the important characteristics of a lambda expression.
- Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
- Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
- Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
- Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.
Lambda Expressions Example
Create the following Java program using any editor of your choice in, say, C:\hitechpoints\workspace\java8\src.
public class Java8LambdaExpression {
public static void main(String args[]) {
Java8LambdaExpression lambdaExpression = new Java8LambdaExpression();
// 1. Addtion operation with type declaration
MathOperation addition = (int a, int b) -> a + b;
// 2. Subtract operation with out type declaration
MathOperation subtraction = (a, b) -> a - b;
// 3. Multiplication operation with return statement along with curly braces
MathOperation multiplication = (int a, int b) -> { return a * b; };
// 4. Division operation without return statement and without curly braces
MathOperation division = (int a, int b) -> a / b;
System.out.println("20 + 10 = " + lambdaExpression.operate(20, 10, addition));
System.out.println("20 - 10 = " + lambdaExpression.operate(20, 10, subtraction));
System.out.println("20 x 10 = " + lambdaExpression.operate(20, 10, multiplication));
System.out.println("20 / 10 = " + lambdaExpression.operate(20, 10, division));
// 5. without parenthesis
OperationService operationService1 = message ->
System.out.println("Hello " + message);
// 6. with parenthesis
OperationService operationService2 = (message) ->
System.out.println("Hi " + message);
operationService1.sayMessage("Elavarasan");
operationService2.sayMessage("Sekar");
}
interface MathOperation {
int operation(int a, int b);
}
interface OperationService {
void sayMessage(String message);
}
private int operate(int a, int b, MathOperation mathOperation) {
return mathOperation.operation(a, b);
}
}
Output for the program
Compile the class using javac compiler as follows
C:\hitechpoints\workspace\java8\src>javac Java8LambdaExpression.java
Now run the Java8LambdaExpression as follows
C:\hitechpoints\workspace\java8\src>java Java8LambdaExpression
It should produce the following output
20 + 10 = 30
20 - 10 = 10
20 x 10 = 200
20 / 10 = 2
Hello Elavarasan
Hi Sekar
Following are the important points to be considered in the above example.
- Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we’ve used various types of lambda expressions to define the operation method of MathOperation interface. Then we have defined the implementation of sayMessage of OperationService.
- Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.
Scope
Using lambda expression, you can refer to any final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.
Scope Example
Create the following Java program using any editor of your choice in, say, C:\hitechpoints\workspace\java8\src.
public class Java8LambdaExpression {
final static String START = "Hello! ";
public static void main(String args[]) {
OperationService operationService = message ->
System.out.println(START + message);
operationService.sayMessage("Elavarasan");
}
interface OperationService {
void sayMessage(String message);
}
}
Output for the program
Compile the class using javac compiler as follows
C:\hitechpoints\workspace\java8\src>javac Java8LambdaExpression.java
Now run the Java8LambdaExpression as follows
C:\hitechpoints\workspace\java8\src>java Java8LambdaExpression
It should produce the following output
Hello! Elavarasan