anewarray 0xbd
Pops a length; pushes a new array of references.
- Opcode
0xbd(189)- Family
- Arrays
- Operands
- 2 bytes: constant pool index
- Length
- 3 bytes
- Operand stack
…, count (int) → …, arrayref- Can throw
NegativeArraySizeException
What it does
Creates an array of references with every element null. The operand names the element type: a class, an interface, or an array class for arrays of arrays.
new String[n] compiles to anewarray java/lang/String; new int[n][] compiles to anewarray [I, because only one dimension is being created.
How often javac uses it
In JDK 25.0.5's java.base module, anewarray appears 19,330 times in 1,518 of its 7,391 classes: 0.99% of all instructions, the 30th most common of 194 opcodes in use. Some of the methods that use it: java.io.File.normalizedList, java.io.File$PathStatus.$values, java.io.FileInputStream.readAllBytes, java.io.FilePermission.init.
Examples
From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; anewarray is highlighted.
private static EnumSwitch$Size[] $values() synthetic in EnumSwitch.Size, compiled by javac 25private static Switches$Color[] $values() synthetic in Switches.Color, compiled by javac 25Related instructions
newarray Pops a length; pushes a new array of a primitive type.multianewarray Pops several lengths; pushes a new multi-dimensional array.aastore Pops an array, an index and a reference value; stores the value in the array.
Specification: JVMS §6.5, anewarray (Java SE 25).