Behind the scenes: How do lambda expressions really work in Java?

Click for: original source

A look into the bytecode to see how Java handles lambdas. What does a lambda expression look like inside Java code and inside the JVM? It is obviously some type of value, and Java permits only two sorts of values: primitive types and object references. By Ben Evans.

Lambdas are obviously not primitive types, so a lambda expression must therefore be some sort of expression that returns an object reference. Programmers who are familiar with inner classes might guess that the lambda is really just syntactic sugar for an anonymous implementation of Runnable.

The article then describes:

  • Call sites – location in the bytecode where a method invocation instruction occurs .
  • Method handles
  • Bootstrapping
  • Decoding the lambda’s bootstrap method
  • The lambda metafactories

This article explores the fine-grained details of exactly how the JVM implements support for lambda expressions. This is one of the more complex platform features you’ll encounter, because it is deep into language implementer territory. Great article!

[Read More]

Tags jvm java web-development app-development software-architecture