Quick revision
The short answer
In JDK 8, javac compiled "id=" + id + ", name=" + name into new StringBuilder().append("id=").append(id).append(", name=").append(name).toString(). Since JDK 9 (JEP 280), javac emits a single invokedynamic instruction whose bootstrap method is StringConcatFactory.makeConcatWithConstants, and the JDK decides at link time how to do the concatenation. That matters because the strategy now lives in the JDK rather than in our bytecode, so a newer JDK can make old class files faster, for example by computing the exact result size and avoiding intermediate copies, without recompiling.
What to remember
In this answer, we saw that JDK 8 javac compiled string concatenation into a StringBuilder chain fixed in the class file, while JDK 9 and later emit one invokedynamic instruction bootstrapped by StringConcatFactory.makeConcatWithConstants with a recipe of constants and typed arguments. Because the JDK chooses the strategy at link time, it can compute exact sizes, avoid intermediate copies and improve the implementation in later releases without recompiling. The javac option -XDstringConcat=inline restores the old chain, mainly for code that must avoid the bootstrap cost at startup.
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.
In JDK 8, javac compiled "id=" + id + ", name=" + name into new StringBuilder().append("id=").append(id).append(", name=").append(name).toString(). Since JDK 9 (JEP 280), javac emits a single invokedynamic instruction whose bootstrap method is StringConcatFactory.makeConcatWithConstants, and the JDK decides at link time how to do the concatenation. That matters because the strategy now lives in the JDK rather than in our bytecode, so a newer JDK can make old class files faster, for example by computing the exact result size and avoiding intermediate copies, without recompiling.
Why this question is asked
Most of us write + for strings without thinking about what the compiler produces, and that is the point of this change: the code we write stayed the same while the code behind it got better. Interviewers ask this at the ten-year level to check that we understand invokedynamic as a tool for moving decisions from compile time to run time, and why that is valuable for a platform. In this answer, we will look at the JDK 8 bytecode, then the JDK 9 bytecode, see what happens when the call site is first linked, explain why the new approach can be faster, and cover the javac flag that brings back the old strategy.
1A Quick Recap of What javac Does With +
The Java language defines + on strings, but the JVM has no instruction for it. So the compiler has to translate each concatenation into method calls. For our expression, the parts are a constant "id=", an int id, a constant ", name=" and a String name.
The question is what calls javac writes into the class file. That choice used to be permanent: whatever javac emitted was what the program ran, on any JVM, until someone recompiled the code.
2JDK 8: A StringBuilder Chain in the Bytecode
Up to JDK 8, javac turned the expression into a StringBuilder chain. We can see it with javap -c on a class compiled with JDK 8, or on a newer JDK with the -XDstringConcat=inline option.
Notice the costs baked into the class file. The builder starts at 16 characters and grows by copying when the parts do not fit, and toString() copies the content once more. The JIT can optimise some of these patterns, but only if it recognises them, and the recognition was fragile.
3JDK 9 and Later: One invokedynamic Instruction
Since JDK 9, javac emits a single invokedynamic instruction for the whole expression. invokedynamic is a bytecode instruction whose target is not fixed in the class file: the first time it runs, the JVM calls a bootstrap method that returns the code to use, and that code is then linked to the call site for good.
8 messages in order. Play them, or step through them one at a time.
Bootstrapping happens once per call site; later calls run the linked code directly.
The class file now says only what to concatenate: a recipe with the constant text and placeholders for an int and a String. How to concatenate is decided by StringConcatFactory inside the JDK we run on. The dynamic arguments, id and name, are passed as ordinary method arguments with their exact types, so the int is not boxed.
4Why the Change Mattered
The first benefit is that the strategy can improve without recompiling. Because the class file holds a recipe, a newer JDK can bring a better StringConcatFactory and every existing class file benefits. The JDK developers have indeed reworked the implementation several times since JDK 9, and libraries compiled years ago got faster on each upgrade.
The second benefit is that the JDK can do the job with fewer copies. It knows the types of all parts, so it can compute the exact length of the result first, including the number of digits in the int, choose Latin-1 or UTF-16 storage from the parts' coders, allocate the final byte array once and write each part into it directly. There is no growing builder and no final copy in toString(). For typical expressions this gives less garbage and faster code.
The third benefit is smaller bytecode. One instruction replaces a chain of calls, which makes methods shorter, and shorter methods are more likely to be inlined by the JIT.
5Getting the Old Strategy Back
javac has a hidden option, -XDstringConcat=inline, that makes it emit the JDK 8 style StringBuilder chain again. The main reason to use it is startup: the first execution of each invokedynamic call site runs the bootstrap method and builds method handles, which costs time. For a large application with thousands of concatenation sites executed once each at startup, that linking cost can be measurable. Code inside the JDK's own java.base module is also compiled this way, because the concatenation machinery itself lives there and cannot depend on itself during startup.
For ordinary services, the default is the right choice, and features such as class data sharing reduce the startup cost of linking. Notice also what this change does not do: it optimises one expression, so concatenating in a loop with += still copies the growing string every iteration.
6Follow-up questions
Does invokedynamic concatenation box primitive arguments?
No. The call site's type includes the primitive types, here int, so the value is passed as an int and converted to digits directly into the result. That is one of the advantages over a generic varargs approach.
Does the change affect behaviour, such as when toString() is called?
The result is the same string. The specification of string conversion was clarified so that each argument is converted to a string as part of the evaluation. In edge cases, such as an argument whose toString() has side effects or changes other arguments, the order of conversions could differ from the old StringBuilder chain, so code should never depend on it.
Is there a limit on the number of parts?
A method handle can take at most 200 or so arguments, so very long concatenation expressions are split by javac into several invokedynamic calls. This is a detail of javac; the result is the same string.
7Conclusion
In this answer, we saw that JDK 8 javac compiled string concatenation into a StringBuilder chain fixed in the class file, while JDK 9 and later emit one invokedynamic instruction bootstrapped by StringConcatFactory.makeConcatWithConstants with a recipe of constants and typed arguments. Because the JDK chooses the strategy at link time, it can compute exact sizes, avoid intermediate copies and improve the implementation in later releases without recompiling. The javac option -XDstringConcat=inline restores the old chain, mainly for code that must avoid the bootstrap cost at startup.
Open with
In JDK 8, javac turned that expression into a StringBuilder chain: new StringBuilder, append the constant, append the int, append the second constant, append the name, toString. That code was fixed in the class file, with a builder that starts at 16 characters, may grow and copy, and then copies again in toString.
Then, if they want more
Since JDK 9, with JEP 280, javac emits a single invokedynamic instruction. Its bootstrap method is StringConcatFactory.makeConcatWithConstants, and the static argument is a recipe, here id= placeholder comma name= placeholder, while id and name are passed as typed arguments, so the int is not boxed.
The first time that line runs, the JDK builds the concatenation code and links it to the call site, and after that it runs directly. Why it matters: the class file now says what to concatenate, and the JDK decides how.
So the JDK can compute the exact length up front, pick Latin-1 or UTF-16 storage, allocate the final array once and write each part straight into it, with no growing builder and no extra copy. And because the strategy lives in the JDK, it has been improved several times since JDK 9 and old class files got faster without recompiling.
The cost is a one-time bootstrap per call site, which is why javac has -XDstringConcat=inline to bring back the StringBuilder chain, used for example where startup matters or inside java.base itself.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 8
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 is "id=" + id + ", name=" + name compiled in JDK 8 versus JDK 9 and later, and why...": https://scrivio-interview-library.netlify.app/java/strings/1158122b11/