Block Lambda Expression(JAVA PROGRAMMING GUIDE)



Read Previous Blog on Lambda expression.Here is a quick recap on what lambda expression really is; Lambda expression are anonymous method which are executed by functional interface and as a result anonymous class is created.A functional interface is interface with only one abstract method. Abstract method is a method that is declared but contains no implementation. To follow up previous tutorial , Here is example of lambda expression and functional interface where parameters have been passed.

interface inf{  //inf is functional interface
int abstractMethod(int parameter1); //this is abstract method with parameter

public class Func { //creation of new class Func 
public static void main(String[] args) {
inf os = (parameter1)->parameter1 % 2;
// os is object name and interface inf is initialized 
//by this block

Things to notice

    • inf os = (parameter1)->parameter1 % 2;
      is equivalent to

      int abstractMethod(int parameter1){
      parameter1 = parameter1 % 2; //parameter1 %= 2;
      return parameter1;
    • No return statement is used while using single lambda espression
    • inf os = (parameter1)->parameter1 % 2;  just variable identifier is passed instead using primitive type + identifier i.e int parameter1; because it already knows parameter’s type through the abstract method.

Lambda Block

Lambda Block means lambda expression with multiple statements.It expands the type of operation one can perform with lambda expression. Multiple statement containing bodies are called expression bodies. Lambda expression with expression bodies are called expression lambdas. Above example was single expression lambda but now we will focus on expression lambdas.Note: When using expression lambdas you will have to explicitly use a return statement to return a value.

interface GreaterNumberFinder{
int finder(int number1, int number2);

public class NumberFinder {

    public static void main(String[] args) {
        GreaterNumberFinder checker =(number1,number2)-->{
            int TempStorer=0;
                TempStorer = number2;
            return TempStorer;  //you will have to explicitly return a value.

            /* instead of this code 
                you can simply use
                    3 way ternary operator
            return number1>number2?number1:number2; 

System.out.println(checker.finder(3, 2));


In this program you can notice how conditional statements are used and new variable TempStorer is declared. These all are legal inside lambda bodies.In fact lambda bodies act like any other method bodies. You will have to remember that return statements returns from lambda instead of that enclosing method.

This is Ashish Thapa. It's just a name after all. Just had a thought that our names should signify our situation and should be dynamic. What if our name upgraded with respect to experiences acquired.

1 Comment

Leave a Comment

Your email address will not be published. Required fields are marked *