Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

Explain how invokedynamic works and how the Java compiler uses it for lambdas and for string concatenation.

An invokedynamic instruction names a bootstrap method instead of a fixed target. The first time a call site runs, the JVM calls that bootstrap method, which returns a CallSite linked to a MethodHandle, and from then on the instruction calls that handle directly. javac uses it for lambdas, with LambdaMetafactory as the bootstrap that spins a class implementing the functional interface, and since Java 9 for string concatenation, with StringConcatFactory choosing the strategy. Because the strategy lives in the JDK, it can improve without recompiling our code.

Why this question is asked

Most Java developers use invokedynamic every day without seeing it: every lambda and every + between strings goes through it. Knowing how it works explains things we notice in practice, such as stack traces with lambda$main$0 frames, why the first call of a lambda is slower, and why a jar compiled years ago can get faster string concatenation on a new JDK. Interviewers ask about it to probe JVM internals beyond the language. In this answer, we will recap how ordinary calls are linked, walk through an invokedynamic call site step by step, then look at the bytecode javac produces for a lambda and for string concatenation.

1A Quick Recap: How Ordinary Calls Are Linked

Before Java 7 the JVM had four call instructions. invokestatic calls a static method, invokevirtual an instance method of a class, invokeinterface a method through an interface, and invokespecial constructors, private methods and super calls. Each one names its target directly in the class file: the owner class, the method name and its type descriptor. When the instruction first runs, the JVM resolves that symbolic reference to a real method, and it can never point anywhere else.

That is fine for Java's own method calls, but it means the compiler has to decide everything. If it wants a lambda to become an object, it must generate a class for it. If it wants a fast string concatenation, it must write the StringBuilder calls into our bytecode, and that code stays there forever.

2How an invokedynamic Call Site Gets Linked

invokedynamic, added in Java 7, takes a different approach. The instruction carries a name, a type descriptor that says what the call takes and returns, and a reference to a bootstrap method, plus optional constant arguments for it. It does not say which method to call.

The first time the JVM executes a particular invokedynamic instruction, it calls the bootstrap method, passing a Lookup object with the calling class's access rights, the name, the MethodType and the constant arguments. The bootstrap method can run any Java code it likes to decide what the call should do, and it returns a CallSite object whose target is a MethodHandle of exactly that type. The JVM links the instruction to that call site. Every later execution of the same instruction calls the call site's target directly, and the JIT can inline it like a normal call. Notice that linkage happens once per call site, the specific instruction in the bytecode, not once per method name.

Step throughFirst and later executions of one invokedynamic call site
Our codeJVMBootstrap methodCallSite target
execute invokedynamic (first time)
bootstrap(lookup, name, type, args)
ConstantCallSite(target handle)
invoke target
result
execute again (later times)
invoke the linked target directly
result

8 messages in order. Play them, or step through them one at a time.

The bootstrap method runs only on the first execution of each call site.

3What javac Emits for a Lambda

When we write a lambda, javac moves its body into a private method of our class, named something like lambda$main$0, and at the place where the lambda appears it emits an invokedynamic whose bootstrap method is LambdaMetafactory.metafactory. We can see this with javap -c -p.

Java
public class Demo {
public static void main(String[] args) {
int base = 10;
IntUnaryOperator add = x -> x + base;
System.out.println(add.applyAsInt(5)); // 15
}
}
// javap -c -p Demo (abridged)
// iload_1
// invokedynamic #7, 0 // InvokeDynamic #0:applyAsInt:(I)Ljava/util/function/IntUnaryOperator;
// astore_2
// private static int lambda$main$0(int, int); // the lambda body, with base as an extra parameter
// BootstrapMethods:
// 0: invokestatic java/lang/invoke/LambdaMetafactory.metafactory(...)

Read the type descriptor of the call site: (I)Ljava/util/function/IntUnaryOperator;. The call takes the captured variable base, an int, and returns an IntUnaryOperator. So the invokedynamic is not the lambda being called; it is the lambda being created. On first execution, LambdaMetafactory generates a small hidden class that implements IntUnaryOperator and whose applyAsInt calls lambda$main$0, and returns a call site that creates instances of it. For a lambda that captures nothing, the call site returns the same instance every time, so no object is allocated after the first.

The reason for this design is that javac does not have to generate a class file per lambda, as it does for anonymous inner classes, and the JDK is free to change how lambda objects are produced without any change to our class files.

4What javac Emits for String Concatenation

