Lambda Expressions …. How it is Implemented In Java (part-1)

Chinmay Venkata Sabbam
art of coding
Published in
6 min readOct 24, 2019

What is a Lambda Expression …….

Lambda Expression is a anonymous Function

Anonymous Function:

Anonymous function is a function definition that is not bound to an identifier. These are a form of nested function, in allowing access to variables in the scope of the containing function (non-local functions). This means anonymous functions need to be implemented using closures.

Let’s Analyze the Anonymous Function with an Example :

Generally Functions or methods in Java Composed with
1. Name
2. Parameter list
3. Body
4. Return type

out of these 4 parameters, which parameters are the most important ??
Parameter list and the body are the most important parameters and the remaining two parameters,Name and Return type are optional…..
Let’s See the below Example :

Lambdas Expression Implementation

Observe the run() Method Here , line no: 3 and line no:9 in above example , Here name is Anonymous and Return Type is Inferred from Runnable Interface . so i.e why Lambda expression is called as Anonymous Function

Examples of Implementing the Lambda Expression

How Lambda Expressions fits in to JAVA ……

Generally two types of Coding Languages are there. one is language which supports the backward compatibility and the other is the language which does not supports the backward compatibility. Java is the language which supports the backward compatibility.

So Lambda Expressions are only applied for the Functional Interfaces. that means Interface which consists single abstract methods like Callable,Runnable and ActionListener and FileFilter etc ….

DisAdvantages of without implementing Lambda Expressions…

Example without Implementing Lambda Expression
Memory Foot print after running above example

if you run the above program and if we observe the memory foot print, you can clearly observe that More Anonymous inner classes was created in memory for every Implementation.

Example with Implementing Lambda Expression
Memory Foot print after running the above Example

if you have syntax sugar and if you did not implemented the Lambda Expressions , it creates more anonymous inner classes.
if you create the more anonymous inner classes in disk → it leads to bloated big jar files , Loading these jar files takes more time. After loading these jar files , they take more memory footprint on JVM to run these more classes and the objects of these classes create more garbage, if you create more garbage, to clear the garbage it takes more run time memory . it never helps in the long run.

How JAVA Compiler Treats the Lambda Expressions…..

Generally in the other JVM Languages like Scala and groovy , they create the anonymous inner classes even though we are using Lambda expressions. but JAVA does not creates, How the Java implements this…

In Java 1.7 , JAVA introduces the new feature called invoke dynamic like static and instant . Actually at that point of time, there is no need of Implement that feature. it is Mostly helpful for Dynamic type languages based on JVM like Scala and groovy etc..(Invoke Dynamic : You can attach and detach dynamically or reattach to the function in during run time). But Java 1.8 they rewrote the invoke Dynamic feature and provides support for Lambda Expression

Compiling structure of Example with Implementing Lambda Expression

if you observe the 4:,21:,26:,38: Compiling structure of Example with Implementing Lambda Expression from above image. we can clearly understand that with invoke dynamic feature , lambda expression is compiled

Transforming Iterators

Generally we have two kinds of iterators are there in java 1. External iterators and 2. Internal iterators. Let’s Analyze the difference between these two iterators with the help of Example

Types of Iterators in Java

if you observe the External iterator Type -1 . we need to control everything, we think that if we have a control on every thing it is fine, but it is difficult because if we changed any thing, in the similar way we need to check everything. you need to control Initialize variable, termination condition(so much confusion between less-than and less-than or equal to) and increment.

To avoid this External iterator Type-2 is in place but still we need to control the object

Generally, We are confuse between simple and familiar. Simple means we do not have any thing burden on you and you can write it very easily but familiar means we looked into so many times, but we don’t want to look again

let’s Analyze these below lines from above example

difference between the internal and external iterators in java

In External Iterators we are passing collection to for , In Internal iterators we are invoking the for on the collection

Advantage of Internal Iterators : Rather than passing the Object to a function, if we call function on object, then there is a benefit called polymorphism. that means You can tell what to do and you can vary the implementation based on the actual type of the object. so that implementation details will be hidden . that is the benefit.

But In 1.7 we have disadvantage because if we implement as above example, anonymous inner classes are created.

Let’s see How the internal iterator is transformed from java 1.7 to java 1.8

internal iterator in java 1.7
  1. Changing to Lambda expression : Consumer is a functional interface so Implement Lambda Expression
Implementing Lambda Expression

2. Removal of DataTypes : List is a Integer , so it does not return other datatype. so remove Integer from above . Java supports type inference , but it is limited to Lambda Expressions.

Removed Integer

3. Removal of parenthesis only for methods having single variables : parenthesis is an optional only for methods having single variables, for methods having no variables, you can give empty parenthesis.for methods having more than one variables, you can give multiple variables inside the parenthesis.

Removal of Parenthesis

4. Applying Method References : if you observe the above parameter e , You simply receiving an parameter — and pass through it. In this cases you can apply method references .

you can learn method references in detail in part 2 post

finally after applying the method references in internal iterator
All stages

--

--