3.11 Arrays of Numbers
To create an array of a numeric type, the
newarray
instruction is used. Its argument is the Java type of the array you want (
boolean
,
float
,
int
,
char
,
double
,
long
,
byte
, or
short
). The length of the array is found on the stack. For example, to create an array of 99
boolean
s,
bipush 99
newarray boolean ; Allocate an array of 99 booleans
astore_0 ; Store it in local variable 0
The result of a
newarray
instruction is a
reference
. We'll talk a lot more about how to work with
reference
s in chapter 4. For the moment, suffice it to say that you use
aload
and
astore
instructions to move them between the stack and the local variable array, just as you use
iload
and
istore
for
int
s. You can't use arithmetical operations on them, but you can
swap
them around and
pop
them off the stack as if they were
int
s or
float
s.
Unlike other
parts
of the virtual machine, the
newarray
instruction does distinguish between
char, short, byte, boolean
, and
int
types. This
permits
the JVM implementation to use a more efficient internal representation of the array.
Since
boolean
arrays are different from
int
arrays, special instructions are necessary to get information into and out of these arrays. These instructions are written as
x
aload
, where
x
is the mnemonic for the type of the array (as shown in Table 3.1).
aload_0 ; Push the reference to the
; array onto the stack
bipush 7
baload ; Push the array[7] onto the stack
To set an array element in an array of
int
s, use the
iastore
instruction. First you push the array reference itself onto the stack, followed by an
int
that represents which element of the array you wish to set. Then push the value you wish to assign. The
iastore
instruction stores the value in the top slot to the array element. For example, to set
array[7]
to 0,
aload_0 ; Push the array reference
bipush 7 ; Push the array index
iconst_0 ; Push the value we wish to set
baload ; Set array[7] to 0
Similarly, use
fastore
to set elements of
float
arrays,
bastore
for arrays of
byte
or
boolean
,
aastore
for arrays of
reference
s, etc. Table 3.10 summarizes the array operations.
To take best advantage of arrays, we must discuss control constructs. These allow you to repeat the same section of code for each element of an array, instead of having to write specific code for each element. Control constructs are important, so all of chapter 5 is devoted to them.
Table 3.10. Array instructions
|
newarray
|
type
|
n
|
An array of
type
of length
n
|
|
anewarray
|
class
|
n
|
An array of
class
of length
n
|
|
multianewarray
|
type dim
|
n
1
…
n
dim
|
An array of
type
with dimensions
n
1
x…x
n
dim
|
|
aaload
|
|
array of
reference
,
n
|
Element
n
of the array
|
|
aastore
|
|
array of
reference
,
n
,
reference
|
Stores
reference
into element
n
of the array
|
|
baload
|
|
array of
byte
or
boolean
,
n
|
Element
n
of the array
|
|
bastore
|
|
array of
byte
or
boolean
,
n
,
int
|
Stores
int
into element
n
of the array
|
|
caload
|
|
array of
char
,
n
|
Element
n
of the array
|
|
castore
|
|
array of
char
,
n
,
int
|
Stores
int-value
into element
n
of the array
|
|
daload
|
|
array of
double
,
n
|
Element
n
of the array
|
|
dastore
|
|
array of
double
,
n
,
double
|
Stores
double
into element
n
of the array
|
|
faload
|
|
array of
float
,
n
|
Element
n
of the array
|
|
fastore
|
|
array of
float
,
n
,
float
|
Stores
float
into element
n
of the array
|
|
iaload
|
|
array of
int
,
n
|
Element
n
of the array
|
|
iastore
|
|
array of
int
,
n
,
int
|
Stores
int
into element
n
of the array
|
|
laload
|
|
array of
long
,
n
|
Element
n
of the array
|
|
lastore
|
|
array of
long
,
n
,
long
|
Stores
long
into element
n
of the array
|
|
saload
|
|
array of
short
,
n
|
Element
n
of the array
|
|
sastore
|
|
array of
short
,
n
,
int
|
Stores
int
into element
n
of the array
|
|