Java Reference
In-Depth Information
For convenience, the System class provides an arraycopy method that al-
lows you to assign the values from one array into another, instead of
looping through each of the array elementsthis is described in more de-
tail in " Utility Methods " on page 665 .
7.4.4. Arrays and Types
Arrays are implicit extensions of Object . Given a class X , classes Y and Z
that extend X , and arrays of each, the class hierarchy looks something
like this:
This class relationship allows polymorphism for arrays. You can assign
an array to a variable of type Object and cast it back. An array of objects
of type Y is usable wherever an array of objects of its supertype X is
required. This seems natural but can require a run time check that is
sometimes unexpected. An array of X can contain either Y or Z refer-
ences, but an array of Y cannot contain references to X or Z objects.
The following code would generate an ArrayStoreException at run time on
either of its final two lines, which violate this rule:
Y[] yArray = new Y[3]; // a Y array
X[] xArray = yArray; // valid: Y is assignable to X
xArray[0] = new Y();
xArray[2] = new X(); // INVALID: can't store X in Y[]
xArray[1] = new Z(); // INVALID: can't store Z in Y[]
 
Search WWH ::




Custom Search