Why Functional References are called “Lambda Expressions” in Java 8?

Many of us are very curious about the fact that why functional references introduced in Java 8 are know as Lambda Expressions. So let’s reveal the fact of word “Lambda Expressions” and make it very simple to understand.

This fact is very much related to the Evaluation process of any expression. Many of us may have solved the mathematical problems and while solving the problems we may have observed how we are reducing the mathematical expression to some values. Similarly we also may have observed in many programming languages how any expression is evaluated.

Let’s take an example,

We have below functions,

Square Function : SQR(x) : x*x
Sum Function : SUM(x, y) : x + y

And using above function let’s create a new function for calculating Sum of Squares,

Sum of Squares Function : SUMSQR(x, y): SUM(SQR(x), SQR(y))

Now, if we evaluate SUMSQR(2+2,3+3) the evaluation process would be same as below,

SUMSQR(2+2, 3+3)
=> SUMSQR(4, 3+3)
=> SUMSQR(4, 9)
=> SQR(4) + SQR(9)
=> 4 * 4 + SQR(9)
=> 16 + SQR(9)
=> 16 + 9 * 9
=> 16 + 81
=> 97

If you observe above evaluation order then,

  • Step 1: Expressions in all function arguments are evaluated to the actual parameter values from left to right
  • Step 2: Function is replaced with function body
  • Step 3: Complete the evaluation at right hand side and calculate actual value
  • Step 4: Complete the evaluation at left hand side and calculate actual value
  • Step 5: Evaluate the expression from left to right

Above process keeps repeat till final answer comes. This evaluation process is known as Substitution Model which reduces the expression to a final value.

This model of substitution is invented long ago and became a basic building block of lambda calculus. And as we all know that functions are the basic building blocks of Functional Programming and function are treated as an expression in mostly all functional languages. This is the reason that Java have referred these function references as “Lambda Expressions”.

And it would also be interesting for you to know that Lambda Calculus or substitution model can only be applied to the expressions which are not causing any side effect. The evaluation must be immutable.

For example, expression “i++” in languages like Java OR C++ increments the value of ‘i’ and assign the incremented value to the same variable which is not allowed in Lambda Calculus. This is the reason that in restricted functional programming the functional expression should always be immutable so that it can work same as the evaluation of any mathematical theory.

Although in Java it is not compulsory to write immutable code in Lambda Expressions. :)

Secret is revealed now…..!!!!! Happy Learning…..!!!!!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>