Before Java 9, javac compiled "Hi " + name + ", you are " + age into a chain of StringBuilder.append calls written into our bytecode. Since Java 9, through JEP 280, it emits a single invokedynamic whose bootstrap is StringConcatFactory.makeConcatWithConstants. The constant parts are passed as a recipe string in which a special marker character stands for each argument.

Java
String greet(String name, int age) {
return "Hi " + name + ", you are " + age; // greet("Ana", 31) -> Hi Ana, you are 31
}
// javap -c -p (abridged)
// aload_1
// iload_2
// invokedynamic #9, 0 // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;I)Ljava/lang/String;
// areturn
// BootstrapMethods:
// 0: invokestatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants(...)
// Method arguments: "Hi \u0001, you are \u0001"
Who decides the strategy
FeatureBytecode javac writesStrategy chosen by
Lambda (Java 8)invokedynamic + private lambda$ methodLambdaMetafactory at first call
String + (Java 9)invokedynamic + recipe stringStringConcatFactory at first call
String + (Java 8)StringBuilder.append chainjavac, fixed in the class file

Notice that the int is passed as an int, with no boxing and no StringBuilder in our code. On first execution, StringConcatFactory looks at the recipe and the argument types and builds a method handle that computes the exact length of the result, allocates the byte array once and copies the parts in. The JDK has changed this internal strategy across releases, and every class compiled for Java 9 or later picked up the improvements without being recompiled. That is the main benefit of moving the strategy out of javac.

5Why the First Call Is Slower

Because the bootstrap method runs on first execution, the first call of each lambda or concatenation site does real work: generating a class or building a method handle tree. That is a small one-time cost per call site, usually well under a millisecond, but an application with thousands of call sites pays it during warm-up. This is one reason startup-focused work such as class data sharing and Project Leyden's ahead-of-time caches try to record these linkages and reuse them on the next start. Once linked, the call site costs about the same as a normal call.

6Follow-up questions

What does a record's toString, equals and hashCode use at bytecode level?

invokedynamic as well. javac generates the three methods in the record class, but each body is a single invokedynamic whose bootstrap method is java.lang.runtime.ObjectMethods.bootstrap, with the record class and method handles for each component's field passed as constant arguments. ObjectMethods then builds the implementation at first call. That is why records' generated methods can be improved by the JDK without recompiling the record.

Is a lambda the same as an anonymous inner class at run time?

No. An anonymous class is a real class file compiled into our jar, and each evaluation creates a new instance. A lambda's class is generated at run time by LambdaMetafactory as a hidden class, and a non-capturing lambda reuses one instance. Also, this inside a lambda refers to the enclosing object, not to the lambda.

Can a call site be relinked after the first call?

Yes, if the bootstrap method returns a MutableCallSite or VolatileCallSite instead of a ConstantCallSite. Dynamic languages use that to change the target when the types they see change. javac's bootstraps return constant call sites, so for lambdas and concatenation the link is permanent.

7Conclusion

In this answer, we saw that an invokedynamic instruction names a bootstrap method instead of a target. On first execution the JVM calls the bootstrap, which returns a CallSite linked to a MethodHandle, and later executions call that target directly. javac uses LambdaMetafactory as the bootstrap for lambdas, which spins a class implementing the functional interface, and since Java 9 StringConcatFactory for string concatenation, so the JDK can change both strategies without recompiling our code.

What to say in the interviewAbout 80 seconds aloud

Open with

invokedynamic is a call instruction that doesn't name its target. Instead it names a bootstrap method, plus a name, a type descriptor, and optional constant arguments. The first time that particular call site runs, the JVM calls the bootstrap method with a Lookup, the name and the MethodType.

Then, if they want more

The bootstrap returns a CallSite whose target is a MethodHandle of that type, and the JVM links the instruction to it, so every later execution calls the target directly and the JIT can inline it.

Linkage happens once per call site. javac uses this for lambdas: it moves the lambda body into a private method like lambda$main$0 and emits an invokedynamic with LambdaMetafactory as the bootstrap.

The call site's type takes the captured variables and returns the functional interface, so the indy creates the lambda object; the metafactory spins a hidden class implementing the interface that calls the private method. Non-capturing lambdas reuse one instance.

Since Java 9, string concatenation compiles to an invokedynamic with StringConcatFactory.makeConcatWithConstants and a recipe string, so the JDK picks the concatenation strategy at run time. The big benefit is that the JDK can change these strategies without recompiling user code. Records' toString, equals and hashCode use the same mechanism through ObjectMethods.bootstrap.

About this answer

Last updated
12 November 2026
Versions it names
Java 9, Java 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain how invokedynamic works and how the Java compiler uses it for lambdas and for...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/9cda7045a2/