Lambda Expressions
Lambda expressions are presented in Java 8 and are touted to be the greatest component of Java 8. Lambda expression facilitates functional programming, and simplifies the development a lot.
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.
Lambda Expressions Example
Create the following Java program using editor and save in some folder like com.mindclues.java8
Java8Lambda.java
package com.prejava.java8 public class Java8Lambda { public static void main(String args[]){ Java8Lambda lamb = new Java8Lambda(); //with type declaration MathOperation add = (int a, int b) -> a + b; //without type declaration MathOperation subtract = (a, b) -> a - b; //with return statement along with curly braces i.e."{ }" MathOperation multiply = (int a, int b) -> { return a * b; }; //without return statement and without curly braces MathOperation divide = (int a, int b) -> a / b; System.out.println("20 + 5 = " + lamb.operate(20, 5, add)); System.out.println("20 - 5 = " + lamb.operate(20, 5, subtract)); System.out.println("20 x 5 = " + lamb.operate(20, 5, multiply)); System.out.println("20 / 5 = " + lamb.operate(20, 5, divide)); //with parenthesis Greetings greetings1 = message -> System.out.println("Hi!! " + message); //without parenthesis Greetings greetings2 = (message) -> System.out.println("Hi!! " + message); greetings1.greet("Suresh"); greetings2.greet("Ramesh"); } private int operate(int a, int b, MathOperation mathOperation){ return mathOperation.operation(a, b); } }
MathOperation.java
package com.prejava.java8
interface MathOperation {
int operation(int a, int b);
}
Greetings.java
package com.prejava.java8
interface Greetings {
void greet(String message);
}
Verify the Result
Compile the class using javac compiler as follows −
$javac Java8Lambda.java
Now run the Java8Tester as follows −
$java Java8Lambda
It should produce the following output −
20 + 5 = 25 20 - 5 = 15 20 x 5 = 100 20 / 5 = 4 Hi!! Suresh Hi!! Ramesh
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 greet of Greetingings.
-
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 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 editor and save in some folder like com.prejava.java8
Java8Tester.java
public class Java8Lambda { final static String salutation = "Hi!! "; public static void main(String args[]){ Greetings greetings1 = message -> System.out.println(salutation + message); greetings1.greet("Suresh"); }
Greetings.java
interface Greetings { void greet(String message); } }
Verify the Result
Compile the class using javac compiler as follows −
$javac Java8Lambda.java
Now run the Java8Lambda as follows −
$java Java8Lambda
It should produce the following output −
Hi!! Suresh
post a comment