Quick revision
The short answer
Since Java 9, "a" + x + "b" compiles to a single invokedynamic instruction whose bootstrap method is StringConcatFactory.makeConcatWithConstants, instead of the chain of StringBuilder.append calls that earlier versions produced. A lambda like s -> print(s) also compiles to invokedynamic, bootstrapped by LambdaMetafactory, with the lambda's body moved into a private synthetic method such as lambda$run$0; the class that implements the functional interface is generated at runtime on first use, not written as a separate class file. javap -c -p shows the invokedynamic instructions, the bootstrap methods and the synthetic methods.
What to remember
In this answer, we saw that invokedynamic links a call site at runtime through a bootstrap method. Since Java 9, string concatenation compiles to one invokedynamic bootstrapped by StringConcatFactory with a recipe such as 'a', placeholder, 'b', where Java 8 emitted StringBuilder appends. A lambda compiles to an invokedynamic bootstrapped by LambdaMetafactory plus a private synthetic method holding its body, and its implementing class is generated at runtime as a hidden class on first use. javap -c -p showed all of this, and a method reference needs no synthetic method at all.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Since Java 9, "a" + x + "b" compiles to a single invokedynamic instruction whose bootstrap method is StringConcatFactory.makeConcatWithConstants, instead of the chain of StringBuilder.append calls that earlier versions produced. A lambda like s -> print(s) also compiles to invokedynamic, bootstrapped by LambdaMetafactory, with the lambda's body moved into a private synthetic method such as lambda$run$0; the class that implements the functional interface is generated at runtime on first use, not written as a separate class file. javap -c -p shows the invokedynamic instructions, the bootstrap methods and the synthetic methods.
Why this question is asked
These two features look like simple syntax, but how they compile affects startup time, memory, the class files we ship and what we see in stack traces and profilers. They are also the two most common uses of invokedynamic, the instruction that lets the JVM decide at runtime how to implement a call. Interviewers ask this to see whether we can look beneath the source code with javap and explain why the JDK chose this design. In this answer, we will recap what invokedynamic is, disassemble string concatenation in old and new Java, disassemble a lambda, see what happens at runtime on the first call, and compare a lambda with a method reference.
1A Quick Recap: What invokedynamic Does
Most bytecode calls name their target directly: invokevirtual calls an instance method, invokestatic a static method, and so on. invokedynamic, added in Java 7, is different. It names a bootstrap method instead of a target. The first time the instruction runs, the JVM calls that bootstrap method, which decides how to implement the call and returns a CallSite, an object wrapping a MethodHandle, a typed reference to executable code. The JVM then links the instruction to that call site permanently, and later executions call the target directly, which the JIT can inline like an ordinary call.
The benefit is that the strategy lives in the JDK, not in our class file. javac only records what we want, such as 'concatenate these values' or 'make a Consumer from this method', and the JDK chooses how, possibly differently in each release, without recompiling our code.
5 messages in order. Play them, or step through them one at a time.
2String Concatenation Before and After Java 9
Up to Java 8, javac translated concatenation into StringBuilder calls. We can still see that today by asking javac to use the old strategy with a hidden option.
Notice how much smaller the new version is: push x, one invokedynamic, return. The constant parts are folded into a recipe string, 'a', a placeholder character (U+0001) for each dynamic argument, then 'b'. The descriptor (I)Ljava/lang/String; says the call takes an int and returns a String. At runtime StringConcatFactory builds an efficient strategy that can compute the exact final length first and copy bytes once, which the old StringBuilder chain, with its default capacity and regrowth, could not. And when the JDK improves the strategy, our old class files get the improvement automatically.
3How a Lambda Compiles
For a lambda, javac does two things. It moves the lambda's body into a new private method of the enclosing class, called a synthetic method because the compiler created it, with a name such as lambda$run$0. And at the place where the lambda appears, it emits an invokedynamic whose bootstrap method is LambdaMetafactory.metafactory, with arguments that describe the functional interface (Consumer.accept) and point at the synthetic method.
Notice the descriptor ()Ljava/util/function/Consumer;. The invokedynamic takes no arguments because this lambda captures nothing; if it used a local variable or this, those would be passed in and appear in the descriptor. The -p flag is needed to see lambda$run$0, because it is private. And there is no extra class file, unlike an anonymous inner class, which javac compiles to Demo$1.class.
4What Happens at Runtime on First Use
The first time run() executes the invokedynamic, the JVM calls LambdaMetafactory. It generates a small class implementing Consumer whose accept method calls lambda$run$0, defines it as a hidden class (since Java 15) attached to Demo, and returns a call site. For a non-capturing lambda like ours, the call site returns the same instance every time, so the lambda costs no allocation after the first call. A capturing lambda creates a new instance per evaluation, holding the captured values.
This is why the first use of each lambda has a small one-time cost, and why applications with thousands of lambdas spend some startup time in LambdaMetafactory, something Project Leyden's ahead-of-time caches aim to reduce. In stack traces, the generated class appears with a name like Demo$$Lambda/0x000001f0c4001a00, and the body appears as lambda$run$0, which is useful to recognise when reading a profile.
5Lambda Versus Method Reference
A method reference such as Demo::print also compiles to an invokedynamic with LambdaMetafactory, but javac does not need a synthetic method, because the target method already exists. The bootstrap arguments point straight at Demo.print. The behaviour is the same; the class file is slightly smaller and the stack trace one frame shorter.
| Source | Bytecode at the call site | Extra methods or classes |
|---|---|---|
| "a" + x + "b" (Java 8) | StringBuilder new, append x3, toString | none |
| "a" + x + "b" (Java 9+) | one invokedynamic, StringConcatFactory | none; recipe in BootstrapMethods |
| s -> print(s) | invokedynamic, LambdaMetafactory | private static lambda$run$0 |
| Demo::print | invokedynamic, LambdaMetafactory | none |
| new Consumer<>() { ... } | new Demo$1, invokespecial | Demo$1.class on disk |
6Follow-up questions
Is the invokedynamic version of string concatenation always faster?
In typical code it is as fast or faster, because the JDK can pick a strategy that sizes the result exactly and avoids intermediate copies. Inside loops that build one large string, an explicit StringBuilder is still the right tool, because each concatenation expression creates a new String. The invokedynamic change improves single expressions, not repeated appends across iterations.
Why did Java not compile lambdas to anonymous inner classes?
Because that would have fixed one implementation strategy in every class file forever, added a class file per lambda, and cost class loading at startup. With invokedynamic the JDK chooses the strategy at runtime and can change it in later releases. Indeed it has: the generated classes became hidden classes in Java 15 without any recompilation of user code.
How can I see the classes generated for lambdas?
They do not exist on disk by default. A JDK system property can dump them to files for inspection; its name changed between releases (in recent JDKs it is jdk.invoke.LambdaMetafactory.dumpProxyClassFiles), so check your version. In stack traces and heap dumps they appear with names containing $$Lambda.
7Conclusion
In this answer, we saw that invokedynamic links a call site at runtime through a bootstrap method. Since Java 9, string concatenation compiles to one invokedynamic bootstrapped by StringConcatFactory with a recipe such as 'a', placeholder, 'b', where Java 8 emitted StringBuilder appends. A lambda compiles to an invokedynamic bootstrapped by LambdaMetafactory plus a private synthetic method holding its body, and its implementing class is generated at runtime as a hidden class on first use. javap -c -p showed all of this, and a method reference needs no synthetic method at all.
Open with
Both use invokedynamic, which links a call site at runtime through a bootstrap method instead of naming a fixed target. For string concatenation, since Java 9 javac emits a single invokedynamic whose bootstrap is StringConcatFactory.makeConcatWithConstants.
Then, if they want more
The constant parts go into a recipe string, so for a plus x plus b the recipe is a, a placeholder, then b, and the descriptor is int to String. At runtime the JDK builds an efficient strategy, for example sizing the result exactly, and future JDKs can improve it without recompiling.
Up to Java 8 javac generated new StringBuilder, a chain of append calls and toString, which you can still get with the hidden option -XDstringConcat=inline.
For the lambda, javac moves the body into a private synthetic method, something like private static void lambda$run$0(String), and at the lambda's location emits an invokedynamic bootstrapped by LambdaMetafactory, with a descriptor returning Consumer and taking any captured values. No class file is written for the lambda.
On first execution, LambdaMetafactory generates a hidden class implementing Consumer that calls the synthetic method; a non-capturing lambda then reuses one instance. With javap -c -p I can see the invokedynamic instructions, the synthetic method, which needs -p because it is private, and with -v the BootstrapMethods table with the recipe or the LambdaMetafactory arguments. A method reference like Demo::print uses the same mechanism without a synthetic method.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 9, Java 15, 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 "How do string concatenation like "a" + x + "b" and a lambda like list.forEach(s ->...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/2fffe6fa9d/