Java 8 - Lambda Expressions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4
 
8/10/22, 8:35 PMJava 8 - Lambda Expressionshttps://www.tutorialspoint.com/java8/java8_lambda_expressions.htm1/4
Ja󰁶a8-La󰁭󰁢󰁤aE󰁸󰁰󰁲󰁥󰁳󰁳󰁩󰁯󰁮󰁳   
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.
󰁓󰁹󰁮󰁴󰁡󰁸   
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 caninference the same from the value of the parameter.
Optional parenthesis around parameter
 − No need to declare a single parameter inparenthesis. For multiple parameters, parentheses are required.
Optional curly braces
 − No need to use curly braces in expression body if the bodycontains a single statement.
Optional return keyword
 − The compiler automatically returns the value if the body has asingle expression to return the value. Curly braces are required to indicate that expressionreturns a value.
󰁌󰁡󰁭󰁢󰁤󰁡󰁅󰁸󰁰󰁲󰁥󰁳󰁳󰁩󰁯󰁮󰁳󰁅󰁸󰁡󰁭󰁰󰁬󰁥   
Create the following Java program using any editor of your choice in, say, C:\> JAVA.
J󰁡󰁶󰁡8T󰁥󰁳󰁴󰁥󰁲.󰁪󰁡󰁶󰁡   
public
 
class
 
Java8Tester
 { 
public
 
static
 
void
 main(
String
 args[]) { 
Java8Tester
 tester = 
new
 
Java8Tester
(); 
//with type declaration
 
MathOperation
 addition = (
int
 a, 
int
 b) -> a + b; 
//with out type declaration
 
MathOperation
 subtraction = (a, b) -> a - b; 
Live Demo
 
8/10/22, 8:35 PMJava 8 - Lambda Expressionshttps://www.tutorialspoint.com/java8/java8_lambda_expressions.htm2/4
 
//with return statement along with curly braces
 
MathOperation
 multiplication = (
int
 a, 
int
 b) -> { 
return
 a * b; }; 
//without return statement and without curly braces
 
MathOperation
 division = (
int
 a, 
int
 b) -> a / b; 
System
.
out
.println("10 + 5 = " + tester.operate(10, 5, addition)); 
System
.
out
.println("10 - 5 = " + tester.operate(10, 5, subtraction));  
System
.
out
.println("10 x 5 = " + tester.operate(10, 5, multiplicatio  
System
.
out
.println("10 / 5 = " + tester.operate(10, 5, division)); 
//without parenthesis
 
GreetingService
 greetService1 = message -> 
System
.
out
.println("Hello " + message); 
//with parenthesis
 
GreetingService
 greetService2 = (message) -> 
System
.
out
.println("Hello " + message);  greetService1.sayMessage("Mahesh");  greetService2.sayMessage("Suresh");  } 
interface
 
MathOperation
 { 
int
 operation(
int
 a, 
int
 b); } 
interface
 
GreetingService
 { 
void
 sayMessage(
String
 message); } 
private
 
int
 operate(
int
 a, 
int
 b, 
MathOperation
 mathOperation) { 
return
 mathOperation.operation(a, b);  } }
V󰁥󰁲󰁩󰁦󰁹󰁴󰁨󰁥R󰁥󰁳󰁵󰁬󰁴  
Compile the class using
 javac
 compiler as follows −
C:\JAVA>javac Java8Tester.java
Now run the Java8Tester as follows −
5
 
8/10/22, 8:35 PMJava 8 - Lambda Expressionshttps://www.tutorialspoint.com/java8/java8_lambda_expressions.htm3/4
C:\JAVA>java Java8Tester
It should produce the following output −
10 + 5 = 15 10 - 5 = 5 10 x 5 = 50 10 / 5 = 2 Hello Mahesh Hello Suresh
Following are the important points to be considered in the above example.Lambda expressions are used primarily to define inline implementation of a functionalinterface, i.e., an interface with a single method only. In the above example, we've usedvarious types of lambda expressions to define the operation method of MathOperationinterface. Then we have defined the implementation of sayMessage of GreetingService.Lambda expression eliminates the need of anonymous class and gives a very simple yetpowerful functional programming capability to Java.
󰁓󰁣󰁯󰁰󰁥   
Using lambda expression, you can refer to any final variable or effectively final variable (which isassigned only once). Lambda expression throws a compilation error, if a variable is assigned avalue the second time.
S󰁣󰁯󰁰󰁥E󰁸󰁡󰁭󰁰󰁬󰁥   
Create the following Java program using any editor of your choice in, say, C:\> JAVA.
Java8Tester.java 
public
 
class
 
Java8Tester
 { 
final
 
static
 
String
 salutation = "Hello! "; 
public
 
static
 
void
 main(
String
 args[]) { 
GreetingService
 greetService1 = message -> 
System
.
out
.println(salutation + message);  greetService1.sayMessage("Mahesh");  } 
interface
 
GreetingService
 { 
void
 sayMessage(
String
 message); 
Live Demo
5
 
8/10/22, 8:35 PMJava 8 - Lambda Expressionshttps://www.tutorialspoint.com/java8/java8_lambda_expressions.htm4/4
 } }
V󰁥󰁲󰁩󰁦󰁹󰁴󰁨󰁥R󰁥󰁳󰁵󰁬󰁴  
Compile the class using
 javac
 compiler as follows −
C:\JAVA>javac Java8Tester.java
Now run the Java8Tester as follows −
C:\JAVA>java Java8Tester
It should produce the following output −
Hello! Mahesh
5

Reward Your Curiosity

Everything you want to read.
Anytime. Anywhere. Any device.
No Commitment. Cancel anytime.
576648e32a3d8b82ca71961b7a986505