The World’s Largest Online Community for Developers

'; java - Lambda expression understandment - LavOzs.Com

Could anyone explain me how what the output of this code would be and why?

I really don't understand how the lambdas exactly work.

interface IntProcessor {
    int process(int i1, int i2);

public static void main(String[] args) {
    IntProcessor ip1 = (x,y) -> x*2 + y;
    IntProcessor ip2 = (a,b) -> a + b*2;
    int x = 3;
    int y = 7;
    x = ip1.process(x,y);
    y = ip2.process(y,x);
    System.out.println(x + ", " + y);

Thanks in advance

Lambdas are a way of providing an implementation of an interface that has only a single abstract method (known as a functional interface).

Given the following interface declaration

interface IntProcessor {
    int process(int input1, int input2);

There were two ways to use it prior to Java 8.

One was to define a class and implement it.

    class MyIntProcessor implements IntProcessor {
        public int process(int a, int b) {
            return  a*a + b*b;

You could then do this.

        IntProcessor ip1 = new MyIntProcessor();
        int n = ip1.process(4,5);
        System.out.println(n); // prints 41

The other way was to create an anonymous class

        IntProcessor ip2 = new IntProcessor() {
            public int process(int a, int b) {
               return a*a + b*b;

And again.

        n = ip2.process(4,5);
        System.out.println(n); // prints 41

With lambdas, you can do the following:

        IntProcessor ip3 = (a,b)-> a*a + b*b;
        n = ip3.process(4,5);
        System.out.println(n); // prints 41
  1. (a,b) are the arguments and they can be any values.
  2. The -> operator indicates a Lambda and the right side will use the argument variables and do something with them.

The return type from the Lambda expression must match the return type of the Functional Interface.

The compiler will take care of finding the appropriate interface and ensuring the arguments match up with their types or report an error if they don't.

IntProcessor is an interface, to instanciate it you need to define the functions, in this case process.

What you are doing as a lambda when creating ip1 and ip2 is defining a implementation of the interface on the fligth, I.e. defining the function while creating the object. In ip2 (x,y) represents the input of the function implementation, this is only a way of declaring what variables names you will use in the body, and to the rigth of -> you are defining the body of the function.

When calling ip1.process(x,y), it will return x*2+y When calling ip2.process(x,y), it will return a+b*2 where a is the value of x passed to the function and b is the value of y passed to the function

Your interface IntProcessor is a functional interface, as it has a Single Abstract Method. So, its implementations (your ip1 and ip2) may be treated as lambda expressions.

You are making implementations on the fly. For example, your ip1 can be viewed as

int process (int input1, int input2) {
    return x*2 + y;

So I think the actual output of your code would be 13, 13 (x=3*2+7, y=7+2*3).

What is a lambda (function)?
What is the difference between a 'closure' and a 'lambda'?
Retrieving Property name from lambda expression
Distinct() with lambda?
list comprehension vs. lambda + filter
What is a lambda expression in C++11?
Why does lambda translation need generation of a static method?