2. Introduction
• Most modern languages adopted Lambda expression as a
fundamental part of functional programming
• JVM based languages such as Scala, Groovy and Clojure and recently
Java 8 have integrated them as key part of the language
• What’s so interesting about Lambda expressions?
• Completely invisible from the JVM’s perspective
• JVM only knows bytecode which is a strict OO specification
• We compared the Scala and Java compilers implement Lambda
expressions and results were pretty surprising!
Takipi
www.takipi.com
www.takipi.com
3. Sample Expression
A simple Lambda expression that converts a list of Strings to a list of
their lengths:
• In Java –
• In Scala – Don’t be tricked
by its simplicity –
behind the scenes
some complex
stuff is going on.
Takipi
www.takipi.com
5. The Code
Bytecode contents of the .class produced by the Scala compiler
This is what
the JVM will
actually
execute
Takipi
www.takipi.com
6. Cont.
• A new instance of a synthetic class
is created and initialized
• JVM’s assumes that this object
holds the Lambda method
what’s going
on inside that
Lambda
object?
Note :
Lambda is defined as an integral part of
method but it resides completely
outside of class
Takipi
www.takipi.com
7. The Lambda Object
• The Lambda class is derived
from scala.runtime.AbstractFunction1.The map() function
polymorphically invokes the overridden apply() whose code is below
–
• Method .length() gets executed automatically
Takipi
www.takipi.com
8. Output
A lot of bytecode is generated
• an additional class and
• a bunch of new methods.
• It shows the complexity behind these constructs
Phew… it was
a quite long
way to get
here
Takipi
www.takipi.com
9. Contd.
• We were expecting Java 8 to implement this the same way, but were
quite surprised to see that they took another approach completely
Takipi
www.takipi.com
11. The Code
• The bytecode here is a bit shorter
• Begins by loading the names var and invokes its .stream() method
• It uses the new invokeDynamic instruction
Takipi
www.takipi.com
12. Cont.
• Dynamic Linking
invokedynamic () stores name and signature of the Lambda in a
separate table in the .class to which the #0 parameter passed to the
instruction points
This new table changed and improved the structure of the bytecode
specification
Takipi
www.takipi.com
13. The Lambda Code
• This is the code for the actual Lambda expression
Advantages :
• Compiled as a static function to avoid passing an
additional this object like we saw in Scala
• invokedynamic approach allows us to invoke the method which is
polymorphic from the .map() function’s perspective without
allocating a wrapper object or invoke a virtual override method
Takipi
www.takipi.com
14. Summary
• It’s fascinating to see how Java, the most “strict” of modern
languages is now using dynamic linkage to power its new Lambda
expressions. It’s also an efficient approach, as no additional class
loading and compilation is needed – the Lambda method is simply
another private method in our class
• Java 8 has really done a very elegant job in using new technology
introduced in Java 7 to implement Lambda expressions in what is a
very straightforward way. It’s pleasant in a way to see that even a
“venerable” lady such as Java can teach us all some new tricks
Takipi
www.takipi.com