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

What javac does

Varargs

The caller builds the array, even for no arguments.

A varargs parameter is an array parameter with the ACC_VARARGS flag. All the work happens at the call site: javac builds the array, even when there are no arguments.

The source

// Varargs: the caller builds the array; the method just takes one.
public class Varargs {
    static int sum(int... values) {
        int total = 0;
        for (int v : values) total += v;
        return total;
    }

    static int callers() {
        return sum() + sum(1) + sum(1, 2, 3);
    }
}

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

What to look for

  • sum() creates an empty array: iconst_0, newarray int.
  • sum(1, 2, 3) is newarray followed by a dup, an index, a value and an iastore for each element.
  • Every call allocates a new array. That is why performance-minded APIs such as List.of also have fixed-arity overloads for small counts.

Varargs.class

static int sum(int... values)

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

4int total = 0;
0iconst_0
1istore_1 // total
5for (int v : values) total += v;
2aload_0 // values
3astore_2
4aload_2
5arraylength
6istore_3
7iconst_0
8istore 4
10iload 4
12iload_3
13if_icmpge 33
16aload_2
17iload 4
19iaload
20istore 5// v
22iload_1 // total
23iload 5// v
25iadd
26istore_1 // total
27iinc 4, 1
30goto 10
6return total;
33iload_1 // total
34ireturn

Open in the viewer

static int callers()

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

10return sum() + sum(1) + sum(1, 2, 3);
0iconst_0
1newarray int
3invokestatic #7// Varargs.sum(int[]) : int
6iconst_1
7newarray int
9dup
10iconst_0
11iconst_1
12iastore
13invokestatic #7// Varargs.sum(int[]) : int
16iadd
17iconst_3
18newarray int
20dup
21iconst_0
22iconst_1
23iastore
24dup
25iconst_1
26iconst_2
27iastore
28dup
29iconst_2
30iconst_3
31iastore
32invokestatic #7// Varargs.sum(int[]) : int
35iadd
36ireturn

Open in the viewer

← Text blocksAll topicsComparing longs, floats and doubles →

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.