Behavior parameterization

Description

We can pass lambda expressions to methods as arguments.

Example

The following code creates a functional interface called Calculator.

Inside the Calculator there is a method called calculate which accepts two int parameters and return an int value.

In the Main class there is an engine method which accepts the functional interface Calculator as the parameter. And it calls the calculate method from the Calculator and outputs the result.

In the main method we call the engine methods four times with different lambda expressions.


public class Main {
  public static void main(String[] argv) {
    engine((x,y)-> x + y);//  w w  w .ja v  a  2 s  .  c  o  m
    engine((x,y)-> x * y);
    engine((x,y)-> x / y);
    engine((x,y)-> x % y);
  }
  private static void engine(Calculator calculator){
    int x = 2, y = 4;
    int result = calculator.calculate(x,y);
    System.out.println(result);
  }
}

@FunctionalInterface
interface Calculator{
  int calculate(int x, int y);
}

The code above generates the following result.

Note

The result of engine method is depending on lambda expressions passed into it.

The behaviour of engine method is parameterized.

Changing the behavior of a method through its parameters is called behavior parameterization.

In behavior parameterization we pass logic encapsulated in lambda expressions to methods as if was data.





















Home »
  Java Lambda »
    Java Lambda Tutorial »




Java Lambda Tutorial