HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › String concatenation

What javac does

String concatenation

StringBuilder chains up to Java 8, one invokedynamic since Java 9.

"Hello, " + name + "!" looks like the simplest expression in Java, but its bytecode changed completely in Java 9. Up to Java 8, javac built a StringBuilder, called append once for each piece, then toString. Since Java 9 (JEP 280) it emits one invokedynamic to StringConcatFactory.makeConcatWithConstants, passing only the values that vary.

The constant pieces move into a recipe string in the BootstrapMethods attribute, where \u0001 (the character U+0001) marks each argument. The JDK decides how to build the string when the call site is first linked, so the strategy can improve with each JDK release without recompiling your code.

The source

// String concatenation: StringBuilder chains before Java 9, one invokedynamic call after.
public class Concat {
    static String greet(String name, int count) {
        return "Hello, " + name + "! You have " + count + " new messages.";
    }

    static String repeat(String s, int times) {
        String out = "";
        for (int i = 0; i < times; i++) {
            out += s;
        }
        return out;
    }
}

Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.

What to look for

  • With --release 8, greet allocates a builder (new, dup, invokespecial) and calls append five times: three constants, the name and the int.
  • From --release 11 on, it pushes name and count and makes one invokedynamic call. The three constant pieces are in the recipe.
  • In repeat, out += s still builds a new String on every pass of the loop in both versions; the invokedynamic version only hides it. Use a StringBuilder yourself in loops.

Concat.class

static String greet(String name, int count)

javac --release 8

4return "Hello, " + name + "! You have " + count + " new messages.";
0new #7// StringBuilder
3dup
4invokespecial #9// new StringBuilder()
7ldc #10// "Hello, "
9invokevirtual #12// StringBuilder.append(String) : StringBuilder
12aload_0 // name
13invokevirtual #12// StringBuilder.append(String) : StringBuilder
16ldc #16// "! You have "
18invokevirtual #12// StringBuilder.append(String) : StringBuilder
21iload_1 // count
22invokevirtual #18// StringBuilder.append(int) : StringBuilder
25ldc #21// " new messages."
27invokevirtual #12// StringBuilder.append(String) : StringBuilder
30invokevirtual #23// StringBuilder.toString() : String
33areturn

Open in the viewer

javac --release 11 to 25

4return "Hello, " + name + "! You have " + count + " new messages.";
0aload_0 // name
1iload_1 // count
2invokedynamic #7// makeConcatWithConstants(String, int) : String ⇐ StringConcatFactory.makeConcatWithConstants "Hello, \1! You have \1 new messages."
7areturn

Open in the viewer

static String repeat(String s, int times)

javac --release 8

8String out = "";
0ldc #27// ""
2astore_2 // out
9for (int i = 0; i < times; i++) {
3iconst_0
4istore_3 // i
5iload_3 // i
6iload_1 // times
7if_icmpge 35
10out += s;
10new #7// StringBuilder
13dup
14invokespecial #9// new StringBuilder()
17aload_2 // out
18invokevirtual #12// StringBuilder.append(String) : StringBuilder
21aload_0 // s
22invokevirtual #12// StringBuilder.append(String) : StringBuilder
25invokevirtual #23// StringBuilder.toString() : String
28astore_2 // out
9for (int i = 0; i < times; i++) {
29iinc 3, 1// i
32goto 5
12return out;
35aload_2 // out
36areturn

Open in the viewer

javac --release 11 to 25

8String out = "";
0ldc #11// ""
2astore_2 // out
9for (int i = 0; i < times; i++) {
3iconst_0
4istore_3 // i
5iload_3 // i
6iload_1 // times
7if_icmpge 24
10out += s;
10aload_2 // out
11aload_0 // s
12invokedynamic #13// makeConcatWithConstants(String, String) : String ⇐ StringConcatFactory.makeConcatWithConstants "\1\1"
17astore_2 // out
9for (int i = 0; i < times; i++) {
18iinc 3, 1// i
21goto 5
12return out;
24aload_2 // out
25areturn

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. StringConcatFactory.makeConcatWithConstants(MethodHandles$Lookup, String, MethodType, String, Object[]) : CallSite
    • String "Hello, \u0001! You have \u0001 new messages."
  2. StringConcatFactory.makeConcatWithConstants(MethodHandles$Lookup, String, MethodType, String, Object[]) : CallSite
    • String "\u0001\u0001"
All topicsLambdas and method references →

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.