newarray 0xbc

Pops a length; pushes a new array of a primitive type.

Opcode
0xbc (188)
Family
Arrays
Operands
  • 1 byte: array type (4–11)
Length
2 bytes
Operand stack
…, count (int) → …, arrayref
Can throw
NegativeArraySizeException

What it does

Creates an array of a primitive type with every element zero (or false). The operand byte is the element type: 4 boolean, 5 char, 6 float, 7 double, 8 byte, 9 short, 10 int, 11 long.

javac also uses it for array initialisers such as {1, 2, 3}: newarray, then a dup, an index, a value and a store for each element. A varargs call builds its array the same way.

How often javac uses it

In JDK 25.0.5's java.base module, newarray appears 3,063 times in 809 of its 7,391 classes: 0.16% of all instructions, the 73rd most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.getBufIfOpen, java.io.BufferedOutputStream.<init>, java.io.BufferedReader.<init>, java.io.BufferedWriter.<init>.

Examples

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

static { … } in EnumSwitch.1, compiled by javac 25
20switch (day) {
0invokestatic #1// DayOfWeek.values() : DayOfWeek[]
6putstatic #7// EnumSwitch$1.$SwitchMap$java$time$DayOfWeek : int[]
9getstatic #7// EnumSwitch$1.$SwitchMap$java$time$DayOfWeek : int[]
12getstatic #13// DayOfWeek.SATURDAY : DayOfWeek
15invokevirtual #17// DayOfWeek.ordinal() : int
… 10 instructions after

Open in the viewer

static int callers() in Varargs, compiled by javac 25
10return sum() + sum(1) + sum(1, 2, 3);
3invokestatic #7// Varargs.sum(int[]) : int
9dup
… 20 instructions after

Open in the viewer

Related instructions

anewarray Pops a length; pushes a new array of references.multianewarray Pops several lengths; pushes a new multi-dimensional array.arraylength Pops an array; pushes its length.

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