multianewarray 0xc5

Pops several lengths; pushes a new multi-dimensional array.

Opcode
0xc5 (197)
Family
Arrays
Operands
  • 2 bytes: class constant index
  • 1 byte: dimensions
Length
4 bytes
Operand stack
…, count1, count2, … → …, arrayref
Can throw
NegativeArraySizeException

What it does

Creates a multi-dimensional array, popping one length for each dimension given. The first operand is the array type (such as [[I); the second is how many dimensions to create, which may be fewer than the type has.

javac uses it for new int[3][4]. When only the first length is given, as in new int[3][], it uses anewarray instead.

How often javac uses it

In JDK 25.0.5's java.base module, multianewarray appears 17 times in 13 of its 7,391 classes: <0.01% of all instructions, the 182nd most common of 194 opcodes in use. Some of the methods that use it: java.lang.invoke.LambdaForm.<clinit>, java.lang.invoke.StringConcatFactory.<clinit>, java.lang.reflect.Executable.sharedGetParameterAnnotations, java.util.zip.CRC32C.<clinit>.

Examples

From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; multianewarray is highlighted.

static int arrays(int n) in Tour, compiled by javac 25
… 24 instructions before
66String[] str = new String[n];
37iload_0 // n
38anewarray #16// String
41astore 9// str
67int[][] grid = new int[n][n];
43iload_0 // n
44iload_0 // n
45multianewarray #18, 2// int[][]
49astore 10// grid
68z[0] = true; b[0] = 1; c[0] = 'a'; s[0] = 2; i[0] = 3; l[0] = 4; f[0] = 5; d[0] = 6; str[0] = "x"; grid[0][0] = 7;
51aload_1 // z
55aload_2 // b
… 90 instructions after

Open in the viewer

static long mix(long a, int b, short c, byte d, double e, float f, char g, boolean h) in Numbers, compiled by javac 25
… 26 instructions before
33if (h) r = -r;
38lload 10// r
40lneg
41lstore 10// r
34int[][] grid = new int[3][4];
45multianewarray #56, 2// int[][]
49astore 12// grid
35grid[1][2] = (int) r;
51aload 12// grid
56lload 10// r
… 31 instructions after

Open in the viewer

Related instructions

anewarray Pops a length; pushes a new array of references.newarray Pops a length; pushes a new array of a primitive type.

Specification: JVMS §6.5, multianewarray (Java SE 25).