Java Reference
In-Depth Information
3.2.1.4
Before we move on to the topic of arrays (which are sort of a hybrid scalar/
object type in Java), let's spend a moment on the operators that can be used in
expressions (Table 3.1). Most deal with numeric or boolean operands. For
completeness, we'll include the operators that deal exclusively with arrays
(the “ [] ”) and classes (“ . ”, new , and instanceof ), even though we haven't
discussed them yet.
Operators listed on the same row in the table have the same precedence.
Operators with the same precedence, except for the unary operators, group
from left to right. Unary operators group from right to left.
Operators
3.2.1.5
Example 3.2 demonstrates the array syntax in Java.
Arrays
Example 3.2 Example array syntax
int [] oned = new int[35]; // array = new type[size]
int alta [] = {1, 3, 5, 14, 11, 6, 24}; // alternative syntax plus
// initialization
int j=0;
for(int i=0; i<35; i++) {
oned[i] = valcomp(i, prop, alta[j]); // array[index]
if (++j > alta.length) { // array.length
j = 0;
}
}
The array can be declared with the [] on either side of the variable name.
While our example uses the primitive type int , array syntax looks just the same
for any objects.
Note that in Java, one doesn't declare the size of the array. It's only in
creating the array with a new that the array gets created to a particular size.
(The {...} syntax is really just a special compiler construct for what is
essentially a new followed by an assignment of values.)
Multidimensional arrays follow the syntax of simple arrays, but with
additional adjacent square brackets, as shown in Example 3.3.
Search WWH ::




Custom Search