For Lambda expressions, the compiler doesn’t translate them into something which is already understood by JVM. Lambda syntax that is written by the developer is desugared into JVM level instructions generated during compilation, which means the actual responsibility of constructing lambda is deferred to runtime.
To make Java less compile time strictly, and allow JVM languages to bind code at runtime, a new invokedynamic bytecode instruction to the JVM and a new java.lang.invoke API package is introduced.
Java can add functional programming constructs. Instead of generating direct bytecode for lambda compiler declares some instructions and delegates the real construction approach to runtime.
So, during the compilation process following information is generated:
- The bootstrap method information is needed for JVM to construct object representation of lambda during runtime
- Lambda body code is generated within an instance or static private method which has the same parameters and return type as lambda’s functional interface abstract method
- The functional interface is added to the invokeDynamic instructions
During runtime, when JVM encounters invokedynamic instruction or recipe it makes a bootstrap call and this is one time only call which is necessary for lambda object construction during runtime