Java Lambda Expression Block Lambda Expressions

Introduction

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);
}

public class Main {
  public static void main(String args[]) {

    // This block lambda computes the factorial of an int value.
    NumericFunc factorial = (n) -> {/*from   w ww  . j ava 2s .  c om*/
      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));
  }
}



PreviousNext

Related