What javac does
Lambdas and method references
A private synthetic method, and an invokedynamic that LambdaMetafactory links.
A lambda is not compiled to an inner class. javac moves its body into a private method named lambdalt;method>lt;n>, static unless the lambda uses this. The lambda expression itself becomes an invokedynamic whose bootstrap method is LambdaMetafactory.metafactory. The first time it runs, the JDK generates a small class that implements the interface and calls the synthetic method, and links the call site to it.
The source
// Lambdas and method references: an invokedynamic call site, and a private synthetic method.
import java.util.function.Function;
import java.util.function.Supplier;
public class Lambda {
static Runnable hello() {
return () -> System.out.println("hello");
}
static Supplier<String> capture(String name) {
return () -> "Hi, " + name;
}
static Function<String, Integer> length() {
return String::length;
}
}Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.
What to look for
hello()is justinvokedynamicandareturn. A lambda that captures nothing takes no arguments, and the JDK can return the same instance every time.capture(name)pushesnamefirst: captured variables are the invokedynamic's arguments, and they become the parameters oflambda$capture$0.- The method reference
String::lengthneeds no synthetic method: its bootstrap arguments point straight atString.length. - Each lambda's bootstrap arguments are the interface method's erased type, a MethodHandle to the code to run, and the type with generics filled in.
- Lambdas compile the same way for every release from 8 on. Only the string concatenation inside
lambda$capture$0changes.
Lambda.class
static Runnable hello()
javac --release 8 to 25 (the same bytecode for every release)
return () -> System.out.println("hello");static Supplier capture(String name)
javac --release 8 to 25 (the same bytecode for every release)
return () -> "Hi, " + name;static Function length()
javac --release 8 to 25 (the same bytecode for every release)
return String::length;private static String lambda$capture$0(String name) synthetic
javac --release 11 to 25
return () -> "Hi, " + name;private static void lambda$hello$0() synthetic
javac --release 8 to 25 (the same bytecode for every release)
return () -> System.out.println("hello");The bootstrap methods
From the BootstrapMethods attribute of the --release 25 class: each invokedynamic names one of these by index.
LambdaMetafactory.metafactory(MethodHandles$Lookup, String, MethodType, MethodType, MethodHandle, MethodType) : CallSite- MethodType
()V - MethodHandle
invokeStatic Lambda.lambda$hello$0() : void - MethodType
()V
- MethodType
LambdaMetafactory.metafactory(MethodHandles$Lookup, String, MethodType, MethodType, MethodHandle, MethodType) : CallSite- MethodType
()Ljava/lang/Object; - MethodHandle
invokeStatic Lambda.lambda$capture$0(String) : String - MethodType
()Ljava/lang/String;
- MethodType
LambdaMetafactory.metafactory(MethodHandles$Lookup, String, MethodType, MethodType, MethodHandle, MethodType) : CallSite- MethodType
(Ljava/lang/Object;)Ljava/lang/Object; - MethodHandle
invokeVirtual String.length() : int - MethodType
(Ljava/lang/String;)Ljava/lang/Integer;
- MethodType
StringConcatFactory.makeConcatWithConstants(MethodHandles$Lookup, String, MethodType, String, Object[]) : CallSite- String
"Hi, \u0001"
- String