stacker stacker -5 years ago 118
Java Question

What does this syntax a -> b -> a + b; produce?

This answer does-java-support-currying defines a

curriedAdd = a -> b -> a + b;

IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b;
IntUnaryOperator adder5 = curriedAdd.apply(5);

I read the relevant section 15.27 of the JLS it states:

The production of a lambda expression is
LambdaExpression: LambdaParameters -> LambdaBody

Does this mean that
a -> b
is one lamba expression and
b -> a + b
another one ?

Answer Source

You can expand the expression to make it easier to understand:

IntFunction<IntUnaryOperator> curriedAdd = a -> { return b -> a + b; };

And indeed you have two lambdas expressions: the inner one which is the unary operator and the outer one which is the function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download