Lambda expressions were introduced in Java 8 to facilitate functional programming and make the code more concise and readable. They allow us to treat functions as method arguments or code as data.
The syntax of a lambda expression consists of three parts:
-
Parameter List: This is the list of parameters that the lambda expression takes as input. It can be empty if the expression doesn’t require any arguments. For example,
(int a, int b)
or()
. -
Arrow Token: The arrow token
->
separates the parameter list from the body of the lambda expression. It signifies that the input parameters are used to define the behavior of the expression. -
Body: This is the actual implementation of the lambda expression. It can be a single statement or a block of statements enclosed in curly braces. If the body is a single statement, curly braces can be omitted. For example,
a + b
or{ System.out.println("Hello"); return a + b; }
.
Here is the general syntax of a lambda expression:
(Parameter List) -> { Body }
Now let’s see a few examples of lambda expressions:
- Addition of two numbers:
(int a, int b) -> a + b
This lambda expression takes two integer parameters
a
andb
, and returns their sum. - Checking if a number is even:
(int number) -> number % 2 == 0
This lambda expression takes an integer parameter
number
and returnstrue
if the number is even, otherwisefalse
. - Printing a message:
() -> System.out.println("Hello, World!")
This lambda expression doesn’t take any parameters and simply prints “Hello, World!”.
Lambda expressions can also be used with functional interfaces, which are interfaces that define a single abstract method. They provide the target type for lambda expressions in Java.
With the introduction of lambda expressions, Java has become more expressive and allows developers to write concise and readable code.
References:
#java #lambdas