Home | | Web Programming | Block Lambda Expressions

Chapter: Java The Complete Reference : The Java Language : Lambda Expressions

Block Lambda Expressions

The body of the lambdas shown in the preceding examples consist of a single expression.

Block Lambda Expressions

 

The body of the lambdas shown in the preceding examples consist of a single expression. These types of lambda bodies are referred to as expression bodies, and lambdas that have expression bodies are sometimes called expression lambdas. In an expression body, the code on the right side of the lambda operator must consist of a single expression. While expression lambdas are quite useful, sometimes the situation will require more than a single expression. To handle such cases, Java supports a second type of lambda expression in which the code on the right side of the lambda operator consists of a block of code that can contain more than one statement. This type of lambda body is called a block body. Lambdas that have block bodies are sometimes referred to as block lambdas.

A block lambda expands the types of operations that can be handled within a lambda expression because it allows the body of the lambda to contain multiple statements. For example, in a block lambda you can declare variables, use loops, specify if and switch statements, create nested blocks, and so on. A block lambda is easy to create. Simply enclose the body within braces as you would any other block of statements.

Aside from allowing multiple statements, block lambdas are used much like the expression lambdas just discussed. One key difference, however, is that you must explicitly use a return statement to return a value. This is necessary because a block lambda body does not represent a single expression.

Here is an example that uses a block lambda to compute and return the factorial of an int value:

 

 

// A block lambda that computes the factorial of an int value.

 

interface NumericFunc { int func(int n);

 

}

 

class BlockLambdaDemo {

 

public static void main(String args[])

 

{

 

// This block lambda computes the factorial of an int value.

NumericFunc factorial = (n) -> {

 

int result = 1;

 

for(int i=1; i <= n; i++) result = i * result;

 

return result;

 

};

 

System.out.println("The factoral of 3 is " + factorial.func(3));

System.out.println("The factoral of 5 is " + factorial.func(5));

 

}

 

}

 

The output is shown here:

 

The factorial of 3 is 6

 

The factorial of 5 is 120

In the program, notice that the block lambda declares a variable called result, uses a for loop, and has a return statement. These are legal inside a block lambda body. In essence, the block body of a lambda is similar to a method body. One other point. When a return statement occurs within a lambda expression, it simply causes a return from the lambda. It does not cause an enclosing method to return.

Another example of a block lambda is shown in the following program. It reverses the characters in a string.

 

// A block lambda that reverses the characters in a string.

 

interface StringFunc { String func(String n);

}

 

class BlockLambdaDemo2 {

 

public static void main(String args[])

 

{

 

// This block lambda reverses the characters in a string.

 StringFunc reverse = (str) -> {

 

String result = ""; int i;

 

for(i = str.length()-1; i >= 0; i--) result += str.charAt(i);

 

return result;

 

};

 

System.out.println("Lambda reversed is " + reverse.func("Lambda"));

 

System.out.println("Expression reversed is " + reverse.func("Expression"));

}

 

}

The output is shown here:

 

Lambda reversed is adbmaL

 

Expression reversed is noisserpxE

 

In this example, the functional interface StringFunc declares the func( ) method. This method takes a parameter of type String and has a return type of String. Thus, in the reverse lambda expression, the type of str is inferred to be String. Notice that the charAt( ) method is called on str. This is legal because of the inference that str is of type String.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Language : Lambda Expressions : Block Lambda Expressions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.