bastore 0x54

Pops an array, an index and a byte or boolean value; stores the value in the array.

Opcode
0x54 (84)
Family
Arrays
Operands
none
Length
1 byte
Operand stack
…, arrayref, index, value (int) → …
Can throw
NullPointerException, ArrayIndexOutOfBoundsException

What it does

Pops an array (byte[] or boolean[]), an index and a value; stores the value at that index, truncating the int to the element type.

The same instruction serves byte[] and boolean[]. The JVM may store a boolean array as bytes or as bits; for boolean[] the value is masked to its lowest bit.

How often javac uses it

In JDK 25.0.5's java.base module, bastore appears 22,081 times in 278 of its 7,391 classes: 1.1% of all instructions, the 26th most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedOutputStream.write, java.io.BufferedReader.readLine, java.io.ByteArrayOutputStream.write, java.io.FileInputStream.readAllBytes.

Example

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

static int arrays(int n) in Tour, compiled by javac 25
… 29 instructions before
67int[][] grid = new int[n][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
59aload_3 // c
… 85 instructions after

Open in the viewer

Related instructions

baload Pops an array and an index; pushes the byte or boolean element.arraylength Pops an array; pushes its length.newarray Pops a length; pushes a new array of a primitive type.

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