HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Lambdas and method references

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 lambda

lt;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 just invokedynamic and areturn. A lambda that captures nothing takes no arguments, and the JDK can return the same instance every time.
  • capture(name) pushes name first: captured variables are the invokedynamic's arguments, and they become the parameters of lambda$capture$0.
  • The method reference String::length needs no synthetic method: its bootstrap arguments point straight at String.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$0 changes.

Lambda.class

static Runnable hello()

javac --release 8 to 25 (the same bytecode for every release)

7return () -> System.out.println("hello");
0invokedynamic #7// run() : Runnable ⇐ LambdaMetafactory.metafactory → Lambda.lambda$hello$0() : void
5areturn

Open in the viewer

static Supplier capture(String name)

javac --release 8 to 25 (the same bytecode for every release)

11return () -> "Hi, " + name;
0aload_0 // name
1invokedynamic #11// get(String) : Supplier ⇐ LambdaMetafactory.metafactory → Lambda.lambda$capture$0(String) : String
6areturn

Open in the viewer

static Function length()

javac --release 8 to 25 (the same bytecode for every release)

15return String::length;
0invokedynamic #15// apply() : Function ⇐ LambdaMetafactory.metafactory → String.length() : int
5areturn

Open in the viewer

private static String lambda$capture$0(String name) synthetic

javac --release 8

11return () -> "Hi, " + name;
0new #19// StringBuilder
3dup
4invokespecial #21// new StringBuilder()
7ldc #22// "Hi, "
9invokevirtual #24// StringBuilder.append(String) : StringBuilder
12aload_0 // name
13invokevirtual #24// StringBuilder.append(String) : StringBuilder
16invokevirtual #28// StringBuilder.toString() : String
19areturn

Open in the viewer

javac --release 11 to 25

11return () -> "Hi, " + name;
0aload_0 // name
1invokedynamic #19// makeConcatWithConstants(String) : String ⇐ StringConcatFactory.makeConcatWithConstants "Hi, \1"
6areturn

Open in the viewer

private static void lambda$hello$0() synthetic

javac --release 8 to 25 (the same bytecode for every release)

7return () -> System.out.println("hello");
0getstatic #23// System.out : PrintStream
3ldc #29// "hello"
5invokevirtual #31// PrintStream.println(String) : void
8return

Open in the viewer

The bootstrap methods

From the BootstrapMethods attribute of the --release 25 class: each invokedynamic names one of these by index.

  1. LambdaMetafactory.metafactory(MethodHandles$Lookup, String, MethodType, MethodType, MethodHandle, MethodType) : CallSite
    • MethodType ()V
    • MethodHandle invokeStatic Lambda.lambda$hello$0() : void
    • MethodType ()V
  2. 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;
  3. 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;
  4. StringConcatFactory.makeConcatWithConstants(MethodHandles$Lookup, String, MethodType, String, Object[]) : CallSite
    • String "Hi, \u0001"
← String concatenationAll topicsSwitch on a String →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.