Java Reference
In-Depth Information
5.4.3 Array Access Expressions
Array access expressions in Java and so in j-- are pretty straightforward. For example, if
the variable
a
references an array object, and
i
is an integer, then
a[i]
is an array access expression. The variable
a
may be replaced by any primary expression
that evaluates to an array object, and
i
may be replaced by any expression evaluating to
an integer. The JVM code is just as straightforward. For example, if
a
is an integer array,
then the code produced for the array access expression would be
aloada'
iloadi'
iaload
Instruction
iaload
pops an index
i
and an (integer) array
a
off the stack and replaces those
two items by the value for
a[i]
.
In j--, as in Java, multiply dimensioned arrays are implemented as arrays of arrays
8
.
That is, j-- arrays are vectors.
5.5 Generating Code for Assignment and Similar Operations
5.5.1 Issues in Compiling Assignment
l-values and r-values
Consider a simple assignment statement;
x=y;
It does not seem to say very much; it asks that the value of variable
y
be stored (as the new
value) in variable
x
. Notice that we want different values for
x
and
y
when interpreting such
an assignment. We often say we want the l-value for
x
, and the r-value for
y
. We may think
of a variable's l-value as its address or location (or in the case of local variables, its stack
frame oset), and we may think of a variable's r-value as its content or the value stored in
(or, as the value of) the variable. This relationship is illustrated in Figure 5.1.
FIGURE 5.1 A variable's l-value and r-value.
The names l-value and r-value come from the corresponding positions in the assignment
expression: the left-hand side of the
=
and the right-hand side of the
=
. Think of
l-value
=
r-value
Notice that while all expressions have r-values, many have no l-values. For example, if
a
is
an array of ten integers and
o
is an object with field
f
,
C
is a class with static field
sf
, and
x
is a local variable, then all of
8
Such arrays are known asIllifevectors, after their inventor, John K. Illife.
Search WWH ::
Custom Search