arraylength 0xbe

Pops an array; pushes its length.

Opcode
0xbe (190)
Family
Arrays
Operands
none
Length
1 byte
Operand stack
…, arrayref → …, length (int)
Can throw
NullPointerException

What it does

Pushes the length of an array: a.length in Java. Every array type shares this one instruction.

The enhanced for loop over an array reads the length once, before the loop starts, into a hidden local variable.

How often javac uses it

In JDK 25.0.5's java.base module, arraylength appears 6,694 times in 1,398 of its 7,391 classes: 0.34% of all instructions, the 54th most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.fill, java.io.BufferedOutputStream.growIfNeeded, java.io.BufferedReader.fill, java.io.BufferedWriter.write.

Examples

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

public static void main(String[] args) in Hello, compiled by javac 25
3String who = args.length > 0 ? args[0] : "world";
0aload_0 // args
5aload_0 // args
11ldc #7// "world"
13astore_1 // who
4System.out.println("Hello, " + who + "!");
14getstatic #9// System.out : PrintStream
17aload_1 // who
18invokedynamic #15// makeConcatWithConstants(String) : String ⇐ StringConcatFactory.makeConcatWithConstants "Hello, \1!"
23invokevirtual #19// PrintStream.println(String) : void
5}

Open in the viewer

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
… 11 instructions after

Open in the viewer

Related instructions

newarray Pops a length; pushes a new array of a primitive type.iaload Pops an array and an index; pushes the int element.

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