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 25
6enum Size { SMALL, MEDIUM, LARGE }
1anewarray #1// EnumSwitch$Size
4dup
6getstatic #3// EnumSwitch$Size.SMALL : EnumSwitch$Size
10dup
12getstatic #7// EnumSwitch$Size.MEDIUM : EnumSwitch$Size
16dup
18getstatic #10// EnumSwitch$Size.LARGE : EnumSwitch$Size

Open in the viewer

private static Switches$Color[] $values() synthetic in Switches.Color, compiled by javac 25
6enum Color { RED, GREEN, BLUE }
1anewarray #1// Switches$Color
4dup
6getstatic #3// Switches$Color.RED : Switches$Color
10dup
12getstatic #7// Switches$Color.GREEN : Switches$Color
16dup
18getstatic #10// Switches$Color.BLUE : Switches$Color

Open in the viewer

Related 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